From f6c795650f79b0e1dff0d1d35e87afd6dd8e89e7 Mon Sep 17 00:00:00 2001 From: Minh Hoang TRINH Date: Mon, 16 Jan 2023 12:01:05 +0100 Subject: [PATCH] tests(insert): add tests for json, text array, integer array --- .../integration2/query/insert/inserts.spec.js | 171 +++++++++++++++++- 1 file changed, 170 insertions(+), 1 deletion(-) diff --git a/test/integration2/query/insert/inserts.spec.js b/test/integration2/query/insert/inserts.spec.js index 5b5ad2a0b7..d6e523bb81 100644 --- a/test/integration2/query/insert/inserts.spec.js +++ b/test/integration2/query/insert/inserts.spec.js @@ -31,7 +31,10 @@ const { createTestTableTwo, createDataType, } = require('../../../util/tableCreatorHelper'); -const { assertNumber } = require('../../../util/assertHelper'); +const { + assertNumber, + assertJsonEquals, +} = require('../../../util/assertHelper'); describe('Inserts', function () { getAllDbs().forEach((db) => { @@ -2133,6 +2136,172 @@ describe('Inserts', function () { ); }); }); + + it('insert json object to json column', async function () { + if (!isPostgreSQL(knex)) { + return this.skip(); + } + const tableName = 'json'; + const jsonObject = { + foo: { + bar: 'baz', + }, + }; + + await knex.schema.dropTableIfExists(tableName); + await knex.schema.createTable(tableName, (table) => { + table.increments(); + table.string('name'); + table.jsonb('content'); + }); + + await knex(tableName) + .insert( + { + name: 'json_object', + content: jsonObject, + }, + 'id' + ) + .testSql(function (tester) { + tester( + 'pg', + `insert into "${tableName}" ("content", "name") values (?, ?) returning "id"`, + [JSON.stringify(jsonObject), 'json_object'] + ); + }) + .then(([insertResult]) => + knex(tableName).where('id', insertResult.id) + ) + .then((result) => { + expect(result.length).to.equal(1); + assertJsonEquals(result[0].content, jsonObject); + }); + }); + + it('insert number array to integer ARRAY column', async function () { + if (!isPostgreSQL(knex)) { + return this.skip(); + } + const tableName = 'integer_array'; + const integerArrayContent = [1, 2, 3, 42, -100]; + + await knex.schema.dropTableIfExists(tableName); + await knex.schema.createTable(tableName, (table) => { + table.increments(); + table.string('name'); + table.specificType('content', 'integer ARRAY'); + }); + + await knex(tableName) + .insert( + { + name: 'integer_array', + content: integerArrayContent, + }, + 'id' + ) + .testSql(function (tester) { + tester( + 'pg', + `insert into "${tableName}" ("content", "name") values (?, ?) returning "id"`, + [integerArrayContent, 'integer_array'] + ); + }) + .then(([insertResult]) => + knex(tableName).where('id', insertResult.id) + ) + .then((result) => { + expect(result.length).to.equal(1); + assertJsonEquals(result[0].content, integerArrayContent); + }); + }); + + describe('text array', () => { + const tableName = 'text_array'; + + beforeEach(async () => { + if (!isPostgreSQL(knex)) { + return true; + } + await knex.schema.dropTableIfExists(tableName); + await knex.schema.createTable(tableName, (table) => { + table.increments(); + table.string('name'); + table.specificType('content', 'text ARRAY'); + }); + }); + + it('#5365 should insert string array to text ARRAY column', async function () { + if (!isPostgreSQL(knex)) { + return this.skip(); + } + + const stringArrayContent = ['SOME TEXT', 'SOME OTHER TEXT']; + + await knex(tableName) + .insert( + { + name: 'array_of_string', + content: stringArrayContent, + }, + 'id' + ) + .testSql(function (tester) { + tester( + 'pg', + `insert into "${tableName}" ("content", "name") values (?, ?) returning "id"`, + [stringArrayContent, 'array_of_string'], + [{ id: 1 }] + ); + }) + .then(([insertResult]) => + knex(tableName).where('id', insertResult.id) + ) + .then((result) => { + expect(result.length).to.equal(1); + expect(result[0].content).to.deep.equal(stringArrayContent); + }); + }); + + it(`#5430 should insert data to text array column if it's an array of object`, async function () { + if (!isPostgreSQL(knex)) { + return this.skip(); + } + + const arrayOfObject = [ + { + foo: { + bar: 'baz', + }, + }, + ]; + + await knex(tableName) + .insert( + { + name: 'array_of_object', + content: arrayOfObject, + }, + 'id' + ) + .testSql(function (tester) { + tester( + 'pg', + `insert into "${tableName}" ("content", "name") values (?, ?) returning "id"`, + [arrayOfObject, 'array_of_object'], + [{ id: 1 }] + ); + }) + .then(([insertResult]) => + knex(tableName).where('id', insertResult.id) + ) + .then((result) => { + expect(result.length).to.equal(1); + assertJsonEquals(result[0].content, arrayOfObject); + }); + }); + }); }); }); });