def test_wrong_format(): date = '{"currentDateTime": "2001.03.01"}' date_io = io.StringIO(date) with patch('urllib.request.urlopen') as mock: mock.return_value.__enter__.return_value = date_io with pytest.raises(ValueError): what_is_year_now.what_is_year_now()
def test_second_format(urlopen): mock = MagicMock() mock.read.return_value = None mock.__enter__.return_value = mock urlopen.return_value = mock with pytest.raises(TypeError): what_is_year_now.what_is_year_now()
def test_incomplete_data(self): """Тест, в котором не сумеем обратится к datetime_str[DMY_SEP_INDEX]""" dict_for_json = '{"currentDateTime": "2020"}' with patch.object(urllib.request, "urlopen", return_value=io.StringIO(dict_for_json)): with self.assertRaises(IndexError): what_is_year_now()
def test_wrong_sep(self): """Проверка на некорректный разделитель.""" dict_json = '{"currentDateTime": "01:12:2020"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): with self.assertRaises(ValueError): what_is_year_now()
def test_wrong_format(self): """Проверка на некорректный формат даты.""" dict_json = '{"currentDateTime": "1.12.2020"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): with self.assertRaises(ValueError): what_is_year_now()
def test_wrong_format(self): dict_json = '{"currentDateTime": "21.12.2020"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): with self.assertRaises(ValueError): what_is_year_now()
def testing_complete data(self): dict_for_json = '{"currentDateTime": "2020"}' with patch.object( urllib.request, "urlopen",return_value=io.StringIO(dict_for_json) ): with self.assertRaises(IndexError): what_is_year_now()
def test3(self): """Тест, проверяющий поведение функции, при дате, заданной в формате 'DD:MM:YYYY'.""" dict_for_json = '{"currentDateTime": "01:03:2020"}' with patch.object(urllib.request, "urlopen", return_value=io.StringIO(dict_for_json)): with self.assertRaises(ValueError): what_is_year_now()
def test_exception_format(): json_return_value = { "$id": "1", "currentDateTime": "2024/04/09T19:29Z", "utcOffset": "00:00:00", "isDayLightSavingsTime": "false", "dayOfTheWeek": "Friday", "timeZoneName": "UTC", "currentFileTime": 132624701535543575, "ordinalDate": "2021-99", "serviceResponse": "null" } with patch('json.load', return_value=json_return_value), pytest.raises(Exception): what_is_year_now.what_is_year_now()
def test_first_format_date(): cur_date = {"currentDateTime": "2020-12-21"} with patch("urllib.request.urlopen"), patch("json.load", return_value=cur_date): year = what_is_year_now() expected_year = 2020 assert year == expected_year
def test_exception_format(urlopen): mock = MagicMock() mock.read.return_value = '{"$id":"1",' \ '"currentDateTime":"2021T01:12Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Monday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132608491427816346,' \ '"ordinalDate":"2021-81",' \ '"serviceResponse":null}' mock.__enter__.return_value = mock urlopen.return_value = mock with pytest.raises(ValueError): what_is_year_now.what_is_year_now()
def test_current_year_getting( mock_api, json_filename, expected, datetime_format_files, request ): json_file = datetime_format_files[json_filename] context_manager_mock = MagicMock() context_manager_mock.__enter__ = MagicMock(return_value=json_file) context_manager_mock.__exit__ = MagicMock(return_value=None) mock_api.return_value = context_manager_mock param_id = re.search(r"\[(?:[a-zA-z ])+\]", string=request.node.name).group(0)[1:-1] if param_id == "invalid": with pytest.raises(ValueError): what_is_year_now() else: assert expected == what_is_year_now()
def testing_point(self): dict_for_json = '{"currentDateTime": "21.12.2020"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_for_json) ): actual = what_is_year_now() expected = 2020 self.assertEqual(actual, expected)
def test_format_v1(): json_return_value = { "$id": "1", "currentDateTime": "2022-04-09T19:29Z", "utcOffset": "00:00:00", "serviceResponse": "null" } with patch('json.load', return_value=json_return_value): year = what_is_year_now.what_is_year_now() assert year == 2022
def test_point(self): """Проверка на дату в формате 'DD.MM.YYYY'.""" dict_json = '{"currentDateTime": "01.12.2020"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): actual = what_is_year_now() expected = 2020 self.assertEqual(actual, expected)
def test_dash(self): """Проверка на дату в формате 'YYYY-MM-DD'.""" dict_json = '{"currentDateTime": "2020-12-01"}' with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): actual = what_is_year_now() expected = 2020 self.assertEqual(actual, expected)
def test_separator_point(self): """Тест, проверяющий поведение функции, при дате, заданной в формате 'DD.MM.YYYY'.""" dict_for_json = '{"currentDateTime": "01.01.2020"}' with patch.object(urllib.request, "urlopen", return_value=io.StringIO(dict_for_json)): actual = what_is_year_now() expected = 2020 self.assertEqual(actual, expected)
def test_no_current_date(urlopen_mock): test_mock = MagicMock() test_mock.getcode.return_value = 200 test_mock.read.return_value = \ '{' \ '"$id":"1",' \ '"currentDT":"2021-04-19T09:50Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Monday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132632994190993994,' \ '"ordinalDate":"2021-109",' \ '"serviceResponse":null' \ '}' test_mock.__enter__.return_value = test_mock urlopen_mock.return_value = test_mock with pytest.raises(KeyError): what_is_year_now.what_is_year_now()
def test_fail_invalid_format(mock_urlopen): cm = MagicMock() cm.getcode.return_value = 200 cm.read.return_value = \ '{' \ '"$id":"1",' \ '"currentDateTime":"21.2021T08:36Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Sunday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132607893986339880,' \ '"ordinalDate":"2021-80",' \ '"serviceResponse":null' \ '}' cm.__enter__.return_value = cm mock_urlopen.return_value = cm with pytest.raises(ValueError): what_is_year_now.what_is_year_now()
def test_invalid_format_raise_value_error(urlopen_mock): api_mock = MagicMock() api_mock.getcode.return_value = 200 api_mock.read.return_value = \ '{' \ '"$id":"1",' \ '"currentDateTime":"22.2021--T08:36Z",' \ '"utcOffset":"01:00:00",' \ '"isDayLightSavingsTime":true,' \ '"dayOfTheWeek":"Monday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132607893986339880,' \ '"ordinalDate":"2021-80",' \ '"serviceResponse":null' \ '}' api_mock.__enter__.return_value = api_mock urlopen_mock.return_value = api_mock with pytest.raises(ValueError): what_is_year_now.what_is_year_now()
def test_big_json(self): """Проверка на дату в формате 'DD.MM.YYYY'.""" dict_json = ( '{"$id":"1","currentDateTime":"2021-12-01T11:03Z","utcOffset":"00:00:00"}' ) with patch.object( urllib.request, "urlopen", return_value=io.StringIO(dict_json) ): actual = what_is_year_now() expected = 2021 self.assertEqual(actual, expected)
def test_separator_dash(self): """Тест, проверяющий поведение функции, при дате, заданной в формате 'YYYY-MM-DD'.""" dict_for_json = '{"currentDateTime": "2020-01-01"}' # Вызываем urlopen из urllib.request, заменив возвращаемое значение # на 'файл' c dict_for_json, к которому в функции применяется json.load with patch.object(urllib.request, "urlopen", return_value=io.StringIO(dict_for_json)): actual = what_is_year_now() expected = 2020 self.assertEqual(actual, expected)
def test_second_format(urlopen): mock = MagicMock() mock.read.return_value = '{"$id":"1",' \ '"currentDateTime":"03.04.2021T04:18Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Saturday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132608491427816346,' \ '"ordinalDate":"2021-81",' \ '"serviceResponse":null}' mock.__enter__.return_value = mock urlopen.return_value = mock year = what_is_year_now.what_is_year_now() assert year == 2021
def test_format_v2(): json_return_value = { "$id": "1", "currentDateTime": "09.04.2023T19:29Z", "utcOffset": "00:00:00", "isDayLightSavingsTime": "false", "dayOfTheWeek": "Friday", "timeZoneName": "UTC", "currentFileTime": 132624701535543575, "ordinalDate": "2023-99", "serviceResponse": "null" } with patch('json.load', return_value=json_return_value): year = what_is_year_now.what_is_year_now() assert year == 2023
def test_first_format(urlopen): mock = MagicMock() mock.read.return_value = '{"$id":"1",' \ '"currentDateTime":"2021-04-03T04:18Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Saturday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132608491427816346,' \ '"ordinalDate":"2021-81",' \ '"serviceResponse":null}' mock.__enter__.return_value = mock # __enter__ - контекестный менеджер (вместо псевдонима resp) mock вместо resp urlopen.return_value = mock year = what_is_year_now.what_is_year_now() assert year == 2021
def test_execute_success(urlopen_mock): api_mock = MagicMock() api_mock.getcode.return_value = 200 api_mock.read.return_value = \ '{' \ '"$id":"1",' \ '"currentDateTime":"2021-03-22T08:36Z",' \ '"utcOffset":"01:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Monday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132607893986339880,' \ '"ordinalDate":"2021-80",' \ '"serviceResponse":null' \ '}' api_mock.__enter__.return_value = api_mock urlopen_mock.return_value = api_mock year = what_is_year_now.what_is_year_now() assert year == 2021
def test_what_is_year_now_alternative_format(urlopen_mock): test_mock = MagicMock() test_mock.getcode.return_value = 200 test_mock.read.return_value = \ '{' \ '"$id":"1",' \ '"currentDateTime":"19.04.2021T09:50Z",' \ '"utcOffset":"00:00:00",' \ '"isDayLightSavingsTime":false,' \ '"dayOfTheWeek":"Monday",' \ '"timeZoneName":"UTC",' \ '"currentFileTime":132632994190993994,' \ '"ordinalDate":"2021-109",' \ '"serviceResponse":null' \ '}' test_mock.__enter__.return_value = test_mock urlopen_mock.return_value = test_mock year = what_is_year_now.what_is_year_now() assert year == 2021
def test_format_2(self, arg): self.assertEqual(what_is_year_now(), 2019)
def test_format_wrong(self, arg): with self.assertRaises(ValueError): what_is_year_now()
def test_wrong_key(): cur_date = {"DateTimeNow": "21/12/2020"} with patch("urllib.request.urlopen"), patch("json.load", return_value=cur_date): with pytest.raises(KeyError): what_is_year_now()