Skip to content

Commit

Permalink
Merge pull request #20 from Sharaal/Refactoring-tests-to-reduce-test-…
Browse files Browse the repository at this point in the history
…code-duplication-#19

Refactoring tests to reduce test code duplication #19
  • Loading branch information
Sharaal committed Jul 3, 2019
2 parents df84d98 + ecdfb0a commit 13f2cc1
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 719 deletions.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -224,7 +224,10 @@ sql.offset = offset =>
sql.defaultPageSize = 10

sql.pagination = (page, { pageSize: pageSize = sql.defaultPageSize } = {}) =>
sql`${sql.limit(pageSize)} ${sql.offset(page * pageSize)}`
() => ({
text: `${sql.limit(pageSize)().text} ${sql.offset(page * pageSize)().text}`,
parameters: []
})

sql.if = (condition, truly, falsy = () => ({ text: '', parameters: [] })) => condition ? truly : falsy

Expand Down
56 changes: 19 additions & 37 deletions test/manipulation-methods/sql.delete.js
Expand Up @@ -2,6 +2,7 @@ const assert = require('power-assert')
const sinon = require('sinon')

const sql = require('../../')
const { testSql } = require('../test')

describe('sql.delete', () => {
beforeEach(() => {
Expand All @@ -24,23 +25,16 @@ describe('sql.delete', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'DELETE FROM "table" WHERE "column1" = $1 AND "column2" = $2 AND "column3" = $3',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg0 = actualArg(0)
const expectedArg0 = expectedArg
assert.deepEqual({ text: actualArg0.text, parameters: actualArg0.parameters }, expectedArg0)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'DELETE FROM "table" WHERE "column1" = $6 AND "column2" = $7 AND "column3" = $8',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'DELETE FROM "table" WHERE "column1" = $1 AND "column2" = $2 AND "column3" = $3',
5: 'DELETE FROM "table" WHERE "column1" = $6 AND "column2" = $7 AND "column3" = $8'
},
parameters: ['value1', 'value2', 'value3']
}
)
})

it('delete rows with SQL Tag', async () => {
Expand All @@ -50,30 +44,18 @@ describe('sql.delete', () => {
}

sql.client = client
const actualRowCount = await sql.delete(
sql`DELETE FROM "table"`
)
const actualRowCount = await sql.delete(sql`DELETE FROM "table"`)

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'DELETE FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg0 = actualArg(0)
const expectedArg0 = expectedArg
assert.deepEqual({ text: actualArg0.text, parameters: actualArg0.parameters }, expectedArg0)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'DELETE FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: 'DELETE FROM "table"',
parameters: []
}
)
})
})
226 changes: 92 additions & 134 deletions test/manipulation-methods/sql.insert.js
Expand Up @@ -2,6 +2,7 @@ const assert = require('power-assert')
const sinon = require('sinon')

const sql = require('../../')
const { testSql } = require('../test')

describe('sql.update', () => {
beforeEach(() => {
Expand All @@ -25,19 +26,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "id"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "id"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "id"',
5: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "id"'
},
parameters: ['value1', 'value2', 'value3']
}
)
})

it('insert multiple rows', async () => {
Expand All @@ -60,19 +58,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9) RETURNING "id"',
parameters: ['value11', 'value12', 'value13', 'value21', 'value22', 'value23', 'value31', 'value32', 'value33']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8), ($9, $10, $11), ($12, $13, $14) RETURNING "id"',
parameters: ['value11', 'value12', 'value13', 'value21', 'value22', 'value23', 'value31', 'value32', 'value33']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9) RETURNING "id"',
5: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8), ($9, $10, $11), ($12, $13, $14) RETURNING "id"'
},
parameters: ['value11', 'value12', 'value13', 'value21', 'value22', 'value23', 'value31', 'value32', 'value33']
}
)
})

it('return array of IDs if inserting single row as array', async () => {
Expand All @@ -93,19 +88,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "id"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "id"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "id"',
5: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "id"'
},
parameters: ['value1', 'value2', 'value3']
}
)
})

it('return overwritten default serial column', async () => {
Expand All @@ -125,19 +117,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "column4"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "column4"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "column4"',
5: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "column4"'
},
parameters: ['value1', 'value2', 'value3']
}
)
})

it('return given serial column', async () => {
Expand All @@ -157,23 +146,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "column4"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg0 = actualArg(0)
const expectedArg0 = expectedArg
assert.deepEqual({ text: actualArg0.text, parameters: actualArg0.parameters }, expectedArg0)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "column4"',
parameters: ['value1', 'value2', 'value3']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($1, $2, $3) RETURNING "column4"',
5: 'INSERT INTO "table" ("column1", "column2", "column3") VALUES ($6, $7, $8) RETURNING "column4"'
},
parameters: ['value1', 'value2', 'value3']
}
)
})

it('insert given keys of row', async () => {
Expand All @@ -193,19 +175,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2") VALUES ($1, $2) RETURNING "id"',
parameters: ['value1', 'value2']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2") VALUES ($6, $7) RETURNING "id"',
parameters: ['value1', 'value2']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2") VALUES ($1, $2) RETURNING "id"',
5: 'INSERT INTO "table" ("column1", "column2") VALUES ($6, $7) RETURNING "id"'
},
parameters: ['value1', 'value2']
}
)
})

it('insert given keys of multiple rows', async () => {
Expand All @@ -229,19 +208,16 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" ("column1", "column2") VALUES ($1, $2), ($3, $4), ($5, $6) RETURNING "id"',
parameters: ['value11', 'value12', 'value21', 'value22', 'value31', 'value32']
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" ("column1", "column2") VALUES ($6, $7), ($8, $9), ($10, $11) RETURNING "id"',
parameters: ['value11', 'value12', 'value21', 'value22', 'value31', 'value32']
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'INSERT INTO "table" ("column1", "column2") VALUES ($1, $2), ($3, $4), ($5, $6) RETURNING "id"',
5: 'INSERT INTO "table" ("column1", "column2") VALUES ($6, $7), ($8, $9), ($10, $11) RETURNING "id"'
},
parameters: ['value11', 'value12', 'value21', 'value22', 'value31', 'value32']
}
)
})

it('insert with SQL Tag and the standard default serial column', async () => {
Expand All @@ -259,19 +235,13 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
)
})

it('insert with SQL Tag and the overwritten default serial column', async () => {
Expand All @@ -290,19 +260,13 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
)
})

it('insert with SQL Tag and the given serial column', async () => {
Expand All @@ -321,18 +285,12 @@ describe('sql.update', () => {

assert(client.query.calledOnce)

const actualArg = client.query.getCall(0).args[0]
const expectedArg = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg.text, parameters: actualArg.parameters }, expectedArg)

const actualArg5 = actualArg(5)
const expectedArg5 = {
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
assert.deepEqual({ text: actualArg5.text, parameters: actualArg5.parameters }, expectedArg5)
testSql(
client.query.getCall(0).args[0],
{
text: 'INSERT INTO "table" SELECT * FROM "table"',
parameters: []
}
)
})
})

0 comments on commit 13f2cc1

Please sign in to comment.