Skip to content

Commit

Permalink
test: add e2e for key
Browse files Browse the repository at this point in the history
  • Loading branch information
zirkelc committed Oct 11, 2024
1 parent db7bb5e commit f3f043f
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions packages/store-s3/tests/store.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,23 @@ beforeAll(async () => {
});

const payload = {
id: randomUUID(),
foo: {
bar: [randomStringInBytes(128 * 1024), randomStringInBytes(128 * 1024)],
},
};

const mockKey = vi.fn<() => string>();
const mockKeyFn = vi.fn<() => string>();
const mockKey = () => {
const key = randomUUID();
mockKeyFn.mockReturnValueOnce(key);
return key;
};

const s3Store = new S3Store({
config,
bucket,
key: mockKey,
key: mockKeyFn,
format: "arn",
});

Expand Down Expand Up @@ -105,9 +111,38 @@ describe("S3Store", () => {
});
});

test("should generate key from payload", async () => {
const s3Store = new S3Store<typeof payload>({
config,
bucket,
key: ({ payload }) => payload.id,
format: "arn",
});

const store = middyStore({
stores: [s3Store],
storingOptions: {
minSize: 0,
},
});

const writeHandler = middy()
.use(store)
.handler(async (input: unknown) => {
return structuredClone(payload);
});

const output = await writeHandler(null, context);

expect(output).toBeDefined();
expect(output).not.toEqual(payload);
expect(output).toEqual({
[MIDDY_STORE]: `arn:aws:s3:::${bucket}/${payload.id}`,
});
});

test("should write and read full payload", async () => {
const key = randomUUID();
mockKey.mockReturnValue(key);
const key = mockKey();

const store = middyStore({
stores: [s3Store],
Expand Down Expand Up @@ -140,8 +175,7 @@ describe("S3Store", () => {
});

test("should write and read partial payload at foo.bar", async () => {
const key = randomUUID();
mockKey.mockReturnValue(key);
const key = mockKey();

const store = middyStore({
stores: [s3Store],
Expand Down Expand Up @@ -177,8 +211,7 @@ describe("S3Store", () => {
});

test("should write and read partial payload at foo.bar[0]", async () => {
const key = randomUUID();
mockKey.mockReturnValue(key);
const key = mockKey();

const store = middyStore({
stores: [s3Store],
Expand Down Expand Up @@ -217,9 +250,8 @@ describe("S3Store", () => {
});

test("should write and read multiple payloads at foo.bar[*]", async () => {
const key = randomUUID();
let index = 0;
mockKey.mockImplementation(() => `${key}-${index++}`);
const key1 = mockKey();
const key2 = mockKey();

const store = middyStore({
stores: [s3Store],
Expand Down Expand Up @@ -248,8 +280,8 @@ describe("S3Store", () => {
expect(output).toEqual({

Check failure on line 280 in packages/store-s3/tests/store.e2e.test.ts

View workflow job for this annotation

GitHub Actions / Test

tests/store.e2e.test.ts > S3Store > should write and read multiple payloads at foo.bar[*]

AssertionError: expected { …(2) } to deeply equal { Object (foo) } - Expected + Received Object { "foo": Object { "bar": Array [ Object { "@middy-store": "arn:aws:s3:::middy-store-s3/f214c4c5-dca5-4a71-bba1-6560c7bd555f", }, Object { "@middy-store": "arn:aws:s3:::middy-store-s3/89489cbe-0d9f-483d-859d-1d4dcbbc3f66", }, ], }, + "id": "b5feff0b-38f4-46b3-b609-31fe5627ea53", } ❯ tests/store.e2e.test.ts:280:18
foo: {
bar: [
{ [MIDDY_STORE]: `arn:aws:s3:::${bucket}/${key}-0` },
{ [MIDDY_STORE]: `arn:aws:s3:::${bucket}/${key}-1` },
{ [MIDDY_STORE]: `arn:aws:s3:::${bucket}/${key1}` },
{ [MIDDY_STORE]: `arn:aws:s3:::${bucket}/${key2}` },
],
},
});
Expand Down

0 comments on commit f3f043f

Please sign in to comment.