Beispiel #1
0
    def test_validate_kwargs_wrong_data(self):
        kwargs = {"name": 67}

        # raises the validate content has an incorrect value
        with self.assertRaises(AsyncOrmFieldError) as exc:
            book = Book()
            book.validate_kwargs(kwargs)

        self.assertIn("is a wrong datatype for field", exc.exception.args[0])
Beispiel #2
0
    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(AsyncOrmModelError) 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'])
Beispiel #3
0
    def test_validate_kwargs_with_forced_id(self):
        kwargs = {"id": 34, "name": "name"}

        # also raises AsyncOrmFieldError because you can not pre-set the object's id
        with self.assertRaises(AsyncOrmFieldError) as exc:
            book = Book()
            book.validate_kwargs(kwargs)

        self.assertEqual(exc.exception.args[0],
                         "Models can not be generated with forced id")
Beispiel #4
0
    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(AsyncOrmModelError) 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()
Beispiel #5
0
    async def test_choices_content_not_in_choices(self):
        # choices defined as lists or tuples
        with self.assertRaises(AsyncOrmFieldError) as exc:
            book = Book(content="telomero")
            await book.save()

        self.assertEqual(exc.exception.args[0],
                         '"telomero" not in field choices')
Beispiel #6
0
    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")
Beispiel #7
0
    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)
Beispiel #8
0
    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"
            ]),
        )
Beispiel #9
0
    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)
Beispiel #10
0
    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)
Beispiel #11
0
    async def test_choices_display(self):
        book = Book(content="hard cover")

        self.assertEqual(book.content_display(), "hard cover book")
Beispiel #12
0
    def test_validate_kwargs_no_error(self):
        kwargs = {"name": "name"}

        # now it correctly validates
        book = Book()
        self.assertEqual(book.validate_kwargs(kwargs), None)
Beispiel #13
0
    def test_instantiated__init__(self):
        # classmethods tests
        book = Book()

        self.assertEqual(book.db_pk, "id")
        self.assertEqual(book.orm_pk, "id")