From ef73bb27aacea3795addac3c89430236f5508d24 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 23 Oct 2019 11:58:16 +0200 Subject: [PATCH] Add graphql tests --- tests/test_graphql.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/test_graphql.py diff --git a/tests/test_graphql.py b/tests/test_graphql.py new file mode 100644 index 0000000..7f48101 --- /dev/null +++ b/tests/test_graphql.py @@ -0,0 +1,35 @@ +import pytest +import json +from fbchat._graphql import ConcatJSONDecoder, queries_to_json, response_to_json + + +@pytest.mark.parametrize( + "content,result", + [ + ("", []), + ('{"a":"b"}', [{"a": "b"}]), + ('{"a":"b"}{"b":"c"}', [{"a": "b"}, {"b": "c"}]), + (' \n{"a": "b" } \n { "b" \n\n : "c" }', [{"a": "b"}, {"b": "c"}]), + ], +) +def test_concat_json_decoder(content, result): + assert result == json.loads(content, cls=ConcatJSONDecoder) + + +def test_queries_to_json(): + assert {"q0": "A", "q1": "B", "q2": "C"} == json.loads( + queries_to_json("A", "B", "C") + ) + + +def test_response_to_json(): + data = ( + '{"q1":{"data":{"b":"c"}}}\r\n' + '{"q0":{"response":[1,2]}}\r\n' + "{\n" + ' "successful_results": 2,\n' + ' "error_results": 0,\n' + ' "skipped_results": 0\n' + "}" + ) + assert [[1, 2], {"b": "c"}] == response_to_json(data)