Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
unit(query): make query to sift by tags
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Dec 9, 2022
1 parent 66eacd0 commit 3bfa9c8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
48 changes: 48 additions & 0 deletions database/queries/post/sift_by_tags.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Model from "%/models/post"
import Factory from "~/factories/post"
import TagFactory from "~/factories/tag"
import PostTagFactory from "~/factories/post_tag"

import siftByTags from "./sift_by_tags"

describe("Database Pipe: Sift by tag", () => {
it("can find on specific tag", async() => {
const tag = await new TagFactory().insertOne()
const model = await new Factory().insertOne()
await new PostTagFactory()
.post(() => Promise.resolve(model))
.tag(() => Promise.resolve(tag))
.insertOne()

const options = siftByTags({}, {
"filter": {
"tagIDs": [ tag.id ]
}
})
const foundModels = await Model.findAll(options)

expect(options).toHaveProperty("include")
expect(foundModels).toHaveLength(1)
expect(foundModels).toHaveProperty("0.id", model.id)
})

it("cannot find on empty tag", async() => {
const tagA = await new TagFactory().insertOne()
const tagB = await new TagFactory().insertOne()
const model = await new Factory().insertOne()
await new PostTagFactory()
.post(() => Promise.resolve(model))
.tag(() => Promise.resolve(tagA))
.insertOne()

const options = siftByTags({}, {
"filter": {
"tagIDs": [ tagB.id ]
}
})
const foundModels = await Model.findAll(options)

expect(options).toHaveProperty("include")
expect(foundModels).toHaveLength(0)
})
})
3 changes: 2 additions & 1 deletion database/queries/post/sift_by_tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export default function<T>(
): FindOptions<T> {
const newState = { ...currentState }

const condition = new Condition()
if (constraints.filter.tagIDs !== "*") {
const condition = new Condition()

condition.isIncludedIn("id", constraints.filter.tagIDs)

if (isUndefined(newState.include)) {
Expand Down

0 comments on commit 3bfa9c8

Please sign in to comment.