Skip to content

Commit

Permalink
adding page tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tywalch committed Jun 5, 2020
1 parent 9bef88d commit d0d624e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 195 deletions.
3 changes: 2 additions & 1 deletion src/clauses.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ let clauses = {
},
page: {
name: "page",
action(entity, state, page = "", options = {}) {
action(entity, state, page = null, options = {}) {
options.page = page;
options.pager = true;
if (entity.client === undefined) {
throw new Error("No client defined on model");
}
Expand Down
61 changes: 34 additions & 27 deletions src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { clauses } = require("./clauses");
const utilities = {
structureFacets: function (
structure,
{ index, type, name } /* istanbul ignore next */ = {},
{ index, type, name } = {},
i,
attributes,
indexSlot,
Expand Down Expand Up @@ -40,7 +40,7 @@ const utilities = {
};

class Entity {
constructor(model /* istanbul ignore next */ = {}, config /* istanbul ignore next */ = {}) {
constructor(model = {}, config = {}) {
this._validateModel(model);
this.client = config.client;
this.model = this._parseModel(model);
Expand Down Expand Up @@ -71,7 +71,7 @@ class Entity {
}
}

collection(collection /* istanbul ignore next */ = "", clauses /* istanbul ignore next */ = {}, facets /* istanbul ignore next */ = {}) {
collection(collection = "", clauses = {}, facets = {}) {
let index = this.model.translations.collections.fromCollectionToIndex[
collection
];
Expand Down Expand Up @@ -166,37 +166,42 @@ class Entity {
formatResponse(response, config = {}) {
let stackTrace = new Error();
try {
let results;

if (config.raw) {
if (response.TableName) {
// a VERY hacky way to deal with PUTs
return {};
results = {};
} else {
return response;
results = response;
}
}
} else {

let data = {};
if (response.Item) {
data = this.cleanseRetrievedData(response.Item, config);
} else if (response.Items) {
data = response.Items.map((item) =>
this.cleanseRetrievedData(item, config),
);
}
let data = {};
if (response.Item) {
data = this.cleanseRetrievedData(response.Item, config);
} else if (response.Items) {
data = response.Items.map((item) =>
this.cleanseRetrievedData(item, config),
);
}

let appliedGets;
if (Array.isArray(data)) {
appliedGets = data.map((item) =>
this.model.schema.applyAttributeGetters(item),
);
} else {
appliedGets = this.model.schema.applyAttributeGetters(data);
if (Array.isArray(data)) {
results = data.map((item) =>
this.model.schema.applyAttributeGetters(item),
);
} else {
results = this.model.schema.applyAttributeGetters(data);
}
}
if (typeof config.page === "string") {
let nextPage = response.LastEvaluatedKey || "";
return [nextPage, appliedGets]

if (config.pager) {
let nextPage = response.LastEvaluatedKey || null;
results = [nextPage, results];
}
return appliedGets;

return results;

} catch (err) {
if (config.originalErr) {
throw err;
Expand All @@ -215,7 +220,8 @@ class Entity {
originalErr: options.originalErr,
raw: options.raw,
params: options.params || {},
page: options.page
page: options.page,
pager: !!options.pager
};
let parameters = Object.assign({}, params);
for (let [name, value] of Object.entries(config.params)) {
Expand Down Expand Up @@ -513,7 +519,8 @@ class Entity {
default:
throw new Error(`Invalid method: ${method}`);
}
if (typeof options.page === "string" && options.page.length) {
// if (typeof options.page === "string" && options.page.length) {
if (Object.keys(options.page || {}).length) {
parameters.ExclusiveStartKey = options.page;
}
return parameters;
Expand Down
46 changes: 35 additions & 11 deletions test/connected.crud.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ const DynamoDB = require("aws-sdk/clients/dynamodb");
const client = new DynamoDB.DocumentClient({
region: "us-east-1",
});
const SERVICE = "BugBeater";
const ENTITY = "TEST_ENTITY"
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
let model = {
service: "BugBeater",
entity: "test",
service: SERVICE,
entity: ENTITY,
table: "electro",
version: "1",
attributes: {
Expand Down Expand Up @@ -279,8 +281,8 @@ describe("Entity", async () => {
it("Should create then delete a record", async () => {
let record = new Entity(
{
service: "testservice",
entity: "testentity",
service: SERVICE,
entity: ENTITY,
table: "electro",
version: "1",
attributes: {
Expand Down Expand Up @@ -366,7 +368,7 @@ describe("Entity", async () => {
describe("Getters/Setters", async () => {
let db = new Entity(
{
service: "testing",
service: SERVICE,
entity: uuidv4(),
table: "electro",
version: "1",
Expand Down Expand Up @@ -451,7 +453,7 @@ describe("Entity", async () => {
let entity = uuidv4();
let db = new Entity(
{
service: "testing",
service: SERVICE,
entity: entity,
table: "electro",
version: "1",
Expand Down Expand Up @@ -498,7 +500,7 @@ describe("Entity", async () => {
date,
someValue: someValue + " wham",
sk: `$${entity}#id_${id}`.toLowerCase(),
pk: `$testing_1#date_${date}`.toLowerCase(),
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(),
},
});
let updateRecord = await db
Expand All @@ -515,7 +517,7 @@ describe("Entity", async () => {
date,
someValue: someValue + " wham",
sk: `$${entity}#id_${id}`.toLowerCase(),
pk: `$testing_1#date_${date}`.toLowerCase(),
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(),
},
],
Count: 1,
Expand All @@ -528,7 +530,7 @@ describe("Entity", async () => {
someValue: "ABDEF wham bam",
__edb_e__: entity,
sk: `$${entity}#id_${id}`.toLowerCase(),
pk: `$testing_1#date_${date}`.toLowerCase(),
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(),
})
}).timeout(10000);
});
Expand Down Expand Up @@ -591,7 +593,7 @@ describe("Entity", async () => {
it("Should filter with the correct field name", async () => {
let db = new Entity(
{
service: "testing",
service: SERVICE,
entity: uuidv4(),
table: "electro",
version: "1",
Expand Down Expand Up @@ -650,7 +652,7 @@ describe("Entity", async () => {
let id = uuidv4();
let db = new Entity(
{
service: "testing",
service: SERVICE,
entity: entity,
table: "electro",
version: "1",
Expand Down Expand Up @@ -711,4 +713,26 @@ describe("Entity", async () => {
.and.to.have.deep.members(expectedMembers);
});
});
describe("Pagination", async () => {
it("Should return a pk and sk that match the last record in the result set, and should be able to be passed in for more results", async () => {
// THIS IS A FOOLISH TEST: IT ONLY FULLY WORKS WHEN THE TABLE USED FOR TESTING IS FULL OF RECORDS. THIS WILL HAVE TO DO UNTIL I HAVE TIME TO FIGURE OUT A PROPER WAY MOCK DDB.
let MallStores = new Entity(model, { client });
let results = await MallStores.scan.page(null, {raw: true});
expect(results).to.be.an("array").and.have.length(2);
// Scan may not return records, dont force a bad test then
let [index, stores] = results;
if (stores.length) {
expect(index).to.have.a.property('pk').and.to.have.a.property('sk');
expect(stores.Items).to.be.an("array")
expect(stores.Items[0]).to.have.a.property('pk').and.to.have.a.property('sk');
let [nextIndex, nextStores] = await MallStores.scan.page(index);
expect(stores).to.be.an("array").and.have.length(2);
expect(nextIndex).to.not.deep.equal(index);
expect(nextStores).to.be.an("array");
if (nextStores.length) {
expect(nextStores[0]).to.not.have.a.property('pk').and.to.not.have.a.property('sk');
}
}
}).timeout(10000);
})
});

0 comments on commit d0d624e

Please sign in to comment.