def update(loader: MockLoader): changed_file = make_file(1) changed_file["name"] = "changed" new_paper = make_paper([changed_file, make_file(2)]) loader.api_data[make_body()["paper"]] = make_list([new_paper]) deleted_file = make_file(0) deleted_file["deleted"] = True loader.api_data[deleted_file["id"]] = deleted_file new_file = make_file(2) loader.api_data[new_file["id"]] = new_file
def build_mock_loader() -> MockLoader: system = make_system() body = make_body() loader = MockLoader(system) old_paper = make_paper([make_file(0), make_file(1)]) loader.api_data = { system["id"]: system, system["body"]: make_list([body]), body["id"]: body, body["paper"]: make_list([old_paper]), body["person"]: make_list([]), body["organization"]: make_list([]), body["meeting"]: make_list([]), } return loader
class TestEmbeddedUpdate(TestCase): body = make_body() def test_embedded_update(self): loader = build_mock_loader() importer = Importer(loader, force_singlethread=True) importer.run(self.body["id"]) paper_id = make_paper([])["id"] self.assertEqual(Paper.objects.count(), 1) file_ids = Paper.by_oparl_id(paper_id).files.values_list("oparl_id", flat=True) self.assertEqual( sorted(file_ids), [make_file(0)["id"], make_file(1)["id"]]) self.assertEqual(File.objects.count(), 2) update(loader) importer.update(self.body["id"]) self.assertEqual(Paper.objects.count(), 1) self.assertEqual(File.objects.count(), 2) file_ids = Paper.by_oparl_id(paper_id).files.values_list("oparl_id", flat=True) self.assertEqual( sorted(file_ids), [make_file(1)["id"], make_file(2)["id"]]) self.assertEqual(File.objects_with_deleted.count(), 3) def test_update_without_change_is_ignored(self): loader = build_mock_loader() importer = Importer(loader, force_singlethread=True) importer.run(self.body["id"]) [paper] = Paper.objects.all() self.assertEqual(paper.history.count(), 1) # The "updated" list still contains the same paper object importer.update(self.body["id"]) [paper] = Paper.objects.all() self.assertEqual(paper.history.count(), 1) # Consistency check: The count is increased if there is actual change update(loader) importer.update(self.body["id"]) [paper] = Paper.objects.all() self.assertEqual(paper.history.count(), 2)
class TestCron(TestCase): """ [WIP] Tests that an file change sends out exactely one mail to only the subscribed user. """ fixtures = ["cron.json"] system = make_system() body = make_body() def external_list_fixture(self): """ Loads a fixture with two papers and two users, with one user beeing subscribed to one paper, and the external lists loaded. Should probably be moved into a json file """ ExternalList(url=self.system["body"], last_update=old_date).save() ExternalList(url=self.body["person"], last_update=old_date).save() ExternalList(url=self.body["meeting"], last_update=old_date).save() ExternalList(url=self.body["organization"], last_update=old_date).save() ExternalList(url=self.body["paper"], last_update=old_date).save() def get_mock_loader(self) -> BaseLoader: return MockLoader( self.system, { self.system["id"]: self.system, self.system["body"]: make_list([self.body]), self.body["id"]: self.body, self.body["meeting"]: make_list([]), self.body["organization"]: make_list([]), self.body["person"]: make_list([]), self.body["paper"]: make_list([]), }, ) @mock.patch("mainapp.functions.minio._minio_singleton", new=MinioMock()) def test_cron(self): """ WIP """ self.external_list_fixture() loader = self.get_mock_loader() self.run_cron(loader, 0) def run_cron(self, loader: BaseLoader, expected_mail_count: int): # Run cron. Check that nothing happend with mock.patch("mainapp.functions.notify_users.send_mail" ) as mocked_send_mail: with mock.patch("importer.loader.get_loader_from_body", new=lambda body_id: loader): call_command("cron") def cron_unfinished(self, loader): # In[] # Mock an extern list with changes to both paper new_date = timezone.now().astimezone().replace(microsecond=0) file1 = make_file(1) file2 = make_file(2) file2["modified"] = new_date file1["modified"] = new_date paper1 = make_paper([file1], paper_id=1) paper1["modified"] = new_date paper2 = make_paper([file2], paper_id=2) paper2["modified"] = new_date loader.api_data[self.body["paper"]] = make_list([paper1, paper2]) # In[] # Check that exactely the one user got one notification for the one paper self.run_cron(loader, 1) # In[] # Check the user only gets one notification for the event self.run_cron(loader, 0)