Skip to content

Commit

Permalink
Additional testing and CI (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele authored Jul 26, 2020
1 parent 180db03 commit 2ed7416
Show file tree
Hide file tree
Showing 27 changed files with 2,382 additions and 88 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ PG_USER=
PG_PW=
PG_DB=
PG_HOST=localhost
PG_PORT=5432
PG_PORT=5432

HASURA_GRAPHQL_ENDPOINT=
HASURA_GRAPHQL_ADMIN_SECRET=
28 changes: 28 additions & 0 deletions .github/workflows/testAndLint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
hasura:
image: hasura/graphql-engine:v1.1.1
ports:
- "127.0.0.1:5001:8080"
env:
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
HASURA_GRAPHQL_ENABLE_CONSOLE: "false"

strategy:
matrix:
Expand All @@ -39,6 +46,15 @@ jobs:
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm run db:migrate
env:
CI: true
PG_HOST: 127.0.0.1
PG_PORT: 5432
PG_USER: postgres
PG_PW: postgres
PG_DB: postgres
- run: npm run hasura:metadata:apply
- run: npm test
env:
CI: true
Expand All @@ -47,3 +63,15 @@ jobs:
PG_USER: postgres
PG_PW: postgres
PG_DB: postgres
- name: Test & publish code coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
CI: true
PG_HOST: 127.0.0.1
PG_PORT: 5432
PG_USER: postgres
PG_PW: postgres
PG_DB: postgres
with:
coverageCommand: npm run test:coverage
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
node_modules
dist
dist
coverage
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">
<a href="https://codeclimate.com/github/Bantr/Lib/maintainability"><img src="https://api.codeclimate.com/v1/badges/e078c3c32d0005fa3fe0/maintainability" /></a>

<a href="https://codeclimate.com/github/Bantr/Lib/test_coverage"><img src="https://api.codeclimate.com/v1/badges/e078c3c32d0005fa3fe0/test_coverage" /></a>
<a href="https://discord.bantr.app">
<img alt="Discord" src="https://img.shields.io/discord/626436103573864448?label=Discord">
</a>
Expand All @@ -13,6 +13,12 @@

This package is used in Bantr projects, to share types, util functions, ...

## Setting up database

- Set up `.env` file (see `.env.example`)
- `npm run db:migrate`
- `npm run hasura:metadata:apply`

## Making changes to DB schema

- Change entities
Expand All @@ -21,7 +27,7 @@ This package is used in Bantr projects, to share types, util functions, ...
- `npm run db:migrate`
- Go into Hasura, reload metadata
- Make changes to Hasura (Add data fields, fix relations, set permissions, ...)
- Export metadata and save metadata.json
- `npm run hasura:metadata:export`

## Releasing a new version

Expand Down
92 changes: 92 additions & 0 deletions entities/entities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import dotenv = require('dotenv');
import * as faker from 'faker';
import { Connection, createConnection } from 'typeorm';

import { entities, Match, Team } from '../entities';
import { RoundWinReason } from '../types/RoundWinReason.enum';
import { Round } from './round.entity';

dotenv.config();

let connection: Connection;
let match: Match;

describe("Entities", () => {
beforeAll(async () => {
connection = await createConnection({
type: "postgres",
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT, 10),
username: process.env.PG_USER,
password: process.env.PG_PW,
database: process.env.PG_DB,
entities: entities,
synchronize: false
//logging: true
});
});

afterAll(() => {
connection.close();
});

beforeEach(async () => {
match = new Match();
match.teams = [];
match.teams.push(new Team());

match.date = faker.date.past(1);
match.externalId = faker.random.uuid();

await match.save();

match = await Match.findOne({
where: { id: match.id },
relations: ["teams", "rounds", "players"]
});
});

// Seems like a stupid test.. and it is
// But this way 'loads' entities and typechecks some stuff which might otherwise go unnoticed
it("Match has an array several things", async () => {
console.log(match);

expect(match.teams).toBeInstanceOf(Array);
expect(match.rounds).toBeInstanceOf(Array);
expect(match.players).toBeInstanceOf(Array);
});

it("Round has arrays of a lot of stuff", async () => {
const roundData = new Round();
roundData.startTick = 1;
roundData.endTick = 1337;
roundData.winReason = RoundWinReason.BombDefused;
await roundData.save();

const round = await Round.findOne({
where: { id: roundData.id },
relations: [
"bombStatusChanges",
"weaponStatuses",
"utilityThrowns",
"utilityActivateds",
"playerJumps",
"kills",
"playerBlinds",
"playerHurts"
]
});

console.log(round);

expect(round.bombStatusChanges).toBeInstanceOf(Array);
expect(round.kills).toBeInstanceOf(Array);

expect(round.playerBlinds).toBeInstanceOf(Array);
expect(round.playerHurts).toBeInstanceOf(Array);
expect(round.playerJumps).toBeInstanceOf(Array);
expect(round.utilityActivateds).toBeInstanceOf(Array);
expect(round.utilityThrowns).toBeInstanceOf(Array);
expect(round.weaponStatuses).toBeInstanceOf(Array);
});
});
65 changes: 59 additions & 6 deletions entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
export { Ban } from './ban.entity'
export { Match } from './match.entity'
export { Notification } from './notification.entity'
export { Player as player } from './player.entity'
export { UserSettings } from './user-settings.entity'
export { User } from './user.entity'
import { Ban } from './ban.entity';
import { BombStatus } from './bombStatus.entity';
import { Kill } from './kill.entity';
import { Match } from './match.entity';
import { Notification } from './notification.entity';
import { Player } from './player.entity';
import { PlayerBlind } from './playerBlind.entity';
import { PlayerHurt } from './playerHurt.entity';
import { PlayerInfo } from './playerInfo.entity';
import { PlayerJump } from './playerJump.entity';
import { Position } from './position.entity';
import { Round } from './round.entity';
import { Team } from './team.entity';
import { UserSettings } from './user-settings.entity';
import { User } from './user.entity';
import { UtilityActivated } from './utilityActivated.entity';
import { UtilityThrown } from './utilityThrown.entity';
import { WeaponStatus } from './weaponStatus.entity';

export { BombStatus } from "./bombStatus.entity";
export { Kill } from "./kill.entity";
export { PlayerBlind } from "./playerBlind.entity";
export { PlayerHurt } from "./playerHurt.entity";
export { PlayerInfo } from "./playerInfo.entity";
export { PlayerJump } from "./playerJump.entity";
export { Position } from "./position.entity";
export { Round } from "./round.entity";
export { Team } from "./team.entity";
export { UtilityActivated } from "./utilityActivated.entity";
export { UtilityThrown } from "./utilityThrown.entity";
export { WeaponStatus } from "./weaponStatus.entity";

export { Ban } from "./ban.entity";
export { Match } from "./match.entity";
export { Notification } from "./notification.entity";
export { Player } from "./player.entity";
export { UserSettings } from "./user-settings.entity";
export { User } from "./user.entity";

export const entities = [
Ban,
Match,
Notification,
Player,
Round,
Team,
User,
UserSettings,
BombStatus,
Kill,
PlayerBlind,
PlayerHurt,
PlayerInfo,
PlayerJump,
Position,
UtilityActivated,
UtilityThrown,
WeaponStatus
];
22 changes: 0 additions & 22 deletions entities/match.entity.test.ts

This file was deleted.

2 changes: 2 additions & 0 deletions entities/match.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ export class Match extends BaseEntity {
players: Player[];

@OneToMany(() => Round, (round) => round.match)
@JoinTable()
rounds: Round[];

@ManyToMany(() => Team, (team) => team.matches)
@JoinTable()
teams: Team[];
}
Loading

0 comments on commit 2ed7416

Please sign in to comment.