# Path: data_center/models.py
# class Course(models.Model):
#     """Course database schema"""
#     no = models.CharField(max_length=20, unique=True, db_index=True)
#     code = models.CharField(max_length=20, blank=True)
#     eng_title = models.CharField(max_length=200, blank=True)
#     chi_title = models.CharField(max_length=200, blank=True)
#     note = models.TextField(blank=True)
#     objective = models.CharField(max_length=80, blank=True)
#     time = models.CharField(max_length=20, blank=True)
#     time_token = models.CharField(max_length=20, blank=True)
#     teacher = models.CharField(max_length=40, blank=True)  # Only save Chinese
#     room = models.CharField(max_length=80, blank=True)
#     credit = models.IntegerField(default=0)
#     limit = models.IntegerField(default=0)
#     prerequisite = models.BooleanField(default=False, blank=True)
#     ys = models.CharField(max_length=10, blank=True)
# 
#     ge = models.CharField(max_length=80, blank=True)
# 
#     hit = models.IntegerField(default=0)
# 
#     syllabus = models.TextField(blank=True)  # pure text
#     has_attachment = models.BooleanField(default=False)  # has pdf
# 
#     def __str__(self):
#         return self.no
# 
#     @property
#     def attachment_url(self):
#         return attachment_url_format % urlquote(self.no)
# 
# class FlatPrerequisite(models.Model):
#     '''
#     store rendered prerequisite in the database
#     alternative: django.contrib.flatpage
#     '''
#     updated_at = models.DateTimeField(auto_now=True)
#     html = models.TextField()
# 
#     @classmethod
#     def update_html(cls, html):
#         if cls.objects.exists():
#             ins = cls.objects.get()
#             ins.html = html
#             ins.save()
#             return ins
#         else:
#             return cls.objects.create(html=html)


from django.test import TestCase
from data_center.models import Course, FlatPrerequisite



class CourseModelTest(TestCase):

    def test_course_can_be_created_with_only_no(self):
        Course.objects.create(no='12345QAQ 010101')

        self.assertEqual(1, Course.objects.count())


class FlatPrerequisiteModelTest(TestCase):

    def test_update_html_creates_object(self):
        FlatPrerequisite.update_html('<3')
        self.assertEqual(FlatPrerequisite.objects.count(), 1)

        self.assertEqual(FlatPrerequisite.objects.get().html, '<3')

    def test_update_html_updates_object(self):

# Path: data_center/models.py
# class Course(models.Model):
#     """Course database schema"""
#     no = models.CharField(max_length=20, unique=True, db_index=True)
#     code = models.CharField(max_length=20, blank=True)
#     eng_title = models.CharField(max_length=200, blank=True)
#     chi_title = models.CharField(max_length=200, blank=True)
#     note = models.TextField(blank=True)
#     objective = models.CharField(max_length=80, blank=True)
#     time = models.CharField(max_length=20, blank=True)
#     time_token = models.CharField(max_length=20, blank=True)
#     teacher = models.CharField(max_length=40, blank=True)  # Only save Chinese
#     room = models.CharField(max_length=80, blank=True)
#     credit = models.IntegerField(default=0)
#     limit = models.IntegerField(default=0)
#     prerequisite = models.BooleanField(default=False, blank=True)
#     ys = models.CharField(max_length=10, blank=True)
# 
#     ge = models.CharField(max_length=80, blank=True)
# 
#     hit = models.IntegerField(default=0)
# 
#     syllabus = models.TextField(blank=True)  # pure text
#     has_attachment = models.BooleanField(default=False)  # has pdf
# 
#     def __str__(self):
#         return self.no
# 
#     @property
#     def attachment_url(self):
#         return attachment_url_format % urlquote(self.no)
# 
# class FlatPrerequisite(models.Model):
#     '''
#     store rendered prerequisite in the database
#     alternative: django.contrib.flatpage
#     '''
#     updated_at = models.DateTimeField(auto_now=True)
#     html = models.TextField()
# 
#     @classmethod
#     def update_html(cls, html):
#         if cls.objects.exists():
#             ins = cls.objects.get()
#             ins.html = html
#             ins.save()
#             return ins
#         else:
#             return cls.objects.create(html=html)


# Path: data_center/tests.py
from django.test import TestCase
from data_center.models import Course, FlatPrerequisite




class CourseModelTest(TestCase):

    def test_course_can_be_created_with_only_no(self):
        Course.objects.create(no='12345QAQ 010101')

        self.assertEqual(1, Course.objects.count())


class FlatPrerequisiteModelTest(TestCase):

    def test_update_html_creates_object(self):
        FlatPrerequisite.update_html('<3')
        self.assertEqual(FlatPrerequisite.objects.count(), 1)

        self.assertEqual(FlatPrerequisite.objects.get().html, '<3')

    def test_update_html_updates_object(self):

        FlatPrerequisite.update_html('<3')