def test_batch_queries_creates_multiple_batches(self): """ Verify that build batch query method returns multiple batches ticket and task queries. """ max_id_limit = 500 settings = DjautotaskSettings().get_settings() batch_size = settings.get('batch_query_size') synchronizer = sync.TimeEntrySynchronizer() sync_job = SyncJob.objects.filter(entity_name='TimeEntry') # Simulate ticket IDs object_ids = random.sample(range(1, max_id_limit), batch_size + 50) _, _patch = mocks.create_mock_call( 'django.db.models.query.QuerySet.values_list', object_ids) batch_query_list = synchronizer.build_batch_queries(sync_job) # With a max batch size of 400, a set of 450 object IDs should result # in 4 Query objects being returned in the list. (2 for tickets, 2 for # tasks) self.assertEqual(len(batch_query_list), 4) _patch.stop()
def test_sync_time_entry(self): """ Test to ensure synchronizer saves a time entry instance locally. """ time_entries = fixture_utils.generate_objects( 'TimeEntry', fixtures.API_TIME_ENTRY_LIST) mocks.api_query_call(time_entries) synchronizer = sync.TimeEntrySynchronizer() synchronizer.sync() self.assertGreater(TimeEntry.objects.all().count(), 0) object_data = fixtures.API_TIME_ENTRY_TICKET instance = TimeEntry.objects.get(id=object_data['id']) ticket = Ticket.objects.first() ticket.id = object_data['TicketID'] instance.ticket = ticket resource = Resource.objects.first() resource.id = object_data['ResourceID'] instance.resource = resource self._assert_sync(instance, object_data) assert_sync_job(TimeEntry)
def test_delete_stale_time_entries(self): """ Verify that time entry is deleted if not returned during a full sync """ time_entries = fixture_utils.generate_objects( 'TimeEntry', fixtures.API_TIME_ENTRY_LIST) mocks.api_query_call(time_entries) synchronizer = sync.TimeEntrySynchronizer() synchronizer.sync() qset = TimeEntry.objects.all() self.assertEqual(qset.count(), len(fixtures.API_TIME_ENTRY_LIST)) mocks.api_query_call([]) synchronizer = sync.TimeEntrySynchronizer(full=True) synchronizer.sync() qset = TimeEntry.objects.all() self.assertEqual(qset.count(), 0)
def test_batch_queries_returns_query_list(self): """ Verify that an empty list is returned when no tickets or tasks are present in the database. """ _, _patch = mocks.create_mock_call( 'django.db.models.query.QuerySet.values_list', []) synchronizer = sync.TimeEntrySynchronizer() sync_job = SyncJob.objects.filter(entity_name='TimeEntry') batch_query_list = synchronizer.build_batch_queries(sync_job) self.assertEqual(len(batch_query_list), 0) _patch.stop()
def test_sync_time_entry_skipped(self): """ Verify that a time entry does not get saved locally if its ticket does not already exist in the local database. """ time_entry_count = TimeEntry.objects.all().count() synchronizer = sync.TimeEntrySynchronizer(full=True) synchronizer.sync() sync_job = SyncJob.objects.last() # Time entry for time entry ticket is not saved locally # and is subsequently removed. self.assertEqual(TimeEntry.objects.all().count(), 0) self.assertEqual(sync_job.added, 0) self.assertEqual(sync_job.updated, 0) self.assertEqual(sync_job.deleted, time_entry_count)