Last active
March 29, 2021 13:53
-
-
Save DBoyara/58b981492b96654f2cec5c329e332676 to your computer and use it in GitHub Desktop.
practice pytest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pytest | |
| import sqlalchemy | |
| @pytest.fixture | |
| def engine(): | |
| return sqlalchemy.create_engine('sqlite://') | |
| @pytest.fixture | |
| def engine_with_table_users(engine): | |
| engine.execute('CREATE TABLE users (username VARCHAR);') | |
| yield engine | |
| engine.execute('DROP TABLE users;') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlalchemy | |
| def number_of_users_in_database(engine): | |
| return engine.execute('SELECT count(*) FROM users;').fetchone()[0] | |
| def get_engine(config, qwerty): | |
| return sqlalchemy.create_engine('sqlite://') | |
| def number_of_users_in_database_2(): | |
| engine = get_engine('config', 'qwerty1234567') | |
| return number_of_users_in_database(engine) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from unittest import mock | |
| import pytest | |
| from database import number_of_users_in_database, number_of_users_in_database_2 | |
| @pytest.fixture | |
| def engine_with_user(engine_with_table_users): | |
| engine_with_table_users.execute( | |
| 'INSERT INTO users (username) VALUES ("otus");' | |
| ) | |
| return engine_with_table_users | |
| @pytest.fixture | |
| def engine_with_n_users(request, engine_with_table_users): | |
| for i in range(request.param): | |
| engine_with_table_users.execute( | |
| 'INSERT INTO users (username) VALUES ("?");', | |
| params=(f'user_{i}', ), | |
| ) | |
| return engine_with_table_users | |
| def test_number_of_users_in_database(engine_with_user): | |
| assert number_of_users_in_database(engine_with_user) == 1 | |
| @pytest.mark.parametrize( | |
| 'engine_with_n_users, expected', [ | |
| (1, 1), | |
| (2, 2), | |
| (3, 3), | |
| ], | |
| indirect=['engine_with_n_users'], | |
| ) | |
| def test_n_users_in_database(engine_with_n_users, expected): | |
| assert number_of_users_in_database(engine_with_n_users) == expected | |
| @mock.patch('database.get_engine', autospec=True) | |
| def test_number_of_users_in_database_2(mocked_get_engine, engine_with_user): | |
| mocked_get_engine.return_value = engine_with_user | |
| # mocked_get_engine.side_effect = lambda a, b: '1' if a else b | |
| assert number_of_users_in_database_2() == 1 | |
| mocked_get_engine.assert_called_once() | |
| mocked_get_engine.assert_called_once_with('config', 'qwerty1234567') | |
| mocked_get_engine.assert_has_calls([ | |
| mock.call('config', 'qwerty1234567'), | |
| ]) | |
| # mocked_get_engine.assert_not_called() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment