Debian Codegraph @

varlink/tests/test_mocks.py

raw
1 import unittest
2
3 import varlink
4 from varlink import mock
5
6 types = """
7 type MyPersonalType (
8 foo: string,
9 bar: string
10 )
11 """
12
13
14 class Service:
15 # TODO: these type annotations are wrong as Python type hints, but are used
16 # by varlink.mock.generate_callable_interface
17 def Test1(self, param1: int) -> str: # type: ignore
18 """return test: MyPersonalType"""
19 return {
20 "test": {
21 "foo": "bim",
22 "bar": "boom",
23 },
24 } # type: ignore
25
26 def Test2(self, param1: str = "test") -> None:
27 pass
28
29 def Test3(self, param1: str) -> str: # type: ignore
30 """return test"""
31 return {"test": param1} # type: ignore
32
33
34 class TestMockMechanisms(unittest.TestCase):
35 @mock.mockedservice(fake_service=Service, fake_types=types, name="org.service.com", address="unix:@foo")
36 def test_init(self):
37 with varlink.Client("unix:@foo") as client:
38 connection = client.open("org.service.com")
39 self.assertEqual(connection.Test1(param1=1)["test"]["bar"], "boom")
40 self.assertEqual(connection.Test3(param1="foo")["test"], "foo")
41
42
43 class TestMockUtilities(unittest.TestCase):
44 def test_cast_type(self):
45 self.assertEqual(mock.cast_type("<class 'int'>"), "int")
46 self.assertEqual(mock.cast_type("<class 'str'>"), "string")
47
48 def test_get_ignored(self):
49 expected_ignore = dir(mock.MockedService)
50 ignored = mock.get_ignored()
51 self.assertEqual(expected_ignore, ignored)
52
53 def test_get_attributs(self):
54 service = Service()
55 attributs = mock.get_interface_attributs(service, mock.get_ignored())
56 expected_result = {"callables": ["Test1", "Test2", "Test3"], "others": []}
57 self.assertEqual(attributs, expected_result)
58
59 def test_generate_callable_interface(self):
60 service = Service()
61 generated_itf = mock.generate_callable_interface(service, "Test1")
62 self.assertEqual(generated_itf, "method Test1(param1: int) -> (test: MyPersonalType)")
63 generated_itf = mock.generate_callable_interface(service, "Test3")
64 self.assertEqual(generated_itf, "method Test3(param1: string) -> (test: string)")