def test_forward_relationship_optimization_correct_representation(self): class EntrySerializer(ModelSerializer): blog = BlogSerializer() class Meta: model = Entry fields = '__all__' request_without_includes = Request(request_factory.get('/')) serializer = EntrySerializer( context={'request': request_without_includes}) result = serializer.to_representation(self.entry) # Remove non deterministic fields result.pop('created_at') result.pop('modified_at') expected = dict([ ('id', 1), ('blog', dict([('type', 'blogs'), ('id', 1)])), ('headline', 'headline'), ('body_text', 'body_text'), ('pub_date', DateField().to_representation(self.entry.pub_date)), ('mod_date', DateField().to_representation(self.entry.mod_date)), ('n_comments', 0), ('n_pingbacks', 0), ('rating', 3), ('authors', [ dict([('type', 'authors'), ('id', '1')]), dict([('type', 'authors'), ('id', '2')]), dict([('type', 'authors'), ('id', '3')]), dict([('type', 'authors'), ('id', '4')]), dict([('type', 'authors'), ('id', '5')]) ]) ]) self.assertDictEqual(expected, result)
def test_forward_relationship_optimization_correct_representation(self): class EntrySerializer(ModelSerializer): blog = BlogSerializer() class Meta: model = Entry fields = "__all__" request_without_includes = Request(request_factory.get("/")) serializer = EntrySerializer(context={"request": request_without_includes}) result = serializer.to_representation(self.entry) # Remove non deterministic fields result.pop("created_at") result.pop("modified_at") expected = dict( [ ("id", 1), ( "blog", dict( [ ("name", "Some Blog"), ("tags", []), ("copyright", datetime.now().year), ("url", "http://testserver/blogs/1"), ] ), ), ("headline", "headline"), ("body_text", "body_text"), ("pub_date", DateField().to_representation(self.entry.pub_date)), ("mod_date", DateField().to_representation(self.entry.mod_date)), ("n_comments", 0), ("n_pingbacks", 0), ("rating", 3), ( "authors", [ dict([("type", "authors"), ("id", "1")]), dict([("type", "authors"), ("id", "2")]), dict([("type", "authors"), ("id", "3")]), dict([("type", "authors"), ("id", "4")]), dict([("type", "authors"), ("id", "5")]), ], ), ] ) self.assertDictEqual(expected, result)