# Path: books/factories.py
# class DebtLoanFactory(factory.DjangoModelFactory):
#     with_who = "ACME co."
#     title = factory.Sequence(lambda n: 'debt_loan_%d' % n)
#     amount = 42
#     category = models.DebtLoan.DEBT
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.DebtLoan
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     title = factory.Sequence(lambda n: 'transaction_%d' % n)
#     amount = 10
#     category = models.Transaction.EXPENSE
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.Transaction
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     class Meta:
# class DebtLoanFactory(factory.DjangoModelFactory):
#     class Meta:
# 
# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(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):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# 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: books/services.py
# def get_months_transactions(user):
# def get_last_months_transactions(user):
# def get_this_years_transactions(user):


from datetime import datetime
from unittest import mock
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase
from books.factories import DebtLoanFactory
from books.factories import TransactionFactory
from books.factories import UserFactory
from books.models import DebtLoan
from books.models import Transaction
from books import services
import pytz
                qs,
                [self.this_month, self.last_month, self.this_year]
            )

    def test_all_time_transactions_list(self):
        with mock.patch('books.services.timezone') as mock_now:
            mock_now.now.return_value = datetime(2015, 4, 23, tzinfo=pytz.utc)

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

            response = c.get(reverse('transaction_list_filter'),
                             {'filter': 'all_time'},
                             follow=True)
            self.assertRedirects(response, reverse('transaction_list'))

            qs = response.context['transactions']
            self.assertSequenceEqual(
                qs,
                [self.this_month, self.last_month, self.this_year, self.last_year]
            )


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

    def test_create_model(self):
        self.assertEqual(0, DebtLoan.objects.count())

# Path: books/factories.py
# class DebtLoanFactory(factory.DjangoModelFactory):
#     with_who = "ACME co."
#     title = factory.Sequence(lambda n: 'debt_loan_%d' % n)
#     amount = 42
#     category = models.DebtLoan.DEBT
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.DebtLoan
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     title = factory.Sequence(lambda n: 'transaction_%d' % n)
#     amount = 10
#     category = models.Transaction.EXPENSE
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = models.Transaction
# 
# Path: books/factories.py
# class TransactionFactory(factory.DjangoModelFactory):
#     class Meta:
# class DebtLoanFactory(factory.DjangoModelFactory):
#     class Meta:
# 
# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(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):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# 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: books/services.py
# def get_months_transactions(user):
# def get_last_months_transactions(user):
# def get_this_years_transactions(user):


# Path: books/test_books.py
from datetime import datetime
from unittest import mock
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase
from books.factories import DebtLoanFactory
from books.factories import TransactionFactory
from books.factories import UserFactory
from books.models import DebtLoan
from books.models import Transaction
from books import services
import pytz

                qs,
                [self.this_month, self.last_month, self.this_year]
            )

    def test_all_time_transactions_list(self):
        with mock.patch('books.services.timezone') as mock_now:
            mock_now.now.return_value = datetime(2015, 4, 23, tzinfo=pytz.utc)

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

            response = c.get(reverse('transaction_list_filter'),
                             {'filter': 'all_time'},
                             follow=True)
            self.assertRedirects(response, reverse('transaction_list'))

            qs = response.context['transactions']
            self.assertSequenceEqual(
                qs,
                [self.this_month, self.last_month, self.this_year, self.last_year]
            )


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

    def test_create_model(self):
        self.assertEqual(0, DebtLoan.objects.count())

        DebtLoanFactory(title='first')