Skip to content

Commit

Permalink
chore: add tests (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoncool authored Sep 20, 2024
1 parent 8aac834 commit 9a832ec
Show file tree
Hide file tree
Showing 18 changed files with 7,323 additions and 2,266 deletions.
3 changes: 3 additions & 0 deletions .depdiffrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignores": []
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
node_modules
scripts
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ jobs:
run: npm run lint
- name: Typecheck
run: npm run typecheck

tests:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 18
cache: 'npm'
- name: Install Packages
run: npm ci
- name: Tests
run: npm run test
143 changes: 142 additions & 1 deletion examples/demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Demo for PostgresKit",
"main": "index.js",
"scripts": {
"build": "tsc",
"build": "rm -rf ./build && rm -rf ./node_modules && npm ci && tsc",
"watch": "tsc -w",
"dev": "tsc-watch -w --onSuccess 'node build/'",
"migration:create": "npx knex migrate:make --migrations-directory ./src/db/migrations -x ts",
Expand All @@ -18,9 +18,11 @@
"@gravity-ui/prettier-config": "^1.1.0",
"@gravity-ui/tsconfig": "^1.0.0",
"@types/node": "^18.16.16",
"@types/supertest": "^6.0.2",
"eslint": "^8.56.0",
"husky": "^9.1.6",
"prettier": "^3.3.3",
"supertest": "^7.0.0",
"tsc-watch": "^5.0.3",
"typescript": "^5.6.2"
},
Expand Down
12 changes: 9 additions & 3 deletions examples/demo/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as path from 'path';

import {initDB} from '@gravity-ui/postgreskit';
import {getTestDsnList, testDbConfig} from '../tests/constants';

import {nodekit} from '../nodekit';

const knexOptions = {
export const knexOptions = {
client: 'pg',
pool: {
min: 0,
Expand All @@ -25,7 +26,7 @@ const knexOptions = {
directory: path.resolve(__dirname, 'seeds'),
loadExtensions: ['.js'],
},
debug: true,
debug: false,
};

const dispatcherOptions = {
Expand All @@ -34,8 +35,13 @@ const dispatcherOptions = {
suppressStatusLogs: true,
};

const connectionString =
process.env.APP_ENV === 'test'
? getTestDsnList()
: `postgresql://${testDbConfig.user}:${testDbConfig.password}@localhost:5432/${testDbConfig.dbName}`;

const {db, CoreBaseModel, helpers} = initDB({
connectionString: 'postgresql://test_user:test_user@localhost:5432/test_postgreskit',
connectionString,
dispatcherOptions,
knexOptions,
logger: {
Expand Down
6 changes: 5 additions & 1 deletion examples/demo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ const app = new ExpressKit(nodekit, {
},
});

app.run();
if (require.main === module) {
app.run();
}

export default app;
33 changes: 33 additions & 0 deletions examples/demo/src/tests/cases/models.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {TeachersModel, TeachersModelColumn} from '../../db/models/teachers';

const data = {
[TeachersModelColumn.Age]: 25,
[TeachersModelColumn.Email]: '[email protected]',
[TeachersModelColumn.Name]: 'Mike Snow',
};

describe('Models', () => {
test('Insert teacher', async () => {
const teacher = await TeachersModel.query(TeachersModel.primary)
.insert(data)
.returning('*')
.timeout(TeachersModel.DEFAULT_QUERY_TIMEOUT);

expect(teacher.toJSON()).toStrictEqual({
...data,
[TeachersModelColumn.SchoolId]: expect.any(Number),
});
});
test('Get inserted teacher', async () => {
const teacher = await TeachersModel.query(TeachersModel.replica)
.findOne({
[TeachersModelColumn.Email]: data[TeachersModelColumn.Email],
})
.timeout(TeachersModel.DEFAULT_QUERY_TIMEOUT);

expect(teacher?.toJSON()).toStrictEqual({
...data,
[TeachersModelColumn.SchoolId]: expect.any(Number),
});
});
});
17 changes: 17 additions & 0 deletions examples/demo/src/tests/cases/routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import request from 'supertest';
import nodekitApp from '../..';

export const app = nodekitApp.express;

describe('Routes', () => {
test('Get root route', async () => {
const response = await request(app).get('/').expect(200);

expect(response.body).toStrictEqual({
school_id: expect.any(Number),
name: expect.any(String),
email: expect.any(String),
age: expect.any(Number),
});
});
});
Loading

0 comments on commit 9a832ec

Please sign in to comment.