@@ -2,6 +2,7 @@ package api
22
33import (
44 "net/http"
5+ "net/http/httptest"
56 "strings"
67 "testing"
78
@@ -25,3 +26,76 @@ func TestGetSymbolForRequest(t *testing.T) {
2526 assert .Equal (t , "tenant mapping must be specified" , err .Error ())
2627 assert .Equal (t , "" , resolved )
2728}
29+
30+ func TestCollectAboutDataLastLink (t * testing.T ) {
31+ reqOffset0 := httptest .NewRequest ("GET" , "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=0" , nil )
32+ reqOffset10 := httptest .NewRequest ("GET" , "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=10" , nil )
33+ reqOffset20 := httptest .NewRequest ("GET" , "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=20" , nil )
34+ reqOffset1000 := httptest .NewRequest ("GET" , "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=1000" , nil )
35+
36+ // First page (count=21, limit=10, offset=0): prevLink omitted, next/last present.
37+ about := CollectAboutData (21 , 0 , 10 , reqOffset0 )
38+ assert .Equal (t , int64 (21 ), about .Count )
39+ assert .Nil (t , about .FirstLink )
40+ assert .Nil (t , about .PrevLink )
41+ assert .NotNil (t , about .NextLink )
42+ assert .Contains (t , * about .NextLink , "offset=10" )
43+ assert .Contains (t , * about .NextLink , "symbol=ISIL%3ADK-BIB1" )
44+ assert .NotNil (t , about .LastLink )
45+ assert .Contains (t , * about .LastLink , "offset=20" )
46+ assert .Contains (t , * about .LastLink , "symbol=ISIL%3ADK-BIB1" )
47+
48+ // Not last page (count=21, limit=10, offset=10): all links present
49+ about = CollectAboutData (21 , 10 , 10 , reqOffset10 )
50+ assert .Equal (t , int64 (21 ), about .Count )
51+ assert .NotNil (t , about .FirstLink )
52+ assert .Contains (t , * about .FirstLink , "offset=0" )
53+ assert .Contains (t , * about .FirstLink , "symbol=ISIL%3ADK-BIB1" )
54+ assert .NotNil (t , about .PrevLink )
55+ assert .Contains (t , * about .PrevLink , "offset=0" )
56+ assert .Contains (t , * about .PrevLink , "symbol=ISIL%3ADK-BIB1" )
57+ assert .NotNil (t , about .NextLink )
58+ assert .Contains (t , * about .NextLink , "offset=20" )
59+ assert .Contains (t , * about .NextLink , "symbol=ISIL%3ADK-BIB1" )
60+ assert .NotNil (t , about .LastLink )
61+ assert .Contains (t , * about .LastLink , "offset=20" )
62+ assert .Contains (t , * about .LastLink , "symbol=ISIL%3ADK-BIB1" )
63+
64+ // Last page (count=20, limit=10, offset=10): lastLink and nextLink should be omitted.
65+ about = CollectAboutData (20 , 10 , 10 , reqOffset10 )
66+ assert .Equal (t , int64 (20 ), about .Count )
67+ assert .NotNil (t , about .FirstLink )
68+ assert .Contains (t , * about .FirstLink , "offset=0" )
69+ assert .Contains (t , * about .FirstLink , "symbol=ISIL%3ADK-BIB1" )
70+ assert .NotNil (t , about .PrevLink )
71+ assert .Contains (t , * about .PrevLink , "offset=0" )
72+ assert .Contains (t , * about .PrevLink , "symbol=ISIL%3ADK-BIB1" )
73+ assert .Nil (t , about .NextLink )
74+ assert .Nil (t , about .LastLink )
75+
76+ // Last partial page (count=21, limit=10, offset=20): lastLink and nextLink should be omitted.
77+ about = CollectAboutData (21 , 20 , 10 , reqOffset20 )
78+ assert .Equal (t , int64 (21 ), about .Count )
79+ assert .NotNil (t , about .FirstLink )
80+ assert .Contains (t , * about .FirstLink , "offset=0" )
81+ assert .Contains (t , * about .FirstLink , "symbol=ISIL%3ADK-BIB1" )
82+ assert .NotNil (t , about .PrevLink )
83+ assert .Contains (t , * about .PrevLink , "offset=10" )
84+ assert .Contains (t , * about .PrevLink , "symbol=ISIL%3ADK-BIB1" )
85+ assert .Nil (t , about .NextLink )
86+ assert .Nil (t , about .LastLink )
87+
88+ // Out-of-range page (count=21, limit=10, offset=1000): prevLink and lastLink should be present.
89+ about = CollectAboutData (21 , 1000 , 10 , reqOffset1000 )
90+ assert .Equal (t , int64 (21 ), about .Count )
91+ assert .NotNil (t , about .FirstLink )
92+ assert .Contains (t , * about .FirstLink , "offset=0" )
93+ assert .Contains (t , * about .FirstLink , "symbol=ISIL%3ADK-BIB1" )
94+ assert .NotNil (t , about .PrevLink )
95+ assert .Contains (t , * about .PrevLink , "offset=20" )
96+ assert .Contains (t , * about .PrevLink , "symbol=ISIL%3ADK-BIB1" )
97+ assert .Nil (t , about .NextLink )
98+ assert .NotNil (t , about .LastLink )
99+ assert .Contains (t , * about .LastLink , "offset=20" )
100+ assert .Contains (t , * about .LastLink , "symbol=ISIL%3ADK-BIB1" )
101+ }
0 commit comments