|
1 | 1 | import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src"; |
2 | 2 | import { DynamoDB, paginateScan, ScanCommandInput } from "@aws-sdk/client-dynamodb"; |
| 3 | +import { BatchGetCommandInput, paginateBatchGet } from "@aws-sdk/lib-dynamodb"; |
3 | 4 | import { HttpResponse } from "@smithy/protocol-http"; |
4 | 5 | import { describe, expect, test as it } from "vitest"; |
5 | 6 |
|
@@ -94,4 +95,136 @@ describe("pagination", () => { |
94 | 95 | }); |
95 | 96 | expect.assertions(7); |
96 | 97 | }); |
| 98 | + |
| 99 | + it("processes batch items until all items are processed or an error is received", async () => { |
| 100 | + const ddb = new DynamoDB({ |
| 101 | + credentials: { |
| 102 | + accessKeyId: "INTEG_TEST", |
| 103 | + secretAccessKey: "INTEG_TEST", |
| 104 | + }, |
| 105 | + region: "us-west-2", |
| 106 | + }); |
| 107 | + |
| 108 | + requireRequestsFrom(ddb) |
| 109 | + .toMatch( |
| 110 | + // first page request |
| 111 | + { |
| 112 | + hostname: /dynamodb/, |
| 113 | + body(b) { |
| 114 | + expect(b).toContain( |
| 115 | + '"RequestItems":{"test":{"Keys":[{"id":{"S":"1"}},{"id":{"S":"2"}},{"id":{"S":"3"}},{"id":{"S":"4"}},{"id":{"S":"5"}}]}}' |
| 116 | + ); |
| 117 | + }, |
| 118 | + }, |
| 119 | + // second page request |
| 120 | + { |
| 121 | + hostname: /dynamodb/, |
| 122 | + body(b) { |
| 123 | + expect(b).toContain('"RequestItems":{"test":{"Keys":[{"id":{"S":"4"}},{"id":{"S":"3"}}]}}'); |
| 124 | + }, |
| 125 | + }, |
| 126 | + // invalid request (duplicate key) |
| 127 | + { |
| 128 | + hostname: /dynamodb/, |
| 129 | + body(b) { |
| 130 | + expect(b).toContain('"RequestItems":{"test":{"Keys":[{"id":{"S":"1"}},{"id":{"S":"1"}}]}}'); |
| 131 | + }, |
| 132 | + } |
| 133 | + ) |
| 134 | + .respondWith( |
| 135 | + // first page response |
| 136 | + new HttpResponse({ |
| 137 | + statusCode: 200, |
| 138 | + headers: {}, |
| 139 | + body: Buffer.from( |
| 140 | + JSON.stringify({ |
| 141 | + Responses: { |
| 142 | + test: [ |
| 143 | + { id: { S: "2" }, name: { S: "Item 2" } }, |
| 144 | + { id: { S: "1" }, name: { S: "Item 1" } }, |
| 145 | + ], |
| 146 | + }, |
| 147 | + UnprocessedKeys: { |
| 148 | + test: { |
| 149 | + Keys: [{ id: { S: "4" } }, { id: { S: "3" } }], |
| 150 | + }, |
| 151 | + }, |
| 152 | + }) |
| 153 | + ), |
| 154 | + }), |
| 155 | + // second page response |
| 156 | + new HttpResponse({ |
| 157 | + statusCode: 200, |
| 158 | + headers: {}, |
| 159 | + body: Buffer.from( |
| 160 | + JSON.stringify({ |
| 161 | + Responses: { |
| 162 | + test: [ |
| 163 | + { id: { S: "3" }, name: { S: "Item 3" } }, |
| 164 | + { id: { S: "4" }, name: { S: "Item 4" } }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + UnprocessedKeys: {}, |
| 168 | + }) |
| 169 | + ), |
| 170 | + }), |
| 171 | + // error response |
| 172 | + new HttpResponse({ |
| 173 | + statusCode: 400, |
| 174 | + headers: {}, |
| 175 | + body: Buffer.from( |
| 176 | + JSON.stringify({ |
| 177 | + message: "Provided list of item keys contains duplicates", |
| 178 | + }) |
| 179 | + ), |
| 180 | + }) |
| 181 | + ); |
| 182 | + |
| 183 | + const requestParams: BatchGetCommandInput = { |
| 184 | + RequestItems: { |
| 185 | + test: { Keys: [{ id: "1" }, { id: "2" }, { id: "3" }, { id: "4" }, { id: "5" }] }, |
| 186 | + }, |
| 187 | + }; |
| 188 | + |
| 189 | + let pages = 0; |
| 190 | + for await (const page of paginateBatchGet({ client: ddb }, requestParams)) { |
| 191 | + pages += 1; |
| 192 | + if (pages === 1) { |
| 193 | + expect(page.Responses?.test).toEqual([ |
| 194 | + { id: "2", name: "Item 2" }, |
| 195 | + { id: "1", name: "Item 1" }, |
| 196 | + ]); |
| 197 | + } else { |
| 198 | + expect(page.Responses?.test).toEqual([ |
| 199 | + { id: "3", name: "Item 3" }, |
| 200 | + { id: "4", name: "Item 4" }, |
| 201 | + ]); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + expect(pages).toEqual(2); |
| 206 | + |
| 207 | + let thrownError; |
| 208 | + |
| 209 | + try { |
| 210 | + for await (const page of paginateBatchGet( |
| 211 | + { client: ddb }, |
| 212 | + { |
| 213 | + RequestItems: { |
| 214 | + test: { Keys: [{ id: "1" }, { id: "1" }] }, |
| 215 | + }, |
| 216 | + } |
| 217 | + )) { |
| 218 | + void page; |
| 219 | + throw new Error("Received unexpected page"); |
| 220 | + } |
| 221 | + } catch (error) { |
| 222 | + thrownError = error; |
| 223 | + } |
| 224 | + |
| 225 | + expect(thrownError).toBeInstanceOf(Error); |
| 226 | + expect((thrownError as Error).message).toBe("Provided list of item keys contains duplicates"); |
| 227 | + |
| 228 | + expect.assertions(11); |
| 229 | + }); |
97 | 230 | }); |
0 commit comments