forked from Vincit/objection.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: tests | ||
|
||
on: | ||
push: | ||
branches: [master] | ||
pull_request: | ||
branches: [master] | ||
|
||
jobs: | ||
run-tests: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [12.x, 14.x, 16.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Run docker compose | ||
run: docker-compose up -d | ||
|
||
- name: Setup test db | ||
run: node setup-test-db.js | ||
|
||
- name: Run tests | ||
run: npm test | ||
|
||
- name: Run typing tests | ||
run: npm run test-typings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,71 @@ | ||
const knex = require('knex'); | ||
|
||
// DATABASES environment variable can contain a comma separated list | ||
// of databases to setup. | ||
const DATABASES = (process.env.DATABASES && process.env.DATABASES.split(',')) || []; | ||
|
||
const knexes = []; | ||
const commands = []; | ||
|
||
if (DATABASES.length === 0 || DATABASES.includes('postgres')) { | ||
const postgres = knex({ | ||
client: 'postgres', | ||
|
||
connection: { | ||
user: 'postgres', | ||
host: 'localhost', | ||
database: 'postgres', | ||
}, | ||
}); | ||
|
||
knexes.push(postgres); | ||
commands.push( | ||
postgres.raw('DROP DATABASE IF EXISTS objection_test'), | ||
postgres.raw('DROP USER IF EXISTS objection'), | ||
postgres.raw('CREATE USER objection SUPERUSER'), | ||
postgres.raw('CREATE DATABASE objection_test') | ||
); | ||
// of databases to setup. Defaults to all databases. | ||
const DATABASES = (process.env.DATABASES && process.env.DATABASES.split(',')) || [ | ||
'postgres', | ||
'mysql', | ||
]; | ||
|
||
async function setup() { | ||
if (DATABASES.includes('postgres')) { | ||
const postgres = await createKnex({ | ||
client: 'postgres', | ||
|
||
connection: { | ||
user: 'postgres', | ||
host: 'localhost', | ||
database: 'postgres', | ||
}, | ||
}); | ||
|
||
await postgres.raw('DROP DATABASE IF EXISTS objection_test'); | ||
await postgres.raw('DROP USER IF EXISTS objection'); | ||
await postgres.raw('CREATE USER objection SUPERUSER'); | ||
await postgres.raw('CREATE DATABASE objection_test'); | ||
|
||
await postgres.destroy(); | ||
} | ||
|
||
if (DATABASES.includes('mysql')) { | ||
const mysql = await createKnex({ | ||
client: 'mysql', | ||
|
||
connection: { | ||
user: 'root', | ||
host: 'localhost', | ||
}, | ||
}); | ||
|
||
await mysql.raw('DROP DATABASE IF EXISTS objection_test'); | ||
await mysql.raw('DROP USER IF EXISTS objection'); | ||
await mysql.raw('CREATE USER objection'); | ||
await mysql.raw('GRANT ALL PRIVILEGES ON *.* TO objection'); | ||
await mysql.raw('CREATE DATABASE objection_test'); | ||
|
||
await mysql.destroy(); | ||
} | ||
} | ||
|
||
if (DATABASES.length === 0 || DATABASES.includes('mysql')) { | ||
const mysql = knex({ | ||
client: 'mysql', | ||
|
||
connection: { | ||
user: 'root', | ||
host: 'localhost', | ||
}, | ||
}); | ||
|
||
knexes.push(mysql); | ||
commands.push( | ||
mysql.raw('DROP DATABASE IF EXISTS objection_test'), | ||
mysql.raw('DROP USER IF EXISTS objection'), | ||
mysql.raw('CREATE USER objection'), | ||
mysql.raw('GRANT ALL PRIVILEGES ON *.* TO objection'), | ||
mysql.raw('CREATE DATABASE objection_test') | ||
); | ||
async function createKnex(config) { | ||
const startTime = new Date(); | ||
|
||
while (true) { | ||
try { | ||
const knexInstance = knex(config); | ||
await knexInstance.raw('SELECT 1'); | ||
return knexInstance; | ||
} catch (err) { | ||
const now = new Date(); | ||
|
||
if (now.getTime() - startTime.getTime() > 60000) { | ||
process.exit(1); | ||
} else { | ||
console.log(`failed to connect to ${config.client}. Trying again soon`); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
commands | ||
.reduce((promise, query) => { | ||
return promise.then(() => query); | ||
}, Promise.resolve()) | ||
.then(() => { | ||
return Promise.all(knexes.map((it) => it.destroy())); | ||
}); | ||
setup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters