@@ -3,6 +3,7 @@ package handler
33import (
44 "context"
55 "errors"
6+ "strings"
67 "testing"
78
89 "github.com/indexdata/crosslink/broker/common"
@@ -160,16 +161,21 @@ func (r *MockIllRepositoryNoSelectedSupplier) GetSelectedSupplierForIllTransacti
160161// configurable results for duplicate-check testing.
161162type mockDuplicateCheckRepo struct {
162163 mocks.MockIllRepositorySuccess
163- duplicateId string
164- err error
165- called bool
166- params ill_db. FindDuplicateIllTransactionParams
164+ duplicate bool
165+ err error
166+ called bool
167+ cql string
167168}
168169
169- func (r * mockDuplicateCheckRepo ) FindDuplicateIllTransaction (ctx common.ExtendedContext , params ill_db.FindDuplicateIllTransactionParams ) (string , error ) {
170+ func (r * mockDuplicateCheckRepo ) ListIllTransactions (ctx common.ExtendedContext , params ill_db.ListIllTransactionsParams , cql * string , symbols []string ) ([]ill_db.IllTransaction , int64 , error ) {
171+ if cql != nil {
172+ r .cql = * cql
173+ }
170174 r .called = true
171- r .params = params
172- return r .duplicateId , r .err
175+ if r .duplicate {
176+ return []ill_db.IllTransaction {{ID : "duplicate-id" }}, 1 , nil
177+ }
178+ return []ill_db.IllTransaction {}, 0 , r .err
173179}
174180
175181func TestCheckDuplicateRequest (t * testing.T ) {
@@ -207,12 +213,11 @@ func TestCheckDuplicateRequest(t *testing.T) {
207213 name string
208214 request * iso18626.Request
209215 peer ill_db.Peer
210- duplicateId string
216+ duplicate bool
211217 repoErr error
212218 wantErr error
213219 wantRepoCalled bool
214220 wantPatronId string
215- wantWindowHrs int32
216221 wantIdentifier string
217222 wantIsbn string
218223 wantIssn string
@@ -248,7 +253,6 @@ func TestCheckDuplicateRequest(t *testing.T) {
248253 wantErr : nil ,
249254 wantRepoCalled : true ,
250255 wantPatronId : "patron-1" ,
251- wantWindowHrs : 1 ,
252256 wantIdentifier : "rec-1" ,
253257 wantTitle : "Test Title" ,
254258 wantSvcType : "Loan" ,
@@ -261,7 +265,6 @@ func TestCheckDuplicateRequest(t *testing.T) {
261265 wantErr : nil ,
262266 wantRepoCalled : true ,
263267 wantPatronId : "patron-1" ,
264- wantWindowHrs : 1 ,
265268 wantIdentifier : "rec-1" ,
266269 wantTitle : "Test Title" ,
267270 wantSvcType : "Loan" ,
@@ -270,11 +273,10 @@ func TestCheckDuplicateRequest(t *testing.T) {
270273 name : "duplicate found - returns ErrDuplicateRequest" ,
271274 request : baseRequest ,
272275 peer : ill_db.Peer {CustomData : directory.Entry {DuplicateCheckWindowHours : & window1 }},
273- duplicateId : "existing-tx-id" ,
276+ duplicate : true ,
274277 wantErr : ErrDuplicateRequest ,
275278 wantRepoCalled : true ,
276279 wantPatronId : "patron-1" ,
277- wantWindowHrs : 1 ,
278280 wantIdentifier : "rec-1" ,
279281 wantTitle : "Test Title" ,
280282 wantSvcType : "Loan" ,
@@ -290,6 +292,22 @@ func TestCheckDuplicateRequest(t *testing.T) {
290292 wantErr : nil ,
291293 wantRepoCalled : false ,
292294 },
295+ {
296+ name : "no service level - skips duplicate check (can't verify same patron)" ,
297+ request : & iso18626.Request {
298+ BibliographicInfo : iso18626.BibliographicInfo {
299+ SupplierUniqueRecordId : "rec-1" ,
300+ Title : "Test Title" ,
301+ },
302+ PatronInfo : & iso18626.PatronInfo {
303+ PatronId : "patron-1" ,
304+ },
305+ },
306+ peer : ill_db.Peer {CustomData : directory.Entry {DuplicateCheckWindowHours : & window1 }},
307+ repoErr : pgx .ErrNoRows ,
308+ wantErr : nil ,
309+ wantRepoCalled : false ,
310+ },
293311 {
294312 name : "isbn passed as parameter to DB query" ,
295313 request : isbnRequest ,
@@ -298,19 +316,28 @@ func TestCheckDuplicateRequest(t *testing.T) {
298316 wantErr : nil ,
299317 wantRepoCalled : true ,
300318 wantPatronId : "patron-2" ,
301- wantWindowHrs : 1 ,
302319 wantIsbn : "978-1234" ,
303320 wantSvcType : "Copy" ,
304321 },
305322 {
306323 name : "duplicate found via isbn - returns ErrDuplicateRequest" ,
307324 request : isbnRequest ,
308325 peer : ill_db.Peer {CustomData : directory.Entry {DuplicateCheckWindowHours : & window1 }},
309- duplicateId : "existing-tx-isbn" ,
326+ duplicate : true ,
310327 wantErr : ErrDuplicateRequest ,
311328 wantRepoCalled : true ,
312329 wantPatronId : "patron-2" ,
313- wantWindowHrs : 1 ,
330+ wantIsbn : "978-1234" ,
331+ wantSvcType : "Copy" ,
332+ },
333+ {
334+ name : "no duplicate - returns nil" ,
335+ request : isbnRequest ,
336+ peer : ill_db.Peer {CustomData : directory.Entry {DuplicateCheckWindowHours : & window1 }},
337+ duplicate : false ,
338+ wantErr : nil ,
339+ wantRepoCalled : true ,
340+ wantPatronId : "patron-2" ,
314341 wantIsbn : "978-1234" ,
315342 wantSvcType : "Copy" ,
316343 },
@@ -320,21 +347,19 @@ func TestCheckDuplicateRequest(t *testing.T) {
320347 t .Run (tt .name , func (t * testing.T ) {
321348 appCtx := common .CreateExtCtxWithArgs (context .Background (), nil )
322349 mockRepo := & mockDuplicateCheckRepo {
323- duplicateId : tt .duplicateId ,
324- err : tt .repoErr ,
350+ duplicate : tt .duplicate ,
351+ err : tt .repoErr ,
325352 }
326353 err := checkDuplicateRequest (appCtx , tt .request , mockRepo , "ISIL:REQ1" , tt .peer )
327354 assert .Equal (t , tt .wantErr , err )
328355 assert .Equal (t , tt .wantRepoCalled , mockRepo .called )
329356 if tt .wantRepoCalled {
330- assert .Equal (t , "ISIL:REQ1" , mockRepo .params .RequesterSymbol .String )
331- assert .Equal (t , tt .wantPatronId , mockRepo .params .PatronID )
332- assert .Equal (t , tt .wantWindowHrs , mockRepo .params .Hours )
333- assert .Equal (t , tt .wantIdentifier , mockRepo .params .Identifier )
334- assert .Equal (t , tt .wantIsbn , mockRepo .params .Isbn )
335- assert .Equal (t , tt .wantIssn , mockRepo .params .Issn )
336- assert .Equal (t , tt .wantTitle , mockRepo .params .Title )
337- assert .Equal (t , tt .wantSvcType , mockRepo .params .ServiceType )
357+ assert .True (t , tt .wantPatronId == "" || strings .Contains (mockRepo .cql , tt .wantPatronId ))
358+ assert .True (t , tt .wantIdentifier == "" || strings .Contains (mockRepo .cql , tt .wantIdentifier ))
359+ assert .True (t , tt .wantIsbn == "" || strings .Contains (mockRepo .cql , tt .wantIsbn ))
360+ assert .True (t , tt .wantIssn == "" || strings .Contains (mockRepo .cql , tt .wantIssn ))
361+ assert .True (t , tt .wantTitle == "" || strings .Contains (mockRepo .cql , tt .wantTitle ))
362+ assert .True (t , tt .wantSvcType == "" || strings .Contains (mockRepo .cql , tt .wantSvcType ))
338363 }
339364 })
340365 }
0 commit comments