Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 100% Unit Test Coverage #31

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added index.ts
Empty file.
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ module.exports = {
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
},
},
moduleDirectories: ['node_modules', 'src'],
modulePaths: ['<rootDir>'],
setupFiles: ['<rootDir>/jest.setup.js'],
Expand Down
188 changes: 188 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"handlebars": "^4.7.8"
},
"devDependencies": {
"@types/fetch-mock": "^7.3.8",
"fetch-mock": "^9.11.0",
"node-fetch": "^2.7.0",
"@jest/globals": "^29.7.0",
"@types/node": "^20.12.12",
"husky": "^9.0.11",
Expand Down
12 changes: 7 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,18 @@ export function lessThanOrEqualNumber(a: any, b: any) {
return `${a} <= ${b}`
}

export function inValues(a: any, b: any[]) {
return `${a} IN ('${b.join("', '").replace(/'/g, "\\'")}')`
export function inValues(a: any, b: any[]): string {
const sanitizedValues = b.map(val => val.replace(/'/g, "\\'"));
return `${a} IN ('${sanitizedValues.join("', '")}')`
}

export function inNumbers(a: any, b: any[]) {
return `${a} IN (${b.join(', ')})`
}

export function notInValues(a: any, b: any[]) {
return `${a} NOT IN ('${b.join("', '").replace(/'/g, "\\'")}')`
export function notInValues(a: any, b: any[]): string {
const sanitizedValues = b.map(val => val.replace(/'/g, "\\'"));
return `${a} NOT IN ('${sanitizedValues.join("', '")}')`
}

export function notInNumbers(a: any, b: any[]) {
Expand All @@ -644,7 +646,7 @@ export function isNumber(a: any, b: any) {
return `${a} IS ${b}`
}

export function isNot(this: any, a: any, b: null) {
export function isNot(this: any, a: any, b: string | null) {
if (b === null) return `${this} IS NOT NULL`
return `${a} IS NOT ${b}`
}
Expand Down
10 changes: 3 additions & 7 deletions src/connections/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ export class CloudflareD1Connection implements Connection {
* Performs a connect action on the current Connection object.
* In this particular use case Cloudflare is a REST API and
* requires an API key for authentication.
*
* @param details - Unused in the Cloudflare scenario.
* @returns Promise<any>
*/
async connect(details: Record<string, any>): Promise<any> {
async connect(): Promise<any> {
return Promise.resolve()
}

Expand Down Expand Up @@ -68,7 +65,6 @@ export class CloudflareD1Connection implements Connection {
if (!this.accountId) throw new Error('Cloudflare account ID is not set')
if (!this.databaseId)
throw new Error('Cloudflare database ID is not set')
if (!query) throw new Error('A SQL query was not provided')

const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${this.accountId}/d1/database/${this.databaseId}/query`,
Expand All @@ -87,8 +83,8 @@ export class CloudflareD1Connection implements Connection {

let json = await response.json()
let error = null
const resultArray = (await json?.result) ?? []
const items = (await resultArray[0]?.results) ?? []
const resultArray = json?.result ?? []
const items = resultArray[0]?.results ?? []
const rawSQL = constructRawQuery(query)

return {
Expand Down
Loading
Loading