Skip to content

Commit

Permalink
Merge d20950e into 1fd35f4
Browse files Browse the repository at this point in the history
  • Loading branch information
SharonItz committed Jan 11, 2019
2 parents 1fd35f4 + d20950e commit c41737a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('build-schema', () => {
class NoPropertyMeta {
prop: string;
}

@model()
class OnePropertyDecorated {
@property()
Expand All @@ -64,6 +65,7 @@ describe('build-schema', () => {

it('does not convert models that have not been decorated with @model()', () => {
class Empty {}

class NoModelMeta {
@property()
foo: string;
Expand Down Expand Up @@ -106,6 +108,7 @@ describe('build-schema', () => {
const topMeta = {
description: 'Test description',
};

@model(topMeta)
class TestModel {
@property()
Expand Down Expand Up @@ -457,12 +460,8 @@ describe('build-schema', () => {
});
expect(customerSchema.properties).to.deepEqual({
id: {type: 'number'},
orders: {
type: 'array',
items: {$ref: '#/definitions/Order'},
},
});
expect(customerSchema.definitions).to.deepEqual({
expect(customerSchema.definitions).to.not.containEql({
Order: {
title: 'Order',
properties: {
Expand Down Expand Up @@ -527,57 +526,52 @@ describe('build-schema', () => {
});
});
});

function expectValidJsonSchema(schema: JsonSchema) {
const ajv = new Ajv();
const validate = ajv.compile(
require('ajv/lib/refs/json-schema-draft-06.json'),
);
const isValid = validate(schema);
const result = isValid
? 'JSON Schema is valid'
: ajv.errorsText(validate.errors!);
expect(result).to.equal('JSON Schema is valid');
}
});

describe('getjsonSchema', () => {
it('gets cached JSON schema if one exists', () => {
@model()
class TestModel {
@property()
foo: number;
}
const cachedSchema: JsonSchema = {
properties: {
cachedProperty: {
type: 'string',
},
},
};
MetadataInspector.defineMetadata(
JSON_SCHEMA_KEY,
cachedSchema,
TestModel,
);

const jsonSchema = getJsonSchema(TestModel);
expect(jsonSchema).to.eql(cachedSchema);
});
function expectValidJsonSchema(schema: JsonSchema) {
const ajv = new Ajv();
const validate = ajv.compile(
require('ajv/lib/refs/json-schema-draft-06.json'),
);
const isValid = validate(schema);
const result = isValid
? 'JSON Schema is valid'
: ajv.errorsText(validate.errors!);
expect(result).to.equal('JSON Schema is valid');
}
});

it('creates JSON schema if one does not already exist', () => {
@model()
class NewModel {
@property()
newProperty: string;
}
describe('getjsonSchema', () => {
it('gets cached JSON schema if one exists', () => {
@model()
class TestModel {
@property()
foo: number;
}

const jsonSchema = getJsonSchema(NewModel);
expect(jsonSchema.properties).to.deepEqual({
newProperty: {
const cachedSchema: JsonSchema = {
properties: {
cachedProperty: {
type: 'string',
},
});
},
};
MetadataInspector.defineMetadata(JSON_SCHEMA_KEY, cachedSchema, TestModel);
const jsonSchema = getJsonSchema(TestModel);
expect(jsonSchema).to.eql(cachedSchema);
});
it('creates JSON schema if one does not already exist', () => {
@model()
class NewModel {
@property()
newProperty: string;
}

const jsonSchema = getJsonSchema(NewModel);
expect(jsonSchema.properties).to.deepEqual({
newProperty: {
type: 'string',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {property} from '../../decorators/model.decorator';
import {Entity, EntityResolver} from '../../model';
import {relation} from '../relation.decorator';
import {HasManyDefinition, RelationType} from '../relation.types';
Expand All @@ -21,8 +20,6 @@ export function hasMany<T extends Entity>(
definition?: Partial<HasManyDefinition>,
) {
return function(decoratedTarget: Object, key: string) {
property.array(targetResolver)(decoratedTarget, key);

const meta: HasManyDefinition = Object.assign(
// default values, can be customized by the caller
{name: key},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ describe('HasMany relation', () => {
expect(_.pick(orders[0], ['customerId', 'description'])).to.eql(newOrder);
});

it('does not create an array of the related model', async () => {
await expect(
customerRepo.create({
name: 'a customer',
orders: [
{
description: 'order 1',
},
],
}),
).to.be.rejectedWith(/`orders` is not defined/);
});

// This should be enforced by the database to avoid race conditions
it.skip('reject create request when the customer does not exist');

Expand Down Expand Up @@ -170,6 +183,7 @@ describe('HasMany relation', () => {

function givenApplicationWithMemoryDB() {
class TestApp extends RepositoryMixin(Application) {}

app = new TestApp();
app.dataSource(new juggler.DataSource({name: 'db', connector: 'memory'}));
}
Expand Down
28 changes: 2 additions & 26 deletions packages/repository/test/unit/decorator/relation.decorator.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ describe('relation decorator', () => {
source: AddressBook,
target: () => Address,
});

expect(jugglerMeta).to.eql({
expect(jugglerMeta).to.not.containEql({
type: Array,
itemType: () => Address,
});

expect(AddressBook.definition.relations).to.eql({
addresses: {
type: RelationType.hasMany,
Expand Down Expand Up @@ -100,33 +98,11 @@ describe('relation decorator', () => {
target: () => Address,
keyTo: 'someForeignKey',
});
expect(jugglerMeta).to.eql({
expect(jugglerMeta).to.not.containEql({
type: Array,
itemType: () => Address,
});
});

context('when interacting with @property.array', () => {
it('does not get its property metadata overwritten by @property.array', () => {
expect(() => {
class Address extends Entity {
addressId: number;
street: string;
province: string;
}

// tslint:disable-next-line:no-unused
class AddressBook extends Entity {
id: number;
@property.array(Entity)
@hasMany(() => Address, {
keyTo: 'someForeignKey',
})
addresses: Address[];
}
}).to.throw(/Decorator cannot be applied more than once/);
});
});
});

describe('belongsTo', () => {
Expand Down

0 comments on commit c41737a

Please sign in to comment.