Skip to content

Commit 147e656

Browse files
committed
Decouple api fixtures from assemble_test_records
In order to use the `assemble_test_records` fixture in non-api-app tests, I had to remove the `api_solr_env` and `basic_solr_assembler` fixtures, and then create an api-specific `assemble_api_test_records` fixture in the api-app tests that did use those fixtures. This fixes a bug in the shelflist-api tests (not yet committed) where too much test data was being created because I was inadvertently calling the `api_solr_env` fixture without wanting to or realizing it.
1 parent fc5bf87 commit 147e656

2 files changed

Lines changed: 47 additions & 27 deletions

File tree

django/sierra/api/tests/test_api.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,28 @@ def api_settings(settings):
19231923
return settings
19241924

19251925

1926+
@pytest.fixture(scope='function')
1927+
def assemble_api_test_records(assemble_test_records, api_solr_env,
1928+
basic_solr_assembler):
1929+
"""
1930+
Pytest fixture. Returns a helper function that assembles & loads a
1931+
set of test records (for one test) into the api_solr_env test-data
1932+
environment fixture.
1933+
1934+
Required args include a `profile` string, the name of the unique
1935+
`id_field` for each record (for test_data record uniqueness), and a
1936+
set of static `test_data` partial records. Returns a tuple of
1937+
default solr_env records and the new test records that were loaded
1938+
from the provided test data. len(env_recs) + len(test_recs) should
1939+
equal the total number of Solr records for that profile.
1940+
"""
1941+
def _assemble_api_test_records(profile, id_field, test_data):
1942+
return assemble_test_records(profile, id_field, test_data,
1943+
env=api_solr_env,
1944+
assembler=basic_solr_assembler)
1945+
return _assemble_api_test_records
1946+
1947+
19261948
# TESTS
19271949
# ---------------------------------------------------------------------
19281950

@@ -2179,8 +2201,8 @@ def test_list_view_pagination(resource, default_limit, max_limit, limit,
21792201
ids=compile_ids(PARAMETERS__FILTER_TESTS__INTENDED) +
21802202
compile_ids(PARAMETERS__FILTER_TESTS__STRANGE))
21812203
def test_list_view_filters(resource, test_data, search, expected, api_settings,
2182-
assemble_test_records, api_client, get_found_ids,
2183-
do_filter_search):
2204+
assemble_api_test_records, api_client,
2205+
get_found_ids, do_filter_search):
21842206
"""
21852207
Given the provided `test_data` records: requesting the given
21862208
`resource` using the provided search filter parameters (`search`)
@@ -2193,7 +2215,7 @@ def test_list_view_filters(resource, test_data, search, expected, api_settings,
21932215

21942216
profile = RESOURCE_METADATA[resource]['profile']
21952217
id_field = RESOURCE_METADATA[resource]['id_field']
2196-
erecs, trecs = assemble_test_records(profile, id_field, test_data)
2218+
erecs, trecs = assemble_api_test_records(profile, id_field, test_data)
21972219

21982220
# First let's do a quick sanity check to make sure the resource
21992221
# returns the correct num of records before the filter is applied.
@@ -2213,8 +2235,8 @@ def test_list_view_filters(resource, test_data, search, expected, api_settings,
22132235
ids=compile_ids(PARAMETERS__ORDERBY_TESTS__INTENDED) +
22142236
compile_ids(PARAMETERS__ORDERBY_TESTS__STRANGE))
22152237
def test_list_view_orderby(resource, test_data, search, expected, api_settings,
2216-
assemble_test_records, api_client, get_found_ids,
2217-
do_filter_search):
2238+
assemble_api_test_records, api_client,
2239+
get_found_ids, do_filter_search):
22182240
"""
22192241
Given the provided `test_data` records: requesting the given
22202242
`resource` using the provided search filter parameters (`search`)
@@ -2223,7 +2245,7 @@ def test_list_view_orderby(resource, test_data, search, expected, api_settings,
22232245
"""
22242246
profile = RESOURCE_METADATA[resource]['profile']
22252247
id_field = RESOURCE_METADATA[resource]['id_field']
2226-
erecs, trecs = assemble_test_records(profile, id_field, test_data)
2248+
erecs, trecs = assemble_api_test_records(profile, id_field, test_data)
22272249
print [r.get('call_number_sort', None) for r in trecs]
22282250
resource_url = '{}{}/'.format(API_ROOT, resource)
22292251
response = do_filter_search(resource_url, search, api_client)
@@ -2235,7 +2257,7 @@ def test_list_view_orderby(resource, test_data, search, expected, api_settings,
22352257
compile_params(PARAMETERS__FIRSTITEMPERLOCATION),
22362258
ids=compile_ids(PARAMETERS__FIRSTITEMPERLOCATION))
22372259
def test_firstitemperlocation_list(test_data, search, expected, api_settings,
2238-
assemble_test_records, api_client,
2260+
assemble_api_test_records, api_client,
22392261
get_found_ids, do_filter_search):
22402262
"""
22412263
The `firstitemperlocation` resource is basically a custom filter
@@ -2257,7 +2279,7 @@ def test_firstitemperlocation_list(test_data, search, expected, api_settings,
22572279
for resource in data.keys():
22582280
profile = RESOURCE_METADATA[resource]['profile']
22592281
id_field = RESOURCE_METADATA[resource]['id_field']
2260-
assemble_test_records(profile, id_field, data[resource])
2282+
assemble_api_test_records(profile, id_field, data[resource])
22612283

22622284
resource_url = '{}firstitemperlocation/'.format(API_ROOT)
22632285
rsp = do_filter_search(resource_url, search, api_client)
@@ -2270,7 +2292,7 @@ def test_firstitemperlocation_list(test_data, search, expected, api_settings,
22702292
compile_params(PARAMETERS__CALLNUMBERMATCHES),
22712293
ids=compile_ids(PARAMETERS__CALLNUMBERMATCHES))
22722294
def test_callnumbermatches_list(test_data, search, expected, api_settings,
2273-
assemble_test_records, api_client,
2295+
assemble_api_test_records, api_client,
22742296
do_filter_search):
22752297
"""
22762298
The `callnumbermatches` resource simply returns an array of
@@ -2287,7 +2309,7 @@ def test_callnumbermatches_list(test_data, search, expected, api_settings,
22872309
for resource in data.keys():
22882310
profile = RESOURCE_METADATA[resource]['profile']
22892311
id_field = RESOURCE_METADATA[resource]['id_field']
2290-
assemble_test_records(profile, id_field, data[resource])
2312+
assemble_api_test_records(profile, id_field, data[resource])
22912313

22922314
resource_url = '{}callnumbermatches/'.format(API_ROOT)
22932315
response = do_filter_search(resource_url, search, api_client)

django/sierra/conftest.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -786,29 +786,27 @@ def _get_linked_view_and_objects(client, ref_obj, link_field):
786786

787787

788788
@pytest.fixture(scope='function')
789-
def assemble_test_records(api_solr_env, basic_solr_assembler):
789+
def assemble_test_records():
790790
"""
791791
Pytest fixture. Returns a helper function that assembles & loads a
792792
set of test records (for one test) into an existing module-level
793793
Solr test-data environment.
794794
795-
Defaults to using `api_solr_env` and `basic_solr_assembler`
796-
fixtures, but you can override these via the `env` and `assembler`
797-
kwargs.
798-
799-
Required args include a `profile` string, a set of static
800-
`test_data` partial records, and the name of the unique `id_field`
801-
for each record (for test_data record uniqueness). Returns a tuple
802-
of default solr_env records and the new test records that were
803-
loaded from the provided test data. len(env_recs) + len(test_recs)
804-
should == the total number of Solr records for that profile.
805-
"""
806-
def _assemble_test_records(profile, id_field, test_data, env=api_solr_env,
807-
assembler=basic_solr_assembler):
795+
Args include a `profile` string, the name of the unique
796+
`id_field` for each record (for test_data record uniqueness), a
797+
set of static `test_data` partial records, the module-level env
798+
(assembler) fixture that serves as context, and the function-level
799+
assembler fixture you're using to load the temporary test data.
800+
Returns a tuple consisting of the records from the `env` assembler
801+
and the new test records that were loaded from the provided test
802+
data. len(env_recs) + len(test_recs) should equal the total number
803+
of Solr records for that profile.
804+
"""
805+
def _assemble_test_records(profile, id_field, test_data, env, assembler):
808806
env_recs = env.records[profile]
809-
test_recs = assembler.load_static_test_data(profile, test_data,
810-
id_field=id_field,
811-
context=env_recs)
807+
test_recs = assembler.load_static_test_data(
808+
profile, test_data, id_field=id_field, context=env_recs
809+
)
812810
return (env_recs, test_recs)
813811
return _assemble_test_records
814812

0 commit comments

Comments
 (0)