def test_equality(self): work1 = Work.from_dict(test_work) work2 = Work.from_dict(test_work) self.assertEqual(work1, work2) self.assertEqual(work1, test_work) self.assertEqual(test_work, work1) self.assertTrue(work1 in [test_work])
async def insert_work_use_case(works_repo: WorkRepository, work: Dict) -> Work: contributors = work.get("contributors", []) if isinstance(contributors, str): contributors = contributors.split("|") try: ident = int(work.get("id")) except (ValueError, TypeError): ident = None clean_work = { "title": work.get("title", "").strip(), "contributors": contributors, "iswc": work.get("iswc") or None, "source": work.get("source") or None, "id": ident, } inserting_work = Work.from_dict(clean_work) existing_work = await works_repo.first( {"iswc": inserting_work.iswc} ) or await works_repo.first({"title": inserting_work.title}) if not existing_work or existing_work.iswc != inserting_work.iswc: return await works_repo.create(inserting_work.to_dict()) return await update_existing_work(works_repo, inserting_work, existing_work,)
async def first(self, filters): for work in self.works: for key, filter in filters.items(): if work.get(key) != filter: break else: return Work.from_dict(work) return None
async def update(self, filters, update): for work in self.works: for key, filter in filters.items(): if work.get(key) != filter: break else: work.update(update) return Work.from_dict(work) return None
async def update_existing_work( works_repo: WorkRepository, inserting_work: Work, existing_work: Work ) -> Work: if not existing_work.iswc and inserting_work.iswc: existing_work.iswc = inserting_work.iswc await works_repo.update( {"title": existing_work.title}, {"iswc": inserting_work.iswc} ) if reconcile_contributors(existing_work, inserting_work): await works_repo.update( {"title": existing_work.title} if not (existing_work.iswc or inserting_work.iswc) else {"iswc": inserting_work.iswc or existing_work.iswc}, {"contributors": existing_work.contributors}, ) if best_title := merge_titles(existing_work, inserting_work): await works_repo.update( {"title": existing_work.title} if not (existing_work.iswc or inserting_work.iswc) else {"iswc": inserting_work.iswc or existing_work.iswc}, {"title": best_title}, )
def test_work_init(self): work = Work(**test_work) self.assertEqual(work.to_dict(), expected_work)
def test_work_to_dict(self): work = Work.from_dict(test_work) self.assertEqual(work.to_dict(), expected_work)
def test_work_from_dict(self): work = Work.from_dict(test_work) self.assertAlmostEqual(work.to_dict().get("iswc"), expected_work.get("iswc"))
async def create(self, work): self.works.append(work) return Work.from_dict(work)
async def create_many(self, works): self.works.extend(works) return [Work.from_dict(work) for work in works]
async def get_works(self): return [Work.from_dict(work) for work in self.works]
async def first(self, query: dict) -> Union[Work, None]: work = await self.db_client.bmat.works.find_one(query) return Work.from_dict(work) if work is not None else None
async def get_works(self, filters: Optional[dict] = None) -> List[Work]: if filters is None: filters = {} cursor = self.db_client.bmat.works.find(filters) works = await cursor.to_list(None) return [Work.from_dict(work) for work in works]
async def create_many(self, works: List[dict]) -> List[Work]: await self.db_client.bmat.works.insert_many(works) return [Work.from_dict(work) for work in works]
async def create(self, work: dict) -> Work: insert = await self.db_client.bmat.works.insert_one(work) logging.debug("inserted work id: %", insert.inserted_id) return Work.from_dict(work)