Skip to content

Commit

Permalink
Added easy tests for model builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Buck committed Nov 7, 2016
1 parent 102f357 commit 3373fe0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
63 changes: 63 additions & 0 deletions src/lib/builders/model.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { test } from 'ava';

import { Model } from './model';
import { Dictionary } from '../models/dictionary';
import { ModelDefinition } from '../models/model-definition';

test(`Model#name sets the friendly name`, t => {
const expectedName = 'TestModel';
const m = newModel().name(expectedName);

const modelDef = m.build();

t.is(modelDef.name, expectedName);
});

test(`Model#prop creates a new Property`, t => {
const expectedPropName = 'propName';
let expectedPropType = Date;
const m = newModel().prop(expectedPropName, x => x.type(expectedPropType));

const modelDef = m.build();

t.is(modelDef.properties[expectedPropName].type, expectedPropType);
});

test(`Model#prop mergers existing Properties`, t => {
const expectedPropName = 'propName';
const expectedPropType = Date;
const expectedLength = 10;
const m = newModel()
.prop(expectedPropName, x => x.type(expectedPropType))
.prop(expectedPropName, x => x.length(expectedLength));

const modelDef = m.build();

t.is(modelDef.properties[expectedPropName].type, expectedPropType);
t.is(modelDef.properties[expectedPropName].length, expectedLength);
});

test(`Model#key creates a Property marked as a key`, t => {
const expectedPropName = 'propName';
const m = newModel().key(expectedPropName, x => x);

const modelDef = m.build();

t.true(modelDef.properties[expectedPropName].key);
});

test(`Model#ref creates a Property marked as a reference`, t => {
const expectedPropName = 'propName';
const expectedRefType = RegExp;
const m = newModel().ref(expectedPropName, expectedRefType);

const modelDef = m.build();

t.is(modelDef.properties[expectedPropName].ref, expectedRefType);
});

test.todo(`Model#inherit links models from the cache`, null);

function newModel(id?: string) {
return new Model(new Dictionary<Model>(), { id: id || Math.random().toString(36).substr(2, 5) });
}
37 changes: 17 additions & 20 deletions src/lib/builders/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { Property } from './property';

export class Model {

private _modelDefinition: ModelDefinition;

constructor(private _modelBuilderCache: Dictionary<Model>, modelDef: ModelDefinition) {
this._modelDefinition = modelDef;
constructor(private _modelBuilderCache: Dictionary<Model>, private _modelDefinition: ModelDefinition) {
this._modelDefinition.properties = this._modelDefinition.properties || {};
}

Expand All @@ -20,6 +17,21 @@ export class Model {
return this;
}

public prop(id: string, cb: (propBuilder: Property) => Property): this {
let propDef = this._modelDefinition.properties[id] || { id };
this._modelDefinition.properties[id] = cb(new Property(propDef)).build();

return this;
}

public key(id: string, cb: (propBuilder: Property) => Property): this {
return this.prop(id, i => cb(i.key()));
}

public ref(id: string, type: Type): this {
return this.prop(id, x => x.ref(type));
}

public inherits<TParent>(id: string|Model): this {
let originalId: string;
if (typeof id === 'string') {
Expand All @@ -37,23 +49,8 @@ export class Model {
return this;
}

public prop(id: string, cb: (propBuilder: Property) => Property): this {
let propDef = this._modelDefinition.properties[id] || { id };
this._modelDefinition.properties[id] = cb(new Property(propDef)).build();

return this;
}

public key(id: string, cb: (propBuilder: Property) => Property): this {
return this.prop(id, i => cb(i.key()));
}

public ref(id: string, type: Type): this {
return this.prop(id, x => x.ref(type));
}

public build(): ModelDefinition {
// TODO: Fix this...
// TODO: Fix this...

if (this._modelDefinition.inherits) {
return Object.assign({}, this._modelDefinition.inherits.build(), this._modelDefinition);
Expand Down

0 comments on commit 3373fe0

Please sign in to comment.