Skip to content

Commit

Permalink
tests(insert): add tests for json, text array, integer array (#5451)
Browse files Browse the repository at this point in the history
  • Loading branch information
minh-hoang-trinh committed Jan 18, 2023
1 parent d102fe3 commit 5caf526
Showing 1 changed file with 170 additions and 1 deletion.
171 changes: 170 additions & 1 deletion test/integration2/query/insert/inserts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
});
});
});
});
});
});

0 comments on commit 5caf526

Please sign in to comment.