Exemple #1
0
 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())
Exemple #2
0
 def test_detail_update(self):
     """Can we update a Post via PUT?"""
     post = PostFactory(title="first")
     count = Post.objects.count()
     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
     ]
     self.put(
         "api-post-detail",
         year=post.pub_date.year,
         month=post.pub_date.month,
         slug=post.slug,
         data=dict(
             remove_m2m(get_instance_data(post)),
             title="second",
             tags=tag_urls,
             startups=startup_urls,
         ),
     )
     self.response_200()
     self.assertEqual(count, Post.objects.count())
     post.refresh_from_db()
     self.assertEqual("second", post.title)
     self.assertCountEqual(tag_list, post.tags.all())
     self.assertCountEqual(startup_list, post.startups.all())
Exemple #3
0
 def test_list_create_m2m(self):
     """Can new Posts be related with tags & startups?"""
     count = Post.objects.count()
     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.build()
     self.post(
         "api-post-list",
         data=dict(
             remove_m2m(get_instance_data(post)),
             tags=tag_urls,
             startups=startup_urls,
         ),
     )
     self.response_201()
     self.assertEqual(count + 1, Post.objects.count())
     post = Post.objects.get(slug=post.slug, pub_date=post.pub_date)
     self.assertCountEqual(tag_list, post.tags.all())
     self.assertCountEqual(startup_list, post.startups.all())
Exemple #4
0
 def test_deserialization_partial_update_unknown_m2m(self):
     """Can we partially deserialize data to a Post model?"""
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug)
         for tag in TagFactory.build_batch(randint(1, 10))
     ]
     post = PostFactory()
     s_post = PostSerializer(
         instance=post,
         data=dict(
             # 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,
         ),
         partial=True,
         **context_kwarg(
             reverse(
                 "api-post-detail",
                 year=post.pub_date.year,
                 month=post.pub_date.month,
                 slug=post.slug,
                 full=True,
             )),
     )
     self.assertFalse(s_post.is_valid())
 def test_deserialization(self):
     """Can we deserialize data to a Startup model?"""
     startup_data = get_instance_data(StartupFactory.build())
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug)
         for tag in TagFactory.build_batch(3) + TagFactory.create_batch(2)
     ]
     data = dict(startup_data, tags=tag_urls)
     s_startup = StartupSerializer(data=data,
                                   **context_kwarg("/api/v1/startup/"))
     self.assertTrue(s_startup.is_valid(), msg=s_startup.errors)
     self.assertEqual(
         Startup.objects.count(),
         0,
         "Unexpected initial condition",
     )
     self.assertEqual(
         Tag.objects.count(),
         2,
         "Unexpected initial condition",
     )
     startup = s_startup.save()
     self.assertEqual(
         Startup.objects.count(),
         1,
         "Serialized Startup not saved",
     )
     self.assertCountEqual(
         startup.tags.values_list("slug", flat=True),
         [],
         "Startup had tags associated with it",
     )
     self.assertEqual(Tag.objects.count(), 2, "Serialized Tags saved")
 def test_tag_delete_post(self):
     """Can we submit a form to delete a Tag?"""
     tag = TagFactory()
     with perm_user(self, "organizer.delete_tag"):
         response = self.post("tag_delete", slug=tag.slug)
         self.assertRedirects(response, reverse("tag_list"))
         self.assertFalse(Tag.objects.filter(slug=tag.slug).exists())
 def test_delete_post(self):
     """Can we submit a form to delete a Startup?"""
     startup = StartupFactory()
     with perm_user(self, "organizer.delete_startup"):
         response = self.post("startup_delete", slug=startup.slug)
         self.assertRedirects(response, reverse("startup_list"))
         self.assertFalse(Startup.objects.filter(pk=startup.pk).exists())
Exemple #8
0
 def test_detail_update(self):
     """Can we update a Tag via PUT?"""
     tag = TagFactory(name="first")
     url = reverse("api-tag-detail", slug=tag.slug)
     self.put(url, data={"name": "second"})
     self.response_200()
     tag.refresh_from_db()
     self.assertEqual(tag.name, "second")
Exemple #9
0
 def test_detail_partial_update(self):
     """Can we update a Startup via PATCH?"""
     startup = StartupFactory(name="first")
     url = reverse("api-startup-detail", slug=startup.slug)
     self.patch(url, data={"name": "second"})
     self.response_200()
     startup.refresh_from_db()
     self.assertEqual(startup.name, "second")
Exemple #10
0
 def test_detail_update_404(self):
     """Do we generate 404 if startup not found?"""
     url = reverse("api-startup-detail", slug="nonexistent")
     self.put(
         url,
         data=get_instance_data(StartupFactory.build()),
     )
     self.response_404()
Exemple #11
0
    def test_detail_partial_update(self):
        """Can we update a Post via PATCH?

        The "gotcha" with patch is that it overwrites
        relations! This may be desirable, or it may be a
        disadvantage when compared to custom viewset
        actions.
        """
        post = PostFactory(
            title="first",
            tags=TagFactory.create_batch(randint(1, 10)),
            startups=StartupFactory.create_batch(randint(1, 10)),
        )
        count = Post.objects.count()
        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
        ]
        self.patch(
            "api-post-detail",
            year=post.pub_date.year,
            month=post.pub_date.month,
            slug=post.slug,
            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,
            ),
        )
        self.response_200()
        self.assertEqual(count, Post.objects.count())
        post.refresh_from_db()
        self.assertEqual("second", post.title)
        self.assertCountEqual(tag_list, post.tags.all())
        self.assertCountEqual(startup_list, post.startups.all())
Exemple #12
0
 def test_detail(self):
     """Is there a detail view for a Tag object"""
     tag = TagFactory()
     url = reverse("api-tag-detail", slug=tag.slug)
     self.get_check_200(url)
     self.assertCountEqual(
         self.response_json,
         TagSerializer(tag, **context_kwarg(url)).data,
     )
Exemple #13
0
 def test_detail(self):
     """Is there a detail view for a Startup object"""
     startup = StartupFactory()
     url = reverse("api-startup-detail", slug=startup.slug)
     self.get_check_200(url)
     self.assertCountEqual(
         self.response_json,
         StartupSerializer(startup, **context_kwarg(url)).data,
     )
 def test_serialization(self):
     """Does an existing Tag serialize correctly?"""
     tag = TagFactory()
     tag_url = reverse("api-tag-detail", slug=tag.slug, full=True)
     s_tag = TagSerializer(tag, **context_kwarg(tag_url))
     self.assertEqual(
         omit_keys("url", s_tag.data),
         omit_keys("id", get_instance_data(tag)),
     )
     self.assertEqual(s_tag.data["url"], tag_url)
 def test_serialization(self):
     """Does an existing Startup serialize correctly?"""
     tag_list = TagFactory.create_batch(3)
     startup = StartupFactory(tags=tag_list)
     startup_url = reverse(
         "api-startup-detail",
         slug=startup.slug,
         full=True,
     )
     s_startup = StartupSerializer(startup, **context_kwarg(startup_url))
     self.assertEqual(
         omit_keys("url", "tags", s_startup.data),
         omit_keys("id", "tags", get_instance_data(startup)),
     )
     tag_urls = [
         reverse("api-tag-detail", slug=tag.slug, full=True)
         for tag in tag_list
     ]
     self.assertCountEqual(s_startup.data["tags"], tag_urls)
     self.assertEqual(s_startup.data["url"], startup_url)
Exemple #16
0
 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
         ],
     )
Exemple #17
0
 def test_absolute_url(self):
     """Do Posts link to their detail view?"""
     p = PostFactory()
     self.assertEqual(
         p.get_absolute_url(),
         reverse(
             "post_detail",
             year=p.pub_date.year,
             month=p.pub_date.month,
             slug=p.slug,
         ),
     )
Exemple #18
0
 def test_list(self):
     """Is there a list of NewsLink objects"""
     url_name = "api-newslink-list"
     newslink_list = NewsLinkFactory.create_batch(10)
     self.get_check_200(url_name)
     self.assertCountEqual(
         self.response_json,
         NewsLinkSerializer(
             newslink_list,
             many=True,
             **context_kwarg(reverse(url_name)),
         ).data,
     )
Exemple #19
0
 def test_list(self):
     """Is there a list of Post objects"""
     url_name = "api-post-list"
     post_list = PostFactory.create_batch(10)
     self.get_check_200(url_name)
     self.assertCountEqual(
         self.response_json,
         PostSerializer(
             post_list,
             many=True,
             **context_kwarg(reverse(url_name)),
         ).data,
     )
Exemple #20
0
 def test_detail(self):
     """Is there a detail view for a NewsLink object"""
     newslink = NewsLinkFactory()
     url = reverse(
         "api-newslink-detail",
         startup_slug=newslink.startup.slug,
         newslink_slug=newslink.slug,
     )
     self.get_check_200(url)
     self.assertCountEqual(
         self.response_json,
         NewsLinkSerializer(newslink, **context_kwarg(url)).data,
     )
Exemple #21
0
 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())
Exemple #22
0
 def test_list(self):
     """Is there a list of Startup objects"""
     url_name = "api-startup-list"
     startup_list = StartupFactory.create_batch(10)
     self.get_check_200(url_name)
     self.assertCountEqual(
         self.response_json,
         StartupSerializer(
             startup_list,
             many=True,
             **context_kwarg(reverse(url_name)),
         ).data,
     )
Exemple #23
0
 def test_detail(self):
     """Is there a detail view for a Post object"""
     post = PostFactory()
     url = reverse(
         "api-post-detail",
         year=post.pub_date.year,
         month=post.pub_date.month,
         slug=post.slug,
     )
     self.get_check_200(url)
     self.assertCountEqual(
         self.response_json,
         PostSerializer(post, **context_kwarg(url)).data,
     )
Exemple #24
0
 def test_detail_update(self):
     """Can we update a Startup via PUT?"""
     startup = StartupFactory(name="first")
     url = reverse("api-startup-detail", slug=startup.slug)
     self.put(
         url,
         data={
             **get_instance_data(startup),
             "name": "second",
         },
     )
     self.response_200()
     startup.refresh_from_db()
     self.assertEqual(startup.name, "second")
 def test_deserialization(self):
     """Can we deserialize data to a NewsLink model?"""
     startup_url = reverse(
         "api-startup-detail",
         slug=StartupFactory().slug,
         full=True,
     )
     nl_data = omit_keys(
         "startup",
         get_instance_data(NewsLinkFactory.build()),
     )
     data = dict(**nl_data, startup=startup_url)
     s_nl = NewsLinkSerializer(data=data,
                               **context_kwarg("/api/v1/newslink/"))
     self.assertTrue(s_nl.is_valid(), msg=s_nl.errors)
Exemple #26
0
 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())
 def test_delete_post(self):
     """Can we submit a form to delete a Post?"""
     with perm_user(self, "blog.delete_post"):
         post = PostFactory()
         response = self.post(
             "post_delete",
             year=post.pub_date.year,
             month=post.pub_date.month,
             slug=post.slug,
         )
         self.assertRedirects(
             response, reverse("post_list")
         )
         self.assertFalse(
             Post.objects.filter(pk=post.pk).exists()
         )
Exemple #28
0
 def test_list_create(self):
     """Can articles be created by POST?"""
     startup = StartupFactory()
     newslink = NewsLinkFactory.build()
     nl_num = NewsLink.objects.count()
     self.post(
         "api-newslink-list",
         data={
             **get_instance_data(newslink),
             "startup":
             reverse(
                 "api-startup-detail",
                 slug=startup.slug,
                 full=True,
             ),
         },
     )
     self.assertEqual(nl_num + 1, NewsLink.objects.count())
     self.assertTrue(
         NewsLink.objects.filter(slug=newslink.slug,
                                 startup=startup).exists())
Exemple #29
0
 def test_detail_update(self):
     """Can we update an article via PUT?"""
     newslink = NewsLinkFactory(title="first")
     self.put(
         "api-newslink-detail",
         startup_slug=newslink.startup.slug,
         newslink_slug=newslink.slug,
         data={
             **get_instance_data(newslink),
             "title":
             "second",
             "startup":
             reverse(
                 "api-startup-detail",
                 slug=newslink.startup.slug,
                 full=True,
             ),
         },
     )
     self.response_200()
     newslink.refresh_from_db()
     self.assertEqual(newslink.title, "second")
Exemple #30
0
 def test_detail_update_404(self):
     """Do we generate 404 if tag not found?"""
     url = reverse("api-tag-detail", slug="nonexistent")
     self.put(url, data={"name": "second"})
     self.response_404()