# 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
        count_accounts = Account.objects.count()
        count_reporters = Reporter.objects.count()
        count_articles = Article.objects.count()

        new_account = Account()
        new_account.email = 'james@example.com'
        new_account.save()

        new_reporter = Reporter()
        new_reporter.account = new_account
        new_reporter.first_name = 'James'
        new_reporter.last_name = 'Doe'
        new_reporter.save()

        new_article = Article()
        new_article.headline = "James's story"
        new_article.reporter = new_reporter
        self.assertRaises(ROAException, new_article.save)  # pub_date is required
        new_article.pub_date = now().date()
        new_article.save()

        # There are 3 lines where new_account is save:
        # new_account.save()
        # new_reporter.save()
        # new_article.save()
        # But only one instance must be save into db.
        # TODO: goal: only one record.
        self.assertEqual(Account.objects.count(), count_accounts + 3)
        # Same case:
        self.assertEqual(Reporter.objects.count(), count_reporters + 2)

# 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

        count_accounts = Account.objects.count()
        count_reporters = Reporter.objects.count()
        count_articles = Article.objects.count()

        new_account = Account()
        new_account.email = 'james@example.com'
        new_account.save()

        new_reporter = Reporter()
        new_reporter.account = new_account
        new_reporter.first_name = 'James'
        new_reporter.last_name = 'Doe'
        new_reporter.save()

        new_article = Article()
        new_article.headline = "James's story"
        new_article.reporter = new_reporter
        self.assertRaises(ROAException, new_article.save)  # pub_date is required
        new_article.pub_date = now().date()
        new_article.save()

        # There are 3 lines where new_account is save:
        # new_account.save()
        # new_reporter.save()
        # new_article.save()
        # But only one instance must be save into db.
        # TODO: goal: only one record.
        self.assertEqual(Account.objects.count(), count_accounts + 3)
        # Same case:
        self.assertEqual(Reporter.objects.count(), count_reporters + 2)

        self.assertEqual(Article.objects.count(), count_articles + 1)