2021-06-03 17:31:24 +02:00
|
|
|
const Sql = require("../base/sql")
|
|
|
|
|
|
|
|
const TABLE_NAME = "test"
|
|
|
|
|
|
|
|
function endpoint(table, operation) {
|
|
|
|
return {
|
|
|
|
datasourceId: "Postgres",
|
|
|
|
operation: operation,
|
|
|
|
entityId: table || TABLE_NAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateReadJson({ table, fields, filters, sort, paginate} = {}) {
|
|
|
|
return {
|
|
|
|
endpoint: endpoint(table || TABLE_NAME, "READ"),
|
|
|
|
resource: {
|
|
|
|
fields: fields || [],
|
|
|
|
},
|
|
|
|
filters: filters || {},
|
|
|
|
sort: sort || {},
|
|
|
|
paginate: paginate || {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateCreateJson(table = TABLE_NAME, body = {}) {
|
|
|
|
return {
|
|
|
|
endpoint: endpoint(table, "CREATE"),
|
|
|
|
body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateUpdateJson(table = TABLE_NAME, body = {}, filters = {}) {
|
|
|
|
return {
|
|
|
|
endpoint: endpoint(table, "UPDATE"),
|
|
|
|
filters,
|
|
|
|
body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateDeleteJson(table = TABLE_NAME, filters = {}) {
|
|
|
|
return {
|
|
|
|
endpoint: endpoint(table, "DELETE"),
|
|
|
|
filters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("SQL query builder", () => {
|
|
|
|
const limit = 500
|
|
|
|
const client = "pg"
|
|
|
|
let sql
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sql = new Sql(client, limit)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should test a basic read", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateReadJson())
|
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [limit],
|
|
|
|
sql: `select * from "${TABLE_NAME}" limit $1`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should test a read with specific columns", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateReadJson({
|
2021-06-03 17:31:24 +02:00
|
|
|
fields: ["name", "age"]
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [limit],
|
|
|
|
sql: `select "name", "age" from "${TABLE_NAME}" limit $1`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should test a where string starts with read", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateReadJson({
|
2021-06-03 17:31:24 +02:00
|
|
|
filters: {
|
|
|
|
string: {
|
|
|
|
name: "John",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: ["John%", limit],
|
|
|
|
sql: `select * from "${TABLE_NAME}" where "name" like $1 limit $2`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should test a where range read", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateReadJson({
|
2021-06-03 17:31:24 +02:00
|
|
|
filters: {
|
|
|
|
range: {
|
|
|
|
age: {
|
|
|
|
low: 2,
|
|
|
|
high: 10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [2, 10, limit],
|
|
|
|
sql: `select * from "${TABLE_NAME}" where "age" between $1 and $2 limit $3`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
2021-06-15 14:03:55 +02:00
|
|
|
it("should test for multiple IDs with OR", () => {
|
|
|
|
const query = sql._query(generateReadJson({
|
|
|
|
filters: {
|
|
|
|
equal: {
|
|
|
|
age: 10,
|
|
|
|
name: "John",
|
|
|
|
},
|
|
|
|
allOr: true,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [10, "John", limit],
|
|
|
|
sql: `select * from "${TABLE_NAME}" where ("age" = $1) or ("name" = $2) limit $3`
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-03 17:31:24 +02:00
|
|
|
it("should test an create statement", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateCreateJson(TABLE_NAME, {
|
2021-06-03 17:31:24 +02:00
|
|
|
name: "Michael",
|
|
|
|
age: 45,
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [45, "Michael"],
|
|
|
|
sql: `insert into "${TABLE_NAME}" ("age", "name") values ($1, $2)`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should test an update statement", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateUpdateJson(TABLE_NAME, {
|
2021-06-03 17:31:24 +02:00
|
|
|
name: "John"
|
|
|
|
}, {
|
|
|
|
equal: {
|
|
|
|
id: 1001,
|
|
|
|
}
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: ["John", 1001],
|
|
|
|
sql: `update "${TABLE_NAME}" set "name" = $1 where "id" = $2`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should test a delete statement", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = sql._query(generateDeleteJson(TABLE_NAME, {
|
2021-06-03 17:31:24 +02:00
|
|
|
equal: {
|
|
|
|
id: 1001,
|
|
|
|
}
|
|
|
|
}))
|
2021-06-03 18:45:19 +02:00
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [1001],
|
|
|
|
sql: `delete from "${TABLE_NAME}" where "id" = $1`
|
|
|
|
})
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
2021-06-03 17:45:43 +02:00
|
|
|
|
|
|
|
it("should work with MS-SQL", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = new Sql("mssql", 10)._query(generateReadJson())
|
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [10],
|
|
|
|
sql: `select top (@p0) * from [${TABLE_NAME}]`
|
|
|
|
})
|
2021-06-03 17:45:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should work with mySQL", () => {
|
2021-06-03 18:45:19 +02:00
|
|
|
const query = new Sql("mysql", 10)._query(generateReadJson())
|
|
|
|
expect(query).toEqual({
|
|
|
|
bindings: [10],
|
|
|
|
sql: `select * from \`${TABLE_NAME}\` limit ?`
|
|
|
|
})
|
2021-06-03 17:45:43 +02:00
|
|
|
})
|
|
|
|
})
|