1919]
2020
2121_TEST_SIERRA_IDS_RESPONSE = [
22- (1 , "b1" , 11 , 12 , "aa" ), (2 , "b2" , 21 , 22 , "bb" ),
23- (3 , "b3" , 31 , 32 , "cc" ), (33 , "b3" , 331 , 332 , "ccc" ),
24- (4 , None , None , None , None ), (5 , "b5" , 51 , 52 , "dd" ),
25- (6 , "b6" , 61 , 62 , "ee" ), (6 , "b66" , 61 , 62 , "ee" ),
26- (7 , "b7" , 71 , 72 , "ff" ), (7 , "b77" , 771 , 772 , "ffff" ),
27- (None , "b4" , None , None , None ), (5 , "b5" , 51 , 52 , "dd" ),
22+ (1 , "1" , " b1" , 11 , 12 , "aa" ), (2 , "2" , "b2" , 21 , 22 , "bb" ),
23+ (3 , "3" , " b3" , 31 , 32 , "cc" ), (33 , "3" , "b3" , 331 , 332 , "ccc" ),
24+ (4 , None , None , None , None , None ), (5 , "5" , "b5" , 51 , 52 , "dd" ),
25+ (6 , "6" , " b6" , 61 , 62 , "ee" ), (6 , "6" , "b66" , 61 , 62 , "ee" ),
26+ (7 , "7" , " b7" , 71 , 72 , "ff" ), (7 , "77" , "b77" , 771 , 772 , "ffff" ),
27+ (None , "4" , " b4" , None , None , None ), (5 , "5" , "b5" , 51 , 52 , "dd" ),
2828]
2929
3030_TEST_BARCODE_DF = pd .DataFrame (
31- [[f"b{ i } " , str (i )] for i in range (1 , 10 )],
31+ [[f"b{ i } " , str (i )] for i in range (1 , 11 )],
3232 columns = ["barcode" , "patron_id" ])
3333_TEST_BARCODE_DF ["patron_id" ] = _TEST_BARCODE_DF ["patron_id" ].astype ("string" )
3434
3535_TEST_ID_DF = pd .DataFrame (
36- [["1" , "b1" , 11 , 12 , "aa" ], # one perfect match
37- ["2" , "b5" , 21 , 22 , "bb" ], # different id and barcode matches
36+ [["1" , "1" , " b1" , 11 , 12 , "aa" ], # one perfect match
37+ ["2" , "2" , " b5" , 21 , 22 , "bb" ], # different id and barcode matches
3838 # no match for patron id 3
39- ["4" , "b4" , 41 , 42 , "dd" ], # two matches -- one perfect, one imperfect
40- ["4" , "b444" , 43 , 44 , "dddd" ],
41- ["5" , "b555" , 51 , 52 , "eeee" ], # two matches -- both imperfect
42- ["5" , "b556" , 53 , 54 , "eeef" ],
43- ["6" , "b6" , 61 , 62 , "ffff" ], # two matches -- both perfect
44- ["6" , "b6" , 63 , 64 , "fffg" ],
45- ["7" , "b777" , 71 , 72 , "gg" ], # two matches -- same except barcode
46- ["7" , "b778" , 71 , 72 , "gg" ],
47- ["8" , None , 81 , 82 , "hh" ], # one imperfect match/no barcode
48- ["9" , "b9" , None , None , None ]], # one perfect match/all null fields
49- columns = ["patron_id" , "barcode" , "ptype_code" , "pcode3" ,
39+ ["4" , "4" , "b4" , 41 , 42 , "dd" ], # two matches -- perfect and imperfect
40+ ["4" , "4" , "b444" , 43 , 44 , "dddd" ],
41+ ["5" , "5" , "b555" , 51 , 52 , "eeee" ], # two matches -- both imperfect
42+ ["5" , "5" , "b556" , 53 , 54 , "eeef" ],
43+ ["6" , "6" , "b6" , 61 , 62 , "ffff" ], # two matches -- both perfect
44+ ["6" , "6" , "b6" , 63 , 64 , "fffg" ],
45+ ["7" , "7" , "b777" , 71 , 72 , "gg" ], # two matches -- same but barcode
46+ ["7" , "7" , "b778" , 71 , 72 , "gg" ],
47+ ["8" , "88" , "b8" , 81 , 82 , "hh" ], # two matches -- same but record_num
48+ ["8" , "89" , "b8" , 81 , 82 , "hh" ],
49+ ["9" , "9" , None , 91 , 92 , "ii" ], # one match/no barcode
50+ ["10" , "10" , "b10" , None , None , None ]], # one match/all null fields
51+ columns = ["patron_id" , "record_num" , "barcode" , "ptype_code" , "pcode3" ,
5052 "patron_home_library_code" ])
51- _TEST_ID_DF ["patron_id" ] = _TEST_ID_DF ["patron_id" ].astype ("string" )
53+ _TEST_ID_DF [["patron_id" , "record_num" ]] = _TEST_ID_DF [
54+ ["patron_id" , "record_num" ]].astype ("string" )
5255
5356
5457class TestPatronDataHelper :
@@ -108,16 +111,20 @@ def test_barcodes_to_patron_ids_with_duplicates(self, mocker):
108111 remove_duplicates = False
109112 ))
110113
111- def test_get_sierra_patron_data_from_ids (self , mocker ):
114+ def test_get_sierra_patron_data_from_ids_pat_ids (self , mocker ):
112115 RESULT = pd .DataFrame (
113- [["1" , "b1" , 11 , 12 , "aa" ], ["2" , "b2" , 21 , 22 , "bb" ],
114- ["3" , "b3" , 31 , 32 , "cc" ], ["33" , "b3" , 331 , 332 , "ccc" ],
115- ["4" , None , None , None , None ],
116- ["5" , "b5" , 51 , 52 , "dd" ], ["6" , "b6" , 61 , 62 , "ee" ],
117- ["6" , "b66" , 61 , 62 , "ee" ], ["7" , "b7" , 71 , 72 , "ff" ],
118- ["7" , "b77" , 771 , 772 , "ffff" ]],
119- columns = ["patron_id" , "barcode" , "ptype_code" , "pcode3" ,
120- "patron_home_library_code" ])
116+ [["1" , "1" , "b1" , 11 , 12 , "aa" ],
117+ ["2" , "2" , "b2" , 21 , 22 , "bb" ],
118+ ["3" , "3" , "b3" , 31 , 32 , "cc" ],
119+ ["33" , "3" , "b3" , 331 , 332 , "ccc" ],
120+ ["4" , None , None , None , None , None ],
121+ ["5" , "5" , "b5" , 51 , 52 , "dd" ],
122+ ["6" , "6" , "b6" , 61 , 62 , "ee" ],
123+ ["6" , "6" , "b66" , 61 , 62 , "ee" ],
124+ ["7" , "7" , "b7" , 71 , 72 , "ff" ],
125+ ["7" , "77" , "b77" , 771 , 772 , "ffff" ]],
126+ columns = ["patron_id" , "record_num" , "barcode" , "ptype_code" ,
127+ "pcode3" , "patron_home_library_code" ])
121128 RESULT ["patron_id" ] = RESULT ["patron_id" ].astype ("string" )
122129
123130 mock_sierra_client = mocker .MagicMock ()
@@ -137,7 +144,48 @@ def test_get_sierra_patron_data_from_ids(self, mocker):
137144 # directly. The workaround is to test the total length of the query
138145 # plus that each id appears in it.
139146 query = mock_sierra_client .execute_query .call_args [0 ][0 ]
140- assert len (query ) == 257
147+ assert len (query ) == 269
148+ assert "WHERE id IN" in query
149+ for el in range (1 , 9 ):
150+ assert str (el ) in query
151+
152+ def test_get_sierra_patron_data_from_ids_record_nums (self , mocker ):
153+ RESULT = pd .DataFrame (
154+ [["1" , "1" , "b1" , 11. , 12. , "aa" ],
155+ ["2" , "2" , "b2" , 21. , 22. , "bb" ],
156+ ["3" , "3" , "b3" , 31. , 32. , "cc" ],
157+ ["33" , "3" , "b3" , 331. , 332. , "ccc" ],
158+ ["5" , "5" , "b5" , 51. , 52. , "dd" ],
159+ ["6" , "6" , "b6" , 61. , 62. , "ee" ],
160+ ["6" , "6" , "b66" , 61. , 62. , "ee" ],
161+ ["7" , "7" , "b7" , 71. , 72. , "ff" ],
162+ ["7" , "77" , "b77" , 771. , 772. , "ffff" ]],
163+ columns = ["patron_id" , "record_num" , "barcode" , "ptype_code" ,
164+ "pcode3" , "patron_home_library_code" ],
165+ index = [0 , 1 , 2 , 3 , 5 , 6 , 7 , 8 , 9 ])
166+ RESULT [["patron_id" , "record_num" ]] = RESULT [
167+ ["patron_id" , "record_num" ]].astype ("string" )
168+
169+ mock_sierra_client = mocker .MagicMock ()
170+ mock_sierra_client .execute_query .return_value = \
171+ _TEST_SIERRA_IDS_RESPONSE
172+
173+ assert_frame_equal (
174+ RESULT , get_sierra_patron_data_from_ids (
175+ mock_sierra_client , [str (el ) for el in range (1 , 9 )] + ["1" ,],
176+ use_record_num = True ,
177+ ))
178+
179+ mock_sierra_client .connect .assert_called_once ()
180+ mock_sierra_client .execute_query .assert_called_once ()
181+ mock_sierra_client .close_connection .assert_called_once ()
182+
183+ # Because the set of record_nums is unordered, it can't be tested
184+ # directly. The workaround is to test the total length of the query
185+ # plus that each id appears in it.
186+ query = mock_sierra_client .execute_query .call_args [0 ][0 ]
187+ assert len (query ) == 277
188+ assert "WHERE record_num IN" in query
141189 for el in range (1 , 9 ):
142190 assert str (el ) in query
143191
@@ -152,14 +200,18 @@ def test_get_sierra_patron_data_from_ids_unisolated(self, mocker):
152200 mock_sierra_client .execute_query .assert_called_once ()
153201 mock_sierra_client .close_connection .assert_not_called ()
154202
155- def test_get_sierra_patron_data_from_ids_without_duplicates (self , mocker ):
203+ def test_get_sierra_patron_data_from_ids_without_duplicates_pat_ids (
204+ self , mocker ):
156205 RESULT = pd .DataFrame (
157- [["1" , "b1" , 11 , 12 , "aa" ], ["2" , "b2" , 21 , 22 , "bb" ],
158- ["3" , "b3" , 31 , 32 , "cc" ], ["33" , "b3" , 331 , 332 , "ccc" ],
159- ["4" , None , None , None , None ], ["5" , "b5" , 51 , 52 , "dd" ],
160- ["6" , "b6" , 61 , 62 , "ee" ]],
161- columns = ["patron_id" , "barcode" , "ptype_code" , "pcode3" ,
162- "patron_home_library_code" ])
206+ [["1" , "1" , "b1" , 11 , 12 , "aa" ],
207+ ["2" , "2" , "b2" , 21 , 22 , "bb" ],
208+ ["3" , "3" , "b3" , 31 , 32 , "cc" ],
209+ ["33" , "3" , "b3" , 331 , 332 , "ccc" ],
210+ ["4" , None , None , None , None , None ],
211+ ["5" , "5" , "b5" , 51 , 52 , "dd" ],
212+ ["6" , "6" , "b6" , 61 , 62 , "ee" ]],
213+ columns = ["patron_id" , "record_num" , "barcode" , "ptype_code" ,
214+ "pcode3" , "patron_home_library_code" ])
163215 RESULT ["patron_id" ] = RESULT ["patron_id" ].astype ("string" )
164216
165217 mock_sierra_client = mocker .MagicMock ()
@@ -172,22 +224,49 @@ def test_get_sierra_patron_data_from_ids_without_duplicates(self, mocker):
172224 remove_duplicates = True
173225 ))
174226
227+ def test_get_sierra_patron_data_from_ids_without_duplicates_record_nums (
228+ self , mocker ):
229+ RESULT = pd .DataFrame (
230+ [["1" , "1" , "b1" , 11. , 12. , "aa" ],
231+ ["2" , "2" , "b2" , 21. , 22. , "bb" ],
232+ ["5" , "5" , "b5" , 51. , 52. , "dd" ],
233+ ["6" , "6" , "b6" , 61. , 62. , "ee" ],
234+ ["7" , "7" , "b7" , 71. , 72. , "ff" ],
235+ ["7" , "77" , "b77" , 771. , 772. , "ffff" ]],
236+ columns = ["patron_id" , "record_num" , "barcode" , "ptype_code" ,
237+ "pcode3" , "patron_home_library_code" ],
238+ index = [0 , 1 , 5 , 6 , 8 , 9 ])
239+ RESULT [["patron_id" , "record_num" ]] = RESULT [
240+ ["patron_id" , "record_num" ]].astype ("string" )
241+
242+ mock_sierra_client = mocker .MagicMock ()
243+ mock_sierra_client .execute_query .return_value = \
244+ _TEST_SIERRA_IDS_RESPONSE
245+
246+ assert_frame_equal (
247+ RESULT , get_sierra_patron_data_from_ids (
248+ mock_sierra_client , [str (el ) for el in range (1 , 9 )],
249+ remove_duplicates = True , use_record_num = True ,
250+ ))
251+
175252 def test_get_sierra_patron_data_from_barcodes (self , mocker ):
176253 RESULT = pd .DataFrame (
177- [["b1" , "1" , 11 , 12 , "aa" ],
178- ["b4" , "4" , 41 , 42 , "dd" ],
179- ["b6" , "6" , None , None , None ],
180- ["b9" , "9" , None , None , None ],
181- ["b2" , "2" , 21 , 22 , "bb" ],
182- ["b3" , "3" , None , None , None ],
183- ["b5" , "5" , None , None , None ],
184- ["b7" , "7" , 71 , 72 , "gg" ],
185- ["b8" , "8" , 81 , 82 , "hh" ]],
186- columns = ["barcode" , "patron_id" , "ptype_code" , "pcode3" ,
187- "patron_home_library_code" ])
188- RESULT ["patron_id" ] = RESULT ["patron_id" ].astype ("string" )
189- TEST_BARCODES = [f"b{ i } " for i in range (1 , 11 )] + ["b1" ,]
190- TEST_IDS = pd .Series ([str (i ) for i in range (1 , 10 )],
254+ [["b1" , "1" , "1" , 11 , 12 , "aa" ],
255+ ["b4" , "4" , "4" , 41 , 42 , "dd" ],
256+ ["b6" , "6" , None , None , None , None ],
257+ ["b8" , "8" , "88" , 81 , 82 , "hh" ],
258+ ["b10" , "10" , "10" , None , None , None ],
259+ ["b2" , "2" , "2" , 21 , 22 , "bb" ],
260+ ["b3" , "3" , None , None , None , None ],
261+ ["b5" , "5" , None , None , None , None ],
262+ ["b7" , "7" , "7" , 71 , 72 , "gg" ],
263+ ["b9" , "9" , "9" , 91 , 92 , "ii" ]],
264+ columns = ["barcode" , "patron_id" , "record_num" , "ptype_code" ,
265+ "pcode3" , "patron_home_library_code" ])
266+ RESULT [["patron_id" , "record_num" ]] = RESULT [
267+ ["patron_id" , "record_num" ]].astype ("string" )
268+ TEST_BARCODES = [f"b{ i } " for i in range (1 , 12 )] + ["b1" ,]
269+ TEST_IDS = pd .Series ([str (i ) for i in range (1 , 11 )],
191270 dtype = "string" , name = "patron_id" )
192271 mocked_barcodes_method = mocker .patch (
193272 "nypl_py_utils.functions.patron_data_helper.barcodes_to_patron_ids" , # noqa: E501
0 commit comments