From f7144b9c165ed91d831157d03c557401137cdef0 Mon Sep 17 00:00:00 2001 From: Jonathan Gramain Date: Thu, 14 Dec 2023 14:06:48 -0800 Subject: [PATCH] ARSN-377 fix DelimiterNonCurrent and add a unit test --- lib/algos/list/delimiterNonCurrent.js | 7 +- .../algos/list/delimiterNonCurrent.spec.js | 119 +++++++++++++++++- 2 files changed, 119 insertions(+), 7 deletions(-) diff --git a/lib/algos/list/delimiterNonCurrent.js b/lib/algos/list/delimiterNonCurrent.js index 4376b98fa..7819abd71 100644 --- a/lib/algos/list/delimiterNonCurrent.js +++ b/lib/algos/list/delimiterNonCurrent.js @@ -52,9 +52,8 @@ class DelimiterNonCurrent extends DelimiterVersions { // Overwrite keyHandler_SkippingVersions to include the last version from the previous listing. // The creation (last-modified) date of this version will be the stale date for the following version. // eslint-disable-next-line camelcase - keyHandler_SkippingVersions(key, value) { - const { key: nonversionedKey, versionId } = this.parseKey(key); - if (nonversionedKey === this.keyMarker) { + keyHandler_SkippingVersions(key, versionId, value) { + if (key === this.keyMarker) { // since the nonversioned key equals the marker, there is // necessarily a versionId in this key const _versionId = versionId; @@ -66,7 +65,7 @@ class DelimiterNonCurrent extends DelimiterVersions { this.setState({ id: 1 /* NotSkipping */, }); - return this.handleKey(key, value); + return this.handleKey(key, versionId, value); } filter(obj) { diff --git a/tests/unit/algos/list/delimiterNonCurrent.spec.js b/tests/unit/algos/list/delimiterNonCurrent.spec.js index 029ed6e80..83a4a6533 100644 --- a/tests/unit/algos/list/delimiterNonCurrent.spec.js +++ b/tests/unit/algos/list/delimiterNonCurrent.spec.js @@ -55,15 +55,15 @@ function getListingKey(key, vFormat) { let expectedParams; if (v === 'v0') { - expectedParams = { gte: `premark${VID_SEP}`, lt: 'prf' }; + expectedParams = { gte: `${keyMarker}${VID_SEP}`, lt: 'prf' }; } else { expectedParams = [ { - gte: `${DbPrefixes.Master}premark${VID_SEP}`, + gte: `${DbPrefixes.Master}${keyMarker}${VID_SEP}`, lt: `${DbPrefixes.Master}prf`, }, { - gte: `${DbPrefixes.Version}premark${VID_SEP}`, + gte: `${DbPrefixes.Version}${keyMarker}${VID_SEP}`, lt: `${DbPrefixes.Version}prf`, }, ]; @@ -389,5 +389,118 @@ function getListingKey(key, vFormat) { assert.deepStrictEqual(delimiter.result(), expectedResult); }); + + it('should end filtering if max keys reached', () => { + const delimiter = new DelimiterNonCurrent({ maxKeys: 1 }, fakeLogger, v); + + const masterKey = 'key'; + + // filter delete marker + const versionId1 = 'version1'; + const versionKey1 = `${masterKey}${VID_SEP}${versionId1}`; + const date1 = '1970-01-01T00:00:00.002Z'; + const value1 = `{"versionId":"${versionId1}", "last-modified": "${date1}", "isDeleteMarker": true}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey1, v), + value: value1, + }), FILTER_ACCEPT); + + // filter second version + const versionId2 = 'version2'; + const versionKey2 = `${masterKey}${VID_SEP}${versionId2}`; + const date2 = '1970-01-01T00:00:00.001Z'; + const value2 = `{"versionId":"${versionId2}", "last-modified": "${date2}"}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey2, v), + value: value2, + }), FILTER_ACCEPT); + + // filter third version + const versionId3 = 'version3'; + const versionKey3 = `${masterKey}${VID_SEP}${versionId3}`; + const date3 = '1970-01-01T00:00:00.000Z'; + const value3 = `{"versionId":"${versionId3}", "last-modified": "${date3}"}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey3, v), + value: value3, + }), FILTER_END); + + + const expectedResult = { + Contents: [ + { + key: masterKey, + value: `{"versionId":"${versionId2}","last-modified":"${date2}","staleDate":"${date1}"}`, + }, + ], + IsTruncated: true, + NextKeyMarker: masterKey, + NextVersionIdMarker: versionId2, + }; + + assert.deepStrictEqual(delimiter.result(), expectedResult); + }); + + it('should return noncurrent versions starting from a marker', () => { + const delimiter = new DelimiterNonCurrent({ + keyMarker: 'key', + versionIdMarker: 'version1', + }, fakeLogger, v); + + const masterKey = 'key'; + + // filter first version + const versionId1 = 'version1'; + const versionKey1 = `${masterKey}${VID_SEP}${versionId1}`; + const date1 = '1970-01-01T00:00:00.002Z'; + const value1 = `{"versionId":"${versionId1}", "last-modified": "${date1}"}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey1, v), + value: value1, + }), FILTER_ACCEPT); + + // filter second version + const versionId2 = 'version2'; + const versionKey2 = `${masterKey}${VID_SEP}${versionId2}`; + const date2 = '1970-01-01T00:00:00.001Z'; + const value2 = `{"versionId":"${versionId2}", "last-modified": "${date2}"}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey2, v), + value: value2, + }), FILTER_ACCEPT); + + // filter third version + const versionId3 = 'version3'; + const versionKey3 = `${masterKey}${VID_SEP}${versionId3}`; + const date3 = '1970-01-01T00:00:00.000Z'; + const value3 = `{"versionId":"${versionId3}", "last-modified": "${date3}"}`; + + assert.strictEqual(delimiter.filter({ + key: getListingKey(versionKey3, v), + value: value3, + }), FILTER_ACCEPT); + + + const expectedResult = { + Contents: [ + { + key: masterKey, + value: `{"versionId":"${versionId2}","last-modified":"${date2}","staleDate":"${date1}"}`, + }, + { + key: masterKey, + value: `{"versionId":"${versionId3}","last-modified":"${date3}","staleDate":"${date2}"}`, + }, + ], + IsTruncated: false, + }; + + assert.deepStrictEqual(delimiter.result(), expectedResult); + }); }); });