Exemplo n.º 1
0
 def test_resource_object_constructor__with_relationships(self):
     ItemResponse = JsonApiResponse('item', ItemModel)
     item = ItemModel(name='pear', price=1.2, quantity=10)
     document = ItemResponse.resource_object(id='abc123',
                                             attributes=item,
                                             relationships={
                                                 'sold_at': {
                                                     'data': {
                                                         'type': 'store',
                                                         'id': 'def456'
                                                     }
                                                 }
                                             }).dict()
     assert document == {
         'id': 'abc123',
         'type': 'item',
         'attributes': {
             'name': 'pear',
             'price': 1.2,
             'quantity': 10,
         },
         'relationships': {
             'sold_at': {
                 'data': {
                     'id': 'def456',
                     'type': 'store',
                     'meta': None,
                 },
                 'links': None,
                 'meta': None,
             }
         }
     }
Exemplo n.º 2
0
 def test_resource_object_constructor__with_invalid_links(self):
     ItemResponse = JsonApiResponse('item', ItemModel)
     item = ItemModel(name='pear', price=1.2, quantity=10)
     with raises(ValidationError) as e:
         ItemResponse.resource_object(
             id='abc123',
             attributes=item,
             links='/items/abc123',
         )
     assert e.value.errors() == [
         {
             'loc': ('links', ),
             'msg': 'value is not a valid dict',
             'type': 'type_error.dict'
         },
     ]
Exemplo n.º 3
0
 def test_resource_object_constructor__with_invalid_relationship(self):
     ItemResponse = JsonApiResponse('item', ItemModel)
     item = ItemModel(name='pear', price=1.2, quantity=10)
     with raises(ValidationError) as e:
         ItemResponse.resource_object(
             id='abc123',
             attributes=item,
             relationships={'sold_at': {
                 'meta': 'rofl'
             }})
     assert e.value.errors() == [
         {
             'loc': ('relationships', 'sold_at', 'meta'),
             'msg': 'value is not a valid dict',
             'type': 'type_error.dict'
         },
     ]
Exemplo n.º 4
0
 def test_resource_object_constructor__no_attributes(self):
     IdentifierResponse = JsonApiResponse('item', dict)
     document = IdentifierResponse.resource_object(id='abc123').dict()
     assert document == {
         'id': 'abc123',
         'type': 'item',
         'attributes': {},
         'relationships': None
     }
Exemplo n.º 5
0
    def test_response_constructed_with_resource_object__list(self):
        @dataclass
        class FakeDBItem:
            item_id: int
            name: str
            price: float
            quantity: int
            created_at: datetime = datetime.utcnow()

        ItemResponse = JsonApiResponse('item',
                                       ItemModelWithOrmMode,
                                       use_list=True)
        items = [
            FakeDBItem(item_id=1, name='apple', price=1.5, quantity=3),
            FakeDBItem(item_id=2, name='pear', price=1.2, quantity=10),
            FakeDBItem(item_id=3, name='orange', price=2.2, quantity=5)
        ]
        response = ItemResponse(data=[
            ItemResponse.resource_object(id=item.item_id, attributes=item)
            for item in items
        ])
        assert response.dict() == {
            'data': [
                {
                    'id': '1',
                    'type': 'item',
                    'attributes': {
                        'name': 'apple',
                        'price': 1.5,
                        'quantity': 3,
                    },
                },
                {
                    'id': '2',
                    'type': 'item',
                    'attributes': {
                        'name': 'pear',
                        'price': 1.2,
                        'quantity': 10,
                    },
                },
                {
                    'id': '3',
                    'type': 'item',
                    'attributes': {
                        'name': 'orange',
                        'price': 2.2,
                        'quantity': 5,
                    },
                },
            ]
        }
Exemplo n.º 6
0
 def test_resource_object_constructor__with_list_response(self):
     ItemResponse = JsonApiResponse('item', ItemModel, use_list=True)
     item = ItemModel(name='pear', price=1.2, quantity=10)
     document = ItemResponse.resource_object(id='abc123',
                                             attributes=item).dict()
     assert document == {
         'id': 'abc123',
         'type': 'item',
         'attributes': {
             'name': 'pear',
             'price': 1.2,
             'quantity': 10,
         },
         'relationships': None
     }
Exemplo n.º 7
0
 def test_response_constructed_with_resource_object(self):
     ItemResponse = JsonApiResponse('item', ItemModel)
     item = ItemModel(name='pear', price=1.2, quantity=10)
     data = ItemResponse.resource_object(id='abc123',
                                         attributes=item).dict()
     assert ItemResponse(data=data).dict() == {
         'data': {
             'id': 'abc123',
             'type': 'item',
             'attributes': {
                 'name': 'pear',
                 'price': 1.2,
                 'quantity': 10,
             },
         }
     }