|
| 1 | +package catalogapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + "github.com/opdev/productctl/internal/catalogapi" |
| 10 | +) |
| 11 | + |
| 12 | +func mockData(size uint) []int { |
| 13 | + d := make([]int, size) |
| 14 | + for i := range d { |
| 15 | + // value is 1-based, to align with the size input. |
| 16 | + d[i] = i + 1 |
| 17 | + } |
| 18 | + |
| 19 | + return d |
| 20 | +} |
| 21 | + |
| 22 | +func mockSuccessfulPaginatedQueryWithInput(inputData []int) func(page, pageSize int) ([]int, int, error) { |
| 23 | + // maximum number of "queries" allowed. |
| 24 | + max := 8 |
| 25 | + counter := 0 |
| 26 | + return func(page, pageSize int) ([]int, int, error) { |
| 27 | + if counter > max { |
| 28 | + return nil, -1, errors.New("TESTING CONSTRAINT REACHED: Max query count reached in mock function - Adjust unit tests to use less queries") |
| 29 | + } |
| 30 | + defer func() { counter++ }() |
| 31 | + start := (page - 1) * pageSize |
| 32 | + // account for start and end points that are outside of inputData bounds. |
| 33 | + if start > len(inputData) { |
| 34 | + return []int{}, 0, nil |
| 35 | + } |
| 36 | + end := start + pageSize |
| 37 | + if end > len(inputData) { |
| 38 | + end = len(inputData) |
| 39 | + } |
| 40 | + return inputData[start:end], len(inputData), nil |
| 41 | + |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +var _ = Describe("Paging", func() { |
| 46 | + var ( |
| 47 | + ctx context.Context |
| 48 | + ) |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + ctx = context.TODO() |
| 52 | + }) |
| 53 | + |
| 54 | + When("querying all records", func() { |
| 55 | + var ( |
| 56 | + inputData []int |
| 57 | + startingPage int |
| 58 | + pageSize int |
| 59 | + ) |
| 60 | + |
| 61 | + BeforeEach(func() { |
| 62 | + inputData = mockData(5) |
| 63 | + startingPage = 1 |
| 64 | + pageSize = catalogapi.DefaultPageSize |
| 65 | + }) |
| 66 | + |
| 67 | + When("the query function returns an error", func() { |
| 68 | + It("should return library errors and the provided error", func() { |
| 69 | + returnedErr := errors.New("query error") |
| 70 | + _, err := catalogapi.QueryAll( |
| 71 | + ctx, |
| 72 | + startingPage, |
| 73 | + pageSize, |
| 74 | + func(page, pageSize int) (returnedItems []int, totalItems int, queryError error) { |
| 75 | + return nil, 0, returnedErr |
| 76 | + }, |
| 77 | + ) |
| 78 | + Expect(err).To(MatchError(returnedErr)) |
| 79 | + Expect(err).To(MatchError(catalogapi.ErrQueryPageFailed)) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + When("you start at page 0", func() { |
| 84 | + BeforeEach(func() { |
| 85 | + startingPage = 1 |
| 86 | + }) |
| 87 | + |
| 88 | + It("should query all records", func() { |
| 89 | + records, err := catalogapi.QueryAll( |
| 90 | + ctx, |
| 91 | + startingPage, |
| 92 | + pageSize, |
| 93 | + mockSuccessfulPaginatedQueryWithInput(inputData), |
| 94 | + ) |
| 95 | + Expect(err).ToNot(HaveOccurred()) |
| 96 | + Expect(records).To(BeEquivalentTo(inputData)) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + When("you start at a specific page", func() { |
| 101 | + |
| 102 | + BeforeEach(func() { |
| 103 | + inputData = mockData(3) |
| 104 | + startingPage = 2 |
| 105 | + // pageSize out of scope, but adjusted to make this assertion easier. |
| 106 | + pageSize = 1 |
| 107 | + }) |
| 108 | + |
| 109 | + It("should only contain the records from that point forward", func() { |
| 110 | + records, err := catalogapi.QueryAll( |
| 111 | + ctx, |
| 112 | + startingPage, |
| 113 | + pageSize, |
| 114 | + mockSuccessfulPaginatedQueryWithInput(inputData), |
| 115 | + ) |
| 116 | + Expect(err).ToNot(HaveOccurred()) |
| 117 | + Expect(records).To(BeEquivalentTo(inputData[(startingPage-1)*pageSize:])) |
| 118 | + }) |
| 119 | + }) |
| 120 | + When("you define a page size", func() { |
| 121 | + BeforeEach(func() { |
| 122 | + inputData = mockData(5) |
| 123 | + startingPage = 1 |
| 124 | + pageSize = 1 |
| 125 | + }) |
| 126 | + It("should use the expected number of queries to query all records", func() { |
| 127 | + totalQueryCount := 0 |
| 128 | + _, err := catalogapi.QueryAll( |
| 129 | + ctx, |
| 130 | + startingPage, |
| 131 | + pageSize, |
| 132 | + func(page, pageSize int) (returnedItems []int, totalItems int, queryError error) { |
| 133 | + totalQueryCount++ |
| 134 | + return mockSuccessfulPaginatedQueryWithInput(inputData)(page, pageSize) |
| 135 | + }, |
| 136 | + ) |
| 137 | + Expect(err).ToNot(HaveOccurred()) |
| 138 | + Expect(totalQueryCount).To(BeEquivalentTo(len(inputData))) |
| 139 | + |
| 140 | + }) |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments