def test_search(self): genre, created = Genre.from_dict({"name": "Science Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) genre, created = Genre.from_dict({"name": "Romance"}) self.assertTrue(created) self.assertIsNotNone(genre.id) genre, created = Genre.from_dict({"name": "Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) self.assertEquals(3, Genre.objects.all().count()) self.assertEquals(2, Genre.search("fiction").count()) self.assertEquals(2, Genre.search("ce").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 "series" in data and data["series"]: defaults["series"] = Series.from_dict(data["series"])[0] if "volume" in data and data["volume"]: defaults["volume"] = data["volume"] book, created = Book.objects.get_or_create(title=data["title"], defaults=defaults) if "authors" in data and data["authors"]: for i in data["authors"]: book.authors.add(Person.from_dict(i)[0]) if "genres" in data and data["genres"]: for i in data["genres"]: book.genres.add(Genre.from_dict(i)[0]) if "links" in data and data["links"]: for i in data["links"]: book.links.add(Link.from_dict(i)[0]) return book, created
def test_delete(self): genre, created = Genre.from_dict({"name": "Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) deleted = genre.delete() self.assertIsNone(genre.id) self.assertEquals((1, {"genres.Genre": 1}), deleted)
def _genre(args, file: TextIO = sys.stdout): genre: Optional[Genre] = None if args.subparser == "add": genre, created = Genre.from_dict({"name": args.name}) if created: stdout.write( _('Successfully added genre "%(name)s" with id "%(pk)d".') % {"name": genre.name, "pk": genre.pk}, "=", ) genre.print(file) else: stdout.write( _('The genre "%(name)s" already exists with id "%(pk)d", aborting...') % {"name": genre.name, "pk": genre.pk}, "", ) elif args.subparser == "delete": genre = Genre.get(args.genre) if genre: genre.delete() stdout.write( _('Successfully deleted genre with id "%(pk)d".') % {"pk": genre.pk}, "" ) else: stdout.write(_("No genre found."), "") elif args.subparser == "edit": genre = Genre.get(args.genre) if genre: genre.edit(args.field, args.value) stdout.write( _('Successfully edited genre "%(name)s" with id "%(pk)d".') % {"name": genre.name, "pk": genre.pk}, "", ) genre.print(file) else: stdout.write(_("No genre found."), "") elif args.subparser == "info": genre = Genre.get(args.genre) if genre: genre.print(file) else: stdout.write(_("No genre found."), "") elif args.subparser == "list": if args.search: genres = Genre.search(args.search) else: genres = Genre.objects.all() stdout.write([_("Id"), _("Name")], "=", [0.05], file=file) for i, has_next in lookahead(genres): stdout.write([i.id, i.name], "_" if has_next else "=", [0.05], file=file)
def test_get(self): genre, created = Genre.from_dict({"name": "Science Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) genre2 = Genre.get("Science Fiction") self.assertIsNotNone(genre2) self.assertEquals(genre, genre2) genre2 = Genre.get("fiction") self.assertIsNotNone(genre2) self.assertEquals(genre, genre2) genre2 = Genre.get(str(genre.id)) self.assertIsNotNone(genre2) self.assertEquals(genre, genre2)
def test_get_or_create(self): genre, created = Genre.from_dict({"name": "Science Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) self.assertEquals(1, Genre.objects.count()) genre2 = Genre.get_or_create("Science Fiction") self.assertIsNotNone(genre2) self.assertEquals(genre, genre2) self.assertEquals(1, Genre.objects.count()) genre2 = Genre.get_or_create(str(genre.id)) self.assertIsNotNone(genre2) self.assertEquals(genre, genre2) self.assertEquals(1, Genre.objects.count()) genre2 = Genre.get_or_create("SciFi") self.assertIsNotNone(genre2) self.assertNotEquals(genre, genre2) self.assertEquals(2, Genre.objects.count())
def test_print(self): genre, created = Genre.from_dict({"name": "Science Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) with StringIO() as cout: genre.print(cout) self.assertEquals( "Field Value " + " \n==================================" + "==================================================================\n" + "Id 1 " + " \n__________________________________" + "__________________________________________________________________\n" + "Name Science Fiction " + " \n__________________________________" + "__________________________________________________________________\n" "Books " + " \n__________________________________" + "__________________________________________________________________\n", cout.getvalue(), )
def test_from_to_dict(self): book, created = Book.objects.get_or_create(title="Cool") self.assertTrue(created) self.assertIsNotNone(book.id) self.assertEquals( { "title": "Cool", "authors": None, "series": None, "volume": 0, "genres": None, "links": None, }, book.to_dict(), ) self.assertEquals((book, False), Book.from_dict(book.to_dict())) self.assertEquals((book, False), Book.from_dict({"title": "Cool"})) author, created = Person.from_dict({"name": "Max Mustermann"}) self.assertTrue(created) self.assertIsNotNone(author.id) series, created = Series.from_dict({"name": "Secret Files"}) self.assertTrue(created) self.assertIsNotNone(series.id) genre, created = Genre.from_dict({"name": "Fiction"}) self.assertTrue(created) self.assertIsNotNone(genre.id) link, created = Link.from_dict({"url": "https://example.com"}) self.assertTrue(created) self.assertIsNotNone(link.id) book, created = Book.objects.get_or_create(title="Example", series=series, volume=1) book.authors.add(author) book.genres.add(genre) book.links.add(link) self.assertTrue(created) self.assertIsNotNone(book.id) self.assertEquals( { "title": "Example", "authors": [{ "name": "Max Mustermann", "links": None }], "series": { "name": "Secret Files", "links": None }, "volume": 1, "genres": [{ "name": "Fiction" }], "links": [{ "url": "https://example.com" }], }, book.to_dict(), ) self.assertEquals((book, False), Book.from_dict(book.to_dict()))
def test_from_to_dict(self): genre, created = Genre.objects.get_or_create(name="Fiction") self.assertTrue(created) self.assertIsNotNone(genre.id) self.assertEquals({"name": "Fiction"}, genre.to_dict()) self.assertEquals((genre, False), Genre.from_dict({"name": "Fiction"}))