Refactoring row test cases.

This commit is contained in:
mike12345567 2021-03-04 13:07:33 +00:00
parent 317d6633a2
commit bd00138bed
3 changed files with 55 additions and 60 deletions

View File

@ -1,66 +1,55 @@
const {
createApplication,
createTable,
supertest,
defaultHeaders,
createLinkedTable,
createAttachmentTable,
makeBasicRow,
} = require("./couchTestUtils");
const { outputProcessing } = require("../../../utilities/rowProcessor") const { outputProcessing } = require("../../../utilities/rowProcessor")
const env = require("../../../environment") const env = require("../../../environment")
const TestConfig = require("./utilities/TestConfiguration")
const { basicRow } = require("./utilities/structures")
describe("/rows", () => { describe("/rows", () => {
let request let request
let server
let appId let appId
let table let table
let row let row
let app let app
let config
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) config = new TestConfig()
request = config.request
}); })
afterAll(() => { afterAll(() => {
server.close() config.end()
}) })
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await config.init()
appId = app.instance._id appId = app.instance._id
table = await createTable(request, appId) table = await config.createTable()
row = makeBasicRow(table._id) row = basicRow(table._id)
}) })
const createRow = async r =>
await request
.post(`/api/${r ? r.tableId : row.tableId}/rows`)
.send(r || row)
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.expect(200)
const loadRow = async id => const loadRow = async id =>
await request await request
.get(`/api/${table._id}/rows/${id}`) .get(`/api/${table._id}/rows/${id}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
describe("save, load, update, delete", () => { describe("save, load, update, delete", () => {
it("returns a success message when the row is created", async () => { it("returns a success message when the row is created", async () => {
const res = await createRow() const res = await request
.post(`/api/${row.tableId}/rows`)
.send(row)
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(`${table.name} saved successfully`) expect(res.res.statusMessage).toEqual(`${table.name} saved successfully`)
expect(res.body.name).toEqual("Test Contact") expect(res.body.name).toEqual("Test Contact")
expect(res.body._rev).toBeDefined() expect(res.body._rev).toBeDefined()
}) })
it("updates a row successfully", async () => { it("updates a row successfully", async () => {
const rec = await createRow() const existing = await config.createRow()
const existing = rec.body
const res = await request const res = await request
.post(`/api/${table._id}/rows`) .post(`/api/${table._id}/rows`)
@ -70,7 +59,7 @@ describe("/rows", () => {
tableId: table._id, tableId: table._id,
name: "Updated Name", name: "Updated Name",
}) })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -79,12 +68,11 @@ describe("/rows", () => {
}) })
it("should load a row", async () => { it("should load a row", async () => {
const rec = await createRow() const existing = await config.createRow()
const existing = rec.body
const res = await request const res = await request
.get(`/api/${table._id}/rows/${existing._id}`) .get(`/api/${table._id}/rows/${existing._id}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -102,12 +90,12 @@ describe("/rows", () => {
name: "Second Contact", name: "Second Contact",
status: "new" status: "new"
} }
await createRow() await config.createRow()
await createRow(newRow) await config.createRow(newRow)
const res = await request const res = await request
.get(`/api/${table._id}/rows`) .get(`/api/${table._id}/rows`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -117,10 +105,10 @@ describe("/rows", () => {
}) })
it("load should return 404 when row does not exist", async () => { it("load should return 404 when row does not exist", async () => {
await createRow() await config.createRow()
await request await request
.get(`/api/${table._id}/rows/not-a-valid-id`) .get(`/api/${table._id}/rows/not-a-valid-id`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404) .expect(404)
}) })
@ -132,7 +120,7 @@ describe("/rows", () => {
const number = {type:"number", constraints: { type: "number", presence: false }} const number = {type:"number", constraints: { type: "number", presence: false }}
const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }} const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }}
table = await createTable(request, appId, { table = await config.createTable({
name: "TestTable2", name: "TestTable2",
type: "table", type: "table",
key: "name", key: "name",
@ -187,7 +175,7 @@ describe("/rows", () => {
attachmentEmpty : "", attachmentEmpty : "",
} }
const id = (await createRow(row)).body._id const id = (await config.createRow(row))._id
const saved = (await loadRow(id)).body const saved = (await loadRow(id)).body
@ -217,8 +205,7 @@ describe("/rows", () => {
describe("patch", () => { describe("patch", () => {
it("should update only the fields that are supplied", async () => { it("should update only the fields that are supplied", async () => {
const rec = await createRow() const existing = await config.createRow()
const existing = rec.body
const res = await request const res = await request
.patch(`/api/${table._id}/rows/${existing._id}`) .patch(`/api/${table._id}/rows/${existing._id}`)
@ -228,7 +215,7 @@ describe("/rows", () => {
tableId: table._id, tableId: table._id,
name: "Updated Name", name: "Updated Name",
}) })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -249,7 +236,7 @@ describe("/rows", () => {
const result = await request const result = await request
.post(`/api/${table._id}/rows/validate`) .post(`/api/${table._id}/rows/validate`)
.send({ name: "ivan" }) .send({ name: "ivan" })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -261,7 +248,7 @@ describe("/rows", () => {
const result = await request const result = await request
.post(`/api/${table._id}/rows/validate`) .post(`/api/${table._id}/rows/validate`)
.send({ name: 1 }) .send({ name: 1 })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -273,18 +260,18 @@ describe("/rows", () => {
describe("enrich row unit test", () => { describe("enrich row unit test", () => {
it("should allow enriching some linked rows", async () => { it("should allow enriching some linked rows", async () => {
const table = await createLinkedTable(request, appId) const table = await config.createLinkedTable()
const firstRow = (await createRow({ const firstRow = await config.createRow({
name: "Test Contact", name: "Test Contact",
description: "original description", description: "original description",
tableId: table._id tableId: table._id
})).body })
const secondRow = (await createRow({ const secondRow = await config.createRow({
name: "Test 2", name: "Test 2",
description: "og desc", description: "og desc",
link: [{_id: firstRow._id}], link: [{_id: firstRow._id}],
tableId: table._id, tableId: table._id,
})).body })
const enriched = await outputProcessing(appId, table, [secondRow]) const enriched = await outputProcessing(appId, table, [secondRow])
expect(enriched[0].link.length).toBe(1) expect(enriched[0].link.length).toBe(1)
expect(enriched[0].link[0]._id).toBe(firstRow._id) expect(enriched[0].link[0]._id).toBe(firstRow._id)
@ -293,15 +280,15 @@ describe("/rows", () => {
}) })
it("should allow enriching attachment rows", async () => { it("should allow enriching attachment rows", async () => {
const table = await createAttachmentTable(request, appId) const table = await config.createAttachmentTable()
const row = (await createRow({ const row = await config.createRow({
name: "test", name: "test",
description: "test", description: "test",
attachment: [{ attachment: [{
url: "/test/thing", url: "/test/thing",
}], }],
tableId: table._id, tableId: table._id,
})).body })
// the environment needs configured for this // the environment needs configured for this
env.CLOUD = 1 env.CLOUD = 1
env.SELF_HOSTED = 1 env.SELF_HOSTED = 1

View File

@ -94,16 +94,18 @@ class TestConfiguration {
return this.updateTable(config) return this.updateTable(config)
} }
async createLinkedTables() { async createLinkedTable() {
const table = await this.createTable() if (!this.table) {
table.primaryDisplay = "name" throw "Must have created a table first."
table.schema.link = { }
const tableConfig = basicTable()
tableConfig.primaryDisplay = "name"
tableConfig.schema.link = {
type: "link", type: "link",
fieldName: "link", fieldName: "link",
tableId: table._id, tableId: this.table._id,
} }
const linkedTable = await this.createTable(table) const linkedTable = await this.createTable(tableConfig)
this.table = table
this.linkedTable = linkedTable this.linkedTable = linkedTable
return linkedTable return linkedTable
} }

View File

@ -49,6 +49,12 @@ const TYPE_TRANSFORM_MAP = {
"": null, "": null,
[undefined]: undefined, [undefined]: undefined,
[null]: null, [null]: null,
parse: date => {
if (date instanceof Date) {
return date.toISOString()
}
return date
},
}, },
[FieldTypes.ATTACHMENT]: { [FieldTypes.ATTACHMENT]: {
"": [], "": [],