Skip to content

Commit

Permalink
Merge pull request #15584 from Budibase/remove-mocks-2
Browse files Browse the repository at this point in the history
Fix CouchDB datasource.
  • Loading branch information
samwho authored Feb 19, 2025
2 parents 07a9b88 + 57fd9e8 commit 0ecb4bf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/backend-core/src/sql/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
SqlQuery,
Table,
TableSourceType,
SEPARATOR,
} from "@budibase/types"
import { DEFAULT_BB_DATASOURCE_ID } from "../constants"
import { Knex } from "knex"
import { SEPARATOR } from "../db"
import environment from "../environment"

const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
Expand Down
21 changes: 11 additions & 10 deletions packages/server/src/integrations/couchdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ const SCHEMA: Integration = {
type: DatasourceFieldType.STRING,
required: true,
},
rev: {
type: DatasourceFieldType.STRING,
required: true,
},
},
},
},
}

class CouchDBIntegration implements IntegrationBase {
export class CouchDBIntegration implements IntegrationBase {
private readonly client: Database

constructor(config: CouchDBConfig) {
Expand All @@ -82,7 +86,8 @@ class CouchDBIntegration implements IntegrationBase {
connected: false,
}
try {
response.connected = await this.client.exists()
await this.client.allDocs({ limit: 1 })
response.connected = true
} catch (e: any) {
response.error = e.message as string
}
Expand All @@ -99,13 +104,9 @@ class CouchDBIntegration implements IntegrationBase {
}

async read(query: { json: string | object }) {
const parsed = this.parse(query)
const params = {
include_docs: true,
...parsed,
}
const params = { include_docs: true, ...this.parse(query) }
const result = await this.client.allDocs(params)
return result.rows.map(row => row.doc)
return result.rows.map(row => row.doc!)
}

async update(query: { json: string | object }) {
Expand All @@ -121,8 +122,8 @@ class CouchDBIntegration implements IntegrationBase {
return await this.client.get(query.id)
}

async delete(query: { id: string }) {
return await this.client.remove(query.id)
async delete(query: { id: string; rev: string }) {
return await this.client.remove(query.id, query.rev)
}
}

Expand Down
129 changes: 66 additions & 63 deletions packages/server/src/integrations/tests/couchdb.spec.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,87 @@
jest.mock("@budibase/backend-core", () => {
const core = jest.requireActual("@budibase/backend-core")
return {
...core,
db: {
...core.db,
DatabaseWithConnection: function () {
return {
allDocs: jest.fn().mockReturnValue({ rows: [] }),
put: jest.fn(),
get: jest.fn().mockReturnValue({ _rev: "a" }),
remove: jest.fn(),
}
},
},
}
})
import { env } from "@budibase/backend-core"
import { CouchDBIntegration } from "../couchdb"
import { generator } from "@budibase/backend-core/tests"

import { default as CouchDBIntegration } from "../couchdb"
function couchSafeID(): string {
// CouchDB IDs must start with a letter, so we prepend an 'a'.
return `a${generator.guid()}`
}

class TestConfiguration {
integration: any
function doc(data: Record<string, any>): string {
return JSON.stringify({ _id: couchSafeID(), ...data })
}

constructor(
config: any = { url: "http://somewhere", database: "something" }
) {
this.integration = new CouchDBIntegration.integration(config)
}
function query(data?: Record<string, any>): { json: string } {
return { json: doc(data || {}) }
}

describe("CouchDB Integration", () => {
let config: any
let couchdb: CouchDBIntegration

beforeEach(() => {
config = new TestConfiguration()
couchdb = new CouchDBIntegration({
url: env.COUCH_DB_URL,
database: couchSafeID(),
})
})

it("calls the create method with the correct params", async () => {
const doc = {
test: 1,
}
await config.integration.create({
json: JSON.stringify(doc),
})
expect(config.integration.client.put).toHaveBeenCalledWith(doc)
it("successfully connects", async () => {
const { connected } = await couchdb.testConnection()
expect(connected).toBe(true)
})

it("calls the read method with the correct params", async () => {
const doc = {
name: "search",
}
it("can create documents", async () => {
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
expect(id).toBeDefined()
expect(ok).toBe(true)
expect(rev).toBeDefined()
})

await config.integration.read({
json: JSON.stringify(doc),
})
it("can read created documents", async () => {
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
expect(id).toBeDefined()
expect(ok).toBe(true)
expect(rev).toBeDefined()

expect(config.integration.client.allDocs).toHaveBeenCalledWith({
include_docs: true,
name: "search",
})
const docs = await couchdb.read(query())
expect(docs).toEqual([
{
_id: id,
_rev: rev,
test: 1,
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
])
})

it("calls the update method with the correct params", async () => {
const doc = {
_id: "1234",
name: "search",
}

await config.integration.update({
json: JSON.stringify(doc),
})
it("can update documents", async () => {
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
expect(ok).toBe(true)

expect(config.integration.client.put).toHaveBeenCalledWith({
...doc,
_rev: "a",
})
const { id: newId, rev: newRev } = await couchdb.update(
query({ _id: id, _rev: rev, test: 2 })
)
const docs = await couchdb.read(query())
expect(docs).toEqual([
{
_id: newId,
_rev: newRev,
test: 2,
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
])
})

it("calls the delete method with the correct params", async () => {
const id = "1234"
await config.integration.delete({ id })
expect(config.integration.client.remove).toHaveBeenCalledWith(id)
it("can delete documents", async () => {
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
expect(ok).toBe(true)

const deleteResponse = await couchdb.delete({ id, rev })
expect(deleteResponse.ok).toBe(true)

const docs = await couchdb.read(query())
expect(docs).toBeEmpty()
})
})

0 comments on commit 0ecb4bf

Please sign in to comment.