Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(insert): add insert tests for json, text array and integer array #5451

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
171 changes: 170 additions & 1 deletion test/integration2/query/insert/inserts.spec.js
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);
});
});
});
});
});
});