mirror of
https://github.com/knex/knex.git
synced 2025-06-26 22:00:25 +00:00
tests(insert): add tests for json, text array, integer array (#5451)
This commit is contained in:
parent
d102fe3b96
commit
5caf526c27
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user