|
9 | 9 | from aind_data_access_api.utils import (
|
10 | 10 | build_docdb_location_to_id_map,
|
11 | 11 | does_metadata_record_exist_in_docdb,
|
| 12 | + fetch_records_by_filter_list, |
12 | 13 | get_record_from_docdb,
|
13 | 14 | get_s3_bucket_and_prefix,
|
14 | 15 | get_s3_location,
|
@@ -138,12 +139,93 @@ def test_paginate_docdb(self, mock_docdb_api_client: MagicMock):
|
138 | 139 | self.assertEqual(expected_results, actual_results)
|
139 | 140 |
|
140 | 141 | @patch("aind_data_access_api.document_db.MetadataDbClient")
|
141 |
| - def test_build_docdb_location_to_id_map( |
| 142 | + def test_fetch_records_by_filter_list( |
142 | 143 | self, mock_docdb_api_client: MagicMock
|
| 144 | + ): |
| 145 | + """Tests fetch_records_by_filter_list""" |
| 146 | + expected_records = [ |
| 147 | + { |
| 148 | + "_id": "70bcf356-985f-4a2a-8105-de900e35e788", |
| 149 | + "name": "prefix1", |
| 150 | + "location": "s3://bucket/prefix1", |
| 151 | + }, |
| 152 | + { |
| 153 | + "_id": "5ca4a951-d374-4f4b-8279-d570a35b2286", |
| 154 | + "name": "prefix2", |
| 155 | + "location": "s3://bucket/prefix2", |
| 156 | + }, |
| 157 | + ] |
| 158 | + mock_docdb_api_client.aggregate_docdb_records.return_value = ( |
| 159 | + expected_records |
| 160 | + ) |
| 161 | + |
| 162 | + records = fetch_records_by_filter_list( |
| 163 | + docdb_api_client=mock_docdb_api_client, |
| 164 | + filter_key="name", |
| 165 | + filter_values=["prefix1", "prefix2", "missing_prefix"], |
| 166 | + ) |
| 167 | + self.assertEqual(expected_records, records) |
| 168 | + mock_docdb_api_client.aggregate_docdb_records.assert_called_once_with( |
| 169 | + pipeline=[ |
| 170 | + { |
| 171 | + "$match": { |
| 172 | + "name": { |
| 173 | + "$in": ["prefix1", "prefix2", "missing_prefix"] |
| 174 | + } |
| 175 | + } |
| 176 | + }, |
| 177 | + ] |
| 178 | + ) |
| 179 | + |
| 180 | + @patch("aind_data_access_api.document_db.MetadataDbClient") |
| 181 | + def test_fetch_records_by_filter_list_projection( |
| 182 | + self, mock_docdb_api_client: MagicMock |
| 183 | + ): |
| 184 | + """Tests fetch_records_by_filter_list with projection""" |
| 185 | + expected_records = [ |
| 186 | + { |
| 187 | + "_id": "70bcf356-985f-4a2a-8105-de900e35e788", |
| 188 | + "name": "prefix1", |
| 189 | + }, |
| 190 | + { |
| 191 | + "_id": "5ca4a951-d374-4f4b-8279-d570a35b2286", |
| 192 | + "name": "prefix2", |
| 193 | + }, |
| 194 | + ] |
| 195 | + mock_docdb_api_client.aggregate_docdb_records.return_value = ( |
| 196 | + expected_records |
| 197 | + ) |
| 198 | + |
| 199 | + records = fetch_records_by_filter_list( |
| 200 | + docdb_api_client=mock_docdb_api_client, |
| 201 | + filter_key="name", |
| 202 | + filter_values=["prefix1", "prefix2", "missing_prefix"], |
| 203 | + projection={"_id": 1, "name": 1}, |
| 204 | + ) |
| 205 | + self.assertEqual(expected_records, records) |
| 206 | + mock_docdb_api_client.aggregate_docdb_records.assert_called_once_with( |
| 207 | + pipeline=[ |
| 208 | + { |
| 209 | + "$match": { |
| 210 | + "name": { |
| 211 | + "$in": ["prefix1", "prefix2", "missing_prefix"] |
| 212 | + } |
| 213 | + } |
| 214 | + }, |
| 215 | + {"$project": {"_id": 1, "name": 1}}, |
| 216 | + ] |
| 217 | + ) |
| 218 | + |
| 219 | + @patch("aind_data_access_api.utils.fetch_records_by_filter_list") |
| 220 | + @patch("aind_data_access_api.document_db.MetadataDbClient") |
| 221 | + def test_build_docdb_location_to_id_map( |
| 222 | + self, |
| 223 | + mock_docdb_api_client: MagicMock, |
| 224 | + mock_fetch_records_by_filter_list: MagicMock, |
143 | 225 | ):
|
144 | 226 | """Tests build_docdb_location_to_id_map"""
|
145 | 227 | bucket = "aind-ephys-data-dev-u5u0i5"
|
146 |
| - mock_docdb_api_client.retrieve_docdb_records.return_value = [ |
| 228 | + mock_fetch_records_by_filter_list.return_value = [ |
147 | 229 | {
|
148 | 230 | "_id": "70bcf356-985f-4a2a-8105-de900e35e788",
|
149 | 231 | "location": (
|
@@ -176,6 +258,16 @@ def test_build_docdb_location_to_id_map(
|
176 | 258 | ),
|
177 | 259 | }
|
178 | 260 | self.assertEqual(expected_map, actual_map)
|
| 261 | + mock_fetch_records_by_filter_list.assert_called_once_with( |
| 262 | + docdb_api_client=mock_docdb_api_client, |
| 263 | + filter_key="location", |
| 264 | + filter_values=[ |
| 265 | + f"s3://{bucket}/ecephys_655019_2000-04-04_04-00-00", |
| 266 | + f"s3://{bucket}/ecephys_567890_2000-01-01_04-00-00", |
| 267 | + f"s3://{bucket}/missing_655019_2000-01-01_01-01-02", |
| 268 | + ], |
| 269 | + projection={"_id": 1, "location": 1}, |
| 270 | + ) |
179 | 271 |
|
180 | 272 |
|
181 | 273 | if __name__ == "__main__":
|
|
0 commit comments