@@ -95,6 +95,80 @@ func (m *mockRows) Conn() *pgx.Conn {
9595
9696var _ pgx.Rows = & mockRows {}
9797
98+ func TestCollectRows (t * testing.T ) {
99+ t .Run ("collect rows without error" , func (t * testing.T ) {
100+ rows := mockRows {
101+ rows : []mockRow {
102+ {
103+ scanUser : & user {ID : 1 , Name : "test" },
104+ },
105+ },
106+ }
107+ records , err := constructpgx .CollectRows [user ](& rows , nil )
108+ require .NoError (t , err )
109+
110+ assert .Equal (t , []user {{ID : 1 , Name : "test" }}, records )
111+ assert .True (t , rows .closed )
112+ })
113+
114+ t .Run ("collect empty rows without error" , func (t * testing.T ) {
115+ rows := mockRows {
116+ rows : []mockRow {},
117+ }
118+ records , err := constructpgx .CollectRows [user ](& rows , nil )
119+ require .NoError (t , err )
120+
121+ assert .Len (t , records , 0 )
122+ assert .True (t , rows .closed )
123+ })
124+
125+ t .Run ("collect rows with initial error" , func (t * testing.T ) {
126+ rows := mockRows {}
127+ initialErr := errors .New ("some initial error" )
128+ records , err := constructpgx .CollectRows [user ](& rows , initialErr )
129+ require .ErrorIs (t , err , initialErr )
130+ assert .False (t , rows .closed )
131+
132+ assert .Empty (t , records )
133+ })
134+
135+ t .Run ("collect rows with scan error" , func (t * testing.T ) {
136+ scanErr := errors .New ("some scan error" )
137+ rows := mockRows {
138+ rows : []mockRow {
139+ {
140+ scanUser : & user {ID : 1 , Name : "test" },
141+ },
142+ {
143+ scanErr : scanErr ,
144+ },
145+ },
146+ }
147+ records , err := constructpgx .CollectRows [user ](& rows , nil )
148+ require .ErrorIs (t , err , scanErr )
149+
150+ assert .Empty (t , records )
151+ assert .True (t , rows .closed )
152+ })
153+
154+ t .Run ("collect rows with iterate error" , func (t * testing.T ) {
155+ iterateErr := errors .New ("some iterate error" )
156+ rows := mockRows {
157+ rows : []mockRow {
158+ {
159+ scanUser : & user {ID : 1 , Name : "test" },
160+ },
161+ },
162+ iterateErr : iterateErr ,
163+ }
164+ records , err := constructpgx .CollectRows [user ](& rows , nil )
165+ require .ErrorIs (t , err , iterateErr )
166+
167+ assert .Empty (t , records )
168+ assert .True (t , rows .closed )
169+ })
170+ }
171+
98172func TestScanRow (t * testing.T ) {
99173 t .Run ("scans row without error" , func (t * testing.T ) {
100174 row := mockRow {
0 commit comments