# 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


class ROATestCase(APITestCase):

    def test_all(self):

        #
        # Get
        #

        account = Account.objects.get(id=1)
        self.assertEqual(account.id, 1)
        self.assertEqual(account.email, 'john@example.com')

        account = Account.objects.get(email='paul@example.com')
        self.assertEqual(account.id, 2)
        self.assertEqual(account.email, 'paul@example.com')

        # All
        accounts = Account.objects.all()
        self.assertEqual(accounts.count(), 2)
        self.assertEqual(accounts[0].id, 1)
        self.assertEqual(accounts[0].email, 'john@example.com')
        self.assertEqual(accounts[1].id, 2)
        self.assertEqual(accounts[1].email, 'paul@example.com')

        # filter

# 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



class ROATestCase(APITestCase):

    def test_all(self):

        #
        # Get
        #

        account = Account.objects.get(id=1)
        self.assertEqual(account.id, 1)
        self.assertEqual(account.email, 'john@example.com')

        account = Account.objects.get(email='paul@example.com')
        self.assertEqual(account.id, 2)
        self.assertEqual(account.email, 'paul@example.com')

        # All
        accounts = Account.objects.all()
        self.assertEqual(accounts.count(), 2)
        self.assertEqual(accounts[0].id, 1)
        self.assertEqual(accounts[0].email, 'john@example.com')
        self.assertEqual(accounts[1].id, 2)
        self.assertEqual(accounts[1].email, 'paul@example.com')

        # filter

        accounts = Account.objects.filter(email='paul@example.com')