def test_validate_kwargs_wrong_data(self): kwargs = {'name': 67} # raises the validate content has an incorrect value with self.assertRaises(FieldError) as exc: book = Book() book.validate_kwargs(kwargs) self.assertIn('is a wrong datatype for field', exc.exception.args[0])
def test_validate_kwargs_with_forced_id(self): kwargs = {'id': 34, 'name': 'name'} # also raises fielderror because you can not pre-set the object's id with self.assertRaises(FieldError) as exc: book = Book() book.validate_kwargs(kwargs) self.assertEqual(exc.exception.args[0], 'Models can not be generated with forced id')
def test_validate_kwargs_with_wrong_fieldname(self): kwargs = {'name': 'name', 'volume': 23} # raises the validate error because volume is not a correct attrib with self.assertRaises(ModelError) as exc: book = Book() book.validate_kwargs(kwargs) # its a list because we validate all kwargs self.assertEqual(exc.exception.args[0], ['"volume" is not an attribute for Book'])
async def test_unique_together(self): # we can not create new books with same name and content together book = Book(**{'name': 'book name 5', 'content': 'hard cover'}) with self.assertRaises(ModelError) as exc: await book.save() self.assertEqual('The model violates a unique constraint', exc.exception.args[0]) # but when any of them are different there is no problem book.name = 'this is a new name' await book.save()
async def create_book(x): book = Book(**{ "name": "book name {}".format(str(x)), "content": "hard cover" }) await book.save()
async def create_book(x): book = Book(**{ 'name': 'book name {}'.format(str(x)), 'content': 'hard cover', }) await book.save()
async def test_choices_content_not_in_choices(self): # choices defined as lists or tuples with self.assertRaises(FieldError) as exc: book = Book(content='telomero') await book.save() self.assertEqual(exc.exception.args[0], '"telomero" not in model choices')
def test_class__init__(self): # classmethods tests # no matter how you import them they are the same object self.assertTrue(Author is Author2) self.assertTrue(Book is Book2) self.assertEqual(Book().cls_tablename(), 'library') self.assertEqual(Author().cls_tablename(), 'Author')
async def test_ordering(self): # since ordering is by id descending self.assertEqual(Book().ordering, ['-id']) # the first book with id lower that 10 book = await Book.objects.filter(id__lt=10)[0] # is 9 self.assertEqual(book.id, 9)
def test_get_fields(self): fields = Book.get_fields() self.assertEqual(len(fields), 7) self.assertEqual( sorted(list(fields.keys())), sorted([ 'id', 'content', 'name', 'author', 'date_created', 'price', 'quantity' ]))
def test_get_fields(self): fields = Book.get_fields() self.assertEqual(len(fields), 7) self.assertEqual( sorted(list(fields.keys())), sorted([ "id", "content", "name", "author", "date_created", "price", "quantity" ]), )
async def test_save_no_id_before_save(self): book = Book( **{ "name": "lord of the rings", "content": "hard cover", "date_created": datetime.now(), }) id_before_save = book.id await book.save() self.assertFalse(id_before_save) self.assertTrue(book.id)
async def test_id_persitent(self): book = Book( **{ "name": "silmarilion", "content": "hard cover", "date_created": datetime.now(), }) await book.save() orig_id = book.id await book.save() self.assertEqual(orig_id, book.id)
async def test_save_no_id_before_save(self): book = Book( **{ 'name': 'lord of the rings', 'content': 'hard cover', 'date_created': datetime.now(), }) id_before_save = book.id await book.save() self.assertFalse(id_before_save) self.assertTrue(book.id)
async def test_id_persitent(self): book = Book( **{ 'name': 'silmarilion', 'content': 'hard cover', 'date_created': datetime.now(), }) await book.save() orig_id = book.id await book.save() self.assertEqual(orig_id, book.id)
async def test_choices_display(self): book = Book(content='hard cover') self.assertEqual(book.content_display(), 'hard cover book')
def test_validate_kwargs_no_error(self): kwargs = {'name': 'name'} # now it correctly validates book = Book() self.assertEqual(book.validate_kwargs(kwargs), None)
def test_instantiated__init__(self): # classmethods tests book = Book() self.assertEqual(book.db_pk, 'id') self.assertEqual(book.orm_pk, 'id')
def book(self, author): return Book(title='title', author=author)