File: test_mocks.py

package info (click to toggle)
python-varlink 32.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 328 kB
  • sloc: python: 2,705; sh: 177; makefile: 29
file content (64 lines) | stat: -rw-r--r-- 2,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import unittest

import varlink
from varlink import mock

types = """
type MyPersonalType (
    foo: string,
    bar: string
)
"""


class Service:
    # TODO: these type annotations are wrong as Python type hints, but are used
    # by varlink.mock.generate_callable_interface
    def Test1(self, param1: int) -> str:  # type: ignore
        """return test: MyPersonalType"""
        return {
            "test": {
                "foo": "bim",
                "bar": "boom",
            },
        }  # type: ignore

    def Test2(self, param1: str = "test") -> None:
        pass

    def Test3(self, param1: str) -> str:  # type: ignore
        """return test"""
        return {"test": param1}  # type: ignore


class TestMockMechanisms(unittest.TestCase):
    @mock.mockedservice(fake_service=Service, fake_types=types, name="org.service.com", address="unix:@foo")
    def test_init(self):
        with varlink.Client("unix:@foo") as client:
            connection = client.open("org.service.com")
            self.assertEqual(connection.Test1(param1=1)["test"]["bar"], "boom")
            self.assertEqual(connection.Test3(param1="foo")["test"], "foo")


class TestMockUtilities(unittest.TestCase):
    def test_cast_type(self):
        self.assertEqual(mock.cast_type("<class 'int'>"), "int")
        self.assertEqual(mock.cast_type("<class 'str'>"), "string")

    def test_get_ignored(self):
        expected_ignore = dir(mock.MockedService)
        ignored = mock.get_ignored()
        self.assertEqual(expected_ignore, ignored)

    def test_get_attributs(self):
        service = Service()
        attributs = mock.get_interface_attributs(service, mock.get_ignored())
        expected_result = {"callables": ["Test1", "Test2", "Test3"], "others": []}
        self.assertEqual(attributs, expected_result)

    def test_generate_callable_interface(self):
        service = Service()
        generated_itf = mock.generate_callable_interface(service, "Test1")
        self.assertEqual(generated_itf, "method Test1(param1: int) -> (test: MyPersonalType)")
        generated_itf = mock.generate_callable_interface(service, "Test3")
        self.assertEqual(generated_itf, "method Test3(param1: string) -> (test: string)")