firebase tests complete
This commit is contained in:
parent
a91cf354b6
commit
97db271fce
|
@ -0,0 +1,36 @@
|
|||
module FirebaseMock {
|
||||
const firebase: any = {}
|
||||
|
||||
firebase.Firestore = function () {
|
||||
this.get = jest.fn(() => [
|
||||
{
|
||||
data: jest.fn(() => ({ result: "test" })),
|
||||
},
|
||||
])
|
||||
|
||||
this.update = jest.fn()
|
||||
this.set = jest.fn()
|
||||
this.delete = jest.fn()
|
||||
|
||||
this.doc = jest.fn(() => ({
|
||||
update: this.update,
|
||||
set: this.set,
|
||||
delete: this.delete,
|
||||
get: jest.fn(() => ({
|
||||
data: jest.fn(() => ({ result: "test" })),
|
||||
})),
|
||||
id: "test_id",
|
||||
}))
|
||||
|
||||
this.where = jest.fn(() => ({
|
||||
get: this.get,
|
||||
}))
|
||||
|
||||
this.collection = jest.fn(() => ({
|
||||
doc: this.doc,
|
||||
where: this.where,
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = firebase
|
||||
}
|
|
@ -92,13 +92,13 @@ module Firebase {
|
|||
|
||||
class FirebaseIntegration implements IntegrationBase {
|
||||
private config: FirebaseConfig
|
||||
private db: Firestore
|
||||
private client: Firestore
|
||||
|
||||
constructor(config: FirebaseConfig) {
|
||||
this.config = config
|
||||
if (config.serviceAccount) {
|
||||
const serviceAccount = JSON.parse(config.serviceAccount)
|
||||
this.db = new Firestore({
|
||||
this.client = new Firestore({
|
||||
projectId: serviceAccount.project_id,
|
||||
credentials: {
|
||||
client_email: serviceAccount.client_email,
|
||||
|
@ -106,7 +106,7 @@ module Firebase {
|
|||
},
|
||||
})
|
||||
} else {
|
||||
this.db = new Firestore({
|
||||
this.client = new Firestore({
|
||||
projectId: config.projectId,
|
||||
credentials: {
|
||||
client_email: config.email,
|
||||
|
@ -118,7 +118,7 @@ module Firebase {
|
|||
|
||||
async create(query: { json: object; extra: { [key: string]: string } }) {
|
||||
try {
|
||||
const documentReference = this.db
|
||||
const documentReference = this.client
|
||||
.collection(query.extra.collection)
|
||||
.doc()
|
||||
await documentReference.set({ ...query.json, id: documentReference.id })
|
||||
|
@ -133,7 +133,7 @@ module Firebase {
|
|||
async read(query: { json: object; extra: { [key: string]: string } }) {
|
||||
try {
|
||||
let snapshot
|
||||
const collectionRef = this.db.collection(query.extra.collection)
|
||||
const collectionRef = this.client.collection(query.extra.collection)
|
||||
if (
|
||||
query.extra.filterField &&
|
||||
query.extra.filter &&
|
||||
|
@ -164,19 +164,19 @@ module Firebase {
|
|||
extra: { [key: string]: string }
|
||||
}) {
|
||||
try {
|
||||
await this.db
|
||||
await this.client
|
||||
.collection(query.extra.collection)
|
||||
.doc(query.json.id)
|
||||
.update(query.json)
|
||||
|
||||
return (
|
||||
await this.db
|
||||
await this.client
|
||||
.collection(query.extra.collection)
|
||||
.doc(query.json.id)
|
||||
.get()
|
||||
).data()
|
||||
} catch (err) {
|
||||
console.error("Error writing to firebase", err)
|
||||
console.error("Error writing to Firestore", err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
@ -186,13 +186,13 @@ module Firebase {
|
|||
extra: { [key: string]: string }
|
||||
}) {
|
||||
try {
|
||||
await this.db
|
||||
await this.client
|
||||
.collection(query.extra.collection)
|
||||
.doc(query.json.id)
|
||||
.delete()
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error("Error writing to mongodb", err)
|
||||
console.error("Error deleting from Firestore", err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { Firestore } = require("@google-cloud/firestore")
|
||||
const firebase = require("@google-cloud/firestore")
|
||||
const FirebaseIntegration = require("../firebase")
|
||||
jest.mock("@google-cloud/firestore")
|
||||
|
||||
|
@ -13,78 +13,64 @@ describe("Firebase Integration", () => {
|
|||
let tableName = "Users"
|
||||
|
||||
beforeEach(() => {
|
||||
config = new TestConfiguration()
|
||||
config = new TestConfiguration({
|
||||
serviceAccount: "{}"
|
||||
})
|
||||
})
|
||||
|
||||
it("calls the create method with the correct params", async () => {
|
||||
const response = await config.integration.create({
|
||||
await config.integration.create({
|
||||
table: tableName,
|
||||
json: {
|
||||
Name: "John"
|
||||
Name: "Test Name"
|
||||
},
|
||||
extra: {
|
||||
collection: "test"
|
||||
}
|
||||
})
|
||||
expect(config.integration.client.put).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
Name: "John"
|
||||
expect(config.integration.client.collection).toHaveBeenCalledWith("test")
|
||||
expect(config.integration.client.set).toHaveBeenCalledWith({
|
||||
Name: "Test Name",
|
||||
id: "test_id"
|
||||
})
|
||||
})
|
||||
|
||||
it("calls the read method with the correct params", async () => {
|
||||
const indexName = "Test"
|
||||
|
||||
const response = await config.integration.read({
|
||||
table: tableName,
|
||||
index: indexName,
|
||||
json: {}
|
||||
})
|
||||
expect(config.integration.client.query).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
IndexName: indexName,
|
||||
})
|
||||
expect(response).toEqual([])
|
||||
})
|
||||
|
||||
it("calls the scan method with the correct params", async () => {
|
||||
const indexName = "Test"
|
||||
|
||||
const response = await config.integration.scan({
|
||||
table: tableName,
|
||||
index: indexName,
|
||||
json: {}
|
||||
})
|
||||
expect(config.integration.client.scan).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
IndexName: indexName,
|
||||
})
|
||||
expect(response).toEqual([{
|
||||
Name: "test"
|
||||
}])
|
||||
})
|
||||
|
||||
it("calls the get method with the correct params", async () => {
|
||||
const response = await config.integration.get({
|
||||
table: tableName,
|
||||
json: {
|
||||
Id: 123
|
||||
Name: "Test"
|
||||
},
|
||||
extra: {
|
||||
collection: "test",
|
||||
filterField: "field",
|
||||
filter: "==",
|
||||
filterValue: "value",
|
||||
}
|
||||
})
|
||||
|
||||
expect(config.integration.client.get).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
Id: 123
|
||||
})
|
||||
expect(config.integration.client.collection).toHaveBeenCalledWith("test")
|
||||
expect(config.integration.client.where).toHaveBeenCalledWith("field", "==", "value")
|
||||
expect(response).toEqual([{ result: "test"}])
|
||||
})
|
||||
|
||||
it("calls the update method with the correct params", async () => {
|
||||
const response = await config.integration.update({
|
||||
table: tableName,
|
||||
json: {
|
||||
Name: "John"
|
||||
id: "test",
|
||||
Name: "Test"
|
||||
},
|
||||
extra: {
|
||||
collection: "test"
|
||||
}
|
||||
})
|
||||
expect(config.integration.client.collection).toHaveBeenCalledWith("test")
|
||||
expect(config.integration.client.update).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
Name: "John"
|
||||
Name: "Test",
|
||||
id: "test"
|
||||
})
|
||||
expect(response).toEqual({
|
||||
result: "test"
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -92,12 +78,15 @@ describe("Firebase Integration", () => {
|
|||
const response = await config.integration.delete({
|
||||
table: tableName,
|
||||
json: {
|
||||
Name: "John"
|
||||
id: "test",
|
||||
Name: "Test"
|
||||
},
|
||||
extra: {
|
||||
collection: "test"
|
||||
}
|
||||
})
|
||||
expect(config.integration.client.delete).toHaveBeenCalledWith({
|
||||
TableName: tableName,
|
||||
Name: "John"
|
||||
})
|
||||
expect(config.integration.client.collection).toHaveBeenCalledWith("test")
|
||||
expect(config.integration.client.doc).toHaveBeenCalledWith("test")
|
||||
expect(config.integration.client.delete).toHaveBeenCalled()
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue