Skip to content

Commit

Permalink
Extend parameter annotatedFor on GET /mappings (#176)
Browse files Browse the repository at this point in the history
Including tests and documentation.
  • Loading branch information
stefandesu committed Jun 27, 2022
1 parent f0dc3b8 commit 2d3fbf4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ Returns an array of mappings. Each mapping has a property `uri` under which the

`annotatedBy=[uri1|uri2|...]` has annotations by user with URI(s)

`annotatedFor=[motivation]` has annotations with a certain motivation (e.g. `assessing`)
`annotatedFor=[motivation]` has annotations with a certain motivation (e.g. `assessing`); value `none` returns mappings that have no annotations at all, value `any` returns mappings that have any kind of annotation, values starting with `!` (e.g. `!assessing`) filter out annotations with that motivation

`annotatedWith=[body]` has annotations with a certian body value (e.g. `+1`)

Expand Down
12 changes: 11 additions & 1 deletion services/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,20 @@ class MappingService {
pipeline.push({ $match: query })
}
// 2. Filter by annotation, and from/to/creator is defined
else if (from || to || creator) {
else if (from || to || creator || annotatedFor === "none" || (annotatedFor || "").startsWith("!")) {
// We'll first filter the mappings, then add annotations and filter by those
const annotationQuery = {}
if (annotatedWith) {
annotationQuery["annotations.bodyValue"] = annotatedWith
}
if (annotatedFor) {
if (annotatedFor === "none") {
annotatedFor = { $exists: false }
} else if (annotatedFor === "any") {
annotatedFor = { $exists: true }
} else if (annotatedFor.startsWith("!")) {
annotatedFor = { $ne: annotatedFor.slice(1) }
}
annotationQuery["annotations.motivation"] = annotatedFor
}
if (annotatedBy) {
Expand Down Expand Up @@ -282,6 +289,9 @@ class MappingService {
annotationQuery["bodyValue"] = annotatedWith
}
if (annotatedFor) {
if (annotatedFor === "any") {
annotatedFor = { $exists: true }
}
annotationQuery["motivation"] = annotatedFor
}
if (annotatedBy) {
Expand Down
21 changes: 21 additions & 0 deletions test/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ describe("Services", () => {
assert.deepStrictEqual(result.map(r => r.uri).sort(), expected)
})

it("should get correct number of mappings when using annotatedFor param with value `any`", async () => {
const annotatedFor = "any"
const result = await services.mapping.getMappings({ limit: 10, offset: 0, annotatedFor })
const expected = _.uniq(annotations.map(a => a.target)).sort()
assert.deepStrictEqual(result.map(r => r.uri).sort(), expected)
})

it("should get correct number of mappings when using annotatedFor param with value `none`", async () => {
const annotatedFor = "none"
const result = await services.mapping.getMappings({ limit: 10, offset: 0, annotatedFor })
const expected = mappings.map(m => m.uri).filter(uri => !annotations.find(a => a.target === uri)).sort()
assert.deepStrictEqual(result.map(r => r.uri).sort(), expected)
})

it("should get correct number of mappings when using annotatedFor param with negative value", async () => {
const annotatedFor = "!assessing"
const result = await services.mapping.getMappings({ limit: 10, offset: 0, annotatedFor })
const expected = mappings.map(m => m.uri).filter(uri => !annotations.find(a => a.target === uri && a.motivation === annotatedFor.slice(1))).sort()
assert.deepStrictEqual(result.map(r => r.uri).sort(), expected)
})

it("should get correct number of mappings when using annotatedBy param", async () => {
const annotatedBy = "test:creator|other:uri"
const result = await services.mapping.getMappings({ limit: 10, offset: 0, annotatedBy })
Expand Down

0 comments on commit 2d3fbf4

Please sign in to comment.