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_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): # 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): # 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 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")