Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example unit-testing for CouchDB map function
const testScriptType = "map"
const testScriptId = "priority"
const priorities = [
"!urgent",
null,
"!soon",
"!normal",
"!backlog",
"!wishlist",
]
const lodash = require("lodash")
const {
Loader
} = require("../../../../../src/couchdb/designdocs/loader")
const {
CouchdbConfigurator
} = require("../../../../../src/couchdb/configurator")
const {
withEmptyDbHandle
} = require("../../../../util/dbFixture")
const {
defaultTaskFactory,
mergeReturnedObjects,
} = require("../../../../util/fake")
const loader = new Loader()
function mapFunctionUsing(emit) {
return loader.evalScript(testScriptType, testScriptId, { emit })
}
function createEmitSpy() {
const emit = jest.fn()
const map = mapFunctionUsing(emit)
return { emit, map }
}
function taskWithoutTags() {
return {
type:"task"
}
}
function taskWithTags(...tagIds) {
return {
type:"task",
tagIds
}
}
describe("Priority map generates keys", () => {
describe("Lower priority tags emit higher priority keys", () => {
test("!urgent is key 0", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!urgent"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(0)
})
test("missing priority is key 1", () => {
const { emit, map } = createEmitSpy()
map(taskWithoutTags())
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(1)
})
test("!soon is key 2", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!soon"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(2)
})
test("!normal is key 3", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!normal"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(3)
})
test("!backlog is key 4", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!backlog"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(4)
})
test("!wishlist is key 5", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!urgent"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(0)
})
test("Invalid priority tag is equivalent to missing priority", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!not-a-real-priority"))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(1)
})
test("Multiple priority tags do not emit multiple times ", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags("!wishlist", "!wishlist"))
expect(emit).toHaveBeenCalledTimes(1)
})
test("Multiple priority tags emit only most urgent key", () => {
const { emit, map } = createEmitSpy()
map(taskWithTags(...lodash.shuffle(["!urgent", "!soon", "!normal", "!backlog", "!wishlist"])))
expect(emit).toHaveBeenCalledTimes(1)
expect(emit).toHaveBeenLastCalledWith(0)
})
})
})
describe("Couchdb 'priority' view filtering and ordering", () => {
test("View ascending orders high priority first", withEmptyDbHandle(async dbHandle => {
const configurator = new CouchdbConfigurator(dbHandle)
await configurator.insertDesignDocs()
//create one task with each possible priority tag (including null)
let tasks = priorities.map(priorityTagId => mergeReturnedObjects(
defaultTaskFactory,
() => (priorityTagId !== null ? {
tagIds: [priorityTagId]
} : null)
))
//store intended order
const randomTaskIds = tasks.map(task => task.id)
//randomise task order
tasks = lodash.shuffle(tasks)
//insert tasks to db
for (const document of tasks) {
await dbHandle.insert({
_id:document.id,
...document
})
}
//check filtering,ordering of tasks in priority db view
const orderedDocuments = await dbHandle.view("lowban", "priority")
expect(orderedDocuments.rows).toStrictEqual([
{ id: randomTaskIds[0], key: 0, value: null },
{ id: randomTaskIds[1], key: 1, value: null },
{ id: randomTaskIds[2], key: 2, value: null },
{ id: randomTaskIds[3], key: 3, value: null },
{ id: randomTaskIds[4], key: 4, value: null },
{ id: randomTaskIds[5], key: 5, value: null },
])
}))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.