# Path: examples/django_rest_framework/frontend/frontend/models.py
# class Account(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     email = models.CharField(max_length=30)
# 
#     api_base_name = 'accounts'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import AccountSerializer
#         return AccountSerializer
# 
# class Article(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     headline = models.CharField(max_length=100)
#     pub_date = models.DateField()
#     reporter = models.ForeignKey(Reporter, related_name='articles')
# 
#     api_base_name = 'articles'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import ArticleSerializer
#         return ArticleSerializer
# 
# class Tag(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     label = models.CharField(max_length=30)
#     articles = models.ManyToManyField(Article, related_name='tags')
# 
#     api_base_name = 'tags'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import TagSerializer
#         return TagSerializer
# 
# class Reporter(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     account = models.OneToOneField(Account)
#     first_name = models.CharField(max_length=30)
#     last_name = models.CharField(max_length=30)
# 
#     api_base_name = 'reporters'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import ReporterSerializer
#         return ReporterSerializer


from django.utils.timezone import now
from rest_framework.test import APITestCase
from django_roa.db.exceptions import ROAException
from .models import Account, Article, Tag, Reporter
        # TODO: doesn't work
        # account = Account.objects.get(id=1)
        # self.assertEqual(account.reporter.id, 1)
        # self.assertEqual(account.reporter.first_name, "John")

        # One to Many
        articles = reporter.articles.all()
        self.assertEqual(articles.count(), 2)
        self.assertEqual(articles[0].id, 1)
        self.assertEqual(articles[0].headline, "John's first story")
        self.assertEqual(articles[1].id, 2)
        self.assertEqual(articles[1].headline, "John's second story")

        # Many to one
        article = Article.objects.get(id=1)
        self.assertEqual(article.reporter.id, 1)
        self.assertEqual(article.reporter.first_name, "John")

        # Many to Many
        tags = articles[0].tags.all()
        self.assertEqual(tags.count(), 2)
        self.assertEqual(tags[0].id, 1)
        self.assertEqual(tags[0].label, 'news')
        self.assertEqual(tags[1].id, 2)
        self.assertEqual(tags[1].label, 'january')

        # Many to Many reversed
        tag = Tag.objects.get(id=1)
        self.assertEqual(tag.label, 'news')
        articles = tag.articles.all()

# Path: examples/django_rest_framework/frontend/frontend/models.py
# class Account(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     email = models.CharField(max_length=30)
# 
#     api_base_name = 'accounts'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import AccountSerializer
#         return AccountSerializer
# 
# class Article(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     headline = models.CharField(max_length=100)
#     pub_date = models.DateField()
#     reporter = models.ForeignKey(Reporter, related_name='articles')
# 
#     api_base_name = 'articles'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import ArticleSerializer
#         return ArticleSerializer
# 
# class Tag(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     label = models.CharField(max_length=30)
#     articles = models.ManyToManyField(Article, related_name='tags')
# 
#     api_base_name = 'tags'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import TagSerializer
#         return TagSerializer
# 
# class Reporter(CommonROAModel):
#     id = models.IntegerField(primary_key=True)  # don't forget it !
#     account = models.OneToOneField(Account)
#     first_name = models.CharField(max_length=30)
#     last_name = models.CharField(max_length=30)
# 
#     api_base_name = 'reporters'
# 
#     @classmethod
#     def serializer(cls):
#         from .serializers import ReporterSerializer
#         return ReporterSerializer


# Path: examples/django_rest_framework/frontend/frontend/tests.py
from django.utils.timezone import now
from rest_framework.test import APITestCase
from django_roa.db.exceptions import ROAException
from .models import Account, Article, Tag, Reporter

        # TODO: doesn't work
        # account = Account.objects.get(id=1)
        # self.assertEqual(account.reporter.id, 1)
        # self.assertEqual(account.reporter.first_name, "John")

        # One to Many
        articles = reporter.articles.all()
        self.assertEqual(articles.count(), 2)
        self.assertEqual(articles[0].id, 1)
        self.assertEqual(articles[0].headline, "John's first story")
        self.assertEqual(articles[1].id, 2)
        self.assertEqual(articles[1].headline, "John's second story")

        # Many to one
        article = Article.objects.get(id=1)
        self.assertEqual(article.reporter.id, 1)
        self.assertEqual(article.reporter.first_name, "John")

        # Many to Many
        tags = articles[0].tags.all()
        self.assertEqual(tags.count(), 2)
        self.assertEqual(tags[0].id, 1)
        self.assertEqual(tags[0].label, 'news')
        self.assertEqual(tags[1].id, 2)
        self.assertEqual(tags[1].label, 'january')

        # Many to Many reversed
        tag = Tag.objects.get(id=1)
        self.assertEqual(tag.label, 'news')
        articles = tag.articles.all()

        self.assertEqual(articles.count(), 3)