Skip to content

Commit 8a612af

Browse files
authored
[IMPROVEMENT] Add new docstrings (#527)
1 parent 603e251 commit 8a612af

25 files changed

+351
-635
lines changed

.pydocstylerc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[pydocstyle]
22
convention = numpy
3-
match_dir = mod_.*|.
3+
add-ignore=D100
4+
match_dir = ^(?!(venv|.venv|migrations|static|logs|install)).*
45

mod_test/nicediff/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Maintains logic for diffs."""

mod_test/nicediff/diff.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99

1010

1111
def zip_(ls: List[str]) -> str:
12+
"""Add an alias for strings joining."""
1213
return ''.join(ls)
1314

1415

15-
# compress words and digits to one list
1616
def compress(s: str) -> List[str]:
17+
"""Compress words and digits to one list."""
1718
return re.split(r'(\W)', s)
1819

1920

20-
# equality factor
2121
def eq(a: List[str], b: List[str], same_regions: Optional[List[List[int]]] = None,
2222
delta_a: int = 0, delta_b: int = 0) -> Union[List[int], List[Union[int, List[str]]]]:
23+
"""Implement equality factor."""
2324
if index.get(zip_(a), dict()).get(zip_(b), None) is None:
2425
e = 0
2526
rez = [] # type: Union[int, Any, List[str]]
@@ -74,8 +75,8 @@ def eq(a: List[str], b: List[str], same_regions: Optional[List[List[int]]] = Non
7475
return index[zip_(a)][zip_(b)]
7576

7677

77-
# processing one line
7878
def _process(test_result: str, correct: str, suffix_id: str) -> Tuple[str, str]:
79+
"""Process one line."""
7980
test_result = html.escape(test_result)
8081
correct = html.escape(correct)
8182
tr_compr = compress(test_result)
@@ -104,9 +105,7 @@ def _process(test_result: str, correct: str, suffix_id: str) -> Tuple[str, str]:
104105

105106

106107
def create_diff_entries(suffix_id: str, id_name: str, events: List[List[object]], compressed_data: List[str]) -> str:
107-
"""
108-
Create the diff entries for the correct or wrong side.
109-
"""
108+
"""Create the diff entries for the correct or wrong side."""
110109
result = ''
111110
idx = 0
112111
for event in events:
@@ -122,9 +121,8 @@ def create_diff_entries(suffix_id: str, id_name: str, events: List[List[object]]
122121
return result
123122

124123

125-
# return generated difference in HTML formatted table
126124
def get_html_diff(test_correct_lines: List[str], test_res_lines: List[str], to_view: bool = True) -> str:
127-
125+
"""Return generated difference in HTML formatted table."""
128126
# variable to keep count of diff lines noted
129127
number_of_noted_diff_lines = 0
130128

tests/TestConfigParser.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Contains tests related to the config handling."""
2+
13
import json
24
from unittest import mock
35

@@ -6,7 +8,10 @@
68

79

810
class TestConfigParser(BaseTestCase):
11+
"""Test config parsing."""
12+
913
def test_parse_config(self):
14+
"""Test parse_config function."""
1015
file_config = "TEST = 'run'"
1116
expected_config = {'TEST': 'run'}
1217

tests/TestLogConfiguration.py

+6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010

1111
class TestLogConfiguration(unittest.TestCase):
12+
"""Test log setup."""
13+
1214
def _test_init_with_log_value(self, debug, result_level):
15+
"""Test logger initialization with specific debug and level."""
1316
joined_path = 'baz'
1417
folder = 'foo'
1518
filename = 'bar'
@@ -48,12 +51,15 @@ def _test_init_with_log_value(self, debug, result_level):
4851
return log_config
4952

5053
def test_init_correctly_initializes_the_instance_when_debug(self):
54+
"""Test log initialization with debug mode and level."""
5155
self._test_init_with_log_value(True, logging.DEBUG)
5256

5357
def test_init_correctly_initializes_the_instance_when_no_debug(self):
58+
"""Test log initialization with info level."""
5459
self._test_init_with_log_value(False, logging.INFO)
5560

5661
def test_create_logger(self):
62+
"""Test logger creation."""
5763
with mock.patch.object(LogConfiguration, '__init__',
5864
return_value=None):
5965
with mock.patch('logging.getLogger') as mock_get:

tests/TestMailer.py

+4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66

77
class TestMailer(unittest.TestCase):
8+
"""Test Mailer features."""
9+
810
def test_that_init_works_correctly(self):
11+
"""Test Mailer initialization."""
912
domain = 'domain'
1013
api_key = 'APIKEY'
1114
sender_name = 'foo'
@@ -20,6 +23,7 @@ def test_that_init_works_correctly(self):
2023
self.assertEqual(actual.sender, sender)
2124

2225
def test_that_send_simple_message_creates_the_appropriate_request(self):
26+
"""Test simple message sending."""
2327
domain = 'domain'
2428
api_key = 'APIKEY'
2529
sender_name = 'foo'

tests/TestRun.py

+5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66

77

88
class mock_application:
9+
"""Mock object for application."""
910

1011
def __init__(self):
1112
self.config = {}
1213
self.root_path = ''
1314

1415

1516
class TestRun(BaseTestCase):
17+
"""Test application running."""
1618

1719
def test_load_secret_keys_files_present(self):
20+
"""Test csrf session and secret keys loading when they are presented."""
1821
secrets = tempfile.NamedTemporaryFile()
1922
csrf = tempfile.NamedTemporaryFile()
2023
application = mock_application()
@@ -35,6 +38,7 @@ def test_load_secret_keys_files_present(self):
3538
self.assertEqual(application.config['CSRF_SESSION_KEY'], b'csrf', 'csrf session key not loaded properly')
3639

3740
def test_load_secret_keys_secrets_not_present(self):
41+
"""Test csrf session and secret keys loading when csrf session key is not presented."""
3842
secrets = tempfile.NamedTemporaryFile()
3943
csrf = "notAvailable"
4044
application = mock_application()
@@ -53,6 +57,7 @@ def test_load_secret_keys_secrets_not_present(self):
5357
self.assertEquals(cmd.exception.code, 1, 'function exited with a wrong code')
5458

5559
def test_load_secret_keys_csrf_not_present(self):
60+
"""Test csrf session and secret keys loading when secret key is not presented."""
5661
secrets = "notAvailable"
5762
csrf = tempfile.NamedTemporaryFile()
5863
application = mock_application()

tests/TestUtility.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
"""Contains tests related to the utility helpers."""
2+
13
from unittest import mock
24

35
from tests.base import BaseTestCase
46

57

68
class TestUtility(BaseTestCase):
9+
"""Test utility helpers."""
710

811
@mock.patch('utility.path')
912
def test_serve_file_download(self, mock_path):
10-
"""
11-
Test function serve_file_download.
12-
"""
13+
"""Test function serve_file_download."""
1314
from utility import serve_file_download
1415

1516
response = serve_file_download('to_download', 'folder', 'accl_folder')

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Contains all project tests."""

tests/base.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Contains base test case with needed setup and helpers."""
2+
13
import os
24
from collections import namedtuple
35
from contextlib import contextmanager
@@ -22,9 +24,7 @@
2224

2325
@contextmanager
2426
def provide_file_at_root(file_name, to_write=None):
25-
"""
26-
Provide file with name file_name at application root.
27-
"""
27+
"""Provide file with name file_name at application root."""
2828
if to_write is None:
2929
to_write = "DATABASE_URI = 'sqlite:///:memory:'"
3030

@@ -48,9 +48,7 @@ def load_file_lines(filepath):
4848

4949

5050
def mock_decorator(f):
51-
"""
52-
Mock login_required decorator.
53-
"""
51+
"""Mock login_required decorator."""
5452
@wraps(f)
5553
def decorated_function(*args, **kwargs):
5654
return f(*args, **kwargs)
@@ -59,6 +57,7 @@ def decorated_function(*args, **kwargs):
5957

6058

6159
def generate_keys():
60+
"""Generate CSRF session and secret keys."""
6261
from utility import ROOT_DIR
6362
secret_csrf_path = f"{os.path.join(ROOT_DIR, '')}secret_csrf"
6463
secret_key_path = f"{os.path.join(ROOT_DIR, '')}secret_key"
@@ -73,6 +72,7 @@ def generate_keys():
7372

7473

7574
def load_config(file):
75+
"""Load start config."""
7676
key_paths = generate_keys()
7777
with open(key_paths['secret_key_path'], 'rb') as secret_key_file:
7878
secret_key = secret_key_file.read()
@@ -102,6 +102,7 @@ def load_config(file):
102102

103103

104104
def mock_api_request_github(url, data=None, timeout=None):
105+
"""Mock all responses to the Github API."""
105106
if url == "https://api.github.com/repos/test/test_repo/commits/abcdef":
106107
return MockResponse({}, 200)
107108
elif url == "https://api.github.com/user":
@@ -139,7 +140,7 @@ def mock_api_request_github(url, data=None, timeout=None):
139140

140141
def generate_signature(data, private_key):
141142
"""
142-
Generate signature token of hook request
143+
Generate signature token of hook request.
143144
144145
:param data: Signature's data
145146
:param private_key: Signature's token
@@ -171,19 +172,19 @@ def generate_git_api_header(event, sig):
171172

172173

173174
class BaseTestCase(TestCase):
175+
"""Base setup for all test cases."""
176+
174177
@mock.patch('config_parser.parse_config', side_effect=load_config)
175178
def create_app(self, mock_config):
176-
"""
177-
Create an instance of the app with the testing configuration
178-
:return:
179-
"""
179+
"""Create an instance of the app with the testing configuration."""
180180
user = namedtuple('user', "name password email github_token")
181181
self.user = user(name="test", password="test123",
182182
email="[email protected]", github_token="abcdefgh")
183183
from run import app
184184
return app
185185

186186
def setUp(self):
187+
"""Set up all entities."""
187188
self.app.preprocess_request()
188189
g.db = create_session(
189190
self.app.config['DATABASE_URI'], drop_tables=True)
@@ -304,13 +305,15 @@ def setUp(self):
304305
@staticmethod
305306
def create_login_form_data(email, password) -> dict:
306307
"""
307-
Creates the form data for a login event.
308+
Create the form data for a login event.
309+
308310
:return: A dictionary containing the name, password and submit fields.
309311
"""
310312
return {'email': email, 'password': password, 'submit': True}
311313

312314
@staticmethod
313315
def create_customize_form(commit_hash, platform, commit_select=None, regression_test=None):
316+
"""Create the request form part."""
314317
if regression_test is None:
315318
regression_test = [1, 2]
316319
if commit_select is None:
@@ -324,9 +327,7 @@ def create_customize_form(commit_hash, platform, commit_select=None, regression_
324327
}
325328

326329
def create_forktest(self, commit_hash, platform, regression_tests=None):
327-
"""
328-
Create a test on fork based on commit and platform
329-
"""
330+
"""Create a test on fork based on commit and platform."""
330331
from flask import g
331332
fork_url = f"https://github.com/{self.user.name}/{g.github['repository']}.git"
332333
fork = Fork(fork_url)
@@ -346,9 +347,7 @@ def create_forktest(self, commit_hash, platform, regression_tests=None):
346347
g.db.commit()
347348

348349
def create_user_with_role(self, user, email, password, role, github_token=None):
349-
"""
350-
Create a user with specified user details and role.
351-
"""
350+
"""Create a user with specified user details and role."""
352351
from flask import g
353352
user = User(self.user.name, email=self.user.email,
354353
password=User.generate_hash(self.user.password), role=role, github_token=github_token)
@@ -357,16 +356,20 @@ def create_user_with_role(self, user, email, password, role, github_token=None):
357356

358357
@staticmethod
359358
def create_random_string(length=32):
359+
"""Generate random string of ASCII symbols."""
360360
import random
361361
import string
362362
random_string = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(length)])
363363
return random_string
364364

365365

366366
class MockResponse:
367+
"""A class to mock HTTP response."""
368+
367369
def __init__(self, json_data, status_code):
368370
self.json_data = json_data
369371
self.status_code = status_code
370372

371373
def json(self):
374+
"""Mock response json method."""
372375
return self.json_data

0 commit comments

Comments
 (0)