Added some unit tests for the enrichment process of rows, in the process found some issues with linking a table to itself, so fixed those so that we can do that in the future if desired.

This commit is contained in:
mike12345567 2021-02-02 11:46:10 +00:00
parent c724f3e701
commit e0ea434f27
7 changed files with 137 additions and 59 deletions

View File

@ -9,9 +9,7 @@ const {
ViewNames, ViewNames,
} = require("../../db/utils") } = require("../../db/utils")
const usersController = require("./user") const usersController = require("./user")
const { coerceRowValues } = require("../../utilities") const { coerceRowValues, enrichRows } = require("../../utilities")
const env = require("../../environment")
const { OBJ_STORE_DIRECTORY } = require("../../constants")
const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}` const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}`
@ -53,32 +51,6 @@ async function findRow(db, appId, tableId, rowId) {
return row return row
} }
/**
* This function "enriches" the input rows with anything they are supposed to contain, for example
* link records or attachment links.
*/
async function enrichRows(appId, table, rows) {
// attach any linked row information
const enriched = await linkRows.attachLinkInfo(appId, rows)
// update the attachments URL depending on hosting
if (env.CLOUD && env.SELF_HOSTED) {
for (let [property, column] of Object.entries(table.schema)) {
if (column.type === "attachment") {
for (let row of enriched) {
if (row[property] == null || row[property].length === 0) {
continue
}
row[property].forEach(attachment => {
attachment.url = `${OBJ_STORE_DIRECTORY}/${appId}/${attachment.url}`
attachment.url = attachment.url.replace("//", "/")
})
}
}
}
}
return enriched
}
exports.patch = async function(ctx) { exports.patch = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const db = new CouchDB(appId) const db = new CouchDB(appId)
@ -249,12 +221,12 @@ exports.fetchTableRows = async function(ctx) {
const db = new CouchDB(appId) const db = new CouchDB(appId)
// special case for users, fetch through the user controller // special case for users, fetch through the user controller
let rows, table = await db.get(ctx.params.tableId) let rows,
table = await db.get(ctx.params.tableId)
if (ctx.params.tableId === ViewNames.USERS) { if (ctx.params.tableId === ViewNames.USERS) {
await usersController.fetch(ctx) await usersController.fetch(ctx)
rows = ctx.body rows = ctx.body
} else { } else {
const response = await db.allDocs( const response = await db.allDocs(
getRowParams(ctx.params.tableId, null, { getRowParams(ctx.params.tableId, null, {
include_docs: true, include_docs: true,
@ -359,7 +331,7 @@ exports.fetchEnrichedRow = async function(ctx) {
let linkedRows = await enrichRows( let linkedRows = await enrichRows(
appId, appId,
table, table,
response.rows.map(row => row.doc), response.rows.map(row => row.doc)
) )
// insert the link rows in the correct place throughout the main row // insert the link rows in the correct place throughout the main row
for (let fieldName of Object.keys(table.schema)) { for (let fieldName of Object.keys(table.schema)) {

View File

@ -108,7 +108,7 @@ exports.save = async function(ctx) {
} }
// update linked rows // update linked rows
await linkRows.updateLinks({ const linkResp = await linkRows.updateLinks({
appId, appId,
eventType: oldTable eventType: oldTable
? linkRows.EventType.TABLE_UPDATED ? linkRows.EventType.TABLE_UPDATED
@ -116,6 +116,9 @@ exports.save = async function(ctx) {
table: tableToSave, table: tableToSave,
oldTable: oldTable, oldTable: oldTable,
}) })
if (linkResp != null && linkResp._rev) {
tableToSave._rev = linkResp._rev
}
// don't perform any updates until relationships have been // don't perform any updates until relationships have been
// checked by the updateLinks function // checked by the updateLinks function

View File

@ -4,6 +4,7 @@ const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const packageJson = require("../../../../package") const packageJson = require("../../../../package")
const jwt = require("jsonwebtoken") const jwt = require("jsonwebtoken")
const env = require("../../../environment") const env = require("../../../environment")
const { cloneDeep } = require("lodash/fp")
const TEST_CLIENT_ID = "test-client-id" const TEST_CLIENT_ID = "test-client-id"
@ -37,11 +38,7 @@ exports.defaultHeaders = appId => {
return headers return headers
} }
exports.createTable = async (request, appId, table) => { exports.BASE_TABLE = {
if (table != null && table._id) {
delete table._id
}
table = table || {
name: "TestTable", name: "TestTable",
type: "table", type: "table",
key: "name", key: "name",
@ -61,6 +58,9 @@ exports.createTable = async (request, appId, table) => {
}, },
} }
exports.createTable = async (request, appId, table) => {
table = table || exports.BASE_TABLE
const res = await request const res = await request
.post(`/api/tables`) .post(`/api/tables`)
.set(exports.defaultHeaders(appId)) .set(exports.defaultHeaders(appId))
@ -68,6 +68,25 @@ exports.createTable = async (request, appId, table) => {
return res.body return res.body
} }
exports.createLinkedTable = async (request, appId) => {
// get the ID to link to
const table = await exports.createTable(request, appId)
table.schema.link = {
type: "link",
fieldName: "link",
tableId: table._id,
}
return exports.createTable(request, appId, table)
}
exports.createAttachmentTable = async (request, appId) => {
const table = await exports.createTable(request, appId)
table.schema.attachment = {
type: "attachment",
}
return exports.createTable(request, appId, table)
}
exports.getAllFromTable = async (request, appId, tableId) => { exports.getAllFromTable = async (request, appId, tableId) => {
const res = await request const res = await request
.get(`/api/${tableId}/rows`) .get(`/api/${tableId}/rows`)

View File

@ -3,7 +3,11 @@ const {
createTable, createTable,
supertest, supertest,
defaultHeaders, defaultHeaders,
createLinkedTable,
createAttachmentTable,
} = require("./couchTestUtils"); } = require("./couchTestUtils");
const { enrichRows } = require("../../../utilities")
const env = require("../../../environment")
describe("/rows", () => { describe("/rows", () => {
let request let request
@ -270,4 +274,44 @@ describe("/rows", () => {
}) })
}) })
describe("enrich row unit test", () => {
it("should allow enriching some linked rows", async () => {
const table = await createLinkedTable(request, appId)
const firstRow = (await createRow({
name: "Test Contact",
description: "original description",
tableId: table._id
})).body
const secondRow = (await createRow({
name: "Test 2",
description: "og desc",
link: [firstRow._id],
tableId: table._id,
})).body
const enriched = await enrichRows(appId, table, [secondRow])
expect(enriched[0].link.length).toBe(1)
expect(enriched[0].link[0]).toBe(firstRow._id)
})
})
it("should allow enriching attachment rows", async () => {
const table = await createAttachmentTable(request, appId)
const row = (await createRow({
name: "test",
description: "test",
attachment: [{
url: "/test/thing",
}],
tableId: table._id,
})).body
// the environment needs configured for this
env.CLOUD = 1
env.SELF_HOSTED = 1
const enriched = await enrichRows(appId, table, [row])
expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${appId}/test/thing`)
// remove env config
env.CLOUD = undefined
env.SELF_HOSTED = undefined
})
}) })

View File

@ -252,7 +252,11 @@ class LinkController {
tableId: table._id, tableId: table._id,
fieldName: fieldName, fieldName: fieldName,
} }
await this._db.put(linkedTable) const response = await this._db.put(linkedTable)
// special case for when linking back to self, make sure rev updated
if (linkedTable._id === table._id) {
table._rev = response.rev
}
} }
} }
return table return table

View File

@ -31,12 +31,15 @@ exports.createLinkView = async appId => {
thisId: doc1.rowId, thisId: doc1.rowId,
fieldName: doc1.fieldName, fieldName: doc1.fieldName,
}) })
// if linking to same table can't emit twice
if (doc1.tableId !== doc2.tableId) {
emit([doc2.tableId, doc2.rowId], { emit([doc2.tableId, doc2.rowId], {
id: doc1.rowId, id: doc1.rowId,
thisId: doc2.rowId, thisId: doc2.rowId,
fieldName: doc2.fieldName, fieldName: doc2.fieldName,
}) })
} }
}
}.toString(), }.toString(),
} }
designDoc.views = { designDoc.views = {

View File

@ -3,6 +3,8 @@ const { DocumentTypes, SEPARATOR } = require("../db/utils")
const fs = require("fs") const fs = require("fs")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
const CouchDB = require("../db") const CouchDB = require("../db")
const { OBJ_STORE_DIRECTORY } = require("../constants")
const linkRows = require("../db/linkedRows")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -211,3 +213,34 @@ exports.getAllApps = async () => {
.map(({ value }) => value) .map(({ value }) => value)
} }
} }
/**
* This function "enriches" the input rows with anything they are supposed to contain, for example
* link records or attachment links.
* @param {string} appId the ID of the application for which rows are being enriched.
* @param {object} table the table from which these rows came from originally, this is used to determine
* the schema of the rows and then enrich.
* @param {object[]} rows the rows which are to be enriched.
* @returns {object[]} the enriched rows will be returned.
*/
exports.enrichRows = async (appId, table, rows) => {
// attach any linked row information
const enriched = await linkRows.attachLinkInfo(appId, rows)
// update the attachments URL depending on hosting
if (env.CLOUD && env.SELF_HOSTED) {
for (let [property, column] of Object.entries(table.schema)) {
if (column.type === "attachment") {
for (let row of enriched) {
if (row[property] == null || row[property].length === 0) {
continue
}
row[property].forEach(attachment => {
attachment.url = `${OBJ_STORE_DIRECTORY}/${appId}/${attachment.url}`
attachment.url = attachment.url.replace("//", "/")
})
}
}
}
}
return enriched
}