def test_week_beginning(self): dt = datetime.datetime(2015, 3, 24, 20, 20, 20, tzinfo=date.timezone) beginning = date.week_beginning(dt) assert beginning.hour == 0 assert beginning.minute == 0 assert beginning.second == 0 assert beginning.date() == dt.date() - datetime.timedelta(days=1)
def test_get(self): week_beg = week_beginning(tz_now()) date_range = DateRangeFactory(beg=week_beg.isoformat()) client = ClientFactory.create() time_entries = [] for day in range(7): time_entries.append( TimeEntryFactory.create(task__project__client=client, added_at=week_beg + datetime.timedelta(days=day))) task = TaskFactory.create(project__client=client) for day in range(7): time_entries.append( TimeEntryFactory.create( task=task, duration=datetime.timedelta(minutes=30), added_at=week_beg + datetime.timedelta(days=day))) self.db.session.commit() timesheet = TimeSheet(client.id, date_range) result = timesheet.get() assert result['client']['name'] == client.name assert result['date_range']['beg'] == week_beg.isoformat() assert len(result['projects']) == 8 total_1 = sum((item.duration for item in time_entries), datetime.timedelta()).total_seconds() total_2 = sum(project['total'] for project in result['projects']) total_3 = sum(result['totals']['time']) total_4 = result['totals']['total'] assert total_1 == total_2 == total_3 == total_4
def test_serialization(self): week_beg = week_beginning(tz_now()) week_end = arrow.get(week_beg).ceil('week').datetime date_range = { 'beg': week_beg, 'end': week_end, } serializer = DateRangeSerializer(strict=True) data = serializer.dump(date_range).data date_range = serializer.load(data).data assert date_range == (week_beg, week_end)
def test_serialization(self): user = User() week_beg = week_beginning(tz_now()) client = ClientFactory.create() time_entries = [] for day in range(7): time_entries.append(TimeEntryFactory.create( task__project__client=client, added_at=week_beg + datetime.timedelta(days=day))) self.db.session.commit() result = user.serialize() assert result['name'] == settings.USER_NAME assert result['open_tasks'] == 7 total_week = sum((item.duration for item in time_entries), datetime.timedelta()).total_seconds() assert result['total_week'] == total_week assert any(item.duration.total_seconds() == result['total_today'] for item in time_entries)
def test_get_report(self): week_beg = week_beginning(tz_now()) date_range = DateRangeFactory.create(beg=week_beg.isoformat()) client = ClientFactory.create() time_entry = TimeEntryFactory.create(task__project__client=client, added_at=week_beg) self.db.session.commit() url = url_for('client_api.report', client_id=client.id) response = self.client.get(url, query_string=date_range) assert response.status_code == 200 assert 'report' in response.json report = response.json['report'] assert report['client']['id'] == client.id assert len(report['projects']) == 1 assert report['total'] == time_entry.duration.total_seconds() url = url_for('client_api.report', client_id=0) response = self.client.get(url) assert response.status_code == 404
def beg(self): return week_beginning(tz_now()).isoformat()