-
Notifications
You must be signed in to change notification settings - Fork 55
Fixtures test3 edit1 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d922f11
9df0f47
2c54327
6c3e5f1
4f3c230
e768d2e
2819984
1d345cb
85bec9b
6d9d3ce
e7ec87a
7956157
45bba61
0097574
a324b09
9c2ebeb
59d5a2a
3781ea2
30d2078
f8e8701
d2ab8af
8a7a81f
16fb337
f452344
3d40352
85afda5
1cd5a8b
2e6c78b
5e9ac7f
c47d3ad
d6a0e8d
746a228
a0e0c49
b990c1b
24f1a58
da6c7c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Environments | ||
| .env | ||
| .venv | ||
| env/ | ||
| venv/ | ||
| ENV/ | ||
| env.bak/ | ||
| venv.bak/ | ||
| pycache/ | ||
| __pycache__/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import pytest | ||
|
|
||
| # ВОПРОС: то есть в этом тесте можно было обойтись совсем-совсем без фикстур? | ||
| # Они получились очень мелкими и многочисленными | ||
|
|
||
| @pytest.fixture() | ||
| def host_name_1(): | ||
| return "host_name_8" | ||
|
|
||
| @pytest.fixture() | ||
| def host_name_2(): | ||
| return "str" | ||
|
|
||
| @pytest.fixture() | ||
| def relative_url(): | ||
| return "relative_url" | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_params_mappings_k_and_v(): | ||
| return {'k': 'k_str', 'v': 'v_str'} | ||
|
|
||
| @pytest.fixture() | ||
| def querypart_k_and_v(get_params_mappings_k_and_v): | ||
| return '?' + '&'.join([f'{k}={v}' for (k, v) in get_params_mappings_k_and_v.items()]) | ||
|
|
||
| @pytest.fixture() | ||
| def url_k_and_v(host_name_2, relative_url, querypart_k_and_v): | ||
| return f'{host_name_2}/{relative_url}{querypart_k_and_v}' | ||
|
|
||
| @pytest.fixture() | ||
| def get_params_mappings_k(): | ||
| return {'k': 'k_str'} | ||
|
|
||
| @pytest.fixture() | ||
| def querypart_k(get_params_mappings_k): | ||
| return '?' + '&'.join([f'{k}={v}' for (k, v) in get_params_mappings_k.items()]) | ||
|
|
||
| @pytest.fixture() | ||
| def url_k(host_name_1, relative_url, querypart_k): | ||
| return f'{host_name_1}/{relative_url}{querypart_k}' | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_params_no_querypart(): | ||
| return {} | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_params_none(): | ||
| return None | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_params_attr_error(): | ||
| return [1,1] | ||
|
|
||
| @pytest.fixture() | ||
| def querypart_no_querypart(get_params_no_querypart): | ||
| return '' | ||
|
|
||
| @pytest.fixture() | ||
| def url_no_querypart(host_name_1, relative_url, querypart_no_querypart): | ||
| return f'{host_name_1}/{relative_url}{querypart_no_querypart}' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| from functions.level_1.five_title import change_copy_item | ||
| import pytest | ||
|
|
||
|
|
||
| def test_change_copy_item(): | ||
| pass | ||
| @pytest.mark.parametrize( | ||
| "title, max_main_item_title_length, expected_result", | ||
| [ | ||
| ('THIS IS TITLE', 100, 'Copy of THIS IS TITLE'), | ||
| ('THIS IS TITLE', 10, 'THIS IS TITLE'), | ||
| ('Copy of something(17)', 100, 'Copy of (18)'), | ||
| ('Copy of something (17)', 100, 'Copy of something (18)'), | ||
| ('Copy of something(1fvd7)', 100, 'Copy of something(1fvd7) (2)'), | ||
| ] | ||
| ) | ||
| def test__change_copy_item__is_valid(title, max_main_item_title_length, expected_result): | ||
| assert change_copy_item(title, max_main_item_title_length) == expected_result |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,21 @@ | ||
| from functions.level_1.four_bank_parser import BankCard, SmsMessage, Expense, parse_ineco_expense | ||
| import datetime | ||
| import decimal | ||
| from typing import NamedTuple | ||
|
|
||
|
|
||
| def test_parse_ineco_expense(): | ||
| pass | ||
| text = "112.3 $, 40004234 13.05.23 17:23 Shop_name authcode jhtfj" | ||
| author = "Masha" | ||
| sent_at = datetime.datetime.now().time() | ||
| # Входное значение для функции | ||
| sms= SmsMessage(text, author, sent_at) | ||
|
|
||
| bank_card1 = BankCard(last_digits='0123', owner= 'User1') | ||
| bank_card2 = BankCard(last_digits = '4234', owner = 'User2') | ||
| # Входное значение для функции | ||
| cards = [bank_card1, bank_card2] | ||
|
|
||
| expense_result=Expense(amount = decimal.Decimal('112.3'), card = BankCard(last_digits ='4234', owner ='User2'), spent_in = 'Shop_name', spent_at = datetime.datetime(2023, 5, 13, 17, 23)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Длинная строка, я бы отформатировал |
||
| assert parse_ineco_expense(sms, cards) == expense_result | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,31 @@ | ||
| from functions.level_1.one_gender import genderalize | ||
| import pytest | ||
|
|
||
|
|
||
| def test_genderalize(): | ||
| pass | ||
| @pytest.fixture | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Эти фикстуры тоже едва ли тебе пригодятся где-то ещё. Ну и не забывай, что место фикстур в конфтесте, а не в файле с тестами |
||
| def verb_male(): | ||
| return "verb_male" | ||
|
|
||
| @pytest.fixture | ||
| def verb_female(): | ||
| return "verb_female" | ||
|
|
||
| @pytest.fixture | ||
| def random_symbols(): | ||
| return "4w5rgt" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "verb_male, verb_female, gender, expected_result", | ||
| [ | ||
| (verb_male, random_symbols, "male", verb_male), | ||
| (verb_male, verb_female, "female", verb_female), | ||
| (verb_male, verb_female, random_symbols, verb_female), | ||
| ] | ||
| ) | ||
| def test__genderalize__is_valid(verb_male, verb_female, gender, expected_result): | ||
| assert genderalize(verb_male, verb_female, gender) == expected_result | ||
|
|
||
|
|
||
| def test__genderalize_only_two_parameters_typeerror(): | ||
| with pytest.raises(TypeError): | ||
| genderalize(verb_male, verb_female) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,46 @@ | ||
| from functions.level_1.three_url_builder import build_url | ||
| import pytest | ||
|
|
||
|
|
||
| def test_build_url(): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "host_name, relative_url, get_params, expected_result", | ||
| [ | ||
| ('host_name_1', 'relative_url', 'get_params_none', 'url_no_querypart'), | ||
| ('host_name_2', 'relative_url', 'get_params_mappings_k_and_v', 'url_k_and_v'), | ||
| ('host_name_1', 'relative_url', 'get_params_mappings_k', 'url_k'), | ||
| ('host_name_1', 'relative_url', 'get_params_no_querypart', 'url_no_querypart'), | ||
| ] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут во-первых не хватает отступа, а во-вторых тут хорошо видно, что ты используешь те же константы, только проксируешь их через фикстуры. Прикол в том, что фикстуры нужны для универсальных штук, а тут у тебя штуки, которые будут нужны для тестирования только этой функции. Поэтому в фикстуры их выносить смысла не имеет. |
||
| ) | ||
| def test__build_url__is_valid(host_name, relative_url, get_params, expected_result, request): | ||
| host_name = request.getfixturevalue(host_name) | ||
| relative_url = request.getfixturevalue(relative_url) | ||
| get_params = request.getfixturevalue(get_params) | ||
| expected_result = request.getfixturevalue(expected_result) | ||
| assert build_url(host_name, relative_url, get_params) == expected_result | ||
|
|
||
|
|
||
| def test__build_url_only_host_name_typeerror(host_name_1): | ||
| with pytest.raises(TypeError): | ||
| build_url(host_name_1) | ||
|
|
||
|
|
||
| def test_build_url_only_get_params_typeerror(get_params_mappings_k): | ||
| with pytest.raises(TypeError): | ||
| build_url(get_params_mappings_k) | ||
|
|
||
|
|
||
| def test__build_url_value_error_no_parmeters(): | ||
| with pytest.raises(TypeError): | ||
| build_url() | ||
|
|
||
| # Вопрос: убрать что ли нафиг этот тест, тк с такими данными результат - не тайпэррор( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Дык и до этого тайпэррор был не из-за функции, а из-за данных. Получается, что это ошибка в дизайне теста: ты думала, что будешь тестировать какой-то кейс поведения функции, а оказалось, что этого кейса нет. |
||
| def test__build_url_bad_key_in_get_params_typeerror(host_name_1, relative_url): | ||
| with pytest.raises(TypeError): | ||
| build_url(host_name_1, relative_url, {(1,1): None}) | ||
|
|
||
|
|
||
| def test__build_url_bad_key_in_get_params_attributeerror(host_name_1, relative_url, get_params_attr_error): | ||
| with pytest.raises(AttributeError): | ||
| build_url(host_name_1, relative_url, get_params_attr_error) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,52 @@ | ||
| from functions.level_1.two_date_parser import compose_datetime_from | ||
| import datetime | ||
| import pytest | ||
|
|
||
|
|
||
| def test_compose_datetime_from(): | ||
| pass | ||
| @pytest.fixture | ||
| def time_str(): | ||
| return "19:55" | ||
|
|
||
| @pytest.fixture | ||
| def expected_result(time_str): | ||
| hour_str, minute_str = time_str.strip().split(":") | ||
| hour_str = int(hour_str) | ||
| minute_str = int(minute_str) | ||
| return datetime.datetime(datetime.date.today().year, | ||
| datetime.date.today().month, | ||
| datetime.date.today().day, | ||
| hour_str, minute_str) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Все многострочные штуки я предпочитаю форматировать вот так: |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "date_str, time_str, expected_result", | ||
| [ | ||
| # ("2023,12,15", time_str, expected_result), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удоли :) |
||
| ("tomorrow", "19:55", datetime.datetime(datetime.date.today().year, | ||
| datetime.date.today().month, | ||
| datetime.date.today().day+1, | ||
| 19,55)), | ||
| ("202yhbmj5", "19:33", datetime.datetime(datetime.date.today().year, | ||
| datetime.date.today().month, | ||
| datetime.date.today().day, | ||
| 19,33)), | ||
| ] | ||
| ) | ||
| def test__compose_datetime_from__is_valid(date_str, time_str, expected_result): | ||
| assert compose_datetime_from(date_str, time_str) == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "date_str, time_str, expected_error, need_time_str", | ||
| [ | ||
| ("202yhbmj5", 14645, AttributeError, 1), | ||
| ("19:33", 0, TypeError, 0) | ||
| ] | ||
| ) | ||
| def test__compose_datetime_from__error(date_str, time_str, expected_error, need_time_str): | ||
| with pytest.raises(expected_error): | ||
| if need_time_str == 1: | ||
| compose_datetime_from(date_str, time_str) | ||
| else: | ||
| compose_datetime_from(date_str) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from functions.level_1_5.three_first import first, NOT_SET | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "items, default, expected_result, items_need", | ||
| [ | ||
| ([1, 2, 3], None, 1, 1), | ||
| ([1, 2, 3], NOT_SET, 1, 1), | ||
| ([], 'default', 'default', 1), | ||
| ('', 'default', 'default', 1), | ||
| (None, 'default', 'default', 1), | ||
| (None, 1234, 1234, 1), | ||
| (None, None, None, 1 ), | ||
| (None, (5, 56), (5, 56), 1), | ||
| (0, 'default', 'd', 0), | ||
| (0, NOT_SET, 'N', 0) | ||
| ] | ||
| ) | ||
| def test__first__is_valid(items, default, expected_result, items_need): | ||
| if items_need == 1: | ||
| assert first(items, default) is expected_result | ||
| else: | ||
| assert first(default) is expected_result | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "items, default, expected_error, items_need, default_need", | ||
| [ | ||
| ([], 0, AttributeError, 1, 0), | ||
| ([], NOT_SET, AttributeError, 1, 1), | ||
| (0, 0, TypeError, 0, 0), | ||
| ] | ||
| ) | ||
| def test__first__errors(items, default, expected_error, items_need, default_need): | ||
| with pytest.raises(expected_error): | ||
| if items_need == 1: | ||
| if default_need == 1: | ||
| first(items, default) | ||
| else: | ||
| first(items) | ||
| else: | ||
| if default_need == 1: | ||
| pass | ||
| else: | ||
| first() | ||
|
|
||
|
|
||
| def test__first__items_is_empty_attributeerror(): | ||
| with pytest.raises(AttributeError): | ||
| first([], ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from functions.level_1_5.five_replace_word import replace_word | ||
| import pytest | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "text, replace_from, replace_to, expected_result", | ||
| [ | ||
| ('one two three four', 'two', 'replace_to', 'one replace_to three four'), | ||
| ('one two three Two four', 'two', 'replace_to','one replace_to three replace_to four'), | ||
| ('one two three four', 'TWo', 'replace_to','one replace_to three four'), | ||
| ('one two three four', '1234', 'replace_to', 'one two three four'), | ||
| ('one two three four', '', 'replace_to', 'one two three four'), | ||
| ('one two three four', 'two', '', 'one three four'), | ||
| ] | ||
| ) | ||
| def test__replace_word__is_valid(text, replace_from, replace_to, expected_result): | ||
| assert replace_word(text, replace_from, replace_to) == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text, replace_from, replace_to, expected_error", | ||
| [ | ||
| ('one two three four', 'two', 123, TypeError), | ||
| ('one two three four', 2, '123', AttributeError), | ||
| (123314, 'two', '123', AttributeError), | ||
| ] | ||
| ) | ||
| def test__replace_word__errors(text, replace_from, replace_to, expected_error): | ||
| with pytest.raises(expected_error): | ||
| replace_word(text, replace_from, replace_to) | ||
|
|
||
|
|
||
| def test__replace_word__not_enough_params_typeerror(): | ||
| with pytest.raises(TypeError): | ||
| replace_word('123', 'bsrbg') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from functions.level_1_5.four_sentiment import check_tweet_sentiment | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text, good_words, bad_words, expected_result", | ||
| [ | ||
| ('g1 b1 g2 b2 g3 b3 ', {'g1','g2'}, {'b1', 'b2'}, None), | ||
| ('g0 b0 g0 b0 g3 b3 ', {'g1','g2'}, {'b1', 'b2'}, None), | ||
| ('g1 b2 g0 b0 g3 b3 ', {'g1','g2'}, {'g1', 'g2'}, None), | ||
| ('g1 b2 g0 b0 g3 b3 g4 b4', {'g1','g2', 'g3'}, {'g1', 'g2'},'GOOD'), | ||
| ('1 2', '5 6 7 4', '2', 'BAD'), | ||
| ('g0 b0 g0 b0 g0 b3 g4 b4', {'g1','g2', 'g3'}, {'g1', 'g2'}, None), | ||
| ('g0 b0 g3 b3 g4 b4', {'g1','g2'}, {'g1', 'g2', 'g3'},'BAD'), | ||
| ('g0 b0 g3 b3 g4 b4', {''}, {'g1', 'g2', 'g3'}, 'BAD'), | ||
| ('g0 b0 g3 b3 g4 b4', {''}, {''}, None), | ||
| ('', {''}, {''}, None), | ||
| ('one two', 'one', 'two', None), | ||
| ] | ||
| ) | ||
|
|
||
| def test__check_tweet_sentiment__is_valid(text, good_words, bad_words, expected_result): | ||
| assert check_tweet_sentiment(text, good_words, bad_words) is expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text, good_words, bad_words, expected_error", | ||
| [ | ||
| # ('', {''}, TypeError), | ||
| (123, '5 6 7 4', '2', AttributeError), | ||
| ] | ||
| ) | ||
|
|
||
| def test__check_tweet_sentiment__errors(text, good_words, bad_words, expected_error): | ||
| with pytest.raises(expected_error): | ||
| check_tweet_sentiment(text, good_words, bad_words) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да! Можно :)