def test_job_number_can_be_changed(self, mock_get_client): old_number = "FS10000" new_number = "FS10001" instant = datetime.datetime(2020, 1, 29, 12, 0, 0, tzinfo=datetime.timezone.utc) job = TalentLinkJobFactory( talentlink_id=1, job_number=old_number, last_imported=instant, homepage=self.homepage, ) advertisements = [ get_advertisement(talentlink_id=1, job_number=new_number) ] mock_get_client.return_value = self.get_mocked_client(advertisements) later = instant + datetime.timedelta(days=1) with freeze_time(later): call_command("import_jobs", stdout=mock.MagicMock()) # No new job has been created self.assertEqual(TalentLinkJob.objects.count(), 1) job.refresh_from_db() self.assertEqual(job.talentlink_id, 1) self.assertEqual(job.job_number, new_number) # The job has been reimported self.assertEqual(job.last_imported, later)
def test_existing_job_imported_date(self, mock_get_client): instant = datetime.datetime(2020, 1, 29, 12, 0, 0, tzinfo=datetime.timezone.utc) job = TalentLinkJobFactory(talentlink_id=164579, last_imported=instant, homepage=self.homepage) self.assertEqual(job.last_imported, instant) mock_get_client.return_value = self.get_mocked_client() later = instant + datetime.timedelta(days=1) with freeze_time(later): call_command("import_jobs", stdout=mock.MagicMock()) job.refresh_from_db() self.assertEqual(job.last_imported, later)
def test_error_cases_are_not_imported(self, mock_update_fn, mock_get_client): instant = datetime.datetime(2020, 1, 29, 12, 0, 0, tzinfo=datetime.timezone.utc) TalentLinkJobFactory( talentlink_id=1, title="Original title 1", last_imported=instant, homepage=self.homepage, ) job_2 = TalentLinkJobFactory( talentlink_id=2, title="Original title 2", last_imported=instant, homepage=self.homepage, ) self.assertEqual(TalentLinkJob.objects.count(), 2) error_message = "This is a test error message" def error_or_original(job, ad, homepage, defaults, import_categories): """ Raise an error for id 1 only""" if job.talentlink_id == 1: raise KeyError(error_message) else: return update_job_from_ad(job, ad, self.homepage, defaults, import_categories) mock_update_fn.side_effect = error_or_original advertisements = [ get_advertisement(talentlink_id=1, title="New title 1"), get_advertisement(talentlink_id=2, title="New title 2"), ] mock_get_client.return_value = self.get_mocked_client(advertisements) out = StringIO() later = instant + datetime.timedelta(days=1) with freeze_time(later): call_command("import_jobs", stdout=out) out.seek(0) output = out.read() self.assertIn(error_message, output) self.assertIn("1 existing jobs updated", output) self.assertIn("1 errors", output) # job_1 should have been deleted from db as it isn't imported self.assertEqual( TalentLinkJob.objects.filter(talentlink_id=1).count(), 0, msg="Jobs not in import should be deleted.", ) job_2.refresh_from_db() self.assertEqual(job_2.title, "New title 2") # job 2 has been updated self.assertEqual(job_2.last_imported, later) # job 2 has been updated