Skip to content

Commit 5e36ef9

Browse files
authored
Merge pull request PaulMcInnis#116 from thebigG/test_cli
-Added tests for cli.py
2 parents 8cee6e5 + effef1e commit 5e36ef9

File tree

3 files changed

+179
-61
lines changed

3 files changed

+179
-61
lines changed

tests/config/test_cli.py

Lines changed: 139 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,163 @@
33
import os
44
import pytest
55
from jobfunnel.config import parse_cli, build_config_dict
6+
from tests.conftest import get_data_path
67

8+
TEST_YAML = os.path.join(get_data_path(), 'test_config.yml')
9+
INCORRECT_TEST_YAML = os.path.join(get_data_path(), '../data/incorrect_test_config.yml')
710

8-
TEST_YAML = os.path.join('tests', 'data', 'test_config.yml')
9-
10-
11-
@pytest.mark.parametrize('argv, exp_exception', [
12-
# Test schema from YAML
13-
(['load', '-s', TEST_YAML], None),
14-
# Test overrideable args
15-
(['load', '-s', TEST_YAML, '-log-level', 'DEBUG'], None),
16-
(['load', '-s', TEST_YAML, '-log-level', 'WARNING'], None),
17-
(['load', '-s', TEST_YAML, '--no-scrape'], None),
11+
inline_args = [
1812
# Test schema from CLI
1913
(['inline', '-csv', 'TEST_search', '-log-level', 'DEBUG', '-cache',
2014
'TEST_cache', '-blf', 'TEST_block_list', '-dl', 'TEST_duplicates_list',
2115
'-log-file', 'TEST_log_file', '-kw', 'I', 'Am', 'Testing', '-l',
2216
'CANADA_ENGLISH', '-ps', 'TESTPS', '-c', 'TestCity', '-cbl',
2317
'Blocked Company', 'Blocked Company 2', '-p', 'INDEED', 'MONSTER',
2418
'-r', '42', '-max-listing-days', '44', '--similar-results', '--random',
25-
'--converging', '-max', '8', '-min', '2', '-algorithm', 'LINEAR'], None),
19+
'--converging', '-max', '8', '-min', '2', '-algorithm', 'LINEAR']),
20+
]
21+
22+
load_args = [
23+
# Test schema from YAML
24+
(['load', '-s', TEST_YAML]),
25+
# Test overrideable args
26+
(['load', '-s', TEST_YAML, '-log-level', 'DEBUG']),
27+
(['load', '-s', TEST_YAML, '--no-scrape'])
28+
]
29+
30+
load_args_invalid_settings = [
31+
# Test incorrect yaml
32+
(['load', '-s', INCORRECT_TEST_YAML], ValueError),
33+
]
34+
35+
invalid_args = [
2636
# Invalid cases
2737
(['load'], SystemExit),
2838
(['load', '-csv', 'boo'], SystemExit),
29-
(['inline', '-csv', 'TEST_search', '-log-level', 'DEBUG', '-cache',
30-
'TEST_cache', '-blf', 'TEST_block_list', '-dl',
31-
'TEST_duplicates_list'], SystemExit),
32-
(['-csv', 'test.csv'], SystemExit),
3339
(['-l',
3440
'CANADA_ENGLISH', '-ps', 'TESTPS', '-c', 'TestCity', '-cbl',
3541
'Blocked Company', 'Blocked Company 2', '-p', 'INDEED', 'MONSTER',
3642
'-r', '42', '-max-listing-days', '44', '--similar-results', '--random',
3743
'--converging', '-max', '8', '-min', '2', '-algorithm',
3844
'LINEAR'], SystemExit),
39-
])
40-
def test_parse_cli_build_config_dict(argv, exp_exception):
41-
"""Functional test to ensure that the CLI functions as we expect
42-
TODO: break down into test_parse_cli and test_config_parser
43-
FIXME: add exception message assertions
45+
(['inline', '-csv', 'TEST_search', '-log-level', 'DEBUG', '-cache',
46+
'TEST_cache', '-blf', 'TEST_block_list', '-dl',
47+
'TEST_duplicates_list'], SystemExit),
48+
(['-csv', 'test.csv'], SystemExit),
49+
]
50+
51+
52+
@pytest.mark.parametrize('argv', inline_args)
53+
def test_parse_cli_inline(argv):
54+
"""
55+
Test the correctness of parse_cli
4456
"""
45-
# FUT
46-
if exp_exception:
47-
with pytest.raises(exp_exception):
48-
args = parse_cli(argv)
49-
cfg = build_config_dict(args)
57+
args = parse_cli(argv)
58+
assert args['do_recovery_mode'] is False
59+
assert args['load | inline'] == 'inline'
60+
assert args['log_level'] == 'DEBUG'
61+
assert args['no_scrape'] is False
62+
assert args['master_csv_file'] == 'TEST_search'
63+
assert args['log_file'] == 'TEST_log_file'
64+
assert args['cache_folder'] == 'TEST_cache'
65+
assert args['block_list_file'] == 'TEST_block_list'
66+
assert args['duplicates_list_file'] == 'TEST_duplicates_list'
67+
assert args['search.keywords'] == ['I', 'Am', 'Testing']
68+
assert args['search.locale'] == 'CANADA_ENGLISH'
69+
assert args['search.province_or_state'] == 'TESTPS'
70+
assert args['search.city'] == 'TestCity'
71+
assert args['search.company_block_list'] == ['Blocked Company', 'Blocked Company 2']
72+
assert args['search.radius'] == 42
73+
assert args['search.remoteness'] == 'ANY'
74+
assert args['search.max_listing_days'] == 44
75+
assert args['search.similar_results'] is True
76+
assert args['proxy.protocol'] is None
77+
assert args['proxy.ip'] is None
78+
assert args['proxy.port'] is None
79+
assert args['delay.random'] is True
80+
assert args['delay.converging'] is True
81+
assert args['delay.max_duration'] == 8.0
82+
assert args['delay.min_duration'] == 2.0
83+
assert args['delay.algorithm'] == 'LINEAR'
84+
85+
86+
@pytest.mark.parametrize('argv', load_args)
87+
def test_parse_cli_load(argv):
88+
args = parse_cli(argv)
89+
# FIXME: This test should only be testing the correctness of parse_cli
90+
# Assertions
91+
92+
assert args['settings_yaml_file'] == TEST_YAML
93+
94+
if '-log-level' in argv and argv[4] == 'DEBUG':
95+
# NOTE: need to always pass log level in same place for this cdtn
96+
assert args['log_level'] == 'DEBUG'
97+
if '--no-scrape' in argv:
98+
assert args['no_scrape'] is True
5099
else:
100+
assert args['no_scrape'] is False
101+
102+
103+
@pytest.mark.parametrize('argv, exception', invalid_args)
104+
def test_parse_cli_invalid_args(argv, exception):
105+
with pytest.raises(exception) as e:
51106
args = parse_cli(argv)
52-
cfg = build_config_dict(args)
53-
54-
# Assertions
55-
assert cfg['master_csv_file'] == 'TEST_search'
56-
assert cfg['cache_folder'] == 'TEST_cache'
57-
assert cfg['block_list_file'] == 'TEST_block_list'
58-
assert cfg['duplicates_list_file'] == 'TEST_duplicates_list'
59-
assert cfg['search']['locale'] == 'CANADA_ENGLISH'
60-
assert cfg['search']['providers'] == ['INDEED', 'MONSTER']
61-
assert cfg['search']['province_or_state'] == 'TESTPS'
62-
assert cfg['search']['city'] == 'TestCity'
63-
assert cfg['search']['radius'] == 42
64-
assert cfg['search']['keywords'] == ['I', 'Am', 'Testing']
65-
assert cfg['search']['max_listing_days'] == 44
66-
assert cfg['search']['company_block_list'] == ['Blocked Company',
67-
'Blocked Company 2']
68-
if '-log-level' in argv:
69-
# NOTE: need to always pass log level in same place for this cdtn
70-
assert cfg['log_level'] == argv[4]
71-
else:
72-
assert cfg['log_level'] == 'INFO'
73-
if '--no-scrape' in argv:
74-
assert cfg['no_scrape']
75-
else:
76-
assert not cfg['no_scrape']
77-
if '--similar-results' in argv:
78-
assert cfg['search']['similar_results']
79-
else:
80-
assert not cfg['search']['similar_results']
81-
82-
assert cfg['delay']['algorithm'] == 'LINEAR'
83-
assert cfg['delay']['max_duration'] == 8
84-
assert cfg['delay']['min_duration'] == 2
85-
assert cfg['delay']['random']
86-
assert cfg['delay']['converging']
107+
assert str(e.value) == '2'
108+
109+
110+
@pytest.mark.parametrize('argv, exception', load_args_invalid_settings)
111+
def test_build_config_dict_invalid_settings(argv, exception):
112+
args = parse_cli(argv)
113+
with pytest.raises(exception) as e:
114+
cfg_dict = build_config_dict(args)
115+
assert str(e.value) == "Invalid Config settings yaml:\n" \
116+
"{'search': [{'radius': ['must be of integer type']}]}"
117+
118+
119+
@pytest.mark.parametrize('argv', load_args)
120+
def test_build_config_dict_load_args(argv):
121+
args = parse_cli(argv)
122+
cfg_dict = build_config_dict(args)
123+
124+
assert cfg_dict['master_csv_file'] == 'TEST_search'
125+
assert cfg_dict['cache_folder'] == 'TEST_cache'
126+
assert cfg_dict['block_list_file'] == 'TEST_block_list'
127+
assert cfg_dict['duplicates_list_file'] == 'TEST_duplicates_list'
128+
assert cfg_dict['log_file'] == 'TEST_log_file'
129+
assert cfg_dict['search'] == {'locale': 'CANADA_ENGLISH', 'providers': ['INDEED', 'MONSTER'],
130+
'province_or_state': 'TESTPS', 'city': 'TestCity', 'radius': 42, 'keywords':
131+
['I', 'Am', 'Testing'], 'max_listing_days': 44, 'company_block_list':
132+
['Blocked Company', 'Blocked Company 2'],
133+
'similar_results': False, 'remoteness': 'ANY'}
134+
if '-log-level' in argv:
135+
assert cfg_dict['log_level'] == 'DEBUG'
136+
else:
137+
assert cfg_dict['log_level'] == 'INFO'
138+
139+
assert cfg_dict['delay'] == {'algorithm': 'LINEAR', 'max_duration': 8, 'min_duration': 2,
140+
'random': True, 'converging': True}
141+
if '--no-scrape' in argv:
142+
assert cfg_dict['no_scrape'] is True
143+
else:
144+
assert cfg_dict['no_scrape'] is False
145+
146+
147+
@pytest.mark.parametrize('argv', inline_args)
148+
def test_build_config_dict_inline_args(argv):
149+
args = parse_cli(argv)
150+
cfg_dict = build_config_dict(args)
151+
assert cfg_dict['master_csv_file'] == 'TEST_search'
152+
assert cfg_dict['cache_folder'] == 'TEST_cache'
153+
assert cfg_dict['block_list_file'] == 'TEST_block_list'
154+
assert cfg_dict['duplicates_list_file'] == 'TEST_duplicates_list'
155+
assert cfg_dict['log_file'] == 'TEST_log_file'
156+
assert cfg_dict['search'] == {'locale': 'CANADA_ENGLISH', 'providers': ['INDEED', 'MONSTER'],
157+
'province_or_state': 'TESTPS', 'city': 'TestCity', 'radius': 42, 'keywords':
158+
['I', 'Am', 'Testing'], 'max_listing_days': 44, 'company_block_list':
159+
['Blocked Company', 'Blocked Company 2'],
160+
'similar_results': True, 'remoteness': 'ANY'}
161+
assert cfg_dict['delay'] == {'random': True, 'converging': True, 'max_duration': 8.0, 'min_duration': 2.0,
162+
'algorithm': 'LINEAR'}
163+
assert cfg_dict['log_level'] == 'DEBUG'
164+
assert cfg_dict['no_scrape'] is False
165+
assert cfg_dict['proxy'] == {}

tests/conftest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
# FIXME
1+
import pytest
2+
import os
3+
4+
5+
# TODO: This should be a fixture. For now it is not because fixtures cannot be easily called as regular functions.
6+
def get_data_path():
7+
"""
8+
Get the data path relative to this file. This is useful for when you want to test a single test file and not the
9+
entire test suite.
10+
:return:
11+
"""
12+
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
master_csv_file: TEST_search
2+
cache_folder: TEST_cache
3+
block_list_file: TEST_block_list
4+
duplicates_list_file: TEST_duplicates_list
5+
log_file: TEST_log_file
6+
search:
7+
locale: CANADA_ENGLISH
8+
providers:
9+
- INDEED
10+
- MONSTER
11+
province_or_state: TESTPS
12+
city: TestCity
13+
radius: 'forty-two'
14+
keywords:
15+
- I
16+
- Am
17+
- Testing
18+
max_listing_days: 44
19+
company_block_list:
20+
- "Blocked Company"
21+
- "Blocked Company 2"
22+
log_level: INFO
23+
delay:
24+
algorithm: LINEAR
25+
max_duration: 8
26+
min_duration: 2
27+
random: True
28+
converging: True

0 commit comments

Comments
 (0)