Skip to content

Commit b063f62

Browse files
wasabigeeksoedirgo
authored andcommitted
test: Add test for creating primary_key column
1 parent 7847af5 commit b063f62

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

test/integration/index.spec.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ describe('/tables', async () => {
314314
is_nullable: false,
315315
comment: 'foo',
316316
// Currently no way to test these:
317-
// isPrimaryKey: true,
318-
// isUnique: true,
317+
// is_unique: true
319318
})
320319

321320
const { data: columns } = await axios.get(`${URL}/columns`)
@@ -330,6 +329,33 @@ describe('/tables', async () => {
330329
await axios.delete(`${URL}/columns/${newTable.id}.1`)
331330
await axios.delete(`${URL}/tables/${newTable.id}`)
332331
})
332+
it('POST /columns for primary key', async () => {
333+
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo' })
334+
await axios.post(`${URL}/columns`, {
335+
table_id: newTable.id,
336+
name: 'bar',
337+
type: 'int2',
338+
is_primary_key: true,
339+
})
340+
341+
// https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
342+
const { data: primaryKeys } = await axios.post(
343+
`${URL}/query`,
344+
{ query: `
345+
SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
346+
FROM pg_index i
347+
JOIN pg_attribute a ON a.attrelid = i.indrelid
348+
AND a.attnum = ANY(i.indkey)
349+
WHERE i.indrelid = '${newTable.name}'::regclass
350+
AND i.indisprimary;
351+
` }
352+
)
353+
assert.equal(primaryKeys.length, 1)
354+
assert.equal(primaryKeys[0].attname, 'bar')
355+
356+
await axios.delete(`${URL}/columns/${newTable.id}.1`)
357+
await axios.delete(`${URL}/tables/${newTable.id}`)
358+
})
333359
it('POST /columns array type', async () => {
334360
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
335361
await axios.post(`${URL}/columns`, {

0 commit comments

Comments
 (0)