|
7 | 7 | from . import solr_test_profiles as tp |
8 | 8 |
|
9 | 9 |
|
10 | | -@pytest.fixture(scope='function') |
11 | | -def shelflist_solr_assembler(solr_data_assembler, solr_profile_definitions): |
| 10 | +@pytest.fixture(scope='module') |
| 11 | +def shelflist_solr_profile_definitions(solr_profile_definitions): |
12 | 12 | """ |
13 | | - Pytest fixture. Returns a Solr test data assembler that defines a |
14 | | - `shelflistitem` profile. |
| 13 | + Module-scoped pytest fixture. Returns the definition for the |
| 14 | + `shelflistitem` profile for generating Solr test data. |
15 | 15 | """ |
16 | | - profile_def = { |
17 | | - 'shelflistitem': { |
18 | | - 'conn': solr_profile_definitions['item']['conn'], |
19 | | - 'user_fields': tp.SHELFLISTITEM_FIELDS, |
20 | | - 'field_gens': tp.SHELFLISTITEM_GENS |
21 | | - } |
| 16 | + pdefs = solr_profile_definitions.copy() |
| 17 | + pdefs['shelflistitem'] = { |
| 18 | + 'conn': solr_profile_definitions['item']['conn'], |
| 19 | + 'user_fields': tp.SHELFLISTITEM_FIELDS, |
| 20 | + 'field_gens': tp.SHELFLISTITEM_GENS |
22 | 21 | } |
| 22 | + return pdefs |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope='module') |
| 26 | +def global_shelflist_solr_assembler(global_solr_data_assembler, |
| 27 | + shelflist_solr_profile_definitions): |
| 28 | + """ |
| 29 | + Module-scoped pytest fixture. Returns a Solr test data assembler |
| 30 | + for the `shelflistitem` profile. |
| 31 | + """ |
| 32 | + assembler = global_solr_data_assembler |
| 33 | + return assembler(tp.SOLR_TYPES, tp.GLOBAL_UNIQUE_FIELDS, tp.GENS, |
| 34 | + shelflist_solr_profile_definitions) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(scope='function') |
| 38 | +def shelflist_solr_assembler(solr_data_assembler, |
| 39 | + shelflist_solr_profile_definitions): |
| 40 | + """ |
| 41 | + Function-scoped pytest fixture. Returns a Solr test data assembler |
| 42 | + for the `shelflistitem` profile. |
| 43 | + """ |
23 | 44 | return solr_data_assembler(tp.SOLR_TYPES, tp.GLOBAL_UNIQUE_FIELDS, |
24 | | - tp.GENS, profile_def) |
| 45 | + tp.GENS, shelflist_solr_profile_definitions) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope='module') |
| 49 | +def shelflist_solr_env(global_shelflist_solr_assembler): |
| 50 | + """ |
| 51 | + Pytest fixture that generates and populates Solr with some random |
| 52 | + background test data for shelflist API tests. Fixture is module- |
| 53 | + scoped, so test data is regenerated each time the test module runs, |
| 54 | + NOT between tests. |
| 55 | + """ |
| 56 | + assembler = global_shelflist_solr_assembler |
| 57 | + gens = assembler.gen_factory |
| 58 | + loc_recs = assembler.make('location', 10) |
| 59 | + itype_recs = assembler.make('itype', 10) |
| 60 | + status_recs = assembler.make('itemstatus', 10) |
| 61 | + shelflistitem_recs = assembler.make('shelflistitem', 200, |
| 62 | + location_code=gens.choice([r['code'] for r in loc_recs]), |
| 63 | + item_type_code=gens.choice([r['code'] for r in itype_recs]), |
| 64 | + status_code=gens.choice([r['code'] for r in status_recs]), |
| 65 | + ) |
| 66 | + assembler.save_all() |
| 67 | + return assembler |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(scope='function') |
| 71 | +def assemble_shelflist_test_records(assemble_test_records, shelflist_solr_env, |
| 72 | + shelflist_solr_assembler): |
| 73 | + """ |
| 74 | + Pytest fixture. Returns a helper function that assembles & loads a |
| 75 | + set of test records (for one test) into the existing module-level |
| 76 | + shelflist_solr_env test-data environment. |
| 77 | +
|
| 78 | + The only required arg is `test_data`, a set of static partial |
| 79 | + records for this test. Default profile used is 'shelflistitem', but |
| 80 | + you may override that via the `profile` kwarg. The default id field |
| 81 | + (for ensuring uniqueness in the test data) is 'id', but you may |
| 82 | + override that via the `id_field` kwarg. Returns a tuple of default |
| 83 | + shelflist_solr_env records and the new test records that were |
| 84 | + loaded from the provided test data. len(env_recs) + len(test_recs) |
| 85 | + should equal the total number of Solr records for that profile. |
| 86 | + """ |
| 87 | + def _assemble_shelflist_test_records(test_data, profile='shelflistitem', |
| 88 | + id_field='id'): |
| 89 | + return assemble_test_records(profile, id_field, test_data, |
| 90 | + env=shelflist_solr_env, |
| 91 | + assembler=shelflist_solr_assembler) |
| 92 | + return _assemble_shelflist_test_records |
0 commit comments