Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBuschmann committed Nov 4, 2017
1 parent 7a7d6f7 commit 3532589
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/specs/annotations/belongs-to-many.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {Table} from '../../../lib/annotations/Table';
import {Model} from '../../../lib/models/Model';
import {createSequelize} from '../../utils/sequelize';
import {BelongsToMany} from '../../../lib/annotations/association/BelongsToMany';

use(chaiAsPromised);

// tslint:disable:max-classes-per-file
describe('BelongsToMany', () => {

const as = 'manyTeams';
const sequelize = createSequelize(false);

@Table
class Team extends Model<Team> {}

@Table
class Player extends Model<Player> {

@BelongsToMany(() => Team, {
as,
through: 'TeamPlayer',
foreignKey: 'playerId',
otherKey: 'teamId',
})
teams: Team[];
}

sequelize.addModels([Team, Player]);

it('should pass as options to sequelize association', () => {
expect(Player['associations']).to.have.property(as);
});

});
47 changes: 47 additions & 0 deletions test/specs/annotations/belongs-to.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {Table} from '../../../lib/annotations/Table';
import {BelongsTo} from '../../../lib/annotations/association/BelongsTo';
import {Model} from '../../../lib/models/Model';
import {createSequelize} from '../../utils/sequelize';

use(chaiAsPromised);

// tslint:disable:max-classes-per-file
describe('BelongsTo', () => {

const as = 'parent';
const sequelize = createSequelize(false);

@Table
class Team extends Model<Team> {}

@Table
class Player extends Model<Player> {

@BelongsTo(() => Team, {as, foreignKey: 'teamId'})
team: Team;
}

sequelize.addModels([Team, Player]);

it('should pass as options to sequelize association', () => {
expect(Player['associations']).to.have.property(as);
});

it('should throw due to missing foreignKey', () => {
const _sequelize = createSequelize(false);

@Table
class Team extends Model<Team> {}

@Table
class Player extends Model<Player> {
@BelongsTo(() => Team)
team: Team;
}

expect(() => _sequelize.addModels([Team, Player])).to.throw(/Foreign key for "\w+" is missing on "\w+"./);
});

});
35 changes: 35 additions & 0 deletions test/specs/annotations/has-many.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {Table} from '../../../lib/annotations/Table';
import {Model} from '../../../lib/models/Model';
import {createSequelize} from '../../utils/sequelize';
import {HasMany} from '../../../lib/annotations/association/HasMany';

use(chaiAsPromised);

// tslint:disable:max-classes-per-file
describe('HasMany', () => {

const as = 'babies';
const sequelize = createSequelize(false);

@Table
class Player extends Model<Player> {}

@Table
class Team extends Model<Team> {

@HasMany(() => Player, {
as,
foreignKey: 'teamId'
})
players: Player[];
}

sequelize.addModels([Team, Player]);

it('should pass as options to sequelize association', () => {
expect(Team['associations']).to.have.property(as);
});

});
36 changes: 36 additions & 0 deletions test/specs/annotations/has-one.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import {Table} from '../../../lib/annotations/Table';
import {Model} from '../../../lib/models/Model';
import {createSequelize} from '../../utils/sequelize';
import {HasOne} from '../../../lib/annotations/association/HasOne';

use(chaiAsPromised);

// tslint:disable:max-classes-per-file
describe('HasOne', () => {

const as = 'baby';
const sequelize = createSequelize(false);

@Table
class Player extends Model<Player> {}

@Table
class Team extends Model<Team> {

@HasOne(() => Player, {
as,
foreignKey: 'teamId'
})
player: Player;
}


sequelize.addModels([Team, Player]);

it('should pass as options to sequelize association', () => {
expect(Team['associations']).to.have.property(as);
});

});

0 comments on commit 3532589

Please sign in to comment.