def test_from_to_dict(self): link, created = Link.objects.get_or_create(link="https://tj.org") self.assertTrue(created) self.assertIsNotNone(link.id) journal, created = Journal.objects.get_or_create(name="Test Journal") journal.links.add(link) self.assertTrue(created) self.assertIsNotNone(journal.id) self.assertEquals( {"name": "Test Journal", "links": [{"url": "https://tj.org"}]}, journal.to_dict(), ) self.assertEquals( (journal, False), Journal.from_dict( {"name": "Test Journal", "links": [{"url": "https://tj.org"}]} ), ) self.assertEquals((journal, False), Journal.from_dict({"name": "Test Journal"})) journal, created = Journal.objects.get_or_create(name="Random Journal") self.assertTrue(created) self.assertIsNotNone(journal.id) self.assertEquals({"name": "Random Journal", "links": None}, journal.to_dict()) self.assertEquals( (journal, False), Journal.from_dict({"name": "Random Journal", "links": None}), ) self.assertEquals( (journal, False), Journal.from_dict({"name": "Random Journal"}) )
def test_print(self): journal, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) with StringIO() as cout: journal.print(cout) self.assertEquals( "Field Value " + " \n==================================" + "==================================================================\n" + "Id 1 " + " \n__________________________________" + "__________________________________________________________________\n" + "Name Science Journal " + " \n__________________________________" + "__________________________________________________________________\n" + "Links " + " \n__________________________________" + "__________________________________________________________________\n" + "Papers " + " \n__________________________________" + "__________________________________________________________________\n", cout.getvalue(), )
def test_search(self): journal, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) journal, created = Journal.from_dict({"name": "Nature Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) journal, created = Journal.from_dict({"name": "Math science"}) self.assertTrue(created) self.assertIsNotNone(journal.id) self.assertEquals(3, Journal.objects.all().count()) self.assertEquals(2, Journal.search("journal").count()) self.assertEquals(2, Journal.search("science").count())
def test_delete(self): journals, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journals.id) deleted = journals.delete() self.assertIsNone(journals.id) self.assertEquals((1, {"journals.Journal": 1}), deleted) journals, created = Journal.from_dict( {"name": "Nature Science Journal", "links": [{"url": "https://nsj.org"}]} ) self.assertTrue(created) self.assertIsNotNone(journals.id) deleted = journals.delete() self.assertIsNone(journals.id) self.assertEquals( (3, {"journals.Journal": 1, "journals.Journal_links": 1, "links.Link": 1}), deleted, )
def test_get(self): journal, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) journal2 = Journal.get("Science Journal") self.assertIsNotNone(journal2) self.assertEquals(journal, journal2) journal2 = Journal.get("science") self.assertIsNotNone(journal2) self.assertEquals(journal, journal2) journal2 = Journal.get(str(journal.id)) self.assertIsNotNone(journal2) self.assertEquals(journal, journal2)
def test_get_or_create(self): journal, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) self.assertEquals(1, Journal.objects.count()) journal2 = Journal.get_or_create("Science Journal") self.assertIsNotNone(journal2) self.assertEquals(journal, journal2) self.assertEquals(1, Journal.objects.count()) journal2 = Journal.get_or_create(str(journal.id)) self.assertIsNotNone(journal2) self.assertEquals(journal, journal2) self.assertEquals(1, Journal.objects.count()) journal2 = Journal.get_or_create("IT Journal") self.assertIsNotNone(journal2) self.assertNotEquals(journal, journal2) self.assertEquals(2, Journal.objects.count())
def from_dict(cls: Type[T], data: Dict) -> Tuple[T, bool]: """Create from dict. Returns True if was crated, i. e. was not found in the DB. """ defaults: Dict = {} if "journal" in data and data["journal"]: defaults["journal"] = Journal.from_dict(data["journal"])[0] if "volume" in data and data["volume"]: defaults["volume"] = data["volume"] if "publishing_date" in data and data["publishing_date"]: defaults["publishing_date"] = datetime.datetime.strptime( data["publishing_date"], "%Y-%m-%d").date() if "bibtex" in data and data["bibtex"]: defaults["bibtex"] = data["bibtex"] paper, created = Paper.objects.get_or_create(title=data["title"], defaults=defaults) if "authors" in data and data["authors"]: for i in data["authors"]: paper.authors.add(Person.from_dict(i)[0]) if "languages" in data and data["languages"]: for i in data["languages"]: paper.languages.add(Language.from_dict(i)[0]) if "links" in data and data["links"]: for i in data["links"]: paper.links.add(Link.from_dict(i)[0]) if "acquisitions" in data and data["acquisitions"]: for i in data["acquisitions"]: Acquisition.from_dict(i, paper) if "files" in data and data["files"]: for i in data["files"]: File.from_dict(i, paper) if "reads" in data and data["reads"]: for i in data["reads"]: Read.from_dict(i, paper) paper.save() return paper, created
def test_from_to_dict(self): author1, created = Person.from_dict({"name": "John Doe"}) self.assertTrue(created) self.assertIsNotNone(author1.id) author2, created = Person.from_dict({"name": "Jane Doe"}) self.assertTrue(created) self.assertIsNotNone(author2.id) journal, created = Journal.from_dict({"name": "Science Journal"}) self.assertTrue(created) self.assertIsNotNone(journal.id) paper, created = Paper.objects.get_or_create( title="Random new stuff", journal=journal, volume="1/2021" ) paper.authors.add(author1) paper.authors.add(author2) self.assertTrue(created) self.assertIsNotNone(paper.id) self.assertEquals( { "title": "Random new stuff", "authors": [ {"name": "Jane Doe", "links": None}, {"name": "John Doe", "links": None}, ], "journal": {"name": "Science Journal", "links": None}, "volume": "1/2021", "publishing_date": None, "languages": None, "files": None, "bibtex": None, "links": None, "acquisitions": None, "reads": None, }, paper.to_dict(), ) self.assertEquals( (paper, False), Paper.from_dict( { "title": "Random new stuff", "authors": [ {"name": "Jane Doe", "links": None}, {"name": "John Doe", "links": None}, ], "journal": {"name": "Science Journal", "links": None}, "volume": "1/2021", "publishing_date": None, "languages": None, "files": None, "bibtex": None, "links": None, "acquisitions": None, "reads": None, } ), ) self.assertEquals( (paper, False), Paper.from_dict( { "title": "Random new stuff", "authors": [ {"name": "Jane Doe"}, {"name": "John Doe"}, ], "journal": {"name": "Science Journal"}, "volume": "1/2021", } ), ) language, created = Language.from_dict({"name": "Englisch"}) self.assertTrue(created) self.assertIsNotNone(language.id) link, created = Link.from_dict({"url": "https://example.com"}) self.assertTrue(created) self.assertIsNotNone(link.id) paper, created = Paper.objects.get_or_create( title="Random Science stuff", publishing_date=datetime.strptime("2021-01-01", "%Y-%m-%d").date(), journal=journal, volume="2/2021", ) paper.authors.add(author1) paper.authors.add(author2) paper.languages.add(language) paper.links.add(link) self.assertTrue(created) self.assertIsNotNone(paper.id) acquisition, created = Acquisition.from_dict( {"date": "2021-01-02", "price": 10}, paper ) self.assertTrue(created) self.assertIsNotNone(acquisition.id) read, created = Read.from_dict( {"started": "2021-01-02", "finished": "2021-01-03"}, paper ) self.assertTrue(created) self.assertIsNotNone(read.id) self.assertEquals( { "title": "Random Science stuff", "authors": [ {"name": "Jane Doe", "links": None}, {"name": "John Doe", "links": None}, ], "journal": {"name": "Science Journal", "links": None}, "volume": "2/2021", "publishing_date": "2021-01-01", "languages": [{"name": "Englisch"}], "files": None, "bibtex": None, "links": [{"url": "https://example.com"}], "acquisitions": [{"date": "2021-01-02", "price": 10.0}], "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], }, paper.to_dict(), ) self.assertEquals( (paper, False), Paper.from_dict( { "title": "Random Science stuff", "authors": [ {"name": "Jane Doe", "links": None}, {"name": "John Doe", "links": None}, ], "journal": {"name": "Science Journal", "links": None}, "volume": "2/2021", "publishing_date": "2021-01-01", "languages": [{"name": "Englisch"}], "files": None, "bibtex": None, "links": [{"url": "https://example.com"}], "acquisitions": [{"date": "2021-01-02", "price": 10.0}], "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], } ), ) self.assertEquals( (paper, False), Paper.from_dict( { "title": "Random Science stuff", "authors": [ {"name": "Jane Doe"}, {"name": "John Doe"}, ], "journal": {"name": "Science Journal"}, "volume": "2/2021", "publishing_date": "2021-01-01", "languages": [{"name": "Englisch"}], "links": [{"url": "https://example.com"}], "acquisitions": [{"date": "2021-01-02", "price": 10.0}], "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], } ), ) with NamedTemporaryFile() as f: f.write(b"Lorem ipsum dolorem") file, created = File.from_dict({"path": f.name}) self.assertTrue(created) self.assertIsNotNone(file.id) self.assertEquals( os.path.basename(f.name), os.path.basename(file.file.name) ) paper, created = Paper.objects.get_or_create( title="Boring Science stuff", publishing_date=datetime.strptime("2021-02-01", "%Y-%m-%d").date(), journal=journal, volume="2/2021", ) paper.authors.add(author1) paper.authors.add(author2) paper.languages.add(language) paper.links.add(link) paper.files.add(file) paper.save() self.assertTrue(created) self.assertIsNotNone(paper.id) self.assertEquals( { "title": "Boring Science stuff", "authors": [ {"name": "Jane Doe", "links": None}, {"name": "John Doe", "links": None}, ], "journal": {"name": "Science Journal", "links": None}, "volume": "2/2021", "publishing_date": "2021-02-01", "languages": [{"name": "Englisch"}], "bibtex": None, "links": [{"url": "https://example.com"}], "files": [ { "path": os.path.join( settings.MEDIA_ROOT, "papers", str(paper.pk), os.path.basename(f.name), ) } ], "acquisitions": None, "reads": None, }, paper.to_dict(), )
def _journal(args: Namespace, file: TextIO = sys.stdout): journal: Optional[Journal] = None if args.subparser == "add": journal, created = Journal.from_dict({ "name": args.name, "links": [Link.get_or_create(link).to_dict() for link in args.link], }) if created: stdout.write( _('Successfully added journal "%(name)s" with id "%(pk)d".') % { "name": journal.name, "pk": journal.pk }, "=", file=file, ) journal.print(file) else: stdout.write( _('The journal "%(name)s" already exists with id "%(pk)d", aborting...' ) % { "name": journal.name, "pk": journal.pk }, "", file=file, ) elif args.subparser == "delete": journal = Journal.get(args.journal) if journal: journal.delete() stdout.write( _('Successfully deleted journal with id "%(pk)d".') % {"pk": journal.pk}, "", file=file, ) else: stdout.write(_("No journal found."), "", file=file) elif args.subparser == "edit": journal = Journal.get(args.journal) if journal: journal.edit(args.field, args.value) stdout.write( _('Successfully edited journal "%(name)s" with id "%(pk)d".') % { "name": journal.name, "pk": journal.pk }, "", file=file, ) journal.print(file) else: stdout.write(_("No journal found."), "", file=file) elif args.subparser == "info": journal = Journal.get(args.journal) if journal: journal.info(file) else: stdout.write([_("No journal found.")], "") elif args.subparser == "list": if args.search: journals = Journal.search(args.search) else: journals = Journal.objects.all() stdout.write( [_("Id"), _("Name"), _("Number of papers")], "=", [0.05, 0.8], file=file) for i, has_next in lookahead(journals): stdout.write( [i.id, i.name, i.papers.count()], "_" if has_next else "=", [0.05, 0.8], file=file, )