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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ wheels/
*.egg-info/
.installed.cfg
*.egg
.idea/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
python:
- "3.9"
install:
- pip install -r requirements.txt
script:
- flake8 .
- pytest --cov=code tests/
- coverage run -m --branch pytest
after_success:
- bash <(curl -s https://codecov.io/bash)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Build Status](https://travis-ci.com/fattybobcat/level_1.svg?branch=main)](https://travis-ci.com/fattybobcat/level_1)
[![codecov](https://codecov.io/gh/fattybobcat/level_1/branch/main/graph/badge.svg?token=D44NANH2J6)](https://codecov.io/gh/fattybobcat/level_1)

# Testing sandbox level 1

Welcome to the testing sandbox: practice-driven testing instrument.
Expand Down
5 changes: 2 additions & 3 deletions code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import io
import re
from typing import Iterable, Any, Optional, Union, Generator
from typing import Iterable, Any, Optional, Generator

from PIL import Image
from requests import get
Expand Down Expand Up @@ -78,7 +78,7 @@ def split_camel_case_words(camel_cased_word: str) -> list[str]:
words_start_indexes = [m.start(0) for m in re.finditer(r'[A-Z]', camel_cased_word)]
if words_start_indexes[0] > 0:
words_start_indexes.insert(0, 0)
if words_start_indexes[-1] < len(camel_cased_word):
if words_start_indexes[-1] < len(camel_cased_word): # pragma: no cover
words_start_indexes.append(len(camel_cased_word))
words = []
for word_start_index, word_end_index in zip(words_start_indexes, words_start_indexes[1:]):
Expand Down Expand Up @@ -107,4 +107,3 @@ def max_with_default(items: Iterable, default: Optional = None):

def is_python_class_name(name: str) -> bool:
return name[0] == name[0].upper() and name[1:] == name[1:].lower()

5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
Pillow==8.1.2
requests==2.25.1
flake8==3.9.2
pytest==6.2.4
pytest-cov==2.12.0
pytest-mock==3.6.1
requests-mock==1.9.2
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
Empty file added tests/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from PIL import Image


SIZE_IMAGE = 100


class MyTestClass:
pass


@pytest.fixture()
def create_image():
image = Image.new('RGBA', size=(SIZE_IMAGE, SIZE_IMAGE))
return image
180 changes: 180 additions & 0 deletions tests/test_code_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import pytest
import ast
import datetime

from code import (flat, is_python_class_name, parse_iso_datetime, chunks,
is_camel_case_word, if_logs_has_any_of_commands, is_path_in_exclude_list,
get_full_class_name, max_with_default, split_camel_case_words, extract_all_constants_from_ast,
has_recursive_calls, get_image_height_in_pixels)
from .conftest import MyTestClass


@pytest.mark.parametrize(
'some_list, expected',
[
([['People'], ['Name', 'Job']], ['People', 'Name', 'Job']),
([[], [1, 'a']], [1, 'a']),
([[], {}], []),
([{}, [1, 2, {'a': 4}]], [1, 2, {'a': 4}])
],
)
def test_flat(some_list, expected):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Еще можно протестировать на передачу пустого списка и словарей в списке

assert flat(some_list) == expected


@pytest.mark.parametrize(
'some_name, expected',
[
('User', True),
('name', False),
('userName', False)
],
)
def test_is_python_class_name(some_name, expected):
assert is_python_class_name(some_name) == expected


@pytest.mark.parametrize(
'iso_datetime, expected',
[
('2008-09-03T20:56:35.450686Z', datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)),
('name', None),
('2018-11-11', datetime.datetime(2018, 11, 11, 0, 0)),
('2010-09-03T20:56:35.45', None),
],
)
def test_parse_iso_datetime(iso_datetime, expected):
assert parse_iso_datetime(iso_datetime) == expected


@pytest.mark.parametrize(
'some_list, chunk_size, expected',
[
([1, 2, 3, 4, 5, 6, 7, 8, 9], 4, [[1, 2, 3, 4], [5, 6, 7, 8], [9]]),
([1, 2, 3, 4, 5], 3, [[1, 2, 3], [4, 5]]),
([], 3, []),
]

)
def test_chunks(some_list, chunk_size, expected):
assert list(chunks(some_list, chunk_size)) == expected


@pytest.mark.parametrize(
'log, some_commands, expected',
[
(['update photo', 'delete'], ['update', 'read', 'exit'], True),
(['user post new recipes', 'user read url'], ['update', 'read', 'exit'], True),
(['delete', 'send'], ['update'], False),
([], ['read'], False),
(['start'], ['start'], True)
],
)
def test_if_logs_has_any_of_commands(log, some_commands, expected):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно проверить 3-е условие - base_command == required_command

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это чтобы полностью повторялась base_command в required_command?

assert if_logs_has_any_of_commands(log, some_commands) == expected


@pytest.mark.parametrize(
'some_word, expected',
[
('CamelCaps', True),
('camelcaps', False),
('CamelHumpedWord', True),
('Ca', False),
('cA', True),
('', False),
],
)
def test_is_camel_case_word(some_word, expected):
assert is_camel_case_word(some_word) == expected


@pytest.mark.parametrize(
'some_path, exclude, expected',
[
('some_path', ['some', 'path'], True),
('some path', ['path'], True),
('some path', [''], True),
('some path', [], False),
('', [], False),
('', [''], True),
]
)
def test_is_path_in_exclude_list(some_path, exclude, expected):
assert is_path_in_exclude_list(some_path, exclude) == expected


@pytest.mark.parametrize(
'some_obj, expected',
[
(4.3, 'float'),
('text', 'str'),
([2], 'list'),
(MyTestClass(), 'tests.conftest.MyTestClass'),
]
)
def test_get_full_class_name(some_obj, expected):
assert get_full_class_name(some_obj) == expected


@pytest.mark.parametrize(
'some_items, default, expected',
[
([1, -2, 3], 1, 3),
('', None, 0),
([], 15, 15),
('AaBbCcXxYyZz', None, 'z'),
]
)
def test_max_with_default(some_items, default, expected):
assert max_with_default(some_items, default) == expected


@pytest.mark.parametrize(
'camel_cased_word, expected',
[
('SomeCamelCasedWord', ['some', 'camel', 'cased', 'word']),
(' CamelCasedWord', [' ', 'camel', 'cased', 'word']),
('CamelCasedsoMeWorD', ['camel', 'casedso', 'me', 'wor', 'd']),
('aCa', ['a', 'ca']),
(" Camel Case WordCase case wordCase", [' ', 'camel ', 'case ', 'word', 'case case word', 'case'])
]
)
def test_split_camel_case_words(camel_cased_word, expected):
assert split_camel_case_words(camel_cased_word) == expected


@pytest.mark.parametrize(
'ast_tree, expected',
[
("fruits = ['mango', 'grapes']", {'mango', 'grapes'}),
("a = 'Bob'", {'Bob'}),
]

)
def test_extract_all_constants_from_ast(ast_tree, expected):
assert {*extract_all_constants_from_ast(ast.parse(ast_tree))} == expected


@pytest.mark.parametrize(
'some_funcdef, expected',
[
('def func(n):\n\treturn func(n/2)', True),
('def func(n):\n\treturn n/2', False),
]
)
def test_has_recursive_calls(some_funcdef, expected):
assert has_recursive_calls(ast.parse(some_funcdef).body[0]) == expected


@pytest.mark.parametrize(
'some_url, expected',
[
('url', None),
('http://climbhub.com/', 100),
],
)
def test_get_image_height_in_pixels(some_url, expected, create_image, requests_mock, mocker):
requests_mock.get(some_url, content=b"request")
mocker.patch('PIL.Image.open', return_value=create_image)
assert get_image_height_in_pixels(some_url) == expected