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