Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jul 19, 2019
1 parent c10aa1a commit baa730c
Show file tree
Hide file tree
Showing 6 changed files with 1,171 additions and 1,224 deletions.
24 changes: 13 additions & 11 deletions lib/mem-server-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var chalk = _interopDefault(require('ansi-colors'));
var chalk$1 = _interopDefault(require('ansi-colors'));
var FakeXMLHttpRequest = _interopDefault(require('fake-xml-http-request'));
var RouteRecognizer = _interopDefault(require('route-recognizer'));
require('pretender');
var qs = _interopDefault(require('qs'));
var Inflector = _interopDefault(require('i'));
var stringUtils = _interopDefault(require('ember-cli-string-utils'));

const chalk = require('ansi-colors');

function primaryKeyTypeSafetyCheck(targetPrimaryKeyType, primaryKey, modelName) {
const primaryKeyType = typeof primaryKey;

Expand All @@ -33,7 +35,7 @@ function startServer(Server, options={}) {
window.Pretender.prototype.timing = options.timing;

let pretender = new window.Pretender(function() {
const MemServer = chalk.cyan('[MemServer]');
const MemServer = chalk$1.cyan('[MemServer]');

if (options.logging) {
this.handledRequest = function(verb, path, request) {
Expand All @@ -48,13 +50,13 @@ function startServer(Server, options={}) {
console.log(JSON.parse(request.responseText));
};
this.passthroughRequest = function(verb, path, request) {
console.log(MemServer, chalk.yellow('[PASSTHROUGH]'), verb, request.url);
console.log(MemServer, chalk$1.yellow('[PASSTHROUGH]'), verb, request.url);
};
}

this.unhandledRequest = function(verb, path, request) {
console.log(MemServer, chalk.red('[UNHANDLED REQUEST]', verb, path));
console.log(chalk.red('UNHANDLED REQUEST WAS:\n'), request);
console.log(MemServer, chalk$1.red('[UNHANDLED REQUEST]', verb, path));
console.log(chalk$1.red('UNHANDLED REQUEST WAS:\n'), request);
console.log(request);
};
}, { trackRequests: false });
Expand Down Expand Up @@ -88,12 +90,12 @@ function startServer(Server, options={}) {

function colorStatusCode(statusCode) {
if (statusCode === 200 || statusCode === 201) {
return chalk.green(statusCode);
return chalk$1.green(statusCode);
} else if (statusCode === 404 || statusCode === 204) {
return chalk.cyan(statusCode);
return chalk$1.cyan(statusCode);
}

return chalk.red(statusCode);
return chalk$1.red(statusCode);
}

const { classify } = stringUtils;
Expand Down Expand Up @@ -263,7 +265,7 @@ function getDefaultRouteHandler(verb, path) {
const ResourceModel = targetNamespace$1.MemServer.Models[classify(resourceName)];

if (!ResourceModel) {
throw new Error(chalk.red(`[MemServer] ${verb} ${path} route handler cannot be generated automatically: ${classify(resourceName)} is not a valid MemServer.Model, please check that your route name matches the model reference or create a custom handler function`));
throw new Error(chalk$1.red(`[MemServer] ${verb} ${path} route handler cannot be generated automatically: ${classify(resourceName)} is not a valid MemServer.Model, please check that your route name matches the model reference or create a custom handler function`));
} else if (verb === 'GET') {
if (lastPath.includes(':')) {
return (request) => {
Expand Down Expand Up @@ -351,9 +353,9 @@ function resetDatabase(models, modelFixtureTree) {
const primaryKey = getModelPrimaryKey(model, existingPrimaryKey, modelName);

if (!primaryKey) {
throw new Error(chalk.red(`[MemServer] DATABASE ERROR: At least one of your ${modelName} fixtures missing a primary key. Please make sure all your ${modelName} fixtures have either id or uuid primaryKey`));
throw new Error(chalk$1.red(`[MemServer] DATABASE ERROR: At least one of your ${modelName} fixtures missing a primary key. Please make sure all your ${modelName} fixtures have either id or uuid primaryKey`));
} else if (primaryKeys.includes(model[primaryKey])) {
throw new Error(chalk.red(`[MemServer] DATABASE ERROR: Duplication in ${modelName} fixtures with ${primaryKey}: ${model[primaryKey]}`));
throw new Error(chalk$1.red(`[MemServer] DATABASE ERROR: Duplication in ${modelName} fixtures with ${primaryKey}: ${model[primaryKey]}`));
}

const existingAttributes = targetNamespace$2.MemServer.Models[modelName].attributes;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from 'ansi-colors';
const chalk = require('ansi-colors');

export function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
Expand Down
34 changes: 18 additions & 16 deletions model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var util = _interopDefault(require('util'));
var chalk = _interopDefault(require('ansi-colors'));
var chalk$1 = _interopDefault(require('ansi-colors'));
var Inflector = _interopDefault(require('i'));
var emberCliStringUtils = require('ember-cli-string-utils');

const chalk = require('ansi-colors');

function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
Expand Down Expand Up @@ -43,7 +45,7 @@ function model(options) {
},
find(param) {
if (!param) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.find(id) cannot be called without a valid id`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.find(id) cannot be called without a valid id`));
} else if (Array.isArray(param)) {
const models = Array.from(targetNamespace.MemServer.DB[this.modelName] || []);

Expand All @@ -53,7 +55,7 @@ function model(options) {
return foundModel ? result.concat([foundModel]) : result;
}, []);
} else if (typeof param !== 'number') {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.find(id) cannot be called without a valid id`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.find(id) cannot be called without a valid id`));
}

const models = Array.from(targetNamespace.MemServer.DB[this.modelName] || []);
Expand All @@ -62,7 +64,7 @@ function model(options) {
},
findBy(options) {
if (!options) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.findBy(id) cannot be called without a parameter`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.findBy(id) cannot be called without a parameter`));
}

const keys = Object.keys(options);
Expand Down Expand Up @@ -110,7 +112,7 @@ function model(options) {
const existingRecord = target.id ? this.find(target.id) : this.findBy({ uuid: target.uuid });

if (existingRecord) {
throw new Error(chalk.red(`[MemServer] ${this.modelName} ${this.primaryKey} ${target[this.primaryKey]} already exists in the database! ${this.modelName}.insert(${util.inspect(options)}) fails`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName} ${this.primaryKey} ${target[this.primaryKey]} already exists in the database! ${this.modelName}.insert(${util.inspect(options)}) fails`));
}

Object.keys(target)
Expand All @@ -123,20 +125,20 @@ function model(options) {
},
update(record) {
if (!record || (!record.id && !record.uuid)) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update(record) requires id or uuid primary key to update a record`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.update(record) requires id or uuid primary key to update a record`));
}

const targetRecord = record.id ? this.find(record.id) : this.findBy({ uuid: record.uuid });

if (!targetRecord) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update(record) failed because ${this.modelName} with ${this.primaryKey}: ${record[this.primaryKey]} does not exist`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.update(record) failed because ${this.modelName} with ${this.primaryKey}: ${record[this.primaryKey]} does not exist`));
}

const recordsUnknownAttribute = Object.keys(record)
.find((attribute) => !this.attributes.includes(attribute));

if (recordsUnknownAttribute) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update ${this.primaryKey}: ${record[this.primaryKey]} fails, ${this.modelName} model does not have ${recordsUnknownAttribute} attribute to update`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.update ${this.primaryKey}: ${record[this.primaryKey]} fails, ${this.modelName} model does not have ${recordsUnknownAttribute} attribute to update`));
}

return Object.assign(targetRecord, record);
Expand All @@ -145,15 +147,15 @@ function model(options) {
const models = targetNamespace.MemServer.DB[this.modelName] || [];

if (models.length === 0) {
throw new Error(chalk.red(`[MemServer] ${this.modelName} has no records in the database to delete. ${this.modelName}.delete(${util.inspect(record)}) failed`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName} has no records in the database to delete. ${this.modelName}.delete(${util.inspect(record)}) failed`));
} else if (!record) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.delete(model) model object parameter required to delete a model`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.delete(model) model object parameter required to delete a model`));
}

const targetRecord = record.id ? this.find(record.id) : this.findBy({ uuid: record.uuid });

if (!targetRecord) {
throw new Error(chalk.red(`[MemServer] Could not find ${this.modelName} with ${this.primaryKey} ${record[this.primaryKey]} to delete. ${this.modelName}.delete(${util.inspect(record)}) failed`));
throw new Error(chalk$1.red(`[MemServer] Could not find ${this.modelName} with ${this.primaryKey} ${record[this.primaryKey]} to delete. ${this.modelName}.delete(${util.inspect(record)}) failed`));
}

const targetIndex = models.indexOf(targetRecord);
Expand All @@ -164,13 +166,13 @@ function model(options) {
},
embed(relationship) { // EXAMPLE: { comments: Comment }
if (typeof relationship !== 'object' || relationship.modelName) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.embed(relationshipObject) requires an object as a parameter: { relationshipKey: $RelationshipModel }`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.embed(relationshipObject) requires an object as a parameter: { relationshipKey: $RelationshipModel }`));
}

const key = Object.keys(relationship)[0];

if (!relationship[key]) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.embed() fails: ${key} Model reference is not a valid. Please put a valid $ModelName to ${this.modelName}.embed()`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.embed() fails: ${key} Model reference is not a valid. Please put a valid $ModelName to ${this.modelName}.embed()`));
}

return Object.assign(this.embedReferences, relationship);
Expand All @@ -187,7 +189,7 @@ function model(options) {
},
serialize(object) { // NOTE: add links object ?
if (Array.isArray(object)) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.serialize(object) expects an object not an array. Use ${this.modelName}.serializer(data) for serializing array of records`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.serialize(object) expects an object not an array. Use ${this.modelName}.serializer(data) for serializing array of records`));
}

const objectWithAllAttributes = this.attributes.reduce((result, attribute) => {
Expand All @@ -207,15 +209,15 @@ function model(options) {
},
getRelationship(parentObject, relationshipName, relationshipModel) {
if (Array.isArray(parentObject)) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.getRelationship expects model input to be an object not an array`));
throw new Error(chalk$1.red(`[MemServer] ${this.modelName}.getRelationship expects model input to be an object not an array`));
}

const targetRelationshipModel = relationshipModel ||
targetNamespace.MemServer.Models[emberCliStringUtils.classify(singularize(relationshipName))];
const hasManyRelationship = pluralize(relationshipName) === relationshipName;

if (!targetRelationshipModel) {
throw new Error(chalk.red(`[MemServer] ${relationshipName} relationship could not be found on ${this.modelName} model. Please put the ${relationshipName} Model object as the third parameter to ${this.modelName}.getRelationship function`));
throw new Error(chalk$1.red(`[MemServer] ${relationshipName} relationship could not be found on ${this.modelName} model. Please put the ${relationshipName} Model object as the third parameter to ${this.modelName}.getRelationship function`));
} else if (hasManyRelationship) {
if (parentObject.id) {
const hasManyIDRecords = targetRelationshipModel.findAll({
Expand Down
Loading

0 comments on commit baa730c

Please sign in to comment.