Skip to content

Commit ba73cda

Browse files
tests added for through options
1 parent 9a26eb5 commit ba73cda

File tree

3 files changed

+140
-10
lines changed

3 files changed

+140
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"test-v4": "npm run prepare-test-v4 && npm run exec-tests",
1313
"cover-v3": "npm run prepare-test-v3 && nyc --exclude lib/models/v4/**/*.js mocha",
1414
"cover-v4": "npm run prepare-test-v4 && nyc --exclude lib/models/v3/**/*.js mocha",
15-
"exec-tests": "mocha test/specs/",
15+
"exec-tests": "mocha",
1616
"test": "npm run test-v4 && npm run test-v3",
1717
"cover": "npm run cover-v4",
1818
"lint": "tslint ."

test/specs/association.spec.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {expectAutoGeneratedFunctions} from "../utils/association";
1313
import {assertInstance} from "../utils/common";
1414
import {AllowNull} from "../../lib/annotations/AllowNull";
15+
import {PrimaryKey} from '../../lib/annotations/PrimaryKey';
1516

1617
use(chaiAsPromised);
1718

@@ -1338,6 +1339,135 @@ describe('association', () => {
13381339

13391340
manyToManyTestSuites(Book3, Author3);
13401341
});
1342+
1343+
describe('set through model via through options', () => {
1344+
1345+
@Table
1346+
class Book66 extends Model<Book66> {
1347+
1348+
@Column
1349+
title: string;
1350+
1351+
@BelongsToMany(() => Author66, {
1352+
through: {
1353+
model: () => AuthorBook66,
1354+
}
1355+
})
1356+
authors: Author66[];
1357+
}
1358+
1359+
@Table
1360+
class AuthorBook66 extends Model<AuthorBook66> {
1361+
1362+
@ForeignKey(() => Book66)
1363+
bookId: number;
1364+
1365+
@ForeignKey(() => Author66)
1366+
authorId: number;
1367+
}
1368+
1369+
@Table
1370+
class Author66 extends Model<Author66> {
1371+
1372+
@Column
1373+
name: string;
1374+
1375+
@BelongsToMany(() => Book66, {
1376+
through: {
1377+
model: () => AuthorBook66,
1378+
}
1379+
})
1380+
books: Book66;
1381+
}
1382+
1383+
manyToManyTestSuites(Book66, Author66, AuthorBook66);
1384+
});
1385+
1386+
describe('set through model string via through options', () => {
1387+
1388+
@Table
1389+
class Book66 extends Model<Book66> {
1390+
1391+
@Column
1392+
title: string;
1393+
1394+
@BelongsToMany(() => Author66, {
1395+
through: {
1396+
model: 'AuthorBook66',
1397+
},
1398+
foreignKey: 'bookId',
1399+
otherKey: 'authorId',
1400+
})
1401+
authors: Author66[];
1402+
}
1403+
1404+
@Table
1405+
class Author66 extends Model<Author66> {
1406+
1407+
@Column
1408+
name: string;
1409+
1410+
@BelongsToMany(() => Book66, {
1411+
through: {
1412+
model: 'AuthorBook66',
1413+
},
1414+
foreignKey: 'authorId',
1415+
otherKey: 'bookId',
1416+
})
1417+
books: Book66;
1418+
}
1419+
1420+
manyToManyTestSuites(Book66, Author66);
1421+
});
1422+
1423+
describe('ThroughOptions', () => {
1424+
1425+
@Table
1426+
class User77 extends Model<User77> {
1427+
1428+
@Column
1429+
name: string;
1430+
1431+
@BelongsToMany(() => User77, {
1432+
through: {
1433+
model: () => Subscription,
1434+
scope: {
1435+
targetType: 'user'
1436+
}
1437+
},
1438+
foreignKeyConstraint: true,
1439+
foreignKey: 'subscriberId',
1440+
otherKey: 'targetId',
1441+
constraints: false,
1442+
})
1443+
usersSubscribedTo: User77[];
1444+
}
1445+
1446+
@Table
1447+
class Subscription extends Model<Subscription> {
1448+
1449+
@PrimaryKey
1450+
@ForeignKey(() => User77)
1451+
@Column
1452+
subscriberId: number;
1453+
1454+
@PrimaryKey
1455+
@Column
1456+
targetId: number;
1457+
1458+
@Column
1459+
targetType: string;
1460+
}
1461+
1462+
sequelize.addModels([User77, Subscription]);
1463+
1464+
it('should set scope in pure sequelize association options', () => {
1465+
expect(User77['associations'].usersSubscribedTo.through)
1466+
.to.have.property('scope').that.eqls({targetType: 'user'});
1467+
});
1468+
1469+
});
1470+
13411471
});
13421472

13431473
describe('One-to-one', () => {

test/specs/model-methods.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,38 @@ describe('model-methods', () => {
88
const sequelize = createSequelize();
99

1010
@Table
11-
class User extends Model<User> {
11+
class User777 extends Model<User777> {
1212

1313
@Column
1414
firstName: string;
1515

1616
@Column
1717
lastName: string;
1818

19-
static createDemoUser(): User {
19+
static createDemoUser(): User777 {
2020

21-
return new User({firstName: 'Peter', lastName: 'Parker'});
21+
return new User777({firstName: 'Peter', lastName: 'Parker'});
2222
}
2323

24-
static findDemoUser(): Promise<User> {
24+
static findDemoUser(): Promise<User777> {
2525

26-
return this.findOne<User>({where: {firstName: 'Peter', lastName: 'Parker'}});
26+
return this.findOne<User777>({where: {firstName: 'Peter', lastName: 'Parker'}});
2727
}
2828
}
2929

30-
sequelize.addModels([User]);
30+
sequelize.addModels([User777]);
3131

3232
beforeEach(() => sequelize.sync({force: true}));
3333

3434
it('should work as expected', () => {
3535

36-
const user = User.createDemoUser();
36+
const user = User777.createDemoUser();
3737

38-
expect(user).to.be.an.instanceOf(User);
38+
expect(user).to.be.an.instanceOf(User777);
3939

4040
return user
4141
.save()
42-
.then(() => User.findDemoUser())
42+
.then(() => User777.findDemoUser())
4343
.then(_user => expect(_user.equals(user)).to.be.true)
4444
;
4545
});

0 commit comments

Comments
 (0)