Skip to content

mirage/factories: Use predictable values instead of random fake values #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions mirage/factories/category.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';
import { dasherize } from '@ember/string';

export default Factory.extend({
category(i) {
return `Category ${i}`;
category: i => `Category ${i}`,

slug() {
return dasherize(this.category);
},

id() {
return dasherize(this.category);
return this.slug;
},

slug() {
return dasherize(this.category);
description() {
return `This is the description for the category called "${this.category}"`;
},

description: () => faker.lorem.sentence(),
created_at: () => faker.date.past(),
crates_cnt: () => faker.random.number({ max: 5000 }),
created_at: '2010-06-16T21:30:45Z',
crates_cnt: 0,
});
31 changes: 15 additions & 16 deletions mirage/factories/crate.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';

export default Factory.extend({
id(i) {
return `crate-${i}`;
name: i => `crate-${i}`,

id() {
return this.name;
},

name() {
return this.id;
description() {
return `This is the description for the crate called "${this.name}"`;
},

description: () => faker.lorem.sentence(),
downloads: () => faker.random.number({ max: 10000 }),
documentation: () => faker.internet.url(),
homepage: () => faker.internet.url(),
repository: () => faker.internet.url(),
max_version: () => faker.system.semver(),
newest_version: () => faker.system.semver(),
downloads: i => (((i + 13) * 42) % 13) * 12345,

created_at: () => faker.date.past(),
updated_at() {
return faker.date.between(this.created_at, new Date());
},
documentation: null,
homepage: null,
repository: null,
max_version: '1.0.0',
newest_version: '1.0.0',

created_at: '2010-06-16T21:30:45Z',
updated_at: '2017-02-24T12:34:56Z',

badges: () => [],
categories: () => [],
Expand Down
23 changes: 10 additions & 13 deletions mirage/factories/version.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';

export default Factory.extend({
id: i => i,

// crate: '...',
const LICENSES = ['MIT/Apache-2.0', 'MIT', 'Apache-2.0'];

num: () => faker.system.semver(),
export default Factory.extend({
num: i => `1.0.${i}`,

created_at: () => faker.date.past(),
updated_at() {
return faker.date.between(this.created_at, new Date());
},
created_at: '2010-06-16T21:30:45Z',
updated_at: '2017-02-24T12:34:56Z',

yanked: false,
license: () => faker.hacker.abbreviation(),
license: i => LICENSES[i % LICENSES.length],

dl_path() {
return `/api/v1/crates/${this.crate}/${this.num}/download`;
},

downloads: () => faker.random.number({ max: 10000 }),
downloads: i => (((i + 13) * 42) % 13) * 1234,

features: () => {},
_authors: () => [],

crate_size: i => (((i + 13) * 42) % 13) * 54321,

afterCreate(version, server) {
let crate = server.schema.crates.find(version.crate);
crate.update({ versions: crate.versions.concat(parseInt(version.id, 10)) });
},
crate_size: () => faker.random.number({ max: 10000000 }),
});
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-ember": "^7.7.2",
"eslint-plugin-prettier": "^3.1.2",
"faker": "^4.1.0",
"loader.js": "^4.7.0",
"normalize.css": "^8.0.1",
"nyc": "^15.0.0",
Expand Down
12 changes: 6 additions & 6 deletions tests/acceptance/crate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module('Acceptance | crate page', function(hooks) {
test('/crates/:crate is accessible', async function(assert) {
assert.expect(0);

this.server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', max_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

Expand All @@ -28,7 +28,7 @@ module('Acceptance | crate page', function(hooks) {
test('/crates/:crate/:version is accessible', async function(assert) {
assert.expect(0);

this.server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', max_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

Expand All @@ -50,7 +50,7 @@ module('Acceptance | crate page', function(hooks) {
});

test('visiting a crate page from the front page', async function(assert) {
this.server.create('crate', { id: 'nanomsg', newest_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', newest_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

await visit('/');
Expand All @@ -64,7 +64,7 @@ module('Acceptance | crate page', function(hooks) {
});

test('visiting /crates/nanomsg', async function(assert) {
this.server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', max_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

Expand All @@ -79,7 +79,7 @@ module('Acceptance | crate page', function(hooks) {
});

test('visiting /crates/nanomsg/', async function(assert) {
this.server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', max_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

Expand All @@ -94,7 +94,7 @@ module('Acceptance | crate page', function(hooks) {
});

test('visiting /crates/nanomsg/0.6.0', async function(assert) {
this.server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
this.server.create('crate', { name: 'nanomsg', max_version: '0.6.1' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crate: 'nanomsg', num: '0.6.1' });

Expand Down
2 changes: 0 additions & 2 deletions tests/helpers/setup-mirage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import faker from 'faker';
import timekeeper from 'timekeeper';

export default function(hooks) {
setupMirage(hooks);

// To have deterministic visual tests, the seed has to be constant
hooks.beforeEach(function() {
faker.seed(12345);
timekeeper.freeze(new Date('11/20/2017 12:00'));
});

Expand Down