Skip to content

Commit

Permalink
More exercise 10 test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Feb 10, 2018
1 parent e12d92f commit 5aa634f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export abstract class SQLDatabase<S extends SQLStatement> {

public abstract getIndicesForTable(tableName: string): Promise<string[]>;
public abstract getAllTriggers(): Promise<string[]>;
public abstract getAllTableNames(): Promise<string[]>;
public abstract getAllViews(): Promise<string[]>;
}
8 changes: 8 additions & 0 deletions src/db/postgres-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,12 @@ ${chalk.magentaBright('>>')} ${highlight(query.trim(), {
sql`select viewname as name from pg_catalog.pg_views;`
)).map((result: any) => result.name as string);
}
public async getAllTableNames(): Promise<string[]> {
return (await this.all(
sql`SELECT table_name as name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';`
)).map((result: any) => result.name as string);
}
}
7 changes: 6 additions & 1 deletion src/db/sqlite-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class SQLiteDB extends SQLDatabase<sqlite.Statement> {
return setupPreparedStatements<
sqlite.Statement,
SQLDatabase<sqlite.Statement>
>(db);
>(db);
})
.then(statements => {
db.statements = statements;
Expand Down Expand Up @@ -125,4 +125,9 @@ ${chalk.magentaBright('>>')} ${highlight(sql.trim(), {
sql`select * from sqlite_master where type = 'view';`
)).map((i: any) => i.name as string);
}
public async getAllTableNames(): Promise<string[]> {
return (await this.all(
sql`SELECT name FROM sqlite_master WHERE type='table';`
)).map((i: any) => i.name as string);
}
}
18 changes: 8 additions & 10 deletions tests/ex010.transactions-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class TransactionsTableTest {
@test('Transactions table exists')
public async transactionsExists() {
let db = await getDb('dev');
let allTables = (await db.all(
sql`SELECT name FROM sqlite_master WHERE type='table'`
)).map(i => i.name);
let allTables = await db.getAllTableNames();
assert.includeMembers(
allTables.map(t => t.toLowerCase()),
['transaction'],
Expand All @@ -67,13 +65,13 @@ class TransactionsTableTest {
@test('Inserting a new transaction completes successfully')
public async insertTransaction() {
let db = await getDb('dev');
let beforeTransactions = await db.all(sql`SELECT * from "Transaction"`);
let beforeTransactions = await db.all(sql`SELECT * from "transaction"`);
let transaction = await db.get(
sql`INSERT INTO "Transaction" (Authorization, OrderId) VALUES (?, ?)`,
sql`INSERT INTO "transaction" ("authorization", orderid) VALUES ($1, $2)`,
'lk1hdklh12ld',
10264
);
let afterTransactions = await db.all(sql`SELECT * from "Transaction"`);
let afterTransactions = await db.all(sql`SELECT * from "transaction"`);
assert.equal(
beforeTransactions.length + 1,
afterTransactions.length,
Expand All @@ -88,7 +86,7 @@ class TransactionsTableTest {
try {
let transaction = await db.run(
sql`
INSERT INTO "Transaction" (Authorization, OrderId) VALUES (?, ?)`,
INSERT INTO "transaction" ("authorization", orderid) VALUES ($1, $2)`,
'lk1hdklh12ld',
191927158
);
Expand All @@ -105,9 +103,9 @@ class TransactionsTableTest {
'At least one error (foreign key constraint violation) should have been thrown'
);
assert.include(
errors[0],
'FOREIGN KEY',
'Error message says something about "FOREIGN KEY'
errors[0].toLowerCase(),
'foreign key',
'Error message says something about "foreign key'
);
}
}

0 comments on commit 5aa634f

Please sign in to comment.