Exemplo n.º 1
0
    def test_from_response_with_relationships(self):
        response_content = data.JsonApiResponse(
            data=data.JsonApiObject(
                type='test',
                relationships={
                    'other':
                    data.Relationship(
                        data=data.ResourceIdentifier(id='1', type='test'))
                }),
            included=[
                mock.MagicMock(id='1',
                               type='test',
                               attributes={'name': 'alice'})
            ])
        orm_api = orm.OrmApi(mock.MagicMock())

        class Test(orm.ApiModel):
            class Meta:
                api = orm_api
                type = 'test'

            other = orm.RelationField(source='other')
            name = orm.AttributeField(source='name')

        test = Test.from_response_content(response_content)
        assert test.other.name == 'alice'
Exemplo n.º 2
0
    def test_refresh_with_relationships(self):
        mock_api = mock.Mock()
        mock_api.endpoint.return_value.get.return_value.content = data.JsonApiResponse(
            data=data.JsonApiObject(
                type='test',
                id='123',
                relationships={
                    'other':
                    data.Relationship(
                        data=data.ResourceIdentifier(id='1', type='test'))
                }),
            included=[
                mock.MagicMock(id='1',
                               type='test',
                               attributes={'name': 'alice'})
            ])
        orm_api = orm.OrmApi(mock_api)

        class Test(orm.ApiModel):
            class Meta:
                api = orm_api
                type = 'test'

            other = orm.RelationField(source='other')
            name = orm.AttributeField(source='name')

        test = Test.from_id('123')
        test.refresh()

        assert test.other.name == 'alice'
Exemplo n.º 3
0
    def test_from_response_with_relationship_with_links_only(self):
        mock_api = mock.MagicMock()
        mock_api.endpoint().get().data = data.ResourceIdentifier(type='test',
                                                                 id='159')
        response_content = data.JsonApiResponse(data=data.JsonApiObject(
            id='1',
            type='test',
            relationships={
                'other':
                data.Relationship(links=data.Dictionary(
                    self='http://example.org/api/rest/test/1/relationships'
                    '/vendor',
                    related='http://example.org/api/rest/test/1/vendor'))
            }))
        orm_api = orm.OrmApi(mock_api)

        class Test(orm.ApiModel):
            class Meta:
                api = orm_api
                type = 'test'

            other = orm.RelationField(source='other')
            name = orm.AttributeField(source='name')

        test = Test.from_response_content(response_content)
        assert test.other.type is 'test'
        assert test.other.id is '159'
Exemplo n.º 4
0
    def test_from_response_with_relationship_with_none_type(self):
        response_content = data.JsonApiResponse(
            data=data.JsonApiObject(id='1', type='test', relationships={
                'other': data.Relationship(data=data.ResourceIdentifier(id=None, type=None))
            })
        )
        orm_api = orm.OrmApi(mock.MagicMock())

        class Test(orm.ApiModel):
            class Meta:
                api = orm_api
                type = 'test'
            other = orm.RelationField(source='other')
            name = orm.AttributeField(source='name')

        test = Test.from_response_content(response_content)
        assert test.other is None
Exemplo n.º 5
0
    def test_issue_19_attributes_are_readable_with_multiple_relations(self):
        response_content = data.JsonApiResponse(
            data=data.JsonApiObject(
                type='designs',
                relationships={'sub_designs': data.Relationship(data=[data.ResourceIdentifier(id=3, type='designs')])},
                attributes={'name': 'doctor_x'}
            ),
        )
        orm_api = orm.OrmApi(mock.MagicMock())

        class Design(orm.ApiModel):
            class Meta:
                type = 'designs'
                api = orm_api

            name = orm.AttributeField('name')
            sub_designs = orm.RelationField('sub_designs')

        design = Design.from_response_content(response_content)
        assert design.name == 'doctor_x'
Exemplo n.º 6
0
 def set(self, value):
     self.instance.relationships[self.source] = data.Relationship(data=value.as_identifier())
     self.cache.set_cache(value)
Exemplo n.º 7
0
 def set(self, values):
     self.instance.relationships[self.source] = data.Relationship(data=[value.as_identifier() for value in values])
     self.cache.set_cache(values)
Exemplo n.º 8
0
 def as_relationship(self):
     return data.Relationship(data=data.ResourceIdentifier(type=self.type, id=self.id))