Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions tests/level_1/test_five_title.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import pytest

from functions.level_1.five_title import change_copy_item


def test_change_copy_item():
pass
@pytest.fixture
def base_book():
return {'name': 'Some book name (1234)', 'max_l': 20}


@pytest.fixture
def base_copy_book(base_book):
return {'name': 'Copy of ' + base_book['name'], 'max_l': 100}


def test_change_copy_item(base_book: dict[str, str | int]):
assert change_copy_item(base_book['name'],
base_book['max_l']) == 'Some book name (1234)'


def test_change_copy_long_item(base_book: dict[str, str | int]):
assert change_copy_item(base_book['name'], 100) == 'Copy of Some book name (1234)'


def test_change_copy_item_number(base_copy_book: dict[str, str | int]):
assert change_copy_item(base_copy_book['name'], base_copy_book['max_l']) ==\
'Copy of Some book name (1235)'


@pytest.mark.parametrize('name, max_l, expected',
[
('Book name 1 (1234)',
50,
'Copy of Book name 1 (1234)'),
('Veeeeeery long book name (1234)',
15,
'Veeeeeery long book name (1234)')
])
def test_several_items(name, max_l, expected):
assert change_copy_item(name, max_l) == expected
51 changes: 49 additions & 2 deletions tests/level_1/test_four_bank_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
import datetime
import decimal

import pytest

from functions.level_1.four_bank_parser import BankCard, SmsMessage, Expense, parse_ineco_expense


def test_parse_ineco_expense():
pass
@pytest.fixture
def base_sms():
return SmsMessage(
text='123.45 RUB, 5559123456789012 20.11.23 21:00 SOFA_SHOP authcode 123456',
author='My Bank',
sent_at=datetime.datetime.now()
)

@pytest.fixture
def no_card_sms():
return SmsMessage(
text='123.45 RUB, no_card 20.11.23 21:00 SOFA_SHOP authcode 123456',
author='My Bank',
sent_at=datetime.datetime.now()
)

@pytest.fixture
def base_cards():
return {'cards':
[
BankCard(
last_digits='9012',
owner='Eugene'
),
BankCard(
last_digits='1234',
owner='Daria'
)
]
}


def test_parse_ineco_expense(base_sms, base_cards):
assert parse_ineco_expense(base_sms, base_cards['cards']) == Expense(
amount=decimal.Decimal('123.45'),
card=BankCard(last_digits='9012', owner='Eugene'),
spent_in='SOFA_SHOP',
spent_at=datetime.datetime.strptime(f'20.11.23 21:00', '%d.%m.%y %H:%M')
)


def test_parse_no_card(no_card_sms, base_cards):
with pytest.raises(IndexError):
parse_ineco_expense(no_card_sms, base_cards['cards'])
23 changes: 21 additions & 2 deletions tests/level_1/test_one_gender.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import pytest

from functions.level_1.one_gender import genderalize


def test_genderalize():
pass
@pytest.fixture
def base_gender_data():
return {'male': 'He goes','female': 'She goes', 'sex': 'male'}


def test_genderalize_male(base_gender_data):
assert genderalize(base_gender_data['male'],
base_gender_data['female'],
base_gender_data['sex']) == base_gender_data['male']


def test_genderalize_female(base_gender_data):
assert genderalize(base_gender_data['male'],
base_gender_data['female'],
'female') == base_gender_data['female']


def test_return_type():
assert type(genderalize('goes', 'goes', 'male') == type(str))
26 changes: 24 additions & 2 deletions tests/level_1/test_three_url_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import pytest

from functions.level_1.three_url_builder import build_url


def test_build_url():
pass
@pytest.fixture
def base_url_data():
return {'base': 'some-url.com','relative': 'relative_part'}


@pytest.fixture
def url_data_params(base_url_data):
return {'params': {'param1': 'value1', 'param2': 'value2'}}


def test_build_url(base_url_data):
assert build_url(base_url_data['base'],
base_url_data['relative']) == \
base_url_data['base']+'/'+base_url_data['relative']


def test_build_url_params(base_url_data: dict[str, str],
url_data_params: dict[str, dict:[str, str]]):
assert build_url(base_url_data['base'],
base_url_data['relative'],
url_data_params['params']) == \
'some-url.com/relative_part?param1=value1&param2=value2'
46 changes: 44 additions & 2 deletions tests/level_1/test_two_date_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
import datetime

import pytest

from functions.level_1.two_date_parser import compose_datetime_from


def test_compose_datetime_from():
pass
@pytest.fixture
def base_date():
return {'day': 'today', 'time': '04:20'}


@pytest.fixture
def future_date():
return {'day': 'tomorrow', 'time': '12:00'}


@pytest.fixture
def wrong_data():
return {'day': '123456','time': 'asdfghjkl'}


def test_compose_datetime_from(base_date):
date = datetime.date.today()
assert compose_datetime_from(base_date['day'],
base_date['time']) == datetime.datetime(date.year,
date.month,
date.day,
int('04'),
int('20'))


def test_compose_datetime_tomorrow(future_date):
date = datetime.date.today()
full_date = datetime.datetime(date.year,
date.month,
date.day,
int('12'),
int('00'))
assert compose_datetime_from(future_date['day'],
future_date['time']) - full_date == \
datetime.timedelta(days=1)


def test_compose_datetime_wrong_data(wrong_data):
with pytest.raises(ValueError):
compose_datetime_from(wrong_data['day'], wrong_data['time'])
24 changes: 24 additions & 0 deletions tests/level_2/test_five_replace_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from functions.level_2.five_replace_word import replace_word


@pytest.mark.parametrize('text, word_from, word_to, expected',
[('one two', 'one', 'three', 'three two'),
('THREE FIVE', 'three', 'six', 'six FIVE')])
def test__replace_word__success(text, word_from, word_to, expected):
assert replace_word(text, word_from, word_to) == expected


@pytest.mark.parametrize('text, word_from, word_to, expected',
[('one two', 'four', 'three', 'one two'),
('THREE FIVE', 'four', 'six', 'THREE FIVE')])
def test__replace_word__fail(text, word_from, word_to, expected):
assert replace_word(text, word_from, word_to) == expected


# @pytest.mark.parametrize('text, word_from, word_to, expected',
# [(1, 'four', 'three', AttributeError),
# ([1, 2], 'four', 'six', AttributeError)])
# def test__replace_word__error(text: str, word_from, word_to, expected):
# with pytest.raises(expected):
# replace_word(text, word_from, word_to)
38 changes: 38 additions & 0 deletions tests/level_2/test_four_sentiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
from functions.level_2.four_sentiment import check_tweet_sentiment


@pytest.mark.parametrize('text, good_words, bad_words, expected',
[('Bright sun is shining',
{'bright', 'shining'},
{'fuck', 'shit'},
'GOOD'),
('Bright fuck is shit',
{'bright', 'shining'},
{'fuck', 'shit'},
'BAD')])
def test__check_tweet_sentiment__success(text: str, good_words: set[str],
bad_words: set[str], expected: str):
assert check_tweet_sentiment(text, good_words, bad_words) == expected


@pytest.mark.parametrize('text, good_words, bad_words, expected',
[('Bright sun is shining',
set(),
set(),
None),
('Bright fuck is shit',
{'bright', 'shining'},
{'bright', 'shining'},
None)])
def test__check_tweet_sentiment__fail(text: str, good_words: set[str],
bad_words: set[str], expected: None):
assert check_tweet_sentiment(text, good_words, bad_words) == expected


# @pytest.mark.parametrize('text, good_words, bad_words, expected',
# [(123, set(), set(), AttributeError)])
# def test__check_tweet_sentiment__error(text: str, good_words: set[str],
# bad_words:set[str], expected):
# with pytest.raises(expected):
# check_tweet_sentiment(text, good_words, bad_words)
29 changes: 29 additions & 0 deletions tests/level_2/test_one_pr_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from functions.level_2.one_pr_url import is_github_pull_request_url

# https://github.com/justatrade/crosses_and_zeros/pull/2


@pytest.mark.parametrize('url, expected',
[('https://github.com/justatrade/oop_bases_challenges/pull/3', True),
('https://github.com/justatrade/crosses_and_zeros/pull/2', True)])
def test__is_github_pull_request_url__succses(url: str, expected: bool):
assert is_github_pull_request_url(url) == expected


@pytest.mark.parametrize('url, expected',
[('https://pornhub.com/justatrade/oop_bases_challenges/pull/3', False),
('https://github.com/justatrade/crosses_and_zeros/push/2', False),
('https://////', False),
('a/b/c/d/e/f', False),
('', False)])
def test__is_github_pull_request_url__fail(url: str, expected: bool):
assert is_github_pull_request_url(url) == expected


# @pytest.mark.parametrize('not_url, expected',
# [(12345, AttributeError),
# (123.45, AttributeError)])
# def test__is_github_pull_request_url__value_error(not_url: str, expected): # не смог подобрать тут хинт, чтобы линтер не ругался
# with pytest.raises(expected):
# is_github_pull_request_url(not_url)
25 changes: 25 additions & 0 deletions tests/level_2/test_three_first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from functions.level_2.three_first import first


@pytest.mark.parametrize('items, default, expected',
[([1, 2, 3], 'some_default', 1),
([1, 2, 3], None, 1)])
def test__first__success(items, default, expected):
assert first(items, default) == expected


@pytest.mark.parametrize('items, default, expected',
[([], 'some_default', 'some_default'),
([], None, None)])
def test__first__empty_list(items, default, expected):
assert first(items, default) == expected


# @pytest.mark.parametrize('items, default, expected',
# [(123, 'some_default', TypeError),
# ([], 'NOT_SET', AttributeError)])
# def test__first__error(items, default, expected):
# with pytest.raises(expected):
# first(items, default)
#
45 changes: 45 additions & 0 deletions tests/level_2/test_two_date_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest
from functions.level_2.two_square_equation import solve_square_equation


@pytest.mark.parametrize('square_coefficient, linear_coefficient,'
'const_coefficient, expected',
[(5.0, -8.0, 3.0, (0.6, 1.0)),
(8.0, 2.0, -1.0, (-0.5, 0.25)),
(0.0, 1.0, -4.0, (4.0, None)),
(4.0, 4.0, 1.0, (-0.5, -0.5))])
def test__two_square_equation__success(square_coefficient,
linear_coefficient,
const_coefficient,
expected):
assert solve_square_equation(square_coefficient,
linear_coefficient,
const_coefficient) == expected


@pytest.mark.parametrize('square_coefficient, linear_coefficient,'
'const_coefficient, expected',
[(1.0, 2.0, 3.0, (None, None))])
def test__two_square_equation__fail(square_coefficient,
linear_coefficient,
const_coefficient,
expected):
assert solve_square_equation(square_coefficient,
linear_coefficient,
const_coefficient) == expected


# @pytest.mark.parametrize('square_coefficient, linear_coefficient,'
# 'const_coefficient, expected',
# [('abc', -8.0, 3.0, TypeError),
# (8.0, 'abc', -1.0, TypeError),
# (0.0, 1.0, 'abc', TypeError),
# (None, None, None, TypeError)])
# def test__two_square_equation__error(square_coefficient,
# linear_coefficient,
# const_coefficient,
# expected):
# with pytest.raises(expected):
# solve_square_equation(square_coefficient,
# linear_coefficient,
# const_coefficient)