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 _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, )