Skip to content

Commit

Permalink
Add Inheritance Tests
Browse files Browse the repository at this point in the history
- add more inheritance tests
- add support for custom discriminator properties
- add ".idea" to gitignore
- prop: add support for extended array items

this is a squash merge of #21
  • Loading branch information
B-Stefan authored and hasezoey committed Sep 24, 2019
1 parent b13c5f8 commit 35a9681
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties

.idea
### Linux ###
*~

Expand Down
8 changes: 4 additions & 4 deletions src/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ function baseProp(

switch (whatis) {
case WhatIsIt.ARRAY:
const virtualSchemaArrayItem = buildSchema(Type, {
_id: typeof rawOptions._id === 'boolean' ? rawOptions._id : true
});
schemas.get(name)[key] = {
...schemas.get(name)[key][0], // [0] is needed, because "initasArray" adds this (empty)
...options,
type: [{
...(typeof options._id === 'boolean' ? { _id: options._id } : {}),
...subSchema
}]
type: [virtualSchemaArrayItem]
};

return;
Expand Down
7 changes: 6 additions & 1 deletion src/typegoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ export function getDiscriminatorModelForClass<T, U extends AnyParamConstructor<T
if (models.get(name)) {
return models.get(name) as ReturnModelType<U, T>;
}
const sch = buildSchema(cl) as mongoose.Schema & {paths: object};

const discriminatorKey = sch.get('discriminatorKey');
if (sch.path(discriminatorKey)) {
sch.paths[discriminatorKey].options.$skipDiscriminatorCheck = true;
}

const sch = buildSchema(cl);
const model = from.discriminator(name, sch, id ? id : name);

return addModelToTypegoose(model, cl);
Expand Down
3 changes: 3 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { suite as ShouldAddTest } from './tests/shouldAdd.test';
import { suite as ShouldRunTests } from './tests/shouldRun.test';
import { suite as StringValidatorTests } from './tests/stringValidator.test';
import { suite as TypeguardsTest } from './tests/typeguards.test';
import { suite as Inheritance } from './tests/inheritance.test';

import { connect, disconnect } from './utils/mongooseConnect';

Expand Down Expand Up @@ -44,5 +45,7 @@ describe('Typegoose', () => {

describe('Ref tests', RefTest.bind(this));

describe('inheritance', Inheritance.bind(this));

describe('customName', customNameTests.bind(this));
});
8 changes: 8 additions & 0 deletions test/models/discriminators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { getDiscriminatorModelForClass, getModelForClass, prop } from '../../src
export class DisMain {
@prop({ required: true, default: 'hello main1' })
public main1: string;

@prop({ default: undefined })
// tslint:disable-next-line:variable-name
public __t: string;
}

export class DisAbove extends DisMain {
@prop({ required: true, default: 'hello above1' })
public above1: string;

@prop({ default: 'DisAbove' })
// tslint:disable-next-line:variable-name
public __t: string;
}

export const DisMainModel = getModelForClass(DisMain);
Expand Down
41 changes: 41 additions & 0 deletions test/models/inheritanceClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { arrayProp, prop } from '../../src/prop';
import { getModelForClass, modelOptions } from '../../src/typegoose';

@modelOptions({
schemaOptions: {
discriminatorKey: 'width',
collection: 'buildings'
}
})
export class Building {
@prop({default: 100})
public width: number;
}

export class OfficeBuilding extends Building {
@prop({default: 4})
public doors: number;
}

export class Garage extends Building {
@prop({default: 10})
public slotsForCars: number;
}

@modelOptions({
schemaOptions: {
collection: 'skyscrapers'
}
})
export class Skyscraper extends OfficeBuilding {
@prop({default: 'Some cool string'})
public name: string;

@prop()
public mainGarage: Garage;

@arrayProp({items: Garage})
public garagesInArea: Garage[];
}

export const model = getModelForClass<Skyscraper, any >(Skyscraper);
43 changes: 43 additions & 0 deletions test/tests/inheritance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import { model as inheritanceClass, Skyscraper } from '../models/inheritanceClass';

export function suite() {
it('should set all direct parent props', async () => {
const instance = await inheritanceClass.create({});
expect(instance.name).to.equals('Some cool string');
expect(instance.doors).to.equals(4);
expect(instance.width).to.equals(100);
});

it('should merge all parent schema options', async () => {
const instance = await inheritanceClass.create({});
expect(instance.schema.get('collection')).to.equals('skyscrapers');
expect(instance.schema.get('discriminatorKey')).to.equals('width');
});

it('should set all parent props for nested schemas', async () => {
const input = {
mainGarage: {
slotsForCars: 3
}
} as Skyscraper;
const instance = await inheritanceClass.create(input);
expect(instance.mainGarage.slotsForCars).to.equals(3);
expect(instance.mainGarage.width).to.equals(100);
expect(instance.mainGarage.doors).to.equals(undefined);
});

it('should set all parent props for nested array items', async () => {
const input = {
garagesInArea: [{
slotsForCars: 2
}]
} as Skyscraper;
const instance = await inheritanceClass.create(input);
expect(instance.garagesInArea).to.be.lengthOf(1);
const firstGarage = instance.garagesInArea.pop();
expect(firstGarage.slotsForCars).to.equals(2);
expect(firstGarage.width).to.equals(100);
expect(firstGarage.doors).to.be.equals(undefined);
});
}
6 changes: 2 additions & 4 deletions test/tests/shouldRun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ export function suite() {
expect(dmmdoc).to.not.be.an('undefined');
expect(dmmdoc.main1).to.equals('hello DMM');
expect(dmmdoc).to.not.have.property('above1');
// any is required otherwise typescript complains about "__t" not existing
expect((dmmdoc as any).__t).to.be.an('undefined');
expect(dmmdoc.__t).to.be.an('undefined');

expect(damdoc).to.not.be.an('undefined');
expect(damdoc.main1).to.equals('hello DAM');
expect(damdoc.above1).to.equals('hello DAM');
// any is required otherwise typescript complains about "__t" not existing
expect((damdoc as any).__t).to.equals('DisAbove');
expect(damdoc.__t).to.equals('DisAbove');
});

it('should make use of addModelToTypegoose', async () => {
Expand Down

0 comments on commit 35a9681

Please sign in to comment.