Skip to content

Commit

Permalink
refactor: Extract normalizeFindSelector method from executeQuery
Browse files Browse the repository at this point in the history
We want to extract this code logic into its own method in order to ease
readability and testing

Also we refactored the code to make it easier to read

This replies to #1506 (comment)
  • Loading branch information
Ldoppea committed Sep 5, 2024
1 parent 29a31bc commit 6050282
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
24 changes: 5 additions & 19 deletions packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,25 +556,11 @@ class PouchLink extends CozyLink {
res = withoutDesignDocuments(res)
withRows = true
} else {
let findSelector = selector
const shouldAddId = !findSelector
if (shouldAddId) {
findSelector = {}
}
if (indexedFields) {
for (const indexedField of indexedFields) {
if (!Object.keys(findSelector).includes(indexedField)) {
findSelector[indexedField] = {
$gt: null
}
}
}
}
if (shouldAddId) {
findSelector['_id'] = {
$gt: null
}
}
const findSelector = helpers.normalizeFindSelector(
selector,
indexedFields
)

const findOpts = {
sort,
selector: findSelector,
Expand Down
2 changes: 2 additions & 0 deletions packages/cozy-pouch-link/src/CozyPouchLink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { find, allDocs, withoutDesignDocuments } from './helpers'
jest.mock('./helpers', () => ({
find: jest.fn(),
allDocs: jest.fn(),
normalizeFindSelector: jest.requireActual('./helpers').default
.normalizeFindSelector,
withoutDesignDocuments: jest.fn()
}))

Expand Down
17 changes: 17 additions & 0 deletions packages/cozy-pouch-link/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ helpers.insertBulkDocs = async (db, docs) => {
return db.bulkDocs(docs, { new_edits: false })
}

helpers.normalizeFindSelector = (selector, indexedFields) => {
let findSelector = selector || {}
if (indexedFields) {
for (const indexedField of indexedFields) {
if (!Object.keys(findSelector).includes(indexedField)) {
findSelector[indexedField] = {
$gt: null
}
}
}
}

return Object.keys(findSelector).length > 0
? findSelector
: { _id: { $gt: null } } // PouchDB does not accept empty selector
}

export default helpers
40 changes: 39 additions & 1 deletion packages/cozy-pouch-link/src/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import helpers from './helpers'
const { withoutDesignDocuments, isDeletedDocument, isDesignDocument } = helpers
const {
withoutDesignDocuments,
isDeletedDocument,
isDesignDocument,
normalizeFindSelector
} = helpers

import PouchDB from 'pouchdb-browser'
import PouchDBFind from 'pouchdb-find'
Expand Down Expand Up @@ -113,4 +118,37 @@ describe('Helpers', () => {
expect(isDeletedDocument({ _id: 'notdeleted' })).toBeFalsy()
})
})

describe('normalizeFindSelector', () => {
it('should add indexed fields in the selector if they are missing', () => {
const selector = {
SOME_FIELD: { $gt: null }
}
const indexedFields = ['SOME_INDEXED_FIELD']

const findSelector = normalizeFindSelector(selector, indexedFields)
expect(findSelector).toStrictEqual({
SOME_FIELD: { $gt: null },
SOME_INDEXED_FIELD: { $gt: null }
})
})

it('should prevent empty selector by adding a selector on _id when no selector is provided', () => {
const selector = undefined
const indexedFields = undefined

const findSelector = normalizeFindSelector(selector, indexedFields)
expect(findSelector).toStrictEqual({ _id: { $gt: null } })
})

it('should not add selector on _id when no selector is provided but there are some indexed fields', () => {
const selector = undefined
const indexedFields = ['SOME_INDEXED_FIELD']

const findSelector = normalizeFindSelector(selector, indexedFields)
expect(findSelector).toStrictEqual({
SOME_INDEXED_FIELD: { $gt: null }
})
})
})
})

0 comments on commit 6050282

Please sign in to comment.