Skip to content

Commit d60da32

Browse files
committed
fix: support value remap in fluent api test
1 parent 513fd3b commit d60da32

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

test/fluent_api/conftest.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture
1313
def run_fluent_api_notecard_api_mapping_test():
14-
def _run_test(fluent_api, notecard_api_name, req_params, rename_map=None):
14+
def _run_test(fluent_api, notecard_api_name, req_params, rename_key_map=None, rename_value_map=None):
1515
card = notecard.Notecard()
1616
card.Transaction = MagicMock()
1717

@@ -21,21 +21,31 @@ def _run_test(fluent_api, notecard_api_name, req_params, rename_map=None):
2121
# There are certain fluent APIs that have keyword arguments that don't
2222
# map exactly onto the Notecard API. For example, note.changes takes a
2323
# '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
24+
# Notecard, it's sent as 'max'. The rename_key_map allows a test to specify
2525
# how a fluent API's keyword args map to Notecard API args, in cases
2626
# where they differ.
27-
if rename_map is not None:
28-
for old_key, new_key in rename_map.items():
27+
if rename_key_map is not None:
28+
for old_key, new_key in rename_key_map.items():
2929
expected_notecard_api_req[new_key] = expected_notecard_api_req.pop(old_key)
3030

31+
# Additionally, some Notecard API args have values that are not directly
32+
# mapped, but are instead derived from the value of another arg. For
33+
# example, note.template takes a 'compact' parameter, but the value of
34+
# that parameter is actually derived from the value of the 'format'
35+
# parameter. The rename_value_map allows a test to specify how a fluent
36+
# API's keyword args map to Notecard API args, in cases where the value
37+
if rename_value_map is not None:
38+
for old_key, new_value in rename_value_map.items():
39+
expected_notecard_api_req[old_key] = new_value
40+
3141
card.Transaction.assert_called_once_with(expected_notecard_api_req)
3242

3343
return _run_test
3444

3545

3646
@pytest.fixture
3747
def run_fluent_api_invalid_notecard_test():
38-
def _run_test(fluent_api, req_params):
48+
def _run_test(fluent_api, req_params, rename_key_map=None, rename_value_map=None):
3949
with pytest.raises(Exception, match='Notecard object required'):
4050
# Call with None instead of a valid Notecard object.
4151
fluent_api(None, **req_params)

test/fluent_api/test_note.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
@pytest.mark.parametrize(
6-
'fluent_api,notecard_api,req_params,rename_map',
6+
'fluent_api,notecard_api,req_params,rename_key_map,rename_value_map',
77
[
88
(
99
note.add,
@@ -12,8 +12,10 @@
1212
'file': 'data.qo',
1313
'body': {'key_a:', 'val_a', 'key_b', 42},
1414
'payload': 'ewogICJpbnRlcnZhbHMiOiI2MCwxMiwxNCIKfQ==',
15+
'port': 50,
1516
'sync': True
1617
},
18+
None,
1719
None
1820
),
1921
(
@@ -30,7 +32,8 @@
3032
},
3133
{
3234
'maximum': 'max'
33-
}
35+
},
36+
None
3437
),
3538
(
3639
note.delete,
@@ -41,7 +44,8 @@
4144
},
4245
{
4346
'note_id': 'note'
44-
}
47+
},
48+
None
4549
),
4650
(
4751
note.get,
@@ -54,17 +58,25 @@
5458
},
5559
{
5660
'note_id': 'note'
57-
}
61+
},
62+
None
5863
),
5964
(
6065
note.template,
6166
'note.template',
6267
{
6368
'file': 'my-settings.db',
6469
'body': {'key_a:', 'val_a', 'key_b', 42},
65-
'length': 42
70+
'length': 42,
71+
'port': 50,
72+
'compact': True
6673
},
67-
None
74+
{
75+
'compact': 'format'
76+
},
77+
{
78+
'format' : 'compact'
79+
}
6880
),
6981
(
7082
note.update,
@@ -77,18 +89,21 @@
7789
},
7890
{
7991
'note_id': 'note'
80-
}
92+
},
93+
None
8194
)
8295
]
8396
)
8497
class TestNote:
8598
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):
99+
self, fluent_api, notecard_api, req_params, rename_key_map,
100+
rename_value_map, run_fluent_api_notecard_api_mapping_test):
88101
run_fluent_api_notecard_api_mapping_test(fluent_api, notecard_api,
89-
req_params, rename_map)
102+
req_params, rename_key_map,
103+
rename_value_map)
90104

91105
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)
106+
self, fluent_api, notecard_api, req_params, rename_key_map,
107+
rename_value_map, run_fluent_api_invalid_notecard_test):
108+
run_fluent_api_invalid_notecard_test(fluent_api, req_params,
109+
rename_key_map, rename_value_map)

0 commit comments

Comments
 (0)