# Path: shiori/bookmark/models.py
# class Category(BaseObject):
#     """ Category model """
#     category = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'category'
# 
#     def __unicode__(self):
#         return self.category
# 
# class Tag(BaseObject):
#     """ Tag model """
#     tag = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'tag'
# 
#     def __unicode__(self):
#         return self.tag
# 
# class Bookmark(BaseObject):
#     """ Bookmark model """
#     url = models.URLField()
#     title = models.CharField(max_length=255)
#     category = models.ForeignKey(Category)
#     tags = models.ManyToManyField(Tag, through='BookmarkTag')
#     registered_datetime = models.DateTimeField(auto_now=True,
#                                                auto_now_add=True)
#     description = models.TextField(blank=True)
#     owner = models.ForeignKey(User)
#     is_hide = models.BooleanField(default=False)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'bookmark'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         """ bookmark permalink path """
#         return "/shiori/b/%s" % self.id
# 
# Path: shiori/bookmark/feed_generator.py
# class LatestEntries(Feed):
#     """ Feed generator class """
#     title = 'Shiori new bookmarks'
#     link = '/shiori/'
#     description = 'Updates on changes and additions to Shiori.'
#     description_template = 'feeds/latest_title.html'
# 
#     def items(self):
#         """ Retrieve latest 5 bookmarks """
#         return Bookmark.objects.order_by('-registered_datetime')[:5]


from django.core.urlresolvers import resolve
from django.test import TestCase, TransactionTestCase
from django.contrib.auth.models import User
from django.views.generic import RedirectView
from django.db import IntegrityError
from shiori.bookmark.models import Category, Tag, Bookmark
from shiori.bookmark.feed_generator import LatestEntries
import shiori.bookmark.views
import shiori_tests.tests.vars as v
# -*- coding: utf-8 -*-


class BookmarkTransactionTest(TransactionTestCase):
    fixtures = ['shiori_tests/test_data/dummy_users.json',
                'shiori_tests/test_data/dummy_data.json']

    def setUp(self):
        self.user0 = User.objects.get(pk=1)
        self.user1 = User.objects.get(pk=2)
        categories = Category.objects.all()
        self.category0 = categories[0]
        self.category1 = categories[1]
        tags = Tag.objects.all()
        self.tag0 = tags[0]
        self.tag1 = tags[1]
        self.bookmark = Bookmark.objects.create(url=v.url0,
                                                title=v.title0,
                                                category=self.category0,
                                                description=v.description0,
                                                owner=self.user0)

    def test_get_categories(self):
        query = Category.objects.get(category=self.category0.category)
        self.assertEqual(query.__unicode__(), self.category0.category)
        self.assertEqual(query.__str__(), self.category0.category)

    def test_save_category_confilict(self):
        with self.assertRaises(IntegrityError):

# Path: shiori/bookmark/models.py
# class Category(BaseObject):
#     """ Category model """
#     category = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'category'
# 
#     def __unicode__(self):
#         return self.category
# 
# class Tag(BaseObject):
#     """ Tag model """
#     tag = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'tag'
# 
#     def __unicode__(self):
#         return self.tag
# 
# class Bookmark(BaseObject):
#     """ Bookmark model """
#     url = models.URLField()
#     title = models.CharField(max_length=255)
#     category = models.ForeignKey(Category)
#     tags = models.ManyToManyField(Tag, through='BookmarkTag')
#     registered_datetime = models.DateTimeField(auto_now=True,
#                                                auto_now_add=True)
#     description = models.TextField(blank=True)
#     owner = models.ForeignKey(User)
#     is_hide = models.BooleanField(default=False)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'bookmark'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         """ bookmark permalink path """
#         return "/shiori/b/%s" % self.id
# 
# Path: shiori/bookmark/feed_generator.py
# class LatestEntries(Feed):
#     """ Feed generator class """
#     title = 'Shiori new bookmarks'
#     link = '/shiori/'
#     description = 'Updates on changes and additions to Shiori.'
#     description_template = 'feeds/latest_title.html'
# 
#     def items(self):
#         """ Retrieve latest 5 bookmarks """
#         return Bookmark.objects.order_by('-registered_datetime')[:5]


# Path: shiori_tests/tests/test_bookmark.py
from django.core.urlresolvers import resolve
from django.test import TestCase, TransactionTestCase
from django.contrib.auth.models import User
from django.views.generic import RedirectView
from django.db import IntegrityError
from shiori.bookmark.models import Category, Tag, Bookmark
from shiori.bookmark.feed_generator import LatestEntries
import shiori.bookmark.views
import shiori_tests.tests.vars as v

# -*- coding: utf-8 -*-


class BookmarkTransactionTest(TransactionTestCase):
    fixtures = ['shiori_tests/test_data/dummy_users.json',
                'shiori_tests/test_data/dummy_data.json']

    def setUp(self):
        self.user0 = User.objects.get(pk=1)
        self.user1 = User.objects.get(pk=2)
        categories = Category.objects.all()
        self.category0 = categories[0]
        self.category1 = categories[1]
        tags = Tag.objects.all()
        self.tag0 = tags[0]
        self.tag1 = tags[1]
        self.bookmark = Bookmark.objects.create(url=v.url0,
                                                title=v.title0,
                                                category=self.category0,
                                                description=v.description0,
                                                owner=self.user0)

    def test_get_categories(self):
        query = Category.objects.get(category=self.category0.category)
        self.assertEqual(query.__unicode__(), self.category0.category)
        self.assertEqual(query.__str__(), self.category0.category)

    def test_save_category_confilict(self):
        with self.assertRaises(IntegrityError):

            query = Category(category=self.category0.category)