mysql tests

This commit is contained in:
Martin McKeaveney 2021-03-16 19:01:51 +00:00
parent 172db5f255
commit c0e9ee282a
3 changed files with 17 additions and 11 deletions

View File

@ -2,7 +2,7 @@ const mysql = {}
const client = { const client = {
connect: jest.fn(), connect: jest.fn(),
query: jest.fn(console.log), query: jest.fn(),
} }
mysql.createConnection = jest.fn(() => client) mysql.createConnection = jest.fn(() => client)

View File

@ -65,7 +65,6 @@ class MySQLIntegration {
// Node MySQL is callback based, so we must wrap our call in a promise // Node MySQL is callback based, so we must wrap our call in a promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.client.connect() this.client.connect()
console.log(this.client.query())
return this.client.query(query.sql, (error, results) => { return this.client.query(query.sql, (error, results) => {
if (error) return reject(error) if (error) return reject(error)
resolve(results) resolve(results)
@ -76,7 +75,7 @@ class MySQLIntegration {
async create(query) { async create(query) {
const results = await this.query(query) const results = await this.query(query)
return results.length ? results : { created: true } return results.length ? results : [{ created: true }]
} }
read(query) { read(query) {
@ -85,12 +84,12 @@ class MySQLIntegration {
async update(query) { async update(query) {
const results = await this.query(query) const results = await this.query(query)
return results.length ? results : { updated: true } return results.length ? results : [{ updated: true }]
} }
async delete(query) { async delete(query) {
const results = await this.query(query) const results = await this.query(query)
return results.length ? results : { deleted: true } return results.length ? results : [{ deleted: true }]
} }
} }

View File

@ -5,6 +5,8 @@ jest.mock("mysql")
class TestConfiguration { class TestConfiguration {
constructor(config = { ssl: {} }) { constructor(config = { ssl: {} }) {
this.integration = new MySQLIntegration.integration(config) this.integration = new MySQLIntegration.integration(config)
this.query = jest.fn(() => [{ id: 1 }])
this.integration.query = this.query
} }
} }
@ -15,13 +17,12 @@ describe("MySQL Integration", () => {
config = new TestConfiguration() config = new TestConfiguration()
}) })
fit("calls the create method with the correct params", async () => { it("calls the create method with the correct params", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);" const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({ const response = await config.integration.create({
sql sql
}) })
console.log(response) expect(config.query).toHaveBeenCalledWith({ sql })
expect(config.integration.client.query).resolves.toHaveBeenCalledWith(sql)
}) })
it("calls the read method with the correct params", async () => { it("calls the read method with the correct params", async () => {
@ -29,7 +30,9 @@ describe("MySQL Integration", () => {
const response = await config.integration.read({ const response = await config.integration.read({
sql sql
}) })
expect(config.integration.client.query).toHaveBeenCalledWith(sql) expect(config.query).toHaveBeenCalledWith({
sql
})
}) })
it("calls the update method with the correct params", async () => { it("calls the update method with the correct params", async () => {
@ -37,7 +40,7 @@ describe("MySQL Integration", () => {
const response = await config.integration.update({ const response = await config.integration.update({
sql sql
}) })
expect(config.integration.client.query).toHaveBeenCalledWith(sql) expect(config.query).toHaveBeenCalledWith({ sql })
}) })
it("calls the delete method with the correct params", async () => { it("calls the delete method with the correct params", async () => {
@ -45,10 +48,14 @@ describe("MySQL Integration", () => {
const response = await config.integration.delete({ const response = await config.integration.delete({
sql sql
}) })
expect(config.integration.client.query).toHaveBeenCalledWith(sql) expect(config.query).toHaveBeenCalledWith({ sql })
}) })
describe("no rows returned", () => { describe("no rows returned", () => {
beforeEach(() => {
config.query.mockImplementation(() => [])
})
it("returns the correct response when the create response has no rows", async () => { it("returns the correct response when the create response has no rows", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);" const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({ const response = await config.integration.create({