def test_running_today(): user = UserFactory() TimeRecordFactory(title='t1', user=user, end_time=None) TimeRecordFactory(title='t2', user=user, end_time=time(11, 0)) d = timezone.now().date() + relativedelta(days=7) TimeRecordFactory(title='t3', date_started=d, user=user, end_time=None) TimeRecordFactory(title='t4', user=UserFactory(), end_time=None) qs = TimeRecord.objects.running_today(user).order_by('title') assert ['t1'] == [obj.title for obj in qs]
def setup_users(self): """Using factories - set-up users for permissions test cases.""" UserFactory(username='******', email='*****@*****.**', is_staff=True, is_superuser=True) UserFactory(username='******', email='*****@*****.**', is_staff=True) UserFactory(username='******', email='*****@*****.**', first_name='William', last_name='Webber')
def test_report_time_by_ticket(): user = UserFactory(username='******') d = timezone.now().date() t1 = TicketFactory(pk=1, contact=ContactFactory()) TimeRecordFactory( ticket=t1, date_started=d, start_time=time(11, 0), end_time=time(11, 30), user=user, ) TimeRecordFactory( ticket=TicketFactory(pk=2, contact=ContactFactory()), date_started=d, start_time=time(10, 0), end_time=time(10, 15), user=user, ) # another date (so not included) TimeRecordFactory( ticket=t1, date_started=d + relativedelta(days=-1), start_time=time(12, 0), end_time=time(12, 10), user=UserFactory(), ) # another user (so not included) TimeRecordFactory( ticket=t1, date_started=d, start_time=time(12, 0), end_time=time(12, 10), user=UserFactory(), ) TimeRecordFactory( ticket=t1, date_started=d, start_time=time(12, 0), end_time=time(12, 10), user=user, ) data = TimeRecord.objects.report_time_by_ticket(user, d) assert { 1: { 'Chargeable': 40.0, 'Fixed-Price': 0, 'Non-Chargeable': 0 }, 2: { 'Chargeable': 15.0, 'Fixed-Price': 0, 'Non-Chargeable': 0 }, } == data
def test_create_invoices_only_billable_time(): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) TimeRecordFactory( ticket=ticket, date_started=date(2012, 7, 1), billable=False, ) InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count()
def api_client(): """Create an admin user, and login using the token.""" user = UserFactory(username='******', is_staff=True) token = Token.objects.create(user=user) client = APIClient() client.credentials(HTTP_AUTHORIZATION='Token {}'.format(token.key)) yield client
def test_set_deleted_already_deleted(): obj = LemonCakeFactory() user = UserFactory() obj.set_deleted(user) obj.refresh_from_db() with pytest.raises(BaseError) as e: obj.set_deleted(user) assert 'is already deleted' in str(e.value)
def test_start_and_stop_too_many(): user = UserFactory() TimeRecordFactory(user=user, end_time=None) TimeRecordFactory(user=user, end_time=None) quick = QuickTimeRecordFactory(user=user) ticket = TicketFactory() with pytest.raises(InvoiceError) as e: TimeRecord.objects.start(ticket, quick) assert 'Cannot start a time record when 2 are already' in str(e.value)
def test_set_deleted(): obj = FruitCakeFactory() assert 0 == obj.deleted_version user = UserFactory() FruitCake.objects.set_deleted(obj, user) obj.refresh_from_db() assert obj.deleted is True assert obj.user_deleted == user assert obj.date_deleted is not None assert 1 == obj.deleted_version
def test_start(): user = UserFactory() quick = QuickTimeRecordFactory(user=user) ticket = TicketFactory() time_record = TimeRecord.objects.start(ticket, quick) assert quick.time_code == time_record.time_code assert ticket == time_record.ticket assert time_record.end_time is None assert time_record.start_time is not None assert quick.user == time_record.user
def test_invoice_create_pdf(self): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2013, 12, 1)) invoice = InvoiceCreate().create(UserFactory(), contact, date(2013, 12, 31)) InvoicePrint().create_pdf(invoice, None)
def test_undelete(): obj = LemonCakeFactory() user = UserFactory() obj.set_deleted(user) obj.refresh_from_db() obj.undelete() obj.refresh_from_db() assert obj.deleted is False assert obj.user_deleted is None assert obj.date_deleted is None
def test_report_total_by_user(): contact = ContactFactory() invoice = InvoiceFactory(contact=contact) InvoiceSettingsFactory() # no time records InvoiceLineFactory(invoice=invoice) # u1's time records invoice_line = InvoiceLineFactory(invoice=invoice) t1 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t1, user=UserFactory(username='******'), invoice_line=invoice_line) # u2's time records invoice_line = InvoiceLineFactory(invoice=invoice) u2 = UserFactory(username='******') t2 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t2, user=u2, invoice_line=invoice_line) invoice_line = InvoiceLineFactory(invoice=invoice) t3 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t3, user=u2, invoice_line=invoice_line) result = invoice.time_analysis() # invoice has a line with no time records assert '' in result # fred recorded time on one ticket assert 'u1' in result u1 = result['u1'] assert 1 == len(u1) assert t1.pk in u1 # sara recorded time on two tickets assert 'u2' in result u2 = result['u2'] assert 2 == len(u2) assert t2.pk in u2 assert t3.pk in u2 # web user added an invoice line, but didn't record time assert 'web' not in result # check net total matches invoice net = Decimal() for user, tickets in result.items(): for ticket_pk, totals in tickets.items(): net = net + totals['net'] assert invoice.net == net
def test_set_deleted(): obj = LemonCakeFactory() user = UserFactory() before = timezone.now() obj.set_deleted(user) after = timezone.now() obj.refresh_from_db() assert obj.deleted is True assert obj.user_deleted == user assert obj.date_deleted >= before assert obj.date_deleted <= after
def test_undelete(): obj = FruitCakeFactory() user = UserFactory() FruitCake.objects.set_deleted(obj, user) assert obj.deleted_version > 0 obj.refresh_from_db() obj.undelete() obj.refresh_from_db() assert obj.deleted is False assert obj.user_deleted is None assert obj.date_deleted is None assert 0 == obj.deleted_version
def test_report_time_by_user_by_week(): user = UserFactory(username='******') from_date = datetime(2015, 12, 20, 0, 0, 0, tzinfo=pytz.utc) to_date = datetime(2016, 1, 7, 0, 0, 0, tzinfo=pytz.utc) TimeRecordFactory( billable=True, date_started=datetime(2015, 12, 21, 6, 0, 0, tzinfo=pytz.utc), start_time=time(11, 0), end_time=time(11, 30), user=user, ) TimeRecordFactory( billable=True, date_started=datetime(2016, 1, 5, 6, 0, 0, tzinfo=pytz.utc), start_time=time(10, 0), end_time=time(10, 3), user=user, ) TimeRecordFactory( billable=True, date_started=datetime(2016, 1, 7, 6, 0, 0, tzinfo=pytz.utc), start_time=time(10, 0), end_time=time(10, 4), user=UserFactory(), ) TimeRecordFactory( billable=False, date_started=datetime(2016, 1, 6, 6, 0, 0, tzinfo=pytz.utc), start_time=time(10, 0), end_time=time(10, 15), user=user, ) data = TimeRecord.objects.report_time_by_user_by_week( from_date, to_date, user) assert { '2015_51': 30, '2015_52': 0, '2016_01': 18, '2016_02': 0, } == data
def test_create_invoices_do_not_bill_twice(): """Check we can't include the time records more than once""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) user = UserFactory() InvoiceCreateBatch().create(user, date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count() InvoiceCreateBatch().create(user, date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count()
def test_report_time_by_user(): green = UserFactory(username='******') red = UserFactory(username='******') to_date = timezone.now() from_date = to_date + relativedelta(months=-1) d = from_date + relativedelta(days=7) TimeRecordFactory( billable=True, date_started=d, start_time=time(11, 0), end_time=time(11, 30), user=red, ) TimeRecordFactory( billable=False, date_started=d, start_time=time(10, 0), end_time=time(10, 15), user=green, ) data = TimeRecord.objects.report_time_by_user(from_date, to_date) assert {'red': 30, 'green': 15} == data
def test_set_deleted_multi(): user = UserFactory() c1 = FruitCakeFactory(number=19, description='c1') FruitCake.objects.set_deleted(c1, user) c2 = FruitCakeFactory(number=19, description='c2') FruitCake.objects.set_deleted(c2, user) FruitCakeFactory(number=19, description='c3') result = FruitCake.objects.filter(number=19).order_by('description') assert 3 == result.count() assert [ (19, True, 1), (19, True, 2), (19, False, 0), ] == [(o.number, o.deleted, o.deleted_version) for o in result]
def test_tickets(): """List of tickets where time was recorded for a user.""" user = UserFactory() today = date.today() # first day of the month from_date = today + relativedelta(day=1) # last day of the month to_date = today + relativedelta(months=+1, day=1, days=-1) # last month last_month = today + relativedelta(months=-1) # next month next_month = today + relativedelta(months=+1) TimeRecordFactory( ticket=TicketFactory(title='t0'), user=user, date_started=last_month, ) TimeRecordFactory( ticket=TicketFactory(title='t1'), user=user, date_started=from_date, ) TimeRecordFactory( ticket=TicketFactory(title='t2'), user=user, date_started=today, ) TimeRecordFactory( ticket=TicketFactory(title='t3'), date_started=to_date, ) TimeRecordFactory( ticket=TicketFactory(title='t4'), user=user, date_started=to_date, end_time=None, ) TimeRecordFactory( ticket=TicketFactory(title='t5'), user=user, date_started=to_date, ) TimeRecordFactory( ticket=TicketFactory(title='t6'), user=user, date_started=next_month, ) qs = TimeRecord.objects.tickets(from_date, to_date, user) assert ['t1', 't2', 't5'] == [x.title for x in qs]
def test_api_ticket(api_client): user = UserFactory(first_name='Andrea', username='******') t1 = TicketFactory( contact=ContactFactory(company_name='', user=user), priority=PriorityFactory(name='Medium'), title='Mow the lawn', user_assigned=UserFactory(username='******'), ) user = UserFactory(first_name='Patrick', username='******') t2 = TicketFactory( contact=ContactFactory(company_name='', user=user), priority=PriorityFactory(name='High'), title='Make a cup of tea', user_assigned=UserFactory(username='******'), ) # get response = api_client.get(reverse('api.crm.ticket')) assert status.HTTP_200_OK == response.status_code assert [ { 'contact': 'andrea', 'due': None, 'id': t1.pk, 'priority': 'Medium', 'title': 'Mow the lawn', 'username': '******', }, { 'contact': 'patrick', 'due': None, 'id': t2.pk, 'priority': 'High', 'title': 'Make a cup of tea', 'username': '******', }, ] == response.data
def test_invoice_download(perm_check): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2013, 12, 1)) invoice = InvoiceCreate().create( UserFactory(), contact, date(2013, 12, 31) ) InvoicePrint().create_pdf(invoice, header_image=None) url = reverse('invoice.download', kwargs={'pk': invoice.pk}) perm_check.staff(url)
def test_start_and_stop(): user = UserFactory() running = TimeRecordFactory(user=user, end_time=None) assert running.end_time is None quick = QuickTimeRecordFactory(user=user) ticket = TicketFactory() time_record = TimeRecord.objects.start(ticket, quick) assert quick.time_code == time_record.time_code assert quick.description == time_record.title assert time_record.billable is False assert time_record.end_time is None assert time_record.start_time is not None assert user == time_record.user running.refresh_from_db() assert running.end_time == time_record.start_time
def test_create_invoices(): """Create an invoice""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) TimeRecordFactory(ticket=ticket, date_started=date(2012, 8, 1)) TimeRecordFactory(ticket=ticket) # action InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) invoices = Invoice.objects.filter(contact=contact) assert 1 == len(invoices) invoice = invoices[0] assert 2 == len(invoice.invoiceline_set.all())
def test_report_charge_non_charge_user(): to_date = timezone.now() from_date = to_date + relativedelta(months=-1) d = from_date + relativedelta(days=7) user = UserFactory() TimeRecordFactory( billable=True, date_started=d, start_time=time(11, 0), end_time=time(11, 10), user=user, ) TimeRecordFactory( billable=False, date_started=d, start_time=time(11, 0), end_time=time(11, 5), user=user, ) # records for other users TimeRecordFactory( billable=True, date_started=d, start_time=time(11, 0), end_time=time(11, 30), ) TimeRecordFactory( billable=False, date_started=d, start_time=time(10, 0), end_time=time(10, 15), ) # do not include this record TimeRecordFactory( billable=False, date_started=d, start_time=time(11, 0), end_time=None, ) data = TimeRecord.objects.report_charge_non_charge( from_date, to_date, user, ) assert {'Chargeable': 10, 'Non-Chargeable': 5} == data
def test_invoice_create_draft(client): user = UserFactory(username='******', is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) assert 0 == Invoice.objects.count() url = reverse('invoice.create.draft', kwargs={'pk': contact.pk}) data = { 'invoice_date': timezone.now().date(), } response = client.post(url, data) assert 302 == response.status_code, response.context['form'].errors assert 1 == Invoice.objects.count() invoice = Invoice.objects.first() assert invoice.number == 1 assert invoice.invoice_number == '000001'
def test_invoice_line_create(client): user = UserFactory(username='******', is_staff=True) assert client.login(username=user.username, password=TEST_PASSWORD) is True InvoiceSettingsFactory() VatSettingsFactory() invoice = InvoiceFactory() assert 0 == InvoiceLine.objects.count() url = reverse('invoice.line.create', args=[invoice.pk]) data = { 'product': ProductFactory().pk, 'description': 'Apple', 'price': Decimal('3'), 'quantity': Decimal('1'), 'units': 'Each', 'vat_code': VatCode.objects.get(slug=VatCode.STANDARD).pk, } response = client.post(url, data) assert 302 == response.status_code, response.context['form'].errors assert 1 == InvoiceLine.objects.count()
def test_report_time_by_contact_user(): user = UserFactory() to_date = timezone.now() from_date = to_date + relativedelta(months=-1) d = from_date + relativedelta(days=7) bob = TicketFactory(contact=ContactFactory(slug='bob')) sam = TicketFactory(contact=ContactFactory(slug='sam')) # these time records are for a different user, so exclude them TimeRecordFactory( ticket=bob, billable=True, date_started=d, start_time=time(11, 0), end_time=time(11, 30), ) TimeRecordFactory( ticket=sam, billable=False, date_started=d, start_time=time(10, 0), end_time=time(10, 15), ) # include these time records TimeRecordFactory( ticket=bob, billable=True, date_started=d, start_time=time(11, 0), end_time=time(11, 30), user=user, ) TimeRecordFactory( ticket=sam, billable=False, date_started=d, start_time=time(10, 0), end_time=time(10, 15), user=user, ) data = TimeRecord.objects.report_time_by_contact(from_date, to_date, user) assert {'bob': 30, 'sam': 15} == data
def test_create_invoices_not_fixed_price(): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) # ticket 1 t1 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t1, title='t1', date_started=date(2012, 7, 1)) # ticket 2 is for fixed price work t2 = TicketFactory(contact=contact, fixed_price=True) TimeRecordFactory(ticket=t2, title='t2', date_started=date(2012, 7, 2)) # ticket 3 t3 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t3, title='t3', date_started=date(2012, 7, 3)) # test InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count() invoice = Invoice.objects.get(contact=contact) assert ['t1', 't3'] == [ x.timerecord.title for x in InvoiceLine.objects.filter(invoice=invoice) ]
def test_str_email_time_summary(): obj = InvoiceUserFactory( user=UserFactory(username='******'), mail_time_summary=True, ) assert 'pat: mail time summary' == str(obj)
def test_str(): obj = InvoiceUserFactory( user=UserFactory(username='******'), mail_time_summary=False, ) assert 'pat' == str(obj)