Example #1
0
    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
Example #2
0
 def test_update(self):
     time_entry = TimeEntryFactory.create()
     self.db.session.commit()
     data = TimeEntryFactory.stub(task=None).__dict__
     data['id'] = time_entry.id
     data['task_id'] = time_entry.task.id
     data['duration'] = data['duration'].total_seconds()
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(data)
     assert not errors
     assert instance.id == time_entry.id
     assert instance.duration.total_seconds() == data['duration']
     assert instance.task_id == time_entry.task.id
Example #3
0
 def test_update(self):
     time_entry = TimeEntryFactory.create()
     self.db.session.commit()
     data = TimeEntryFactory.stub(task=None).__dict__
     data['id'] = time_entry.id
     data['task_id'] = time_entry.task.id
     data['duration'] = data['duration'].total_seconds()
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(data)
     assert not errors
     assert instance.id == time_entry.id
     assert instance.duration.total_seconds() == data['duration']
     assert instance.task_id == time_entry.task.id
Example #4
0
 def test_serialization(self):
     time_entry = TimeEntryFactory.create()
     self.db.session.commit()
     serializer = TimeEntrySerializer(strict=True)
     data = serializer.dump(time_entry).data
     assert data['id'] == time_entry.id
     assert data['duration'] == time_entry.duration.total_seconds()
     assert data['comment'] == time_entry.comment
     assert data['task_id'] == time_entry.task.id
     assert 'added_at' in data
Example #5
0
    def test_delete_time_entry(self):
        time_entry = TimeEntryFactory.create()
        self.db.session.commit()

        url = url_for('time_entry_api.time_entry', time_entry_id=time_entry.id)
        response = self.client.delete(url)
        assert response.status_code == 200

        response = self.client.delete(url)
        assert response.status_code == 404
Example #6
0
 def test_validate_comment_null(self):
     task = TaskFactory.create()
     self.db.session.commit()
     valid_data = TimeEntryFactory.stub(task=None, comment=None).__dict__
     valid_data['task_id'] = task.id
     valid_data['duration'] = valid_data['duration'].total_seconds()
     assert valid_data['comment'] is None
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(valid_data)
     assert not errors
Example #7
0
 def test_validate_comment_null(self):
     task = TaskFactory.create()
     self.db.session.commit()
     valid_data = TimeEntryFactory.stub(task=None, comment=None).__dict__
     valid_data['task_id'] = task.id
     valid_data['duration'] = valid_data['duration'].total_seconds()
     assert valid_data['comment'] is None
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(valid_data)
     assert not errors
Example #8
0
 def test_serialization(self):
     time_entry = TimeEntryFactory.create()
     self.db.session.commit()
     serializer = TimeEntrySerializer(strict=True)
     data = serializer.dump(time_entry).data
     assert data['id'] == time_entry.id
     assert data['duration'] == time_entry.duration.total_seconds()
     assert data['comment'] == time_entry.comment
     assert data['task_id'] == time_entry.task.id
     assert 'added_at' in data
     assert data['task_total_time'] == data['duration']
Example #9
0
 def test_serialization(self):
     task = TaskFactory.create()
     time_entry = TimeEntryFactory.create(task=task)
     self.db.session.commit()
     serializer = TaskSerializer(strict=True)
     data = serializer.dump(task).data
     assert data['id'] == task.id
     assert data['title'] == task.title
     assert data['url'] == task.url
     assert data['description'] == task.description
     assert data['project_id'] == task.project.id
     assert data['total_time'] == time_entry.duration.total_seconds()
     assert data['is_closed'] is False
     time_data = data['time_entries'][0]
     assert time_data['id'] == time_entry.id
     assert time_data['comment'] == time_entry.comment
     assert time_data['task_id'] == task.id
Example #10
0
 def test_serialization(self):
     task = TaskFactory.create()
     time_entry = TimeEntryFactory.create(task=task)
     self.db.session.commit()
     serializer = TaskSerializer(strict=True)
     data = serializer.dump(task).data
     assert data['id'] == task.id
     assert data['title'] == task.title
     assert data['url'] == task.url
     assert data['description'] == task.description
     assert data['project_id'] == task.project.id
     assert data['total_time'] == time_entry.duration.total_seconds()
     assert data['is_closed'] is False
     time_data = data['time_entries'][0]
     assert time_data['id'] == time_entry.id
     assert time_data['comment'] == time_entry.comment
     assert time_data['task_id'] == task.id
     assert 'task_total_time' not in time_data
Example #11
0
    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
Example #12
0
    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)
Example #13
0
    def test_update_time_entry(self):
        time_entry = TimeEntryFactory.create()
        self.db.session.commit()

        url = url_for('time_entry_api.time_entry', time_entry_id=time_entry.id)
        time_entry_data = {
            'task_id': time_entry.task_id,
            'duration': 2160,
        }
        response = self.client.put(url, json=time_entry_data)
        assert response.status_code == 200
        assert 'time_entry' in response.json
        assert (response.json['time_entry']['duration'] ==
                time_entry_data['duration'])
        assert (response.json['time_entry']['task_total_time'] ==
                time_entry_data['duration'])

        url = url_for('time_entry_api.time_entry', time_entry_id=0)
        response = self.client.put(url, json=time_entry_data)
        assert response.status_code == 404
Example #14
0
 def test_validate_duration(self):
     task = TaskFactory.create()
     self.db.session.commit()
     data = TimeEntryFactory.stub(task=None).__dict__
     data['task_id'] = task.id
     # Min
     invalid_data = data.copy()
     invalid_data['duration'] = 30
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
     # Max
     invalid_data = data.copy()
     invalid_data['duration'] = 101 * 3600
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
     # Seconds
     invalid_data = data.copy()
     invalid_data['duration'] = 11111
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
Example #15
0
 def test_validate_duration(self):
     task = TaskFactory.create()
     self.db.session.commit()
     data = TimeEntryFactory.stub(task=None).__dict__
     data['task_id'] = task.id
     # Min
     invalid_data = data.copy()
     invalid_data['duration'] = 30
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
     # Max
     invalid_data = data.copy()
     invalid_data['duration'] = 101 * 3600
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
     # Seconds
     invalid_data = data.copy()
     invalid_data['duration'] = 11111
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(invalid_data)
     assert 'duration' in errors
Example #16
0
 def test_validate_task_id(self):
     data = TimeEntryFactory.stub(task=None).__dict__
     data['task_id'] = 0
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(data)
     assert 'task_id' in errors
Example #17
0
 def test_validate_task_id(self):
     data = TimeEntryFactory.stub(task=None).__dict__
     data['task_id'] = 0
     serializer = TimeEntrySerializer(exclude=['added_at'])
     instance, errors = serializer.load(data)
     assert 'task_id' in errors