def test_add_get_favorite(self):
        """
        test adding a favorite for the user on FavoriteItem
        no duplicates for favorites!
        """
        favitem = FavoriteItem(name="Product 1")
        favitem.save()
        self.assertFalse(Favorites.objects.get_favorite(self.user, favitem))

        fav = Favorites.objects.add_favorite(self.user, favitem)
        fav_len = len(Favorites.objects.all())
        self.assertTrue(fav_len == 1)

        # should not duplicate
        if not Favorites.objects.get_favorite(self.user, favitem):
            termprint("WARNING", "Not found....adding")
            fav_duplicate = Favorites.objects.add_favorite(self.user, favitem)

        fav_len = len(Favorites.objects.all())
        termprint("INFO", "%s rows found " % fav_len)
        self.assertTrue(fav_len == 1)

        favget = Favorites.objects.get_favorite(self.user, favitem)
        self.assertTrue(favget)

        # test the templat tag get
        termprint("INFO", my_favorites(self.user))
        self.assertTrue(my_favorites(self.user))
 def test_ajax_urls(self):
     """
     Tests ajax url to check on item favorited stats.
     Checks is_favorited, add, remove.
     """
     # first create a sample item to be favorited
     favitem = FavoriteItem(name="Product 1")
     favitem.save()
     ctype = ContentType.objects.get_for_model(type(favitem)).pk
     client = Client()
     client.login(username=self.user.username, password="******")
     response = client.get(reverse("in_favs", kwargs={"content_type": ctype, "object_id": favitem.pk}))
     response_data = simplejson.loads(response.content)
     termprint("WARNING", response.content)
     self.assertFalse(response_data.get("in_favorites"))
     # add in a favorite
     fav = Favorites.objects.add_favorite(self.user, favitem)
     fav_len = len(Favorites.objects.all())
     # check again - should return true
     response = client.get(
         reverse("in_favs", kwargs={"content_type": ctype, "object_id": favitem.pk}),
         {},
         **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}
     )
     response_data = simplejson.loads(response.content)
     termprint("INFO", response.content)
     self.assertTrue(response_data.get("in_favorites"))
    def test_remove_get_favorite(self):
        """
        test adding a favorite for the user on FavoriteItem
        no duplicates for favorites!
        """
        favitem = FavoriteItem(name="Product 1")
        favitem.save()
        self.assertFalse(Favorites.objects.get_favorite(self.user, favitem))

        fav = Favorites.objects.add_favorite(self.user, favitem)
        fav_len = len(Favorites.objects.all())
        self.assertTrue(fav_len == 1)

        # delete it
        fav_del = Favorites.objects.delete_favorite(self.user, favitem)

        fav_len = len(Favorites.objects.all())
        self.assertTrue(fav_len == 0)

        termprint("INFO", my_favorites(self.user))
        self.assertFalse(my_favorites(self.user))