Skip to content

Commit

Permalink
chore(test): TestSelectAll fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed Aug 20, 2023
1 parent 9173ad6 commit cf64028
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
21 changes: 17 additions & 4 deletions dal/query_select_funcs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package dal

import "errors"
import (
"errors"
"math"
)

// SelectAllIDs is a helper method that for a given reader returns all IDs as a strongly typed slice.
func SelectAllIDs[T comparable](reader Reader, limit int) (ids []T, err error) {
if reader == nil {
panic("reader is a required parameter, got nil")
}
ids = make([]T, 0, limit)
for i := 0; limit <= 0 || i < limit; i++ {
if limit >= 0 {
ids = make([]T, 0, limit)
} else {
ids = make([]T, 0)
limit = math.MaxInt
}
for ; limit > 0; limit-- {
var record Record
if record, err = reader.Next(); err != nil {
if errors.Is(err, ErrNoMoreRecords) {
Expand All @@ -27,7 +35,12 @@ func SelectAllRecords(reader Reader, limit int) (records []Record, err error) {
if reader == nil {
panic("reader is a required parameter, got nil")
}
records = make([]Record, 0, limit)
if limit >= 0 {
records = make([]Record, 0, limit)
} else {
records = make([]Record, 0)
limit = math.MaxInt
}
for i := 0; limit <= 0 || i < limit; i++ {
var record Record
if record, err = reader.Next(); err != nil {
Expand Down
54 changes: 46 additions & 8 deletions dal/query_select_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestSelectAll(t *testing.T) {
type args struct {
reader Reader
reader func() Reader
limit int
}
type testCase[T comparable] struct {
Expand All @@ -18,13 +18,51 @@ func TestSelectAll(t *testing.T) {
wantIds []T
wantErr error
}

getRecordsReader := func() Reader {
return NewRecordsReader([]Record{
&record{key: &Key{ID: 1, collection: "test"}},
&record{key: &Key{ID: 2, collection: "test"}},
&record{key: &Key{ID: 3, collection: "test"}},
&record{key: &Key{ID: 4, collection: "test"}},
})
}

tests := []testCase[int]{
{name: "nil_reader", shouldPanic: true, args: args{reader: nil}},
{name: "empty_reader", args: args{reader: EmptyReader{}}, wantIds: []int{}, wantErr: nil},
{name: "nil_reader", shouldPanic: true, args: args{reader: func() Reader {
return nil
}}},
{name: "empty_reader", args: args{reader: func() Reader {
return &EmptyReader{}
}}, wantIds: []int{}, wantErr: nil},
{
name: "with_records_0_limit",
args: args{
limit: 0,
reader: getRecordsReader,
},
wantIds: []int{},
},
{
name: "with_records_limit_2",
args: args{
limit: 2,
reader: getRecordsReader,
},
wantIds: []int{1, 2},
},
{
name: "with_records_no_limit",
args: args{
reader: getRecordsReader,
limit: -1,
},
wantIds: []int{1, 2, 3, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertErr := func(err error) {
assertErr := func(t *testing.T, err error) {
if tt.wantErr == nil && err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -43,8 +81,8 @@ func TestSelectAll(t *testing.T) {
}
}()
}
gotIds, err := SelectAllIDs[int](tt.args.reader, tt.args.limit)
assertErr(err)
gotIds, err := SelectAllIDs[int](tt.args.reader(), tt.args.limit)
assertErr(t, err)
assert.Equal(t, tt.wantIds, gotIds)
})
t.Run("SelectAllRecords", func(t *testing.T) {
Expand All @@ -55,8 +93,8 @@ func TestSelectAll(t *testing.T) {
}
}()
}
gotRecords, err := SelectAllRecords(tt.args.reader, tt.args.limit)
assertErr(err)
gotRecords, err := SelectAllRecords(tt.args.reader(), tt.args.limit)
assertErr(t, err)
if err == nil {
assert.NotNil(t, gotRecords)
}
Expand Down

0 comments on commit cf64028

Please sign in to comment.