# Path: shownotes/models.py
# class Show(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.TextField()
#     last_updated = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.id, self.name)
# 
#     @classmethod
#     def exists(cls, number):
#         return Show.objects.filter(pk=number).count() == 1
# 
# class Topic(models.Model):
#     name = models.CharField(max_length=100)
# 
#     def note_count(self):
#         return Note.objects.filter(topic__id=self.id).count()
# 
#     def __unicode__(self):
#         return self.name
# 
#     @classmethod
#     def get_by_id(cls, *ids):
#         if len(ids) == 0:
#             return []
#         qs = [Q(id=x) for x in ids]
#         query = reduce(lambda x, y: x | y, qs)
#         return Topic.objects.filter(query)
# 
#     @classmethod
#     def exists(cls, name):
#         return Topic.objects.filter(name=name).count() > 0
# 
# class Note(models.Model):
#     show = models.ForeignKey(Show)
#     topic = models.ForeignKey(Topic)
#     title = models.TextField()
# 
#     def text_entry(self):
#         try:
#             return TextEntry.get_by_note(self)[0].text
#         except:
#             return ''
# 
#     def urls(self):
#         return UrlEntry.get_by_note(self)
# 
#     def __unicode__(self):
#         return u'{} [{}]: {}'.format(
#             self.topic, self.show.id, self.title[:20])
# 
#     @classmethod
#     def get_by_topic(cls, topic):
#         return Note.objects.filter(topic=topic)
# 
# class TextEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
# 
#     def text_html(self):
#         return self.text.replace('\n', '<br>')
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('text', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return TextEntry.objects.filter(note=note)
# 
# class UrlEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
#     url = models.TextField()
# 
#     def link(self):
#         return u'<a href={}>{}</a>'.format(self.url, self.url)
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('url', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return UrlEntry.objects.filter(note=note)


import unittest
from datetime import datetime
from shownotes.models import Show, Topic, Note, TextEntry, UrlEntry

    def test_show_to_unicode(self):
        show = Show(id=7, name='showname', last_updated=datetime.now())
        self.assertEqual(unicode(show), u'7: showname')

    def test_show_exists(self):
        Show(id=7, name='showname', last_updated=datetime.now()).save()
        self.assertIs(Show.exists(7), True)


class TopicTest(unittest.TestCase):
    def setUp(self):
        set_up_database()

    def tearDown(self):
        tear_down_database()

    def test_notes_to_retrieve(self):
        self.assertEqual(Topic.objects.get(id=1).note_count(), 3)
        self.assertEqual(Topic.objects.get(id=2).note_count(), 0)
        self.assertEqual(Topic.objects.get(id=3).note_count(), 0)

    def test_topic_to_unicode(self):
        self.assertEqual(unicode(Topic.objects.get(id=1)), u'topic name')

    def test_get_by_id(self):
        self.assertEqual(len(Topic.get_by_id()), 0)
        self.assertEqual(len(Topic.get_by_id(2)), 1)
        self.assertEqual(len(Topic.get_by_id(1, 3)), 2)
        self.assertEqual(len(Topic.get_by_id(1, 2, 3)), 3)

# Path: shownotes/models.py
# class Show(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.TextField()
#     last_updated = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.id, self.name)
# 
#     @classmethod
#     def exists(cls, number):
#         return Show.objects.filter(pk=number).count() == 1
# 
# class Topic(models.Model):
#     name = models.CharField(max_length=100)
# 
#     def note_count(self):
#         return Note.objects.filter(topic__id=self.id).count()
# 
#     def __unicode__(self):
#         return self.name
# 
#     @classmethod
#     def get_by_id(cls, *ids):
#         if len(ids) == 0:
#             return []
#         qs = [Q(id=x) for x in ids]
#         query = reduce(lambda x, y: x | y, qs)
#         return Topic.objects.filter(query)
# 
#     @classmethod
#     def exists(cls, name):
#         return Topic.objects.filter(name=name).count() > 0
# 
# class Note(models.Model):
#     show = models.ForeignKey(Show)
#     topic = models.ForeignKey(Topic)
#     title = models.TextField()
# 
#     def text_entry(self):
#         try:
#             return TextEntry.get_by_note(self)[0].text
#         except:
#             return ''
# 
#     def urls(self):
#         return UrlEntry.get_by_note(self)
# 
#     def __unicode__(self):
#         return u'{} [{}]: {}'.format(
#             self.topic, self.show.id, self.title[:20])
# 
#     @classmethod
#     def get_by_topic(cls, topic):
#         return Note.objects.filter(topic=topic)
# 
# class TextEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
# 
#     def text_html(self):
#         return self.text.replace('\n', '<br>')
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('text', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return TextEntry.objects.filter(note=note)
# 
# class UrlEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
#     url = models.TextField()
# 
#     def link(self):
#         return u'<a href={}>{}</a>'.format(self.url, self.url)
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('url', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return UrlEntry.objects.filter(note=note)


# Path: shownotes/tests/test_models.py
import unittest
from datetime import datetime
from shownotes.models import Show, Topic, Note, TextEntry, UrlEntry


    def test_show_to_unicode(self):
        show = Show(id=7, name='showname', last_updated=datetime.now())
        self.assertEqual(unicode(show), u'7: showname')

    def test_show_exists(self):
        Show(id=7, name='showname', last_updated=datetime.now()).save()
        self.assertIs(Show.exists(7), True)


class TopicTest(unittest.TestCase):
    def setUp(self):
        set_up_database()

    def tearDown(self):
        tear_down_database()

    def test_notes_to_retrieve(self):
        self.assertEqual(Topic.objects.get(id=1).note_count(), 3)
        self.assertEqual(Topic.objects.get(id=2).note_count(), 0)
        self.assertEqual(Topic.objects.get(id=3).note_count(), 0)

    def test_topic_to_unicode(self):
        self.assertEqual(unicode(Topic.objects.get(id=1)), u'topic name')

    def test_get_by_id(self):
        self.assertEqual(len(Topic.get_by_id()), 0)
        self.assertEqual(len(Topic.get_by_id(2)), 1)
        self.assertEqual(len(Topic.get_by_id(1, 3)), 2)
        self.assertEqual(len(Topic.get_by_id(1, 2, 3)), 3)

        self.assertEqual(len(Topic.get_by_id(1, 2, 3, 4)), 3)