class BookTest(TestCase): def setUp(self): self.author1 = AuthorFactory(name="Author 1") self.author2 = AuthorFactory(name="Author 2") self.book = Book(title="MyBook") self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete() def test_can_list_authors(self): self.assertEqual("Author 1, Author 2", self.book.list_authors()) def test_string_method(self): self.assertEqual("MyBook by Author 1, Author 2", self.book.__str__()) def test_custom_save_method(self): self.assertIsNone(self.book.date_reviewed) self.book.review = "My review" self.book.save() self.assertIsNotNone(self.book.date_reviewed)
class BookTest(TestCase): # Django requires an explicit setup() when running tests in PTVS @classmethod def setUpClass(cls): django.setup() super(BookTest, cls).setUpClass() # setUp will run before each test def setUp(self): self.author1 = AuthorFactory(name="Author 1") self.author2 = AuthorFactory(name="Author 2") self.book = Book(title="MyBook") self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) # tearDown will run after each test def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete() # UNIT TESTS def test_can_list_authors(self): self.assertEqual("Author 1, Author 2", self.book.list_authors()) def test_string_method(self): self.assertEqual("MyBook by Author 1, Author 2", self.book.__str__()) def test_custom_save_method(self): self.assertIsNone(self.book.date_reviewed) self.book.review = "My review" self.book.save() self.assertIsNotNone(self.book.date_reviewed)
class BookTest(TestCase): def setUp(self): self.author1 = AuthorFactory(name="Author 1") self.author2 = AuthorFactory(name="Author 2") self.book = Book(title="MyBook") self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete() def test_can_list_authors(self): self.assertEqual("Author 1, Author 2", self.book.list_authors()) def test_string_method(self): self.assertEqual("MyBook by Author 1, Author 2", self.book.__str__()) def test_custom_save_method(self): self.assertIsNone(self.book.date_reviewed) self.book.review = "My Review" self.book.save() self.assertIsNotNone(self.book.date_reviewed)
class BookTest(TestCase): def setUp(self): self.author1 = AuthorFactory(name="Author-1") self.author2 = AuthorFactory(name="Author-2") self.book = Book(title="MyBook") self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete() def test_can_list_authors(self): self.assertEqual("Author-1, Author-2", self.book.list_authors()) def test__str__method(self): self.assertEqual("MyBook by Author-1, Author-2", self.book.__str__()) def test_custom_save_method(self): self.assertIsNone(self.book.date_reviewed) self.book.review = "My Review" self.book.save() # Now check that the date_review has been automatically filled with the time when the record was saved self.assertIsNotNone(self.book.date_reviewed)
class BookTest(TestCase): # function that runs before the tests def setUp(self): # lets create authors and override their value self.author1 = AuthorFactory(name="Author 1") self.author2 = AuthorFactory(name="Author 2") self.book = Book(title="Book 1") self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) # function that runs after the tests def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete() def test_can_list_authors(self): self.assertEqual("Author 1, Author 2", self.book.list_authors()) def test_string_method(self): self.assertEqual("Book 1 by Author 1, Author 2", self.book.__str__()) def test_custom_save_method(self): self.assertIsNone(self.book.date_reviewed) self.book.review = "A review" self.book.save() self.assertIsNotNone(self.book.date_reviewed)
class BookTest(TestCase): def setUp(self): self.author1 = AuthorFactory(name='Author 1') self.author2 = AuthorFactory(name='Author 2') self.book = Book(title='MyBook') self.book.save() self.book.authors.add(self.author1.pk, self.author2.pk) def tearDown(self): self.author1.delete() self.author2.delete() self.book.delete()
def api_book_detail_view(request, item_id): try: book = Book.objects.get(pk=item_id) except Book.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "GET": serializer = BookSerializer(book) return Response(serializer.data) if request.method == "PUT": serializer = BookSerializer(Book, data=request.data) data = {} if serializer.is_valid(): serializer.save() data["success"] = "Update Successful" return Response(data=data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == "DELETE": operation = Book.delete() data = {} if operation: data["success"] = "Delete Successful" else: data["success"] = "Delete Failed" return Response(data=data)
class BookCreateUpdate(SingleObjectTemplateResponseMixin, ModelFormMixin, ProcessFormView): model = Book template_name = "book-create-update.html" success_url = "/books" form_class = BookCreateUpdateForm def create_or_update_object(self, data): author_list = Book.get_m2m_id_list("filter_form", data, "author") isbn_list = Book.get_m2m_id_list("filter_form", data, "isbn") if not self.object: self.object = Book() self.object.title = data.get("title") self.object.publication_date = data.get("publication_date") self.object.num_of_pages = data.get("num_of_pages") self.object.link_to_cover = data.get("link_to_cover") self.object.publication_lang = data.get("publication_lang") self.object.save() self.object.author.set(author_list) self.object.isbn.set(isbn_list) def get_object(self, queryset=None): try: return super().get_object() except AttributeError: return None def form_valid(self, form): data = self.request.POST.copy() self.create_or_update_object(data) return HttpResponseRedirect(self.get_success_url()) def get(self, request, *args, **kwargs): self.object = self.get_object() return super().get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.object = self.get_object() if "title" not in self.request.POST: self.object.delete() return HttpResponseRedirect(self.get_success_url()) return super().post(request, *args, **kwargs)
class BookModelsTestCase(TransactionTestCase): def setUp(self): self.book = Book(**example_book_attrs) self.author = Author(**example_author_attrs) self.isbn = ISBN(**example_isbn_attrs) def tearDown(self): if self.book.pk: self.book.delete() def test_book_title_char_length_data_error(self): self.book.title = "a" * 257 self.assertRaises(DataError) self.book.title = "a" * 256 self.assertEqual(self.book.title, "a" * 256) def test_book_title_not_str_no_errors(self): self.book.title = 1 self.book.save() self.assertTrue(self.book.pk) self.assertTrue(self.book.__str__(), "1")
def other_basic_book_with_2_authors(db, other_basic_book_with_2_authors_data): obj = Book(**other_basic_book_with_2_authors_data) obj.save() yield obj obj.delete()
def other_basic_book_with_the_same_published_date(db, other_basic_book_with_the_same_published_date_data): obj = Book(**other_basic_book_with_the_same_published_date_data) obj.save() yield obj obj.delete()
def other_basic_book(db, other_basic_book_data): obj = Book(**other_basic_book_data) obj.save() yield obj obj.delete()
def basic_book(db, basic_book_data): obj = Book(**basic_book_data) obj.save() yield obj obj.delete()