Coverage report: + 100% +
+ + ++ coverage.py v7.3.2, + created at 2023-11-16 01:34 +0300 +
+From 7afef6f4263daf1d2f87e5ce2e6cba10f1099a19 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:38:22 +0300 Subject: [PATCH 01/23] Create morse.py --- testing_hw/issue-01/morse.py | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 testing_hw/issue-01/morse.py diff --git a/testing_hw/issue-01/morse.py b/testing_hw/issue-01/morse.py new file mode 100644 index 0000000..1e37d72 --- /dev/null +++ b/testing_hw/issue-01/morse.py @@ -0,0 +1,55 @@ +"""Encoding text according to Morse code table""" + +import doctest + +LETTER_TO_MORSE = {'A': '.-', 'B': '-...', + 'C': '-.-.', 'D': '-..', 'E': '.', + 'F': '..-.', 'G': '--.', 'H': '....', + 'I': '..', 'J': '.---', 'K': '-.-', + 'L': '.-..', 'M': '--', 'N': '-.', + 'O': '---', 'P': '.--.', 'Q': '--.-', + 'R': '.-.', 'S': '...', 'T': '-', + 'U': '..-', 'V': '...-', 'W': '.--', + 'X': '-..-', 'Y': '-.--', 'Z': '--..', + '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', + '7': '--...', '8': '---..', '9': '----.', + '0': '-----', ', ': '--..--', '.': '.-.-.-', + '?': '..--..', '/': '-..-.', '-': '-....-', + '(': '-.--.', ')': '-.--.-'} + + +def encode(message: str) -> str: + """ + Кодирует строку в соответсвии с таблицей азбуки Морзе + + >>> encode('SOS') # doctest: +NORMALIZE_WHITESPACE + '... --- ...' + >>> encode('SOS') + '... --- ...' + >>> encode(".)") + '.-.-.- -.--.-' + >>> encode('') + '' + >>> encode(911) + Traceback (most recent call last): + ... + TypeError: 'int' object is not iterable + >>> encode('sos') + Traceback (most recent call last): + ... + KeyError: 's' + >>> encode(';)') + Traceback (most recent call last): + ... + KeyError: ';' + """ + encoded_signs = [ + LETTER_TO_MORSE[letter] for letter in message + ] + + return ' '.join(encoded_signs) + + +if __name__ == "__main__": + doctest.testmod() From 341fafb008c06a17f1e3cba130d732a28e7d08e6 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:41:15 +0300 Subject: [PATCH 02/23] Create result.txt --- testing_hw/issue-01/result.txt | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 testing_hw/issue-01/result.txt diff --git a/testing_hw/issue-01/result.txt b/testing_hw/issue-01/result.txt new file mode 100644 index 0000000..5e455dc --- /dev/null +++ b/testing_hw/issue-01/result.txt @@ -0,0 +1,49 @@ +PS D:\python> python -m doctest -o NORMALIZE_WHITESPACE -v morse.py +Trying: + encode('SOS') # doctest: +NORMALIZE_WHITESPACE +Expecting: + '... --- ...' +ok +Trying: + encode('SOS') +Expecting: + '... --- ...' +ok +Trying: + encode(".)") +Expecting: + '.-.-.- -.--.-' +ok +Trying: + encode('') +Expecting: + '' +ok +Trying: + encode(911) +Expecting: + Traceback (most recent call last): + ... + TypeError: 'int' object is not iterable +ok +Trying: + encode('sos') +Expecting: + Traceback (most recent call last): + ... + KeyError: 's' +ok +Trying: + encode(';)') +Expecting: + Traceback (most recent call last): + ... + KeyError: ';' +ok +1 items had no tests: + morse +1 items passed all tests: + 7 tests in morse.encode +7 tests in 2 items. +7 passed and 0 failed. +Test passed. From 953acad88e4f63a18059124835667900907f1d0b Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:59:17 +0300 Subject: [PATCH 03/23] Create README.md --- testing_hw/issue-01/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 testing_hw/issue-01/README.md diff --git a/testing_hw/issue-01/README.md b/testing_hw/issue-01/README.md new file mode 100644 index 0000000..4bf1248 --- /dev/null +++ b/testing_hw/issue-01/README.md @@ -0,0 +1,8 @@ +# issue-01 +При запуске в PyСharm все тесты проходят, но не выводится отчет. + +Для вывода отчета о пройденных тестах переходим в директории с файлом morse.py и в терминале выполнияем команду: + +```python -m doctest -o NORMALIZE_WHITESPACE -v morse.py``` + +(Добавляем ```-o NORMALIZE_WHITESPACE```, так как этот флаг позволяет не различать последовательности пробелов, что необходимо для одного из тестов) From eaca119390d20e46f686e9fa9266aac6f037aa5e Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:28:19 +0300 Subject: [PATCH 04/23] Create morse_2.py --- testing_hw/issue-02/morse_2.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 testing_hw/issue-02/morse_2.py diff --git a/testing_hw/issue-02/morse_2.py b/testing_hw/issue-02/morse_2.py new file mode 100644 index 0000000..0ac69d4 --- /dev/null +++ b/testing_hw/issue-02/morse_2.py @@ -0,0 +1,48 @@ +"""Decoding text according to Morse code table""" + +import pytest + + +LETTER_TO_MORSE = {'A': '.-', 'B': '-...', + 'C': '-.-.', 'D': '-..', 'E': '.', + 'F': '..-.', 'G': '--.', 'H': '....', + 'I': '..', 'J': '.---', 'K': '-.-', + 'L': '.-..', 'M': '--', 'N': '-.', + 'O': '---', 'P': '.--.', 'Q': '--.-', + 'R': '.-.', 'S': '...', 'T': '-', + 'U': '..-', 'V': '...-', 'W': '.--', + 'X': '-..-', 'Y': '-.--', 'Z': '--..', + '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', + '7': '--...', '8': '---..', '9': '----.', + '0': '-----', ', ': '--..--', '.': '.-.-.-', + '?': '..--..', '/': '-..-.', '-': '-....-', + '(': '-.--.', ')': '-.--.-'} + +MORSE_TO_LETTER = { + morse: symbol for symbol, morse in LETTER_TO_MORSE +} + + +def decode(morse_message: str) -> str: + """ + Декодирует строку из азбуки Морзе в английский + """ + decoded_letters = [ + MORSE_TO_LETTER[letter] for letter in morse_message.split() + ] + + return ''.join(decoded_letters) + + +@pytest.mark.parametrize( + "source_string,result", + [ + ('SOS', '... --- ...'), + ('', ''), + ('.)', '.-.-.- -.--.-'), + pytest.param(123, '', marks=pytest.mark.xfail), + ], +) +def test_decode(source_string, result): + assert decode(source_string) == result From 504201eea2e552bc9e1c0c46e5874fed3657504e Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:28:55 +0300 Subject: [PATCH 05/23] Create result_2.txt --- testing_hw/issue-02/result_2.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 testing_hw/issue-02/result_2.txt diff --git a/testing_hw/issue-02/result_2.txt b/testing_hw/issue-02/result_2.txt new file mode 100644 index 0000000..30b3796 --- /dev/null +++ b/testing_hw/issue-02/result_2.txt @@ -0,0 +1,13 @@ +PS D:\python> python -m pytest -v morse.py +================================================================= test session starts ================================================================= +platform win32 -- Python 3.10.7, pytest-7.4.3, pluggy-1.3.0 -- D:\python\python.exe +cachedir: .pytest_cache +rootdir: D:\python +collected 4 items + +morse.py::test_encode[SOS-... --- ...] PASSED [ 25%] +morse.py::test_encode[-] PASSED [ 50%] +morse.py::test_encode[.)-.-.-.- -.--.-] PASSED [ 75%] +morse.py::test_encode[123-] XFAIL [100%] + +============================================================ 3 passed, 1 xfailed in 0.09s ============================================================= From f7d030bae161559482bbe376097d550a25712276 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:35:51 +0300 Subject: [PATCH 06/23] Create README.md --- testing_hw/issue-02/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 testing_hw/issue-02/README.md diff --git a/testing_hw/issue-02/README.md b/testing_hw/issue-02/README.md new file mode 100644 index 0000000..36a0ded --- /dev/null +++ b/testing_hw/issue-02/README.md @@ -0,0 +1,6 @@ +# issue-02 +Для начала нужно установить пакет **pyest** с помощью команды: ```pip install -U pytest``` + +Затем ```import pytest``` в рабочий файл. + +Для вывода отчета о пройденных тестах в терминал и выполнияем команду: ```python -m pytest -v morse_2.py``` From 1e82e30713c184d25c1e1f4578e9524f69b7c457 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:36:12 +0300 Subject: [PATCH 07/23] Update result_2.txt --- testing_hw/issue-02/result_2.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing_hw/issue-02/result_2.txt b/testing_hw/issue-02/result_2.txt index 30b3796..9fd1d3e 100644 --- a/testing_hw/issue-02/result_2.txt +++ b/testing_hw/issue-02/result_2.txt @@ -1,4 +1,4 @@ -PS D:\python> python -m pytest -v morse.py +PS D:\python> python -m pytest -v morse_2.py ================================================================= test session starts ================================================================= platform win32 -- Python 3.10.7, pytest-7.4.3, pluggy-1.3.0 -- D:\python\python.exe cachedir: .pytest_cache From d8bb51f7436e613abafa8e9b380e558c146f418a Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 22:41:39 +0300 Subject: [PATCH 08/23] Create onehotencoding.py --- testing_hw/issue-03/onehotencoding.py | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 testing_hw/issue-03/onehotencoding.py diff --git a/testing_hw/issue-03/onehotencoding.py b/testing_hw/issue-03/onehotencoding.py new file mode 100644 index 0000000..38d8793 --- /dev/null +++ b/testing_hw/issue-03/onehotencoding.py @@ -0,0 +1,63 @@ +""" +Encoding a value into a binary representation +based on the ordinal number of the first element encountered +""" +from typing import List, Tuple +import unittest + + +def fit_transform(*args: str) -> List[Tuple[str, List[int]]]: + """ + fit_transform(iterable) + fit_transform(arg1, arg2, *args) + """ + if len(args) == 0: + raise TypeError('expected at least 1 arguments, got 0') + + categories = args if isinstance(args[0], str) else list(args[0]) + uniq_categories = set(categories) + bin_format = f'{{0:0{len(uniq_categories)}b}}' + + seen_categories = dict() + transformed_rows = [] + + for cat in categories: + bin_view_cat = (int(b) for b in bin_format.format(1 << len(seen_categories))) + seen_categories.setdefault(cat, list(bin_view_cat)) + transformed_rows.append((cat, seen_categories[cat])) + + return transformed_rows + + +class TestOneHotEncoder(unittest.TestCase): + def test_symbols(self): + actual = ['.', ',', '.'] + expected = [('.', [0, 1]), (',', [1, 0]), ('.', [0, 1])] + self.assertEqual(fit_transform(actual), expected) + + def test_string(self): + actual = '^' + expected = [('^', [1])] + self.assertEqual(fit_transform(actual), expected) + + def test_nums(self): + actual = [3] + expected = [(3, [1])] + self.assertEqual(fit_transform(actual), expected) + + def test_num(self): + actual = 3 + self.assertRaises(TypeError, fit_transform, actual) + + def test_none(self): + actual = None + self.assertIsNone(actual) + + def test_empty(self): + actual = [] + expected = [] + self.assertTrue(fit_transform(actual) == expected) + + +if __name__ == '__main__': + unittest.main() From 4a6d00685734b6d7330e9f49e1af029a2d3e13b4 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 22:43:50 +0300 Subject: [PATCH 09/23] Create result_3.txt --- testing_hw/issue-03/result_3.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 testing_hw/issue-03/result_3.txt diff --git a/testing_hw/issue-03/result_3.txt b/testing_hw/issue-03/result_3.txt new file mode 100644 index 0000000..46dd4e3 --- /dev/null +++ b/testing_hw/issue-03/result_3.txt @@ -0,0 +1,12 @@ +PS D:\python> python -m unittest -v morse.TestOneHotEncoder +test_empty (morse.TestOneHotEncoder) ... ok +test_none (morse.TestOneHotEncoder) ... ok +test_num (morse.TestOneHotEncoder) ... ok +test_nums (morse.TestOneHotEncoder) ... ok +test_string (morse.TestOneHotEncoder) ... ok +test_symbols (morse.TestOneHotEncoder) ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 0.004s + +OK From 52897cdc32f70a564fad7d748328f6a1f6786b79 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 22:45:34 +0300 Subject: [PATCH 10/23] Update README.md --- testing_hw/issue-02/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing_hw/issue-02/README.md b/testing_hw/issue-02/README.md index 36a0ded..94665f8 100644 --- a/testing_hw/issue-02/README.md +++ b/testing_hw/issue-02/README.md @@ -1,5 +1,5 @@ # issue-02 -Для начала нужно установить пакет **pyest** с помощью команды: ```pip install -U pytest``` +Для начала нужно установить пакет **pytest** с помощью команды: ```pip install -U pytest``` Затем ```import pytest``` в рабочий файл. From d17ed2671c3b5c9432e56ad108578283b82dd89e Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 22:48:03 +0300 Subject: [PATCH 11/23] Create README.md --- testing_hw/issue-03/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 testing_hw/issue-03/README.md diff --git a/testing_hw/issue-03/README.md b/testing_hw/issue-03/README.md new file mode 100644 index 0000000..f0e9482 --- /dev/null +++ b/testing_hw/issue-03/README.md @@ -0,0 +1,5 @@ +# issue-03 + +Для начала нужно испортировать пакет **unittest** с помощью команды: ```import unittest``` + +Для вывода отчета о пройденных тестах в терминале выполнияем команду: ```python -m unittest -v morse.TestOneHotEncoder``` From 2744f22f05d5b79aa69f54dac12f8864fceb349f Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 23:09:06 +0300 Subject: [PATCH 12/23] Create result_4.txt --- testing_hw/issue-04/result_4.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 testing_hw/issue-04/result_4.txt diff --git a/testing_hw/issue-04/result_4.txt b/testing_hw/issue-04/result_4.txt new file mode 100644 index 0000000..4281273 --- /dev/null +++ b/testing_hw/issue-04/result_4.txt @@ -0,0 +1,9 @@ +PS D:\python> python -m pytest onehotencoding_pytest.py +================================================================= test session starts ================================================================= +platform win32 -- Python 3.10.7, pytest-7.4.3, pluggy-1.3.0 +rootdir: D:\python +collected 4 items + +onehotencoding_pytest.py .... [100%] + +================================================================== 4 passed in 0.09s ================================================================== From 6edc7576d8daa450682900151d8bb3fe7c51d0df Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 23:10:42 +0300 Subject: [PATCH 13/23] Create onehotencoding_pytest.py --- testing_hw/issue-04/onehotencoding_pytest.py | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 testing_hw/issue-04/onehotencoding_pytest.py diff --git a/testing_hw/issue-04/onehotencoding_pytest.py b/testing_hw/issue-04/onehotencoding_pytest.py new file mode 100644 index 0000000..b49bdf0 --- /dev/null +++ b/testing_hw/issue-04/onehotencoding_pytest.py @@ -0,0 +1,51 @@ +""" +Encoding a value into a binary representation +based on the ordinal number of the first element encountered +""" +from typing import List, Tuple +import pytest + + +def fit_transform(*args: str) -> List[Tuple[str, List[int]]]: + """ + fit_transform(iterable) + fit_transform(arg1, arg2, *args) + """ + if len(args) == 0: + raise TypeError('expected at least 1 arguments, got 0') + + categories = args if isinstance(args[0], str) else list(args[0]) + uniq_categories = set(categories) + bin_format = f'{{0:0{len(uniq_categories)}b}}' + + seen_categories = dict() + transformed_rows = [] + + for cat in categories: + bin_view_cat = (int(b) for b in bin_format.format(1 << len(seen_categories))) + seen_categories.setdefault(cat, list(bin_view_cat)) + transformed_rows.append((cat, seen_categories[cat])) + + return transformed_rows + + +def test_int_raises_type_error(): + with pytest.raises(TypeError): + fit_transform(3) + + +def test_none_raises_type_error(): + with pytest.raises(TypeError): + fit_transform(None) + + +def test_string(): + assert fit_transform('^') == [('^', [1])] + + +def test_empty_list(): + assert fit_transform([]) == [] + + +def test_empty_list(): + assert fit_transform(['.', ',', '.']) == [('.', [0, 1]), (',', [1, 0]), ('.', [0, 1])] From 0fd60452519aa282540d3769d64e7fb367c5d8f6 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Wed, 15 Nov 2023 23:12:30 +0300 Subject: [PATCH 14/23] Create README.md --- testing_hw/issue-04/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 testing_hw/issue-04/README.md diff --git a/testing_hw/issue-04/README.md b/testing_hw/issue-04/README.md new file mode 100644 index 0000000..5d61211 --- /dev/null +++ b/testing_hw/issue-04/README.md @@ -0,0 +1,7 @@ +# issue-04 + +Для начала нужно установить пакет **pytest** с помощью команды: ```pip install -U pytest``` + +Затем ```import pytest``` в рабочий файл. + +Для вывода отчета о пройденных тестах в терминале выполнияем команду: ```python -m pytest -v onehotencoding_pytest.py``` From 048aeff614960dab30c12cffae68e7fb3b6027eb Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:41:55 +0300 Subject: [PATCH 15/23] Create what_is_year_now.py --- testing_hw/issue-05/what_is_year_now.py | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 testing_hw/issue-05/what_is_year_now.py diff --git a/testing_hw/issue-05/what_is_year_now.py b/testing_hw/issue-05/what_is_year_now.py new file mode 100644 index 0000000..ec8d141 --- /dev/null +++ b/testing_hw/issue-05/what_is_year_now.py @@ -0,0 +1,34 @@ +import urllib.request +import json + +API_URL = 'http://worldclockapi.com/api/json/utc/now' + +YMD_SEP = '-' +YMD_SEP_INDEX = 4 +YMD_YEAR_SLICE = slice(None, YMD_SEP_INDEX) + +DMY_SEP = '.' +DMY_SEP_INDEX = 5 +DMY_YEAR_SLICE = slice(DMY_SEP_INDEX + 1, DMY_SEP_INDEX + 5) + + +def what_is_year_now() -> int: + """ + Получает текущее время из API-worldclock и извлекает из поля 'currentDateTime' год + Предположим, что currentDateTime может быть в двух форматах: + * YYYY-MM-DD - 2019-03-01 + * DD.MM.YYYY - 01.03.2019 + """ + with urllib.request.urlopen(API_URL) as resp: + resp_json = json.load(resp) + + datetime_str = resp_json['currentDateTime'] + if datetime_str[YMD_SEP_INDEX] == YMD_SEP: + year_str = datetime_str[YMD_YEAR_SLICE] + elif datetime_str[DMY_SEP_INDEX] == DMY_SEP: + year_str = datetime_str[DMY_YEAR_SLICE] + else: + raise ValueError('Invalid format') + + return int(year_str) + From d4ff3ffcc4602291a4afba067e972d342e00f831 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:43:55 +0300 Subject: [PATCH 16/23] Create test_what_is_year_day_now.py --- .../issue-05/test_what_is_year_day_now.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 testing_hw/issue-05/test_what_is_year_day_now.py diff --git a/testing_hw/issue-05/test_what_is_year_day_now.py b/testing_hw/issue-05/test_what_is_year_day_now.py new file mode 100644 index 0000000..68c912c --- /dev/null +++ b/testing_hw/issue-05/test_what_is_year_day_now.py @@ -0,0 +1,47 @@ +import pytest +import urllib.request +from what_is_year_now import what_is_year_now +from unittest.mock import patch +from io import StringIO + + +def test_format_yyyy_mm_dd(): + """ + Test checks date format: YYYY-MM-DD + """ + date = StringIO('{"currentDateTime": "1980-01-01"}') + expected = 1980 + with patch.object(urllib.request, 'urlopen', return_value=date): + actual = what_is_year_now() + assert expected == actual + + +def test_format_dd_mm_yyyy(): + """ + Test checks date format: DD-MM-YYYY + """ + date = StringIO('{"currentDateTime": "01.01.1980"}') + expected = 1980 + with patch.object(urllib.request, 'urlopen', return_value=date): + actual = what_is_year_now() + assert expected == actual + + +def test_invalid_data(): + """ + Test checks date is not valid + """ + date = StringIO('{"currentDateTime": "ast$@%ro098123"}') + with pytest.raises(ValueError): + with patch.object(urllib.request, 'urlopen', return_value=date): + what_is_year_now() + + +def test_no_date_at_all(): + """ + Test checks json format with no date at all + """ + date = StringIO('{"Russia": "Moscow"}') + with pytest.raises(KeyError): + with patch.object(urllib.request, 'urlopen', return_value=date): + what_is_year_now() From 9a44594b1d5385c07f1eef293015f03e6db1cfba Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:44:44 +0300 Subject: [PATCH 17/23] Create result_5.txt --- testing_hw/issue-05/result_5.txt | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 testing_hw/issue-05/result_5.txt diff --git a/testing_hw/issue-05/result_5.txt b/testing_hw/issue-05/result_5.txt new file mode 100644 index 0000000..5401778 --- /dev/null +++ b/testing_hw/issue-05/result_5.txt @@ -0,0 +1,33 @@ +PS D:\pythonProject1> pytest --cov=what_is_year_now +================================================================= test session starts ================================================================= +platform win32 -- Python 3.10.7, pytest-7.4.3, pluggy-1.3.0 +rootdir: D:\pythonProject1 +plugins: cov-4.1.0 +collected 4 items + +test_what_is_year_now.py .... [100%] + +---------- coverage: platform win32, python 3.10.7-final-0 ----------- +Name Stmts Miss Cover +----------------------------------------- +what_is_year_now.py 19 0 100% +----------------------------------------- +TOTAL 19 0 100% + + +================================================================== 4 passed in 0.34s ================================================================== +PS D:\pythonProject1> python -m pytest --cov . --cov-report html +================================================================= test session starts ================================================================= +platform win32 -- Python 3.10.7, pytest-7.4.3, pluggy-1.3.0 +rootdir: D:\pythonProject1 +plugins: cov-4.1.0 +collected 4 items + +test_what_is_year_now.py .... [100%] + +---------- coverage: platform win32, python 3.10.7-final-0 ----------- +Coverage HTML written to dir htmlcov + + +================================================================== 4 passed in 0.37s ================================================================== + From 194bc048927e8e5a5819f45e617d77e254771614 Mon Sep 17 00:00:00 2001 From: Veronika Kirenkina <112555585+veronica-kirenkina@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:45:24 +0300 Subject: [PATCH 18/23] Create index.html --- testing_hw/issue-05/index.html | 109 +++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 testing_hw/issue-05/index.html diff --git a/testing_hw/issue-05/index.html b/testing_hw/issue-05/index.html new file mode 100644 index 0000000..ba700c5 --- /dev/null +++ b/testing_hw/issue-05/index.html @@ -0,0 +1,109 @@ + + +
+ ++ coverage.py v7.3.2, + created at 2023-11-16 01:34 +0300 +
+| Module | +statements | +missing | +excluded | +coverage | +
|---|---|---|---|---|
| issue05\test_what_is_year_now.py | +27 | +0 | +0 | +100% | +
| issue05\what_is_year_now.py | +19 | +0 | +0 | +100% | +
| Total | +46 | +0 | +0 | +100% | +
+ No items found using the specified filter. +
++ « prev + ^ index + » next + + coverage.py v7.3.2, + created at 2023-11-16 01:33 +0300 +
+ +1import pytest
+2import urllib.request
+3from what_is_year_now import what_is_year_now
+4from unittest.mock import patch
+5from io import StringIO
+ + +8def test_format_yyyy_mm_dd():
+9 """
+10 Test checks date format: YYYY-MM-DD
+11 """
+12 date = StringIO('{"currentDateTime": "1980-01-01"}')
+13 expected = 1980
+14 with patch.object(urllib.request, 'urlopen', return_value=date):
+15 actual = what_is_year_now()
+16 assert expected == actual
+ + +19def test_format_dd_mm_yyyy():
+20 """
+21 Test checks date format: DD-MM-YYYY
+22 """
+23 date = StringIO('{"currentDateTime": "01.01.1980"}')
+24 expected = 1980
+25 with patch.object(urllib.request, 'urlopen', return_value=date):
+26 actual = what_is_year_now()
+27 assert expected == actual
+ + +30def test_invalid_data():
+31 """
+32 Test checks date is not valid
+33 """
+34 date = StringIO('{"currentDateTime": "ast$@%ro098123"}')
+35 with pytest.raises(ValueError):
+36 with patch.object(urllib.request, 'urlopen', return_value=date):
+37 what_is_year_now()
+ + +40def test_no_date_at_all():
+41 """
+42 Test checks json format with no date at all
+43 """
+44 date = StringIO('{"Russia": "Moscow"}')
+45 with pytest.raises(KeyError):
+46 with patch.object(urllib.request, 'urlopen', return_value=date):
+47 what_is_year_now()
+ ++ « prev + ^ index + » next + + coverage.py v7.3.2, + created at 2023-11-16 01:33 +0300 +
+ +1import urllib.request
+2import json
+ + +5API_URL = 'http://worldclockapi.com/api/json/utc/now'
+ +7YMD_SEP = '-'
+8YMD_SEP_INDEX = 4
+9YMD_YEAR_SLICE = slice(None, YMD_SEP_INDEX)
+ +11DMY_SEP = '.'
+12DMY_SEP_INDEX = 5
+13DMY_YEAR_SLICE = slice(DMY_SEP_INDEX + 1, DMY_SEP_INDEX + 5)
+ + +16def what_is_year_now() -> int:
+17 """
+18 Получает текущее время из API-worldclock и извлекает из поля 'currentDateTime' год
+19 Предположим, что currentDateTime может быть в двух форматах:
+20 * YYYY-MM-DD - 2019-03-01
+21 * DD.MM.YYYY - 01.03.2019
+22 """
+23 with urllib.request.urlopen(API_URL) as resp:
+24 resp_json = json.load(resp)
+ +26 datetime_str = resp_json['currentDateTime']
+27 if datetime_str[YMD_SEP_INDEX] == YMD_SEP:
+28 year_str = datetime_str[YMD_YEAR_SLICE]
+29 elif datetime_str[DMY_SEP_INDEX] == DMY_SEP:
+30 year_str = datetime_str[DMY_YEAR_SLICE]
+31 else:
+32 raise ValueError('Invalid format')
+ +34 return int(year_str)
+ +