def test_deserialization(self): """Can we deserialize data to a Post model?""" tag_list = TagFactory.create_batch(randint(1, 10)) tag_urls = [ reverse("api-tag-detail", slug=tag.slug) for tag in tag_list ] startup_list = StartupFactory.create_batch(randint(1, 10)) startup_urls = [ reverse("api-startup-detail", slug=startup.slug) for startup in startup_list ] post_data = remove_m2m(get_instance_data(PostFactory.build())) data = dict( **post_data, tags=tag_urls, startups=startup_urls, ) s_post = PostSerializer( data=data, **context_kwarg(reverse("api-post-list", full=True)), ) self.assertTrue(s_post.is_valid(), msg=s_post.errors) post = s_post.save() self.assertCountEqual(tag_list, post.tags.all()) self.assertCountEqual(startup_list, post.startups.all())
def test_update_post(self): """Can we submit a form to update posts?""" with perm_user(self, "blog.change_post"): post = PostFactory( tags=TagFactory.create_batch(randint(1, 5)), startups=StartupFactory.create_batch( randint(1, 5) ), ) self.assertNotEqual(post.title, "django") post_data = omit_keys( "id", get_instance_data(post) ) response = self.post( "post_update", year=post.pub_date.year, month=post.pub_date.month, slug=post.slug, data=dict(post_data, title="django"), ) post.refresh_from_db() self.assertEqual( post.title, "django", response.content.decode("utf8"), ) self.assertRedirects( response, post.get_absolute_url() )
def test_create_post(self): """Can we submit a form to create Posts?""" with perm_user(self, "blog.add_post"): post_num = Post.objects.count() tag = TagFactory() startup = StartupFactory() post_data = { **omit_keys( "id", get_instance_data(PostFactory.build()), ), "tags": [tag.pk], "startups": [startup.pk], } self.assertFalse( Post.objects.filter( slug=post_data["slug"] ).exists() ) response = self.post( "post_create", data=post_data ) self.assertEqual( Post.objects.count(), post_num + 1, response.content.decode("utf8"), ) post = Post.objects.get(slug=post_data["slug"]) self.assertIn(tag, post.tags.all()) self.assertIn(startup, post.startups.all()) self.assertRedirects( response, post.get_absolute_url() )
def setUpTestData(cls): """Generate test data for entire suite""" User = get_user_model() cls.test_user = User.objects.create_superuser( email="*****@*****.**", password="******" ) cls.p1_pk = PostFactory().pk cls.t1_pk = TagFactory().pk cls.s1_pk = StartupFactory().pk
def test_creation(self): """Can we save new posts based on input?""" tag = TagFactory() startup = StartupFactory() post = PostFactory.build() self.assertFalse(Post.objects.filter(slug=post.slug).exists()) bounded_form = PostForm( data={ **get_instance_data(post), "tags": [tag.pk], "startups": [startup.pk], }) self.assertTrue(bounded_form.is_valid(), bounded_form.errors) bounded_form.save() self.assertTrue(Post.objects.filter(slug=post.slug).exists())
def test_update(self): """Can we update posts based on input?""" tag = TagFactory() startup = StartupFactory() post = PostFactory(tags=[tag], startups=[startup]) self.assertNotEqual(post.title, "django") pform = PostForm( instance=post, data=dict( get_instance_data(post), title="django", tags=[tag.pk], startups=[startup.pk], ), ) self.assertTrue(pform.is_valid(), pform.errors) pform.save() post.refresh_from_db() self.assertEqual(post.title, "django")
def test_serialization(self): """Does an existing Posts serialize correctly?""" tag_list = TagFactory.create_batch(3) startup_list = StartupFactory.create_batch(2) post = PostFactory(tags=tag_list, startups=startup_list) post_ctxt = context_kwarg(f"/api/v1/startup/{post.slug}") s_post = PostSerializer(post, **post_ctxt) self.assertEqual( remove_m2m("url", s_post.data), remove_m2m("id", get_instance_data(post)), ) self.assertEqual( s_post.data["url"], reverse( "api-post-detail", year=post.pub_date.year, month=post.pub_date.month, slug=post.slug, full=True, ), ) self.assertCountEqual( s_post.data["tags"], [ reverse( "api-tag-detail", slug=tag.slug, full=True, ) for tag in tag_list ], ) self.assertCountEqual( s_post.data["startups"], [ reverse( "api-startup-detail", slug=startup.slug, full=True, ) for startup in startup_list ], )
def test_deserialization_partial_update(self): """Can we partially deserialize data to a Post model?""" tag_list = TagFactory.create_batch(randint(1, 10)) tag_urls = [ reverse("api-tag-detail", slug=tag.slug) for tag in tag_list ] startup_list = StartupFactory.create_batch(randint(1, 10)) startup_urls = [ reverse("api-startup-detail", slug=startup.slug) for startup in startup_list ] post = PostFactory(title="first", tags=TagFactory.create_batch(3)) s_post = PostSerializer( instance=post, data=dict( title="second", # necessary due to bug in DRF # https://github.com/encode/django-rest-framework/issues/6341 slug=post.slug, pub_date=post.pub_date, # remove above once DRF fixed tags=tag_urls, startups=startup_urls, ), partial=True, **context_kwarg( reverse( "api-post-detail", year=post.pub_date.year, month=post.pub_date.month, slug=post.slug, full=True, )), ) self.assertTrue(s_post.is_valid(), msg=s_post.errors) post = s_post.save() self.assertEqual("second", post.title) self.assertCountEqual(tag_list, post.tags.all()) self.assertCountEqual(startup_list, post.startups.all())
def test_delete(self): """Does deleting a post leave related objects?""" tags = TagFactory.create_batch(5) startups = StartupFactory.create_batch(3, tags=tags) post = PostFactory(tags=tags, startups=startups) self.assertIn(tags[0], post.tags.all()) self.assertIn(startups[0], post.startups.all()) self.assertEqual( Tag.objects.count(), 5, "Unexpected initial condition", ) self.assertEqual( Startup.objects.count(), 3, "Unexpected initial condition", ) post.delete() self.assertEqual(Tag.objects.count(), 5, "Unexpected change") self.assertEqual(Startup.objects.count(), 3, "Unexpected change")
def test_deserialization_update(self): """Can we deserialize to an existing Post model?""" tag_list = TagFactory.create_batch(randint(1, 10)) tag_urls = [ reverse("api-tag-detail", slug=tag.slug) for tag in tag_list ] startup_list = StartupFactory.create_batch(randint(1, 10)) startup_urls = [ reverse("api-startup-detail", slug=startup.slug) for startup in startup_list ] post = PostFactory(title="first", tags=TagFactory.create_batch(3)) post_data = remove_m2m(get_instance_data(post)) data = dict( post_data, title="second", tags=tag_urls, startups=startup_urls, ) s_post = PostSerializer( post, data=data, **context_kwarg( reverse( "api-post-detail", year=post.pub_date.year, month=post.pub_date.month, slug=post.slug, full=True, )), ) self.assertTrue(s_post.is_valid(), msg=s_post.errors) post = s_post.save() self.assertEqual("second", post.title) self.assertCountEqual(tag_list, post.tags.all()) self.assertCountEqual(startup_list, post.startups.all())