Skip to content

Commit 1ede5d6

Browse files
authored
Merge pull request #75 from haydenroche5/fluent_api_test_refactor
2 parents 5d55bfd + faa8d0c commit 1ede5d6

File tree

7 files changed

+357
-280
lines changed

7 files changed

+357
-280
lines changed

test/conftest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
import os
3+
import pytest
4+
from unittest.mock import MagicMock
5+
6+
sys.path.insert(0,
7+
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8+
9+
import notecard # noqa: E402
10+
11+
12+
@pytest.fixture
13+
def run_fluent_api_notecard_api_mapping_test():
14+
def _run_test(fluent_api, notecard_api_name, req_params, rename_map=None):
15+
card = notecard.Notecard()
16+
card.Transaction = MagicMock()
17+
18+
fluent_api(card, **req_params)
19+
expected_notecard_api_req = {'req': notecard_api_name, **req_params}
20+
21+
# There are certain fluent APIs that have keyword arguments that don't
22+
# map exactly onto the Notecard API. For example, note.changes takes a
23+
# 'maximum' parameter, but in the JSON request that gets sent to the
24+
# Notecard, it's sent as 'max'. The rename_map allows a test to specify
25+
# how a fluent API's keyword args map to Notecard API args, in cases
26+
# where they differ.
27+
if rename_map is not None:
28+
for old_key, new_key in rename_map.items():
29+
expected_notecard_api_req[new_key] = expected_notecard_api_req.pop(old_key)
30+
31+
card.Transaction.assert_called_once_with(expected_notecard_api_req)
32+
33+
return _run_test
34+
35+
36+
@pytest.fixture
37+
def run_fluent_api_invalid_notecard_test():
38+
def _run_test(fluent_api, req_params):
39+
with pytest.raises(Exception, match='Notecard object required'):
40+
# Call with None instead of a valid Notecard object.
41+
fluent_api(None, **req_params)
42+
43+
return _run_test

test/test_card.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import pytest
2+
from notecard import card
3+
4+
5+
@pytest.mark.parametrize(
6+
'fluent_api,notecard_api,req_params',
7+
[
8+
(
9+
card.attn,
10+
'card.attn',
11+
{
12+
'mode': 'arm',
13+
'files': ['data.qi', 'my-settings.db'],
14+
'seconds': 60,
15+
'payload': 'ewogICJpbnRlcnZhbHMiOiI2MCwxMiwxNCIKfQ==',
16+
'start': True
17+
}
18+
),
19+
(
20+
card.status,
21+
'card.status',
22+
{}
23+
),
24+
(
25+
card.time,
26+
'card.time',
27+
{}
28+
),
29+
(
30+
card.temp,
31+
'card.temp',
32+
{'minutes': 5}
33+
),
34+
(
35+
card.version,
36+
'card.version',
37+
{}
38+
),
39+
(
40+
card.voltage,
41+
'card.voltage',
42+
{
43+
'hours': 1,
44+
'offset': 2,
45+
'vmax': 1.1,
46+
'vmin': 1.2
47+
}
48+
),
49+
(
50+
card.wireless,
51+
'card.wireless',
52+
{
53+
'mode': 'auto',
54+
'apn': 'myapn.nb'
55+
}
56+
)
57+
]
58+
)
59+
class TestCard:
60+
def test_fluent_api_maps_notecard_api_correctly(
61+
self, fluent_api, notecard_api, req_params,
62+
run_fluent_api_notecard_api_mapping_test):
63+
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
64+
req_params)
65+
66+
def test_fluent_api_fails_with_invalid_notecard(
67+
self, fluent_api, notecard_api, req_params,
68+
run_fluent_api_invalid_notecard_test):
69+
run_fluent_api_invalid_notecard_test(fluent_api, req_params)

test/test_env.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from notecard import env
3+
4+
5+
@pytest.mark.parametrize(
6+
'fluent_api,notecard_api,req_params',
7+
[
8+
(
9+
env.default,
10+
'env.default',
11+
{'name': 'my_var', 'text': 'my_text'}
12+
),
13+
(
14+
env.get,
15+
'env.get',
16+
{'name': 'my_var'}
17+
),
18+
(
19+
env.modified,
20+
'env.modified',
21+
{}
22+
),
23+
(
24+
env.set,
25+
'env.set',
26+
{'name': 'my_var', 'text': 'my_text'}
27+
)
28+
]
29+
)
30+
class TestEnv:
31+
def test_fluent_api_maps_notecard_api_correctly(
32+
self, fluent_api, notecard_api, req_params,
33+
run_fluent_api_notecard_api_mapping_test):
34+
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
35+
req_params)
36+
37+
def test_fluent_api_fails_with_invalid_notecard(
38+
self, fluent_api, notecard_api, req_params,
39+
run_fluent_api_invalid_notecard_test):
40+
run_fluent_api_invalid_notecard_test(fluent_api, req_params)

test/test_file.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
from notecard import file
3+
4+
5+
@pytest.mark.parametrize(
6+
'fluent_api,notecard_api,req_params',
7+
[
8+
(
9+
file.changes,
10+
'file.changes',
11+
{
12+
'tracker': 'tracker',
13+
'files': ['file_1', 'file_2', 'file_3']
14+
}
15+
),
16+
(
17+
file.delete,
18+
'file.delete',
19+
{
20+
'files': ['file_1', 'file_2', 'file_3']
21+
}
22+
),
23+
(
24+
file.stats,
25+
'file.stats',
26+
{}
27+
),
28+
(
29+
file.pendingChanges,
30+
'file.changes.pending',
31+
{}
32+
)
33+
]
34+
)
35+
class TestFile:
36+
def test_fluent_api_maps_notecard_api_correctly(
37+
self, fluent_api, notecard_api, req_params,
38+
run_fluent_api_notecard_api_mapping_test):
39+
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
40+
req_params)
41+
42+
def test_fluent_api_fails_with_invalid_notecard(
43+
self, fluent_api, notecard_api, req_params,
44+
run_fluent_api_invalid_notecard_test):
45+
run_fluent_api_invalid_notecard_test(fluent_api, req_params)

test/test_hub.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pytest
2+
from notecard import hub
3+
4+
5+
@pytest.mark.parametrize(
6+
'fluent_api,notecard_api,req_params',
7+
[
8+
(
9+
hub.get,
10+
'hub.get',
11+
{}
12+
),
13+
(
14+
hub.log,
15+
'hub.log',
16+
{
17+
'text': 'com.blues.tester',
18+
'alert': True,
19+
'sync': True
20+
}
21+
),
22+
(
23+
hub.set,
24+
'hub.set',
25+
{
26+
'product': 'com.blues.tester',
27+
'sn': 'foo',
28+
'mode': 'continuous',
29+
'outbound': 2,
30+
'inbound': 60,
31+
'duration': 5,
32+
'sync': True,
33+
'align': True,
34+
'voutbound': '2.3',
35+
'vinbound': '3.3',
36+
'host': 'http://hub.blues.foo'
37+
}
38+
),
39+
(
40+
hub.status,
41+
'hub.status',
42+
{}
43+
),
44+
(
45+
hub.sync,
46+
'hub.sync',
47+
{}
48+
),
49+
(
50+
hub.syncStatus,
51+
'hub.sync.status',
52+
{'sync': True}
53+
)
54+
]
55+
)
56+
class TestHub:
57+
def test_fluent_api_maps_notecard_api_correctly(
58+
self, fluent_api, notecard_api, req_params,
59+
run_fluent_api_notecard_api_mapping_test):
60+
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
61+
req_params)
62+
63+
def test_fluent_api_fails_with_invalid_notecard(
64+
self, fluent_api, notecard_api, req_params,
65+
run_fluent_api_invalid_notecard_test):
66+
run_fluent_api_invalid_notecard_test(fluent_api, req_params)

test/test_note.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
from notecard import note
3+
4+
5+
@pytest.mark.parametrize(
6+
'fluent_api,notecard_api,req_params,rename_map',
7+
[
8+
(
9+
note.add,
10+
'note.add',
11+
{
12+
'file': 'data.qo',
13+
'body': {'key_a:', 'val_a', 'key_b', 42},
14+
'payload': 'ewogICJpbnRlcnZhbHMiOiI2MCwxMiwxNCIKfQ==',
15+
'sync': True
16+
},
17+
None
18+
),
19+
(
20+
note.changes,
21+
'note.changes',
22+
{
23+
'file': 'my-settings.db',
24+
'tracker': 'inbound-tracker',
25+
'maximum': 2,
26+
'start': True,
27+
'stop': True,
28+
'delete': True,
29+
'deleted': True
30+
},
31+
{
32+
'maximum': 'max'
33+
}
34+
),
35+
(
36+
note.delete,
37+
'note.delete',
38+
{
39+
'file': 'my-settings.db',
40+
'note_id': 'my_note',
41+
},
42+
{
43+
'note_id': 'note'
44+
}
45+
),
46+
(
47+
note.get,
48+
'note.get',
49+
{
50+
'file': 'my-settings.db',
51+
'note_id': 'my_note',
52+
'delete': True,
53+
'deleted': True
54+
},
55+
{
56+
'note_id': 'note'
57+
}
58+
),
59+
(
60+
note.template,
61+
'note.template',
62+
{
63+
'file': 'my-settings.db',
64+
'body': {'key_a:', 'val_a', 'key_b', 42},
65+
'length': 42
66+
},
67+
None
68+
),
69+
(
70+
note.update,
71+
'note.update',
72+
{
73+
'file': 'my-settings.db',
74+
'note_id': 'my_note',
75+
'body': {'key_a:', 'val_a', 'key_b', 42},
76+
'payload': 'ewogICJpbnRlcnZhbHMiOiI2MCwxMiwxNCIKfQ=='
77+
},
78+
{
79+
'note_id': 'note'
80+
}
81+
)
82+
]
83+
)
84+
class TestNote:
85+
def test_fluent_api_maps_notecard_api_correctly(
86+
self, fluent_api, notecard_api, req_params, rename_map,
87+
run_fluent_api_notecard_api_mapping_test):
88+
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
89+
req_params, rename_map)
90+
91+
def test_fluent_api_fails_with_invalid_notecard(
92+
self, fluent_api, notecard_api, req_params, rename_map,
93+
run_fluent_api_invalid_notecard_test):
94+
run_fluent_api_invalid_notecard_test(fluent_api, req_params)

0 commit comments

Comments
 (0)