def test_from_model(self): page2 = RegPageModel.objects.create(pk=1, slug='foo2') v = AnyUrlValue.from_model(page2) self.assertEqual(v.url_type.prefix, 'any_urlfield.regpagemodel') # app_label.modelname self.assertEqual(v.type_value, page2.id) self.assertEqual(v.to_db_value(), 'any_urlfield.regpagemodel://1')
def test_resolve_objects(self): """ Make sure ID values are properly stored and serialized. """ page3 = RegPageModel.objects.create(slug='foo3') UrlModel.objects.create(url=AnyUrlValue.from_model(page3)) UrlModel.objects.create(url=AnyUrlValue.from_model(page3)) qs = list(UrlModel.objects.all()) with self.assertNumQueries(1): AnyUrlField.resolve_objects(qs) self.assertTrue(qs[0].url._resolved_objects) self.assertTrue(qs[1].url._resolved_objects) with self.assertNumQueries(0): for obj in qs: self.assertEqual(str(obj.url), '/modelform/') self.assertTrue(obj.url.exists()) self.assertEqual(obj.url.get_object(), page3)
def test_dumpdata(self): """ See if the dumpdata routines handle the value properly. """ page2 = RegPageModel.objects.create(slug='foo2') UrlModel.objects.create( url=AnyUrlValue.from_model(page2) ) json_data = serializers.serialize("json", UrlModel.objects.all()) data = json.loads(json_data) self.assertEqual(data, [{"fields": {"url": "any_urlfield.regpagemodel://1"}, "model": "any_urlfield.urlmodel", "pk": 1}])