Skip to content

Commit

Permalink
Add more tests for reset script (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 26, 2020
1 parent ac227b0 commit eb93a64
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
66 changes: 65 additions & 1 deletion test/import-reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,62 @@ describe("Import and Reset Script", () => {

describe("Reset Script", () => {

it("should clear the database", async () => {
it("should abort reset script when given no as input", async () => {
try {
await exec("echo no | NODE_ENV=test ./bin/reset.js")
assert.fail("Expected reset script to abort.")
} catch (error) {
assert(error.stdout.includes("Is that okay?"))
assert(error.stderr.includes("Error: Aborting"))
}
})

const type = "annotations"
it(`should delete ${type}`, async () => {
await exec(`yes | NODE_ENV=test ./bin/reset.js -t ${type}`)
for (let collection of ["concepts", "mappings", "terminologies", "annotations", "concordances"]) {
const result = await server.db.collection(collection).find({}).toArray()
if (collection == type) {
assert.strictEqual(result.length, 0)
} else {
assert.notStrictEqual(result.length, 0)
}
}
})

it("should delete concepts from certain scheme", async () => {
const scheme = "http://dewey.info/scheme/edition/e23/"
try {
await exec(`echo no | NODE_ENV=test ./bin/reset.js -s ${scheme}`)
assert.fail("Expected exec to fail.")
} catch (error) {
assert(error.stdout.includes(`from scheme ${scheme}`))
assert(error.stdout.includes("concepts will be deleted"))
}
})

it("should delete mappings from certain concordance", async () => {
const concordance = "http://coli-conc.gbv.de/concordances/ddc_rvk_medizin"
try {
await exec(`echo no | NODE_ENV=test ./bin/reset.js -c ${concordance}`)
assert.fail("Expected exec to fail.")
} catch (error) {
assert(error.stdout.includes(`from concordance ${concordance}`))
assert(error.stdout.includes("mappings will be deleted"))
}
})

it("should delete entities with specific URIs", async () => {
let results
results = await server.db.collection("concepts").find({}).toArray()
const lengthBefore = results.length
const uris = results.map(r => r.uri).slice(0, 2)
await exec(`yes | NODE_ENV=test ./bin/reset.js ${uris.join(" ")}`)
results = await server.db.collection("concepts").find({}).toArray()
assert.strictEqual(results.length, lengthBefore - uris.length)
})

it("should clear the whole database", async () => {
// Clear database
await exec("yes | NODE_ENV=test ./bin/reset.js")
for (let collection of ["concepts", "mappings", "terminologies"]) {
Expand All @@ -159,6 +214,15 @@ describe("Import and Reset Script", () => {
}
})

it("should fail if trying to clear the database a second time", async () => {
try {
await exec("yes | NODE_ENV=test ./bin/reset.js")
assert.fail("Expected reset script to fail if there are no entities to delete.")
} catch (error) {
// Ignore error
}
})

})

})
4 changes: 3 additions & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ const cpexec = require("child_process").exec
*/
async function exec(command, options) {
return new Promise((resolve, reject) => {
cpexec(command, options || {}, (error, stdout) => {
cpexec(command, options || {}, (error, stdout, stderr) => {
if (error) {
error.stdout = stdout
error.stderr = stderr
return reject(error)
}
resolve(stdout)
Expand Down

0 comments on commit eb93a64

Please # to comment.