Skip to content

Commit

Permalink
test(indexDocuments): test and fix impl #35
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Jun 14, 2023
1 parent a5d7a2f commit b9658b8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export function adapter({
name: string
fields: string[] | { [field: string]: 'ASC' | 'DESC' }[]
}) {
meta
return meta
.get(db)
.chain(() =>
$database(db)
Expand Down
62 changes: 60 additions & 2 deletions test/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,69 @@ async (
})

await t.step('indexDocuments', async (t) => {
await t.step('should create the index', async () => {})
await t.step('should create the index', async () => {
await a.createDatabase(DB)

await a.createDocument({
db: DB,
id: '10-caddyshack',
doc: { title: 'Caddyshack', year: '1978', genre: ['comedy'] },
})

await a.createDocument({
db: DB,
id: '12-ghostbusters',
doc: { title: 'Ghostbusters', year: '1980', genre: ['comedy'] },
})

const res = await a.indexDocuments({
db: DB,
name: 'idx-title-year',
fields: ['title', 'year'],
})

assertObjectMatch(res as any, { ok: true })

await a.removeDatabase(DB)
})

await t.step('should create the index with obj fields', async () => {
await a.createDatabase(DB)

await a.createDocument({
db: DB,
id: '10-caddyshack',
doc: { title: 'Caddyshack', year: '1978', genre: ['comedy'] },
})

await a.createDocument({
db: DB,
id: '12-ghostbusters',
doc: { title: 'Ghostbusters', year: '1980', genre: ['comedy'] },
})

const res = await a.indexDocuments({
db: DB,
name: 'idx-title-year',
fields: [{ title: 'DESC' }, { year: 'ASC' }],
})

assertObjectMatch(res as any, { ok: true })

await a.removeDatabase(DB)
})

await t.step(
'should return a HyperErr(404) if the database does not exist',
async () => {},
async () => {
const res = await a.indexDocuments({
db: DB,
name: 'idx-title-year',
fields: [{ title: 'DESC' }, { year: 'ASC' }],
})

assertObjectMatch(res as any, { ok: false, status: 404 })
},
)
})

Expand Down
6 changes: 6 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const queryOptions = ({
fields?: string[]
sort?: string[] | { [field: string]: 'ASC' | 'DESC' }[]
}) => {
/**
* Notice use_index is not mapped here, as MongoDB
* internally chooses an index to use.
*
* So use_index is effectively ignored
*/
const options: {
limit?: number
projection?: { [field: string]: 0 | 1 }
Expand Down

0 comments on commit b9658b8

Please sign in to comment.