add tests

This commit is contained in:
2025-05-29 03:05:15 +03:00
parent 6183962318
commit 2faadd8a69
4 changed files with 53 additions and 3 deletions

View File

@@ -3,9 +3,10 @@
## aiohttp
```
from jsonrpc.backend.aiohttp import APIView
from jsonrpc.backend.aiohttp import APIHandler
app.router.add_view('/api', APIView)
handler = APIHandler(jsonrpc)
app.router.add_view('/api', APIHandler)
```
## Flask

View File

@@ -12,7 +12,7 @@ from .. import JSONRPC
pathlib.Path(__file__).parent.resolve()
class APIView:
class APIHandler:
def __init__(self, jsonrpc):
self.jsonrpc = jsonrpc

2
tests/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'

47
tests/test_jsonrpc.py Normal file
View File

@@ -0,0 +1,47 @@
__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
import unittest
from jsonrpc import JSONRPC
jsonrpc = JSONRPC()
@jsonrpc.method('boo')
def index() -> str:
return 'Welcome to JSON-RPC'
class Calc(unittest.TestCase):
def test_method_exist(self):
self.assertIn('boo', jsonrpc.methods)
def test_example(self):
self.assertEqual(
jsonrpc.example('boo'),
{'jsonrpc': '2.0', 'method': 'boo', 'id': 1}
)
def test_evaluate(self):
request = {'jsonrpc': '2.0', 'method': 'boo', 'id': 1}
response = jsonrpc(request)
self.assertEqual(
response,
{'jsonrpc': '2.0', 'result': 'Welcome to JSON-RPC', 'id': 1}
)
def test_error(self):
request = {'jsonrpc': '2.0', 'method': 'bla-bla', 'id': 1}
response = jsonrpc(request)
print(response)
self.assertEqual(
response,
{
'jsonrpc': '2.0',
'error': {
'code': -32601,
'message': 'Метод не найден: bla-bla'
},
'id': 1
}
)