Skip to content

Commit 87981cf

Browse files
committed
Add some basic unit tests for the file_helper module
Test the 'happy' path of the file_helper.py module parse_word_list_argument and get_combined_word_lists functions.
1 parent dea75f1 commit 87981cf

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

tests/__init__.py

Whitespace-only changes.

tests/helpers/__init__.py

Whitespace-only changes.

tests/helpers/test_file_helper.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import itertools
2+
import pytest
3+
import sys
4+
5+
from collections import namedtuple
6+
from lib.helpers.file_helper import parse_word_list_argument, get_combined_word_lists
7+
8+
WORDLIST_FILES = {
9+
'simpsons': ['marge', 'bart', 'homer', 'lisa', 'maggie'],
10+
'family_guy': ['stewie', 'lois', 'peter', 'brian', 'maggie', 'chris'],
11+
'flinstones': ['fred', 'bambam', 'wilma', 'barney', 'betty'],
12+
'duplicates': ['marge', 'bart'],
13+
}
14+
15+
WordlistFiles = namedtuple('WordlistFiles', 'files words')
16+
17+
@pytest.fixture(scope='session')
18+
def wordlist(tmpdir_factory):
19+
tmpdir = tmpdir_factory.mktemp('wordlists')
20+
21+
def create_file(filename):
22+
file = tmpdir.join(filename)
23+
file.write('\n'.join(WORDLIST_FILES[filename]))
24+
return str(file)
25+
26+
words = list(itertools.chain.from_iterable(WORDLIST_FILES.values()))
27+
files = [create_file(file) for file in WORDLIST_FILES.keys()]
28+
29+
return WordlistFiles(files=files, words=words)
30+
31+
def test_parse_word_list_argument(wordlist):
32+
argument = ','.join(wordlist.files + ['/non-existing-file'])
33+
result = parse_word_list_argument(argument)
34+
35+
assert wordlist.files == result # non-existing files should be discarded
36+
37+
def test_get_combined_word_lists(wordlist):
38+
result = get_combined_word_lists(','.join(wordlist.files))
39+
40+
assert wordlist.files == result['file_paths']
41+
assert wordlist.words == result['words']
42+

0 commit comments

Comments
 (0)