# Path: accounts/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     username = factory.Sequence(lambda n: 'admin%d' % n)
#     password = 'secret'
#     email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.username)
# 
#     class Meta:
#         model = User
# 
#     @classmethod
#     def _create(cls, model_class, *args, **kwargs):
#         manager = cls._get_manager(model_class)
#         return manager.create_user(*args, **kwargs)
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()


from django.contrib.auth.models import User
from django.core.management import call_command
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase
from accounts.factories import UserFactory
from books.models import Transaction



class HomePageTests(TestCase):
    def setUp(self):
        self.user = UserFactory()

    def test_home_not_logged_in(self):
        c = Client()
        response = c.get(reverse('home'))
        self.assertRedirects(response, reverse('login'))

    def test_home_logged_in(self):
        c = Client()
        logged_in = c.login(username=self.user.username, password='secret')
        self.assertTrue(logged_in)

        response = c.get(reverse('home'))
        self.assertRedirects(response, reverse('transaction_list'))


class PopulateTests(TestCase):
    def test_populate_command(self):
        call_command('populate')
        self.assertTrue(User.objects.get(username='admin'))

# Path: accounts/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     username = factory.Sequence(lambda n: 'admin%d' % n)
#     password = 'secret'
#     email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.username)
# 
#     class Meta:
#         model = User
# 
#     @classmethod
#     def _create(cls, model_class, *args, **kwargs):
#         manager = cls._get_manager(model_class)
#         return manager.create_user(*args, **kwargs)
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()


# Path: finance/test_finance.py
from django.contrib.auth.models import User
from django.core.management import call_command
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase
from accounts.factories import UserFactory
from books.models import Transaction




class HomePageTests(TestCase):
    def setUp(self):
        self.user = UserFactory()

    def test_home_not_logged_in(self):
        c = Client()
        response = c.get(reverse('home'))
        self.assertRedirects(response, reverse('login'))

    def test_home_logged_in(self):
        c = Client()
        logged_in = c.login(username=self.user.username, password='secret')
        self.assertTrue(logged_in)

        response = c.get(reverse('home'))
        self.assertRedirects(response, reverse('transaction_list'))


class PopulateTests(TestCase):
    def test_populate_command(self):
        call_command('populate')
        self.assertTrue(User.objects.get(username='admin'))

        self.assertEqual(Transaction.objects.count(), 20)