Пример #1
0
    def test_crud(self):
        with self.app_context():
            StoreModel('test').save_to_db()
            item = ItemModel('test', 19.99, 1)

            # verifica se o item não existe no banco de dados
            # o terceiro argumento é uma mensagem de erro personalizada, que pode ajudar muito no decorrer das execuções de testes
            self.assertIsNone(
                ItemModel.find_by_name('test'),
                f'Found an item with name {item.name!r} on database, but expected not to.'
            )

            item.save_to_db()
            # verifica se o item existe no banco de dados
            self.assertIsNotNone(
                ItemModel.find_by_name('test'),
                f'Not found an item with name {item.name!r} on database, but expected to.'
            )

            item.delete_from_db()
            # verifica se o item não existe no banco de dados
            self.assertIsNone(
                ItemModel.find_by_name('test'),
                f'Found an item with name {item.name!r} on database, but expected not to.'
            )
Пример #2
0
class ItemTest(TestCase):
    def setUp(self):
        self.item = ItemModel('Caneta', 1.5)

    def test_create_item(self):
        self.assertEqual(self.item.name, 'Caneta')
        self.assertEqual(self.item.price, 1.5)

    def test_save_item(self):
        with patch('app.models.item.db.session.add') as mocked_db_add:
            with patch(
                    'app.models.item.db.session.commit') as mocked_db_commit:
                self.item.save_to_db()

                mocked_db_add.assert_called()
                mocked_db_commit.assert_called()

    def test_delete_item(self):
        with patch('app.models.item.db.session.delete') as mocked_db_delete:
            with patch(
                    'app.models.item.db.session.commit') as mocked_db_commit:
                self.item.delete_from_db()

                mocked_db_delete.assert_called()
                mocked_db_commit.assert_called()

    def test_find_by_name(self):
        app = create_app()
        app.testing = True

        expected = self.item

        with app.app_context():
            self.item.save_to_db()
            resp = ItemModel.find_by_name('Caneta')

            self.assertEqual(resp.name, expected.name)
            self.assertEqual(resp.price, expected.price)

    def test_json(self):
        item_json = self.item.json()
        expected = {'name': 'Caneta', 'price': 1.5}

        self.assertDictEqual(item_json, expected)