-
Notifications
You must be signed in to change notification settings - Fork 13
add unit tests #1
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
Open
katyaaa86
wants to merge
24
commits into
python-testing-sandbox:main
Choose a base branch
from
katyaaa86:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5db8207
add unit tests
998facb
Update README.md
katyaaa86 20fcd90
fix travis
d95335f
Merge branch 'main' of https://github.com/katyaaa86/level_1
0f77601
fix travis
96e538a
fix requirements
cadc794
Update .travis.yml
katyaaa86 5c8be1f
fix test with requests.get
c8c41a1
Merge branch 'main' of https://github.com/katyaaa86/level_1
38dfa49
add flake8
2f7a2d6
Update requirements.txt
katyaaa86 4c357bb
add coverage to badge
katyaaa86 c033353
Update .travis.yml
katyaaa86 6c57899
Update .travis.yml
katyaaa86 22f8917
increase coverage to 100%
0bba451
add coverage badge
2d4369d
add coverage badge
katyaaa86 2da8156
add coverage badge
katyaaa86 35a264b
add coverage badge
katyaaa86 e752b19
fix tests with ast tree
7f983d7
add new cases to tests
90decb6
Add flake-master and pytest-cov
f91cdec
add no cover comment because condition is always met
ebc54d4
fix tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name": "rose", "revision": "6", "url": "https://raw.githubusercontent.com/Melevir/flake_master_presets/master/presets/rose.cfg", "filepath": null} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=. tests/ | ||
- coverage run -m --branch pytest | ||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
Pillow==8.1.2 | ||
requests==2.25.1 | ||
pytest==6.2.2 | ||
pytest-cov==2.11.1 | ||
coverage==5.5 | ||
pytest-mock==3.5.1 | ||
flake8==3.8.4 | ||
requests-mock==1.8.0 | ||
python-coveralls==2.9.3 | ||
astpretty===2.1.0 | ||
flake-master==0.0.1 | ||
pydocstyle==5.1.1 | ||
flake8-2020==1.6.0 | ||
flake8-blind-except==0.2.0 | ||
flake8-bugbear==20.11.1 | ||
flake8-builtins==1.5.3 | ||
flake8-commas==2.0.0 | ||
flake8-comprehensions==3.3.1 | ||
flake8-debugger==4.0.0 | ||
flake8-docstrings==1.5.0 | ||
flake8-eradicate==1.0.0 | ||
flake8-polyfill==1.0.2 | ||
flake8-print==4.0.0 | ||
flake8-quotes==3.2.0 | ||
flake8-string-format==0.3.0 | ||
flake8-fixme==1.1.1 | ||
flake8-annotations-complexity==0.0.6 | ||
flake8-variables-names==0.0.4 | ||
flake8-class-attributes-order==0.1.2 | ||
flake8-broken-line==0.3.0 | ||
flake8-tidy-imports==4.2.1 | ||
flake8-typing-imports==1.10.1 | ||
dlint==0.11.0 | ||
flake8-if-statements==0.1.0 | ||
flake8-functions==0.0.5 | ||
flake8-annotations-coverage==0.0.5 | ||
flake8-expression-complexity==0.0.9 | ||
flake8-printf-formatting==1.1.2 | ||
flake8-multiline-containers==0.0.17 | ||
flake8-absolute-import==1.0 | ||
flake8-simplify==0.13.0 | ||
mypy-extensions==0.4.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[flake8] | ||
max-line-length = 120 | ||
max-complexity = 8 | ||
max-annotations-complexity = 4 | ||
ignore = W503, P103, D | ||
exclude = node_modules,env,venv,venv36,tests/test_files/ | ||
var_names_exclude_pathes = node_modules,env,venv,venv36 | ||
assert_allowed_in_pathes = tests,migrations,env,venv,venv36 | ||
adjustable-default-max-complexity = 8 | ||
per-file-ignores = | ||
__init__.py: F401 | ||
tests/*: TAE001 | ||
ban-relative-imports = True | ||
min-coverage-percents = 100 | ||
|
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
from PIL import Image | ||
|
||
|
||
@pytest.fixture() | ||
def fixture_create_image(): | ||
image = Image.new('RGBA', size=(10, 10)) | ||
return image | ||
|
||
|
||
class MockObject: | ||
def __init__(self): | ||
self.size = 'SML' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import ast | ||
import datetime | ||
import pytest | ||
from contextlib import nullcontext as does_not_raise | ||
|
||
from code import ( | ||
chunks, flat, is_python_class_name, max_with_default, is_path_in_exclude_list, split_camel_case_words, | ||
is_camel_case_word, if_logs_has_any_of_commands, has_recursive_calls, extract_all_constants_from_ast, | ||
parse_iso_datetime, get_image_height_in_pixels, get_full_class_name) | ||
from tests.conftest import MockObject | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'some_list, chunk_size, expected', | ||
[ | ||
([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4], [5]]), | ||
(['a', 'b', 'c', 'd'], 3, [['a', 'b', 'c'], ['d']]), | ||
([], 1, []), | ||
], | ||
) | ||
def test_chunks(some_list, chunk_size, expected): | ||
generator = chunks(some_list, chunk_size) | ||
assert list(generator) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'some_list, expected', | ||
[ | ||
([[1, 2], [3, 4], [5]], [1, 2, 3, 4, 5]), | ||
([['a', 'b', 'c'], ['d']], ['a', 'b', 'c', 'd']), | ||
([[1, 2, {}], ['a', 'b', {}]], [1, 2, {}, 'a', 'b', {}]), | ||
([], []), | ||
([[], []], []), | ||
], | ||
katyaaa86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
def test_flat(some_list, expected): | ||
assert flat(some_list) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'name, expected, expectation', | ||
[ | ||
('Classname', True, does_not_raise()), | ||
('ClassName', False, does_not_raise()), | ||
('classname', False, does_not_raise()), | ||
('className', False, does_not_raise()), | ||
('', None, pytest.raises(IndexError)), | ||
], | ||
) | ||
def test_is_python_class_name(name, expected, expectation): | ||
with expectation: | ||
assert is_python_class_name(name) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'items, default, expected', | ||
[ | ||
([[1, 2], [3, 4], [5]], None, [5]), | ||
katyaaa86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
([[1, 2], [3, 4], [5]], 1, [5]), | ||
([3, 4, 5], 1, 5), | ||
('', 1, 1), | ||
('', None, 0), | ||
], | ||
) | ||
katyaaa86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_max_with_default(items, default, expected): | ||
assert max_with_default(items, default) == expected | ||
|
||
|
||
def test_max_with_default_with_exception(): | ||
with pytest.raises(TypeError): | ||
assert max_with_default([[1, 2], '3, 4', (5, 'b')], 1) is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'path, exclude, expected', | ||
[ | ||
('abc def', ['a', 'b', 'c', 'z'], True), | ||
('abc def', ['z'], False), | ||
('', [], False), | ||
], | ||
) | ||
def test_is_path_in_exclude_list(path, exclude, expected): | ||
assert is_path_in_exclude_list(path, exclude) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'camel_cased_word, expected, expectation', | ||
[ | ||
('camelCasedWord', ['camel', 'cased', 'word'], does_not_raise()), | ||
('CamelCasedWord', ['camel', 'cased', 'word'], does_not_raise()), | ||
('CamelCasedWorD', ['camel', 'cased', 'wor', 'd'], does_not_raise()), | ||
('', None, pytest.raises(IndexError)), | ||
('camelcaseword', None, pytest.raises(IndexError)), | ||
], | ||
katyaaa86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
def test_split_camel_case_words(camel_cased_word, expected, expectation): | ||
with expectation: | ||
assert split_camel_case_words(camel_cased_word) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'uppercase_letters_amount, expected', | ||
[ | ||
('camelCasedWord', True), | ||
('camelCased', True), | ||
('camelcasedword', False), | ||
('', False), | ||
], | ||
) | ||
def test_is_camel_case_word(uppercase_letters_amount, expected): | ||
assert is_camel_case_word(uppercase_letters_amount) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'log, commands, expected', | ||
[ | ||
(['request to api', 'response from api'], ['request', 'exit'], True), | ||
(['first request to api', 'first response from api'], ['request', 'exit'], True), | ||
(['request', 'response'], ['request', 'exit'], True), | ||
(['request', 'response'], ['exit'], False), | ||
([], ['exit'], False), | ||
], | ||
) | ||
def test_if_logs_has_any_of_commands(log, commands, expected): | ||
assert if_logs_has_any_of_commands(log, commands) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'funcdef, expected', | ||
[ | ||
('def func():\n return None', False), | ||
('def func():\n return func()', True), | ||
], | ||
) | ||
def test_has_recursive_calls(funcdef, expected): | ||
assert has_recursive_calls(ast.parse(funcdef).body[0]) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'ast_tree, expected', | ||
[ | ||
('a = "string"', ['string']), | ||
('def func():\n return 5', []), | ||
], | ||
) | ||
def test_extract_all_constants_from_ast(ast_tree, expected): | ||
assert extract_all_constants_from_ast(ast.parse(ast_tree).body[0]) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'iso_datetime, expected', | ||
[ | ||
('2019-12-04Z', datetime.datetime(2019, 12, 4, 0, 0)), | ||
('2019-12-04', datetime.datetime(2019, 12, 4, 0, 0)), | ||
('2019-12-04W', None), | ||
], | ||
) | ||
def test_parse_iso_datetime(iso_datetime, expected): | ||
assert parse_iso_datetime(iso_datetime) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'url, expected', | ||
[ | ||
('url', None), | ||
('http://test.com', 10), | ||
], | ||
) | ||
def test_get_image_height_in_pixels(requests_mock, mocker, url, expected, fixture_create_image): | ||
requests_mock.get(url, content=b'abcdef') | ||
mocker.patch( | ||
'PIL.Image.open', | ||
return_value=fixture_create_image, | ||
) | ||
assert get_image_height_in_pixels(url) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'obj, expected', | ||
[ | ||
(MockObject(), 'tests.conftest.MockObject'), | ||
([], 'list'), | ||
], | ||
) | ||
def test_get_full_class_name(obj, expected): | ||
assert get_full_class_name(obj) == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.