Beispiel #1
0
    def test_unpack_invalid_json(self):
        class Pet(Message):
            sound = String()

        protocol = JSONProtocol(Pet)

        with self.assertRaises(ValidationError) as e:
            protocol.unpack(b'')

        self.assertEqual(e.exception.description,
                         "Invalid JSONProtocol: Expected object or value")
        self.assertEqual(e.exception.path, [])

        with self.assertRaises(ValidationError) as e:
            protocol.unpack(b'fs"ad')
Beispiel #2
0
    def test_repeat(self):
        class Pet(Message):
            sounds: Repeat[str]

        protocol = JSONProtocol(Pet)

        self.assertEqual(protocol.encode(Pet(['hiss!', '(slither)'])),
                         {'sounds': ['hiss!', '(slither)']})
        self.assertEqual(protocol.decode({"sounds": ['hiss!']}),
                         Pet(['hiss!']))
        self.assertEqual(protocol.decode({}), Pet())

        self.assertEqual(protocol.pack(Pet()), b'{}')
        self.assertEqual(protocol.pack(Pet([])), b'{"sounds":[]}')
        self.assertEqual(protocol.pack(Pet(['hiss!'])),
                         b'{"sounds":["hiss!"]}')

        self.assertEqual(protocol.unpack(b'{}'), Pet())
        self.assertEqual(protocol.unpack(b'{"sounds":["hiss!"]}'),
                         Pet(['hiss!']))
Beispiel #3
0
    def test_openapi_simple(self):
        class PetSimple(Message):
            id: int

        class PetServiceSimple(Service):
            class Meta:
                name = 'PetService'

            @http.GET('./pet/{id}')
            def get_pet(self, request: PetSimple) -> PetSimple:
                return request

            @http.POST('./pet')
            def create_pet_body(self, request: PetSimple) -> PetSimple:
                return request

        reflect = Reflect()
        reflect.add(PetServiceSimple)
        schema = make_openapi_schema(reflect)
        protocol = JSONProtocol(OpenAPISchema)
        with open(TEST_DIR + '/data/openapi_simple.json', 'rb') as f:
            schema_correct = protocol.unpack(f.read())
            self.assertEqual(schema.paths, schema_correct.paths)
            self.assertEqual(schema.definitions, schema_correct.definitions)