-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_utilities.py
82 lines (64 loc) · 2.63 KB
/
test_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from datetime import date
from os.path import dirname
import pytest
from snowex_db.utilities import *
def test_read_nline():
"""
Test we can read a specific numbe of lines from a file
"""
f = join(dirname(__file__), 'data', 'density.csv')
line = read_n_lines(f, 1)
assert line[0] == '# Location,Grand Mesa\n'
def test_find_files():
"""
Test we can find files using patterns and extensions
"""
d = join(dirname(__file__), 'data')
files = find_files(d, 'adf', 'w001001x')
assert len(files) == 2
@pytest.mark.parametrize("kw, lines, expected", [
# Typical use
('snow', ['snowpits', 'nothing'], 0),
# Didn't find anything
('ice', ['snow', 'ex', 'is', 'awesome'], -1)
])
def test_find_kw_in_lines(kw, lines, expected):
"""
test finding a keyword in a list of strings
"""
assert find_kw_in_lines(kw, lines, addon_str='') == expected
class TestAssignDefaultKwargs():
"""
Test the function assign_default_kwargs. This class is necessary so
we can add attributes to it without raising exceptions.
"""
@pytest.mark.parametrize("kwargs, defaults, leave, expected_kwargs, expected_attr", [
# Assert missing attributes are added to object and removed from kwargs
({}, {'test': False}, [], {}, {'test': False}),
# Assert we don't overwrite the kwargs provided by user
({'test': True, }, {'test': False}, [], {}, {'test': True}),
# Assert we leave non-default keys and still assign defaults
({'stays': True, }, {'test': False}, [], {'stays': True}, {'test': False}),
# Assert keys can be left in the mod kwargs but still be used
({'leave_test': True}, {'test': False, 'leave_test': True}, ['leave_test'], {'leave_test': True},
{'test': False, 'leave_test': True}),
])
def test_assign_default_kwargs(self, kwargs, defaults, leave, expected_kwargs, expected_attr):
"""
Test we can assign object attributes to an object given kwargs and defaults
"""
# Make a dummy object for testing
# Modify obj, and removed default kw in kwargs
mod_kwargs = assign_default_kwargs(self, kwargs, defaults, leave)
# 1. Test We have removed kw from mod_kwargs
for k, v in expected_kwargs.items():
assert v == mod_kwargs[k]
# 2. Test the object has the attributes/values
for k, v in expected_attr.items():
assert getattr(self, k) == v
def test_get_file_creation_date():
"""
Test the get_file_creation_date produces a datetime object
"""
result = get_file_creation_date(__file__)
assert type(result) is date