""" Exmples of usage with tests.

Tests in this file represent examples taken from JSON-RPC specification.
http://www.jsonrpc.org/specification#examples

"""
import unittest
import json
from ..jsonrpc import JSONRPCResponseManager


def isjsonequal(json1, json2):
    return json.loads(json1) == json.loads(json2)


class TestJSONRPCExamples(unittest.TestCase):
    def setUp(self):
        self.dispatcher = {
            "subtract": lambda a, b: a - b,
        }

    def test_rpc_call_with_positional_parameters(self):
        req = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}'  # noqa
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertEqual(
            response.json,
            '{"jsonrpc": "2.0", "result": 19, "id": 1}'
        )

        req = '{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}'  # noqa
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertEqual(
            response.json,
            '{"jsonrpc": "2.0", "result": -19, "id": 2}'
        )

    def test_rpc_call_with_named_parameters(self):
        def subtract(minuend=None, subtrahend=None):
            return minuend - subtrahend

        dispatcher = {
            "subtract": subtract,
        }

        req = '{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}'  # noqa
        response = JSONRPCResponseManager.handle(req, dispatcher)
        self.assertEqual(
            response.json,
            '{"jsonrpc": "2.0", "result": 19, "id": 3}'
        )

        req = '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}'  # noqa
        response = JSONRPCResponseManager.handle(req, dispatcher)
        self.assertEqual(
            response.json,
            '{"jsonrpc": "2.0", "result": 19, "id": 4}',
        )

    def test_notification(self):
        req = '{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}'
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertEqual(response, None)

        req = '{"jsonrpc": "2.0", "method": "foobar"}'
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertEqual(response, None)

    def test_rpc_call_of_non_existent_method(self):
        req = '{"jsonrpc": "2.0", "method": "foobar", "id": "1"}'
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertTrue(isjsonequal(
            response.json,
            '{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}'  # noqa
        ))

    def test_rpc_call_with_invalid_json(self):
        req = '{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]'
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertTrue(isjsonequal(
            response.json,
            '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}'  # noqa
        ))

    def test_rpc_call_with_invalid_request_object(self):
        req = '{"jsonrpc": "2.0", "method": 1, "params": "bar"}'
        response = JSONRPCResponseManager.handle(req, self.dispatcher)
        self.assertTrue(isjsonequal(
            response.json,
            '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}'  # noqa
        ))

@@ -3412,28 +3412,480 @@
 : null%7D'  # noqa%0A        ))%0A
+%0A    def test_rpc_call_batch_invalid_json(self):%0A        req = %22%22%22%5B%0A            %7B%22jsonrpc%22: %222.0%22, %22method%22: %22sum%22, %22params%22: %5B1,2,4%5D, %22id%22: %221%22%7D,%0A            %7B%22jsonrpc%22: %222.0%22, %22method%22%0A        %5D%22%22%22%0A        response = JSONRPCResponseManager.handle(req, self.dispatcher)%0A        self.assertTrue(isjsonequal(%0A            response.json,%0A            '%7B%22jsonrpc%22: %222.0%22, %22error%22: %7B%22code%22: -32700, %22message%22: %22Parse error%22%7D, %22id%22: null%7D'  # noqa%0A        ))%0A

add invalid batch request example