beforeEach: function() { var db = new Db({ users: [], homeAddresses: [ {id: 1, name: '123 Hyrule Way'}, {id: 2, name: '12 Goron City'}, ] }); schema = new Schema(db); var User = Model.extend({ homeAddresses: Mirage.hasMany() }); var HomeAddress = Model.extend(); schema.registerModels({ user: User, homeAddress: HomeAddress }); child1 = schema.homeAddress.find(1); child2 = schema.homeAddress.find(2); }
test('it can return all models', function(assert) { var db = new Db(); db.createCollection('users'); db.users.insert([{id: 1, name: 'Link'}, {id: 2, name: 'Zelda'}]); var schema = new Schema(db); var User = Model.extend(); schema.registerModel('user', User); var users = schema.user.all(); assert.ok(users instanceof Collection, 'it returns a collection'); assert.ok(users[0] instanceof User, 'each member of the collection is a model'); assert.equal(users.length, 2); assert.deepEqual(users[1].attrs, {id: '2', name: 'Zelda'}); });
test('foreign keys should be named appropriately for multiword model names', function(assert) { let schema = new Schema(new Db(), { wordSmith: Model, post: Model.extend({ author: belongsTo('word-smith') }) }); assert.deepEqual(schema._registry.wordSmith.foreignKeys, []); assert.deepEqual(schema._registry.post.foreignKeys, ['authorId']); let post = schema.posts.create(); let author = post.createAuthor(); assert.ok(post); assert.ok(author); assert.equal(author.modelName, 'word-smith'); });
test('it can return all models', function(assert) { let db = new Db({ users: [ { id: 1, name: 'Link' }, { id: 2, name: 'Zelda' } ] }); let User = Model.extend(); let schema = new Schema(db, { user: User }); let users = schema.users.all(); assert.ok(users instanceof Collection, 'it returns a collection'); assert.ok(users.models[0] instanceof User, 'each member of the collection is a model'); assert.equal(users.models.length, 2); assert.deepEqual(users.models[1].attrs, { id: '2', name: 'Zelda' }); });
test('schemas with a single belongsTo with a different property name have correct foreign keys', function(assert) { let schema = new Schema(new Db(), { user: Model, project: Model.extend({ owner: belongsTo('user') }) }); // Fks are set up correctly assert.deepEqual(schema._registry.user.foreignKeys, []); assert.deepEqual(schema._registry.project.foreignKeys, ['ownerId']); let project = schema.projects.create(); let user = project.createOwner(); assert.ok(user); assert.ok(project); });
test('foreign keys should be named appropriately for multiword properties and model names', function(assert) { let schema = new Schema(new Db(), { wordSmith: Model, post: Model.extend({ brilliantWriter: belongsTo('word-smith') }) }); assert.deepEqual(schema._registry.wordSmith.foreignKeys, []); assert.deepEqual(schema._registry.post.foreignKeys, ['brilliantWriterId']); let post = schema.posts.create(); let brilliantWriter = post.createBrilliantWriter(); assert.ok(post); assert.ok(brilliantWriter); assert.equal(brilliantWriter.modelName, 'word-smith'); });
test('schemas with a single hasMany have correct foreign keys', function(assert) { let schema = new Schema(new Db(), { user: Model.extend({ projects: hasMany() }), project: Model }); // Fks are set up correctly assert.deepEqual(schema._registry.user.foreignKeys, []); assert.deepEqual(schema._registry.project.foreignKeys, ['userId']); let user = schema.users.create(); let project = user.createProject(); assert.ok(user); assert.ok(project); });
test('foreign keys should be named appropriately for multiword properties', function(assert) { let schema = new Schema(new Db(), { author: Model, post: Model.extend({ wordSmith: belongsTo('author') }) }); // Fks are set up correctly assert.deepEqual(schema._registry.author.foreignKeys, []); assert.deepEqual(schema._registry.post.foreignKeys, ['wordSmithId']); let post = schema.posts.create(); let wordSmith = post.createWordSmith(); assert.ok(post); assert.ok(wordSmith); assert.equal(wordSmith.modelName, 'author'); });
beforeEach: function() { this.server = new Server({ environment: 'development', modelsMap: { author: Model.extend(), } }); this.server.timing = 0; this.server.logging = false; this.authors = [ {id: 1, name: 'Ganon'}, ]; this.server.db.loadData({ authors: this.authors }); this.schema = this.server.schema; },
test('a model can have multiple belongsTo associations of the same type', function(assert) { let schema = new Schema(new Db(), { user: Model, project: Model.extend({ admin: belongsTo('user'), specialUser: belongsTo('user') }) }); assert.deepEqual(schema._registry.user.foreignKeys, []); assert.deepEqual(schema._registry.project.foreignKeys, ['adminId', 'specialUserId']); let project = schema.projects.create(); let admin = project.createAdmin(); let specialUser = project.createSpecialUser(); assert.ok(project); assert.ok(admin); assert.equal(admin.modelName, 'user'); assert.ok(specialUser); assert.equal(specialUser.modelName, 'user'); });
beforeEach: function() { this.server = new Server({ environment: 'development', models: { author: Model.extend({}) } }); this.server.timing = 0; this.server.logging = false; this.schema = this.server.schema; this.serializer = new JSONAPISerializer(); this.body = { data: { type: 'authors', attributes: { 'first-name': 'Ganon', 'last-name': 'Dorf', } } }; },
import ActiveModelSerializer from 'ember-cli-mirage/serializers/active-model-serializer'; import { hasMany, belongsTo } from 'ember-cli-mirage'; import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; import SerializerRegistry from 'ember-cli-mirage/serializer-registry'; import { module, test } from 'qunit'; module('Integration | Serializer | ActiveModelSerializer', { beforeEach() { let db = new Db(); this.schema = new Schema(db); this.schema.registerModels({ wordSmith: Model.extend({ blogPosts: hasMany() }), blogPost: Model.extend({ wordSmith: belongsTo() }) }); let link = this.schema.wordSmith.create({ name: 'Link', age: 123 }); link.createBlogPost({ title: 'Lorem' }); link.createBlogPost({ title: 'Ipsum' }); this.schema.wordSmith.create({ name: 'Zelda', age: 230 }); this.registry = new SerializerRegistry(this.schema, { application: ActiveModelSerializer, wordSmith: ActiveModelSerializer.extend({ attrs: ['id', 'name'],
import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; import {module, test} from 'qunit'; let schema; let User = Model.extend(); module('Integration | ORM | #first', function(hooks) { hooks.beforeEach(function() { let db = new Db(); db.createCollection('users'); db.users.insert([{ id: 1, name: 'Link' }, { id: 2, name: 'Zelda' }]); schema = new Schema(db); schema.registerModel('user', User); }); test('it can find the first model', function(assert) { let user = schema.users.first(); assert.ok(user instanceof User); assert.deepEqual(user.attrs, { id: '1', name: 'Link' }); }); });
import Model from 'ember-cli-mirage/orm/model'; import Schema from 'ember-cli-mirage/orm/schema'; import Db from 'ember-cli-mirage/db'; import {module, test} from 'qunit'; var schema, link; module('Integration | Schema | belongsTo instantiating with params', { beforeEach() { let db = new Db({ users: [ { id: 1, name: 'Link' } ], addresses: [] }); let User = Model.extend(); let Address = Model.extend({ user: Mirage.belongsTo() }); schema = new Schema(db); schema.registerModels({ user: User, address: Address }); link = schema.user.find(1); } }); test('the child accepts a saved parents id', function(assert) {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers import Mirage from 'ember-cli-mirage'; import Server from 'ember-cli-mirage/server'; import Model from 'ember-cli-mirage/orm/model'; import Serializer from 'ember-cli-mirage/serializer'; import {module, test} from 'qunit'; module('Integration | Serializers | Base | Full Request', { beforeEach() { this.server = new Server({ environment: 'development', models: { author: Model.extend({ posts: Mirage.hasMany() }), post: Model.extend({ author: Mirage.belongsTo(), comments: Mirage.hasMany() }), comment: Model.extend({ post: Mirage.belongsTo() }) }, serializers: { application: Serializer.extend({ embed: true, root: false }), author: Serializer.extend({ embed: true, attrs: ['id', 'first'],
import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; import SerializerRegistry from 'ember-cli-mirage/serializer-registry'; import { belongsTo, hasMany } from 'ember-cli-mirage'; import { module, test } from 'qunit'; module('Integration | Mirage Serializer | V2Serializer', { beforeEach() { let db = new Db(); this.schema = new Schema(db); this.schema.registerModels({ book: Model.extend({ author: belongsTo() }), author: Model.extend({ books: hasMany() }), blogPost: Model.extend() }); const author = this.schema.authors.create({ name: 'User Name' }); author.createBook({ title: 'A Book' }); author.createBook({ title: 'A Different Book' }); this.schema.blogPosts.create({ title: 'A Blog Post' }); this.registry = new SerializerRegistry(this.schema, {
import {module, test} from 'qunit'; var schema, child1, child2; module('Integration | Schema | hasMany instantiating with params', { beforeEach() { let db = new Db({ users: [], homeAddresses: [ { id: 1, name: '123 Hyrule Way' }, { id: 2, name: '12 Goron City' } ] }); schema = new Schema(db); let User = Model.extend({ homeAddresses: Mirage.hasMany() }); let HomeAddress = Model.extend(); schema.registerModels({ user: User, homeAddress: HomeAddress }); child1 = schema.homeAddress.find(1); child2 = schema.homeAddress.find(2); } }); test('children have fks added to their attrs', function(assert) { let newChild = schema.homeAddress.new();
import {module, test} from 'qunit'; import Server from 'ember-cli-mirage/server'; import Model from 'ember-cli-mirage/orm/model'; import PostShorthandRouteHandler from 'ember-cli-mirage/route-handlers/shorthands/post'; import JSONAPISerializer from 'ember-cli-mirage/serializers/json-api-serializer'; module('Integration | Route Handlers | POST shorthand', { beforeEach() { this.server = new Server({ environment: 'development', models: { author: Model.extend({}) } }); this.server.timing = 0; this.server.logging = false; this.schema = this.server.schema; this.serializer = new JSONAPISerializer(); this.body = { data: { type: 'authors', attributes: { 'first-name': 'Ganon', 'last-name': 'Dorf' } } }; }, afterEach() {
// jscs:disable disallowVar import Model from 'ember-cli-mirage/orm/model'; import Schema from 'ember-cli-mirage/orm/schema'; import Db from 'ember-cli-mirage/db'; import Inflector from 'ember-inflector'; import {module, test} from 'qunit'; var db, schema, HeadOfState; module('Integration | ORM | inflector-collectionName integration', { beforeEach() { Inflector.inflector.irregular('head-of-state', 'heads-of-state'); HeadOfState = Model.extend(); db = new Db({}); schema = new Schema(db); schema.registerModel('headOfState', HeadOfState); } }); test(' [regression] collection creation respects irregular plural rules', function(assert) { assert.equal(schema.db._collections.length, 1); assert.equal(schema.db._collections[0].name, 'headsOfState'); });
import Mirage from 'ember-cli-mirage'; import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; export default { setup() { return new Schema(new Db(), { wordSmith: Model.extend({ blogPosts: Mirage.hasMany() }), blogPost: Model.extend({ wordSmith: Mirage.belongsTo(), fineComments: Mirage.hasMany() }), fineComment: Model.extend({ blogPost: Mirage.belongsTo() }), greatPhoto: Model, foo: Model.extend({ bar: Mirage.belongsTo() }), bar: Model.extend({ baz: Mirage.belongsTo() }), baz: Model.extend({ quuxes: Mirage.hasMany() }), quux: Model.extend({
// jscs:disable disallowVar import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; import Collection from 'ember-cli-mirage/orm/collection'; import {module, test} from 'qunit'; var schema; var User = Model.extend(); module('Integration | ORM | #find', { beforeEach() { let db = new Db({ users: [ { id: 1, name: 'Link' }, { id: 2, name: 'Zelda' } ] }); schema = new Schema(db, { user: User }); } }); test('it can find a model by id', function(assert) { let zelda = schema.users.find(2); assert.ok(zelda instanceof User); assert.deepEqual(zelda.attrs, { id: '2', name: 'Zelda' }); }); test('it returns null if no model is found for an id', function(assert) { let user = schema.users.find(4);
import Mirage from 'ember-cli-mirage'; import Schema from 'ember-cli-mirage/orm/schema'; import Model from 'ember-cli-mirage/orm/model'; import Db from 'ember-cli-mirage/db'; export default { setup() { let db = new Db(); this.schema = new Schema(db); this.schema.registerModels({ wordSmith: Model.extend({ blogPosts: Mirage.hasMany() }), blogPost: Model.extend({ wordSmith: Mirage.belongsTo(), fineComments: Mirage.hasMany() }), fineComment: Model.extend({ blogPost: Mirage.belongsTo() }), greatPhoto: Model }); return this.schema; } };