Ejemplo n.º 1
0
    def test_compound_entity(self):
        d1 = DerivedClass.build()

        json_d1 = JsonFormatter.serialize(d1)
        self.assertTrue(json_d1)

        d2 = JsonFormatter.deserialize(json_d1, DerivedClass)
        self.assertTrue(d2 is not None)
        self.assertFalse(d2 is d1)
        self.assertTrue(d2 == d1)
        self.assertIsInstance(d2, DerivedClass)

        json_d2 = JsonFormatter.serialize(d2)
        self.assertTrue(json_d2)

        d3 = JsonFormatter.deserialize(json_d2, DerivedClass)
        self.assertTrue(d3 is not None)
        self.assertFalse(d3 is d1)
        self.assertFalse(d3 is d2)
        self.assertTrue(d3 == d2)
        self.assertTrue(d3 == d1)

        d1.characters.append(Character(20, 123123))
        self.assertTrue(
            len(d1.characters) > len(d2.characters)
            and len(d1.characters) > len(d3.characters))
Ejemplo n.º 2
0
 def __read_from_config(cls) -> Optional[UrlInfo]:
     if os.path.exists(cls.__file_name):
         with open(cls.__file_name) as file:
             json = file.read()
             if len(json) > 0:
                 return JsonFormatter.deserialize(json, UrlInfo)
     return None
Ejemplo n.º 3
0
    def test_self_serializing(self):
        ch = Character(18, 100)
        json_ch = ch.to_json_str()
        self.assertTrue(json_ch)

        ch2 = JsonFormatter.deserialize(json_ch, Character)
        self.assertTrue(ch2)
        self.assertTrue(ch2 == ch)
        self.assertFalse(ch is ch2)
Ejemplo n.º 4
0
    def test_generic_type(self):
        c1: MyCollection[DerivedClass] = MyCollection(
            [DerivedClass.build() for i in range(200)])

        json_c1 = JsonFormatter.serialize(c1)
        self.assertTrue(json_c1)

        c2 = JsonFormatter.deserialize(json_c1, MyCollection[DerivedClass])
        self.assertTrue(c2 == c1)
        self.assertFalse(c1 is c2)
Ejemplo n.º 5
0
    def do_post(self, server_method: str, json_request_dto: str, client_address: Tuple[str, int]) -> str:
        handler = self.__method_handler.get(server_method)
        if handler is None:
            raise ServerLogicException(400, f"Unsupported server method {server_method}!")

        dto_type = handler.__annotations__["dto"]
        request_dto = JsonFormatter.deserialize(json_request_dto, dto_type)
        if not hasattr(request_dto, "data"):
            raise ServerLogicException(400, f"Server work only with dto types ! Stop spam !")

        UrlManager.resolve_client_address(request_dto, client_address)
        response_dto = handler(request_dto)
        return JsonFormatter.serialize(response_dto)
Ejemplo n.º 6
0
 def send(path: str, data: JsonContract, response_type: type) -> JsonContract:
     data_json = JsonFormatter.serialize(data)
     response_json = BaseNetworkManager.send(path, data_json)
     return JsonFormatter.deserialize(response_json, response_type)
Ejemplo n.º 7
0
 def get(path: str, response_type: type) -> JsonContract:
     response_json = BaseNetworkManager.get(path)
     return JsonFormatter.deserialize(response_json, response_type)
Ejemplo n.º 8
0
 def test_simple_entity(self):
     ch = Character(18, 100)
     json_ch = JsonFormatter.serialize(ch)
     ch_val = JsonFormatter.deserialize(json_ch, Character)
     self.assertTrue(ch is not ch_val)
     self.assertTrue(ch == ch_val)