ember-data-factory-guy 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/.travis.yml +7 -0
- data/Gruntfile.js +18 -3
- data/README.md +122 -16
- data/bower.json +6 -2
- data/dist/ember-data-factory-guy.js +264 -107
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/dist/factory_guy_has_many.js +188 -0
- data/ember-data-factory-guy.gemspec +1 -1
- data/lib/ember-data-factory-guy.rb +0 -1
- data/package.json +4 -4
- data/src/factory_guy.js +114 -89
- data/src/{factory_guy_helper_mixin.js → factory_guy_test_mixin.js} +2 -1
- data/src/model_definition.js +98 -0
- data/src/sequence.js +16 -0
- data/src/store.js +40 -23
- data/tests/active_model_adapter_factory_test.js +45 -5
- data/tests/factory_guy_test.js +93 -0
- data/tests/fixture_adapter_factory_test.js +14 -40
- data/tests/index.html +1 -0
- data/tests/rest_adapter_factory_test.js +18 -4
- data/tests/support/factories/project_factory.js +3 -1
- data/tests/support/factories/user_factory.js +1 -1
- data/tests/support/libs/sinon.js +4287 -0
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +2 -2
- data/tests/test_setup.js +4291 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +155 -113
- metadata +8 -3
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gruntfile.js
CHANGED
@@ -6,17 +6,32 @@ module.exports = function(grunt) {
|
|
6
6
|
|
7
7
|
concat: {
|
8
8
|
dist: {
|
9
|
-
src: [
|
9
|
+
src: [
|
10
|
+
'src/sequence.js',
|
11
|
+
'src/model_definition.js',
|
12
|
+
'src/factory_guy.js',
|
13
|
+
'src/store.js',
|
14
|
+
'src/factory_guy_test_mixin.js'],
|
10
15
|
dest: "dist/ember-data-factory-guy.js"
|
11
16
|
},
|
17
|
+
extra: {
|
18
|
+
src: ['src/has_many.js'],
|
19
|
+
dest: "dist/factory_guy_has_many.js"
|
20
|
+
},
|
12
21
|
gem: {
|
13
22
|
files: {
|
14
|
-
"vendor/assets/javascripts/ember_data_factory_guy.js": [
|
23
|
+
"vendor/assets/javascripts/ember_data_factory_guy.js": [
|
24
|
+
'src/factory_guy.js',
|
25
|
+
'src/store.js',
|
26
|
+
'src/factory_guy_test_mixin.js'],
|
15
27
|
"vendor/assets/javascripts/factory_guy_has_many.js": ['src/has_many.js']
|
16
28
|
}
|
17
29
|
},
|
18
30
|
test: {
|
19
|
-
src: [
|
31
|
+
src: [
|
32
|
+
'tests/support/factories/*.js',
|
33
|
+
'tests/support/models/*.js',
|
34
|
+
'tests/support/libs/*.js'],
|
20
35
|
dest: "tests/test_setup.js"
|
21
36
|
}
|
22
37
|
},
|
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
Ember Data Factory Guy [![Build Status](https://secure.travis-ci.org/danielspaniel/ember-data-factory-guy.png?branch=master)](http://travis-ci.org/danielspaniel/ember-data-factory-guy)
|
2
|
+
=================
|
3
|
+
|
1
4
|
# Using as Gem
|
2
5
|
|
3
6
|
To Use with in Rails project or project with sprockets:
|
@@ -14,6 +17,22 @@ then:
|
|
14
17
|
$ bundle install
|
15
18
|
```
|
16
19
|
|
20
|
+
# Using as bower component
|
21
|
+
|
22
|
+
Add as one of your dependencies in bower.json file:
|
23
|
+
```json
|
24
|
+
"dependencies": {
|
25
|
+
"foo-dependency": "latest",
|
26
|
+
"other-foo-dependency": "latest",
|
27
|
+
"ember-data-factory-guy": "latest"
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
then:
|
32
|
+
```
|
33
|
+
$ bower install
|
34
|
+
```
|
35
|
+
|
17
36
|
# How this works
|
18
37
|
|
19
38
|
Add fixtures to the store using the:
|
@@ -22,6 +41,16 @@ Add fixtures to the store using the:
|
|
22
41
|
* DS.RestAdapter
|
23
42
|
* DS.ActiveModelAdapter
|
24
43
|
|
44
|
+
NOTE: The benefit of using FactoryGuy is that you can run your tests with the
|
45
|
+
default adapter that you store normally uses. In other words: You do not have
|
46
|
+
to use the DS.FixtureAdapter. But if you do choose to use the Fixture adapter,
|
47
|
+
which does not run any faster, and does not handle associations as elegantly ( if at all ),
|
48
|
+
you may run into problems with accessing associations.
|
49
|
+
If you do get these errors try requiring the factory_guy_has_many.js file
|
50
|
+
( located in dist dir and vendor dir ) AFTER you require ember-data,
|
51
|
+
but BEFORE you require your models.
|
52
|
+
|
53
|
+
|
25
54
|
```javascript
|
26
55
|
|
27
56
|
////////////////////////////////////////////
|
@@ -29,6 +58,7 @@ Add fixtures to the store using the:
|
|
29
58
|
|
30
59
|
User = DS.Model.extend({
|
31
60
|
name: DS.attr 'string',
|
61
|
+
type: DS.attr 'string',
|
32
62
|
projects: DS.hasMany 'project'
|
33
63
|
})
|
34
64
|
|
@@ -37,15 +67,23 @@ Add fixtures to the store using the:
|
|
37
67
|
})
|
38
68
|
|
39
69
|
////////////////////////////////////////////
|
40
|
-
//
|
41
|
-
|
70
|
+
// FactoryGuy definitions for models
|
42
71
|
FactoryGuy.define('user', {
|
72
|
+
// sequences to be used in attributes definition
|
73
|
+
sequences: {
|
74
|
+
userName: function(num) {
|
75
|
+
return 'User' + num;
|
76
|
+
}
|
77
|
+
},
|
78
|
+
|
43
79
|
// default values for 'user' attributes
|
44
80
|
default: {
|
45
|
-
|
81
|
+
type: 'normal'
|
82
|
+
name: FactoryGuy.generate('userName'),
|
46
83
|
},
|
47
84
|
// named 'user' type with custom attributes
|
48
85
|
admin: {
|
86
|
+
type: 'superuser'
|
49
87
|
name: 'Admin'
|
50
88
|
}
|
51
89
|
});
|
@@ -55,23 +93,34 @@ Add fixtures to the store using the:
|
|
55
93
|
});
|
56
94
|
|
57
95
|
//////////////////////////////////////////////////////////////////
|
58
|
-
//
|
96
|
+
// ** Make one fixture at time **
|
59
97
|
// building json with FactoryGuy.build
|
60
98
|
//
|
61
|
-
var userJson = FactoryGuy.build('user') // {id: 1, name: 'User1'}
|
62
|
-
|
63
|
-
var
|
99
|
+
var userJson = FactoryGuy.build('user') // {id: 1, name: 'User1', type: 'normal'}
|
100
|
+
// note the sequence used in the name attribute
|
101
|
+
var user2Json = FactoryGuy.build('user') // {id: 2, name: 'User2', type: 'normal'}
|
102
|
+
var customUserJson = FactoryGuy.build('user', name: 'bob') // {id: 3, name: 'bob', type: 'normal'}
|
103
|
+
var namedUserJson = FactoryGuy.build('admin') // {id: 4, name: 'Admin', type: 'superuser'}
|
104
|
+
|
105
|
+
//////////////////////////////////////////////////////////////////
|
106
|
+
// ** Make a list of fixtures **
|
107
|
+
// building json with FactoryGuy.buildList
|
108
|
+
//
|
109
|
+
var userJson = FactoryGuy.buildList('user',2) // [ {id: 1, name: 'User1', type: 'normal'}, {id: 2, name: 'User2', type: 'normal'} ]
|
64
110
|
|
65
111
|
//////////////////////////////////////////////////////////////////
|
66
112
|
// store.makeFixture method creates model in the store
|
113
|
+
// store.makeList method creates list of models in the store
|
67
114
|
//
|
68
115
|
// with DS.Fixture adapter
|
69
116
|
// makeFixture returns json
|
117
|
+
// makeList returns json
|
70
118
|
//
|
71
|
-
store.makeFixture('user'); // user.FIXTURES = {id: 1, name: 'User1'}
|
72
|
-
store.makeFixture('user', {name: 'bob'}); // user.FIXTURES = {id: 2, name: 'bob'}
|
73
|
-
store.makeFixture('admin'); // user.FIXTURES = {id: 3, name: 'Admin'}
|
74
|
-
store.makeFixture('admin', name: 'My name'); // user.FIXTURES = {id: 4, name: 'My name'}
|
119
|
+
store.makeFixture('user'); // user.FIXTURES = {id: 1, name: 'User1', type: 'normal'}
|
120
|
+
store.makeFixture('user', {name: 'bob'}); // user.FIXTURES = {id: 2, name: 'bob', type: 'normal'}
|
121
|
+
store.makeFixture('admin'); // user.FIXTURES = {id: 3, name: 'Admin', type: 'superuser'}
|
122
|
+
store.makeFixture('admin', name: 'My name'); // user.FIXTURES = {id: 4, name: 'My name', type: 'normal'}
|
123
|
+
|
75
124
|
|
76
125
|
// Use store.find to get the model instance
|
77
126
|
store.makeFixture('user');
|
@@ -83,6 +132,9 @@ Add fixtures to the store using the:
|
|
83
132
|
var project = store.makeFixture('project');
|
84
133
|
var user = store.makeFixture('user', projects: [project.id]);
|
85
134
|
|
135
|
+
// and for lists
|
136
|
+
var users = store.makeList('user', 2, projects: [project.id]);
|
137
|
+
|
86
138
|
// with fixture adapter all associations are treated as async, so it's
|
87
139
|
// a bit clunky to get this associated data. When using DS.FixtureAdapter
|
88
140
|
// in view specs though, this clunk is dealt with for you.
|
@@ -95,16 +147,18 @@ Add fixtures to the store using the:
|
|
95
147
|
|
96
148
|
//////////////////////////////////////////////////////////////////
|
97
149
|
// store.makeFixture method creates model and adds it to store
|
150
|
+
// store.makeList methods creates list of models and ads each to the store
|
98
151
|
//
|
99
152
|
// with DS.ActiveModelAdapter/DS.RestAdapter
|
100
153
|
//
|
101
154
|
// returns a model instances so you can synchronously
|
102
155
|
// start asking for data, as soon as you get the model
|
103
156
|
//
|
104
|
-
var user = store.makeFixture('user');
|
105
|
-
var user = store.makeFixture('user'
|
106
|
-
var user = store.makeFixture('
|
107
|
-
var user = store.makeFixture('admin'
|
157
|
+
var user = store.makeFixture('user'); // user.toJSON() = {id: 1, name: 'User1', type: 'normal'}
|
158
|
+
var user = store.makeFixture('user'); // user.toJSON() = {id: 2, name: 'User2', type: 'normal'}
|
159
|
+
var user = store.makeFixture('user', {name: 'bob'}); // user.toJSON() = {id: 3, name: 'bob', type: 'normal'}
|
160
|
+
var user = store.makeFixture('admin'); // user.toJSON() = {id: 4, name: 'Admin', type: 'superuser'}
|
161
|
+
var user = store.makeFixture('admin', name: 'Nother Admin'); // user.toJSON() = {id: 5, name: 'Nother Admin', type: 'superuser'}
|
108
162
|
|
109
163
|
// and to setup associations ...
|
110
164
|
|
@@ -114,5 +168,57 @@ Add fixtures to the store using the:
|
|
114
168
|
user.get('projects.length') == 1;
|
115
169
|
user.get('projects.firstObject.user') == user;
|
116
170
|
|
171
|
+
// and to create lists
|
172
|
+
var users = store.makeList('user');
|
117
173
|
|
118
|
-
```
|
174
|
+
```
|
175
|
+
|
176
|
+
Extra Goodies
|
177
|
+
=============
|
178
|
+
|
179
|
+
The code bundled in dist/ember-data-factory-guy.js includes a mixin named FactoryGuyTestMixin which
|
180
|
+
can be used in your tests to make it easier to access the store and make fixtures.
|
181
|
+
|
182
|
+
```javascript
|
183
|
+
|
184
|
+
// Let's say you have a helper for your tests named TestHelper declared in a file.
|
185
|
+
|
186
|
+
TestHelper = Ember.Object.createWithMixins(FactoryGuyTestMixin);
|
187
|
+
|
188
|
+
|
189
|
+
// Then in your tests you can use it like so:
|
190
|
+
|
191
|
+
|
192
|
+
var testHelper, store;
|
193
|
+
|
194
|
+
module('User Model', {
|
195
|
+
setup: function() {
|
196
|
+
testHelper = TestHelper.setup(App);
|
197
|
+
},
|
198
|
+
teardown: function() {
|
199
|
+
Em.run(function() { testHelper.teardown(); });
|
200
|
+
}
|
201
|
+
});
|
202
|
+
|
203
|
+
// You could at this point, make fixtures with testHelper.make('user'), but
|
204
|
+
// to be even more concise in tests you could add this method to your tests
|
205
|
+
var make = function(name, opts) { return testHelper.make(name, opts); }
|
206
|
+
|
207
|
+
|
208
|
+
test("make a user using fixture adapter", function() {
|
209
|
+
// useFixtureAdapter method is built into FactoryGuyTestMixin, and usually
|
210
|
+
// this would be called in the setup function
|
211
|
+
testHelper.useFixtureAdapter();
|
212
|
+
var json = make('user');
|
213
|
+
equal(User.FIXTURES.length, 1);
|
214
|
+
});
|
215
|
+
|
216
|
+
// assuming your default adapter is ActiveModelAdapter or RESTAdapter
|
217
|
+
test("make a user using your applications default adapter", function() {
|
218
|
+
var user = make('user');
|
219
|
+
equal(store.all('user').get('content.length'), 1)
|
220
|
+
equal(user instanceof DS.Model, true);
|
221
|
+
});
|
222
|
+
|
223
|
+
|
224
|
+
```
|
data/bower.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ember-data-factory-guy",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.1.2",
|
4
4
|
"authors": [
|
5
5
|
"Daniel Sudol <dansudol@yahoo.com>",
|
6
6
|
"Opak Alex <opak.alexandr@gmail.com>"
|
@@ -20,7 +20,6 @@
|
|
20
20
|
"jquery": "1.10.2",
|
21
21
|
"ember": "~1.6.0",
|
22
22
|
"ember-data": "~1.0.0-beta.7",
|
23
|
-
"emblem.js": "*",
|
24
23
|
"chance": "latest",
|
25
24
|
"qunit": "latest"
|
26
25
|
},
|
@@ -29,6 +28,11 @@
|
|
29
28
|
"**/.*",
|
30
29
|
"node_modules",
|
31
30
|
"bower_components",
|
31
|
+
"bower.json",
|
32
|
+
"gruntfile.js",
|
33
|
+
"package.json",
|
34
|
+
"README.md",
|
35
|
+
"src",
|
32
36
|
"test",
|
33
37
|
"tests"
|
34
38
|
]
|
@@ -1,137 +1,277 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
modelIds: {},
|
1
|
+
Sequence = function (fn) {
|
2
|
+
var index = 1;
|
3
|
+
var fn = fn;
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
this.next = function () {
|
6
|
+
return fn.call(this, index++);
|
7
|
+
}
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
this.reset = function () {
|
10
|
+
index = 1;
|
11
|
+
}
|
12
|
+
}
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
},
|
14
|
+
function MissingSequenceError(message) {
|
15
|
+
this.toString = function() { return message }
|
16
|
+
}
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
ModelDefinition = function (model, config) {
|
19
|
+
var sequences = {};
|
20
|
+
var defaultAttributes = {};
|
21
|
+
var namedModels = {};
|
22
|
+
var modelId = 1;
|
23
|
+
this.model = model;
|
24
|
+
|
25
|
+
/**
|
26
|
+
@param name model name like 'user' or named type like 'admin'
|
27
|
+
@return boolean true if name is this definitions model or this definition
|
28
|
+
contains a named model with that name
|
29
|
+
*/
|
30
|
+
this.matchesName = function (name) {
|
31
|
+
return model == name || namedModels[name];
|
32
|
+
}
|
22
33
|
|
23
|
-
|
34
|
+
this.merge = function (config) {
|
35
|
+
}
|
24
36
|
|
25
|
-
|
26
|
-
|
37
|
+
/**
|
38
|
+
@param sequenceName
|
39
|
+
@returns output of sequence function
|
40
|
+
*/
|
41
|
+
this.generate = function (sequenceName) {
|
42
|
+
if (!sequences[sequenceName]) {
|
43
|
+
throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+ model+"' definition")
|
44
|
+
}
|
45
|
+
return sequences[sequenceName].next();
|
46
|
+
}
|
27
47
|
|
28
|
-
|
48
|
+
this.build = function (name, opts) {
|
49
|
+
var modelAttributes = namedModels[name] || {};
|
50
|
+
var fixture = $.extend({}, defaultAttributes, modelAttributes, opts);
|
51
|
+
for (attr in fixture) {
|
52
|
+
if (typeof fixture[attr] == 'function') {
|
53
|
+
fixture[attr] = fixture[attr].call(this)
|
54
|
+
}
|
55
|
+
}
|
56
|
+
if (!fixture.id) {
|
57
|
+
fixture.id = modelId++;
|
58
|
+
}
|
59
|
+
return fixture;
|
60
|
+
}
|
29
61
|
|
30
|
-
|
62
|
+
/**
|
63
|
+
Build a list of fixtures
|
31
64
|
|
32
|
-
@param model
|
33
|
-
@param
|
65
|
+
@param name model name or named model type
|
66
|
+
@param number of fixtures to build
|
67
|
+
@param opts attribute options
|
68
|
+
@returns array of fixtures
|
34
69
|
*/
|
35
|
-
|
36
|
-
var
|
37
|
-
for (
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
70
|
+
this.buildList = function (name, number, opts) {
|
71
|
+
var arr = [];
|
72
|
+
for (var i = 0; i < number; i++) {
|
73
|
+
arr.push(this.build(name, opts))
|
74
|
+
}
|
75
|
+
return arr;
|
76
|
+
}
|
77
|
+
|
78
|
+
this.reset = function () {
|
79
|
+
modelId = 1;
|
80
|
+
}
|
81
|
+
|
82
|
+
var parseDefault = function (object) {
|
83
|
+
if (!object) {
|
84
|
+
return
|
85
|
+
}
|
86
|
+
defaultAttributes = object;
|
87
|
+
}
|
88
|
+
|
89
|
+
var parseSequences = function (object) {
|
90
|
+
if (!object) {
|
91
|
+
return
|
92
|
+
}
|
93
|
+
for (sequenceName in object) {
|
94
|
+
var sequenceFn = object[sequenceName];
|
95
|
+
if (typeof sequenceFn != 'function') {
|
96
|
+
throw new Error('Problem with [' + sequenceName + '] sequence definition. Sequences must be functions')
|
42
97
|
}
|
98
|
+
object[sequenceName] = new Sequence(sequenceFn);
|
43
99
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
100
|
+
sequences = object;
|
101
|
+
}
|
102
|
+
|
103
|
+
var parseConfig = function (config) {
|
104
|
+
parseSequences(config.sequences);
|
105
|
+
delete config.sequences;
|
106
|
+
|
107
|
+
parseDefault(config.default);
|
108
|
+
delete config.default;
|
109
|
+
|
110
|
+
namedModels = config;
|
111
|
+
}
|
112
|
+
|
113
|
+
// initialize
|
114
|
+
parseConfig(config);
|
115
|
+
}
|
116
|
+
FactoryGuy = Ember.Object.reopenClass({
|
117
|
+
modelDefinitions: {},
|
47
118
|
|
48
119
|
/**
|
120
|
+
```javascript
|
49
121
|
|
50
|
-
|
122
|
+
Person = DS.Model.extend({
|
123
|
+
type: DS.attr('string'),
|
124
|
+
name: DS.attr('string')
|
125
|
+
})
|
126
|
+
|
127
|
+
FactoryGuy.define('person', {
|
128
|
+
sequences: {
|
129
|
+
personName: function(num) {
|
130
|
+
return 'person #' + num;
|
131
|
+
},
|
132
|
+
personType: function(num) {
|
133
|
+
return 'person type #' + num;
|
134
|
+
}
|
135
|
+
},
|
136
|
+
default: {
|
137
|
+
type: 'normal',
|
138
|
+
name: FactoryGuy.generate('personName')
|
139
|
+
},
|
140
|
+
dude: {
|
141
|
+
type: FactoryGuy.generate('personType')
|
142
|
+
},
|
143
|
+
});
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
For the Person model, you can define fixtures like 'dude' or just use 'person'
|
148
|
+
and get default values.
|
149
|
+
|
150
|
+
And to get those fixtures you would call them this way:
|
151
|
+
|
152
|
+
FactoryGuy.build('person') or FactoryGuy.build('dude')
|
153
|
+
|
154
|
+
@param model the model to define
|
155
|
+
@param config your model definition object
|
51
156
|
*/
|
52
|
-
|
53
|
-
if (
|
54
|
-
this.
|
157
|
+
define: function (model, config) {
|
158
|
+
if (this.modelDefinitions[model]) {
|
159
|
+
this.modelDefinitions[model].merge(config);
|
160
|
+
} else {
|
161
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
162
|
+
}
|
163
|
+
},
|
164
|
+
|
165
|
+
generate: function (sequenceName) {
|
166
|
+
return function () {
|
167
|
+
return this.generate(sequenceName);
|
55
168
|
}
|
56
|
-
return this.fixtureStore[model];
|
57
169
|
},
|
58
170
|
|
171
|
+
|
59
172
|
/**
|
60
173
|
|
61
|
-
|
62
|
-
|
174
|
+
@param name fixture name could be model name like 'user'
|
175
|
+
or specific user like 'admin'
|
176
|
+
@returns model associated with fixture name
|
63
177
|
*/
|
64
178
|
lookupModelForName: function (name) {
|
65
|
-
|
66
|
-
|
67
|
-
if (
|
68
|
-
model
|
179
|
+
for (model in this.modelDefinitions) {
|
180
|
+
var definition = this.modelDefinitions[model];
|
181
|
+
if (definition.matchesName(name)) {
|
182
|
+
return definition.model;
|
69
183
|
}
|
70
184
|
}
|
71
|
-
return model;
|
72
185
|
},
|
73
186
|
|
74
187
|
/**
|
75
|
-
|
188
|
+
|
189
|
+
@param name fixture name could be model name like 'user'
|
190
|
+
or specific user like 'admin'
|
191
|
+
@returns definition associated with model
|
76
192
|
*/
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
193
|
+
lookupDefinitionForName: function (name) {
|
194
|
+
for (model in this.modelDefinitions) {
|
195
|
+
var definition = this.modelDefinitions[model];
|
196
|
+
if (definition.matchesName(name)) {
|
197
|
+
return definition;
|
198
|
+
}
|
199
|
+
}
|
81
200
|
},
|
82
201
|
|
202
|
+
|
83
203
|
/**
|
84
|
-
|
204
|
+
Build fixtures for model or specific fixture name. For example:
|
85
205
|
|
86
|
-
|
87
|
-
|
206
|
+
FactoryGuy.build('user') for User model
|
207
|
+
FactoryGuy.build('bob') for User model with bob attributes
|
88
208
|
|
89
|
-
|
90
|
-
|
91
|
-
|
209
|
+
@param name fixture name
|
210
|
+
@param opts options that will override default fixture values
|
211
|
+
@returns {*}
|
92
212
|
*/
|
93
213
|
build: function (name, opts) {
|
94
|
-
var
|
95
|
-
if (!
|
96
|
-
throw new Error("
|
214
|
+
var definition = this.lookupDefinitionForName(name);
|
215
|
+
if (!definition) {
|
216
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
97
217
|
}
|
218
|
+
return definition.build(name, opts);
|
219
|
+
},
|
220
|
+
|
221
|
+
/**
|
222
|
+
Build list of fixtures for model or specific fixture name. For example:
|
223
|
+
|
224
|
+
FactoryGuy.buildList('user', 2) for 2 User models
|
225
|
+
FactoryGuy.build('bob', 2) for 2 User model with bob attributes
|
98
226
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
227
|
+
@param name fixture name
|
228
|
+
@param number number of fixtures to create
|
229
|
+
@param opts options that will override default fixture values
|
230
|
+
@returns list of fixtures
|
231
|
+
*/
|
232
|
+
buildList: function (name, number, opts) {
|
233
|
+
var definition = this.lookupDefinitionForName(name);
|
234
|
+
if (!definition) {
|
235
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
105
236
|
}
|
106
|
-
return
|
237
|
+
return definition.buildList(name, number, opts);
|
107
238
|
},
|
108
239
|
|
109
240
|
/**
|
110
|
-
|
111
|
-
|
241
|
+
Clear model instances from FIXTURES array, and from store cache.
|
242
|
+
Reset the id sequence for the models back to zero.
|
112
243
|
*/
|
113
244
|
resetModels: function (store) {
|
114
245
|
var typeMaps = store.typeMaps;
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
246
|
+
for (model in this.modelDefinitions) {
|
247
|
+
var definition = this.modelDefinitions[model];
|
248
|
+
definition.reset();
|
249
|
+
try {
|
250
|
+
var modelType = store.modelFor(definition.model);
|
251
|
+
if (store.usingFixtureAdapter()) {
|
252
|
+
modelType.FIXTURES = [];
|
253
|
+
}
|
119
254
|
store.unloadAll(modelType);
|
255
|
+
} catch (e) {
|
120
256
|
}
|
121
|
-
} else {
|
122
|
-
for (model in typeMaps) {
|
123
|
-
store.unloadAll(typeMaps[model].type);
|
124
|
-
}
|
257
|
+
// } else {
|
258
|
+
// for (model in typeMaps) {
|
259
|
+
// store.unloadAll(typeMaps[model].type);
|
260
|
+
// }
|
261
|
+
// }
|
262
|
+
|
263
|
+
// for (model in this.modelDefinitions) {
|
264
|
+
// this.modelDefinitions[model].reset();
|
125
265
|
}
|
126
|
-
this.modelIds = {}
|
127
266
|
},
|
128
267
|
|
129
268
|
/**
|
130
|
-
|
131
|
-
|
269
|
+
Push fixture to model's FIXTURES array.
|
270
|
+
Used when store's adapter is a DS.FixtureAdapter.
|
132
271
|
|
133
|
-
|
134
|
-
|
272
|
+
@param modelClass DS.Model type
|
273
|
+
@param fixture the fixture to add
|
274
|
+
@returns json fixture data
|
135
275
|
*/
|
136
276
|
pushFixture: function (modelClass, fixture) {
|
137
277
|
if (!modelClass['FIXTURES']) {
|
@@ -141,7 +281,6 @@ FactoryGuy = Ember.Object.reopenClass({
|
|
141
281
|
return fixture;
|
142
282
|
}
|
143
283
|
})
|
144
|
-
|
145
284
|
DS.Store.reopen({
|
146
285
|
|
147
286
|
usingFixtureAdapter: function() {
|
@@ -153,9 +292,9 @@ DS.Store.reopen({
|
|
153
292
|
Make new fixture and save to store. If the store is using FixtureAdapter,
|
154
293
|
will push to FIXTURE array, otherwise will use push method on adapter.
|
155
294
|
|
156
|
-
@param name
|
157
|
-
@param options
|
158
|
-
@returns
|
295
|
+
@param name name of fixture
|
296
|
+
@param options fixture options
|
297
|
+
@returns json or record
|
159
298
|
*/
|
160
299
|
makeFixture: function (name, options) {
|
161
300
|
var modelName = FactoryGuy.lookupModelForName(name);
|
@@ -163,31 +302,48 @@ DS.Store.reopen({
|
|
163
302
|
var modelType = this.modelFor(modelName);
|
164
303
|
|
165
304
|
if (this.usingFixtureAdapter()) {
|
166
|
-
this.
|
305
|
+
this.setBelongsToFixturesAdapter(modelType, modelName, fixture);
|
167
306
|
return FactoryGuy.pushFixture(modelType, fixture);
|
168
307
|
} else {
|
169
308
|
var self = this;
|
170
309
|
var model;
|
171
310
|
Em.run( function() {
|
172
311
|
model = self.push(modelName, fixture);
|
173
|
-
self.
|
312
|
+
self.setBelongsToRESTAdapter(modelType, modelName, model);
|
174
313
|
});
|
175
314
|
return model;
|
176
315
|
}
|
177
316
|
},
|
178
317
|
|
179
318
|
/**
|
180
|
-
|
319
|
+
Make a list of Fixtures
|
320
|
+
|
321
|
+
@param name name of fixture
|
322
|
+
@param number number of fixtures
|
323
|
+
@param options fixture options
|
324
|
+
@returns list of json fixtures or records depending on the adapter type
|
325
|
+
*/
|
326
|
+
makeList: function (name, number, options) {
|
327
|
+
var arr = [];
|
328
|
+
for (var i = 0; i < number; i++) {
|
329
|
+
arr.push(this.makeFixture(name, options))
|
330
|
+
}
|
331
|
+
return arr;
|
332
|
+
},
|
333
|
+
|
334
|
+
/**
|
335
|
+
Set the belongsTo association for FixtureAdapter,
|
181
336
|
with models that have a hasMany association.
|
182
337
|
|
183
|
-
For example if a
|
184
|
-
on each project that the
|
185
|
-
now has the belongsTo
|
338
|
+
For example if a user hasMany projects, then set the user.id
|
339
|
+
on each project that the user hasMany of, so that the project
|
340
|
+
now has the belongsTo user association setup.
|
186
341
|
|
187
|
-
@param
|
188
|
-
@param model
|
342
|
+
@param modelType model type like App.User
|
343
|
+
@param modelName model name like 'user'
|
344
|
+
@param parentFixture parent to assign as belongTo
|
189
345
|
*/
|
190
|
-
|
346
|
+
setBelongsToFixturesAdapter: function (modelType, modelName, parentFixture) {
|
191
347
|
var store = this;
|
192
348
|
var adapter = this.adapterFor('application');
|
193
349
|
var relationShips = Ember.get(modelType, 'relationshipNames');
|
@@ -206,18 +362,18 @@ DS.Store.reopen({
|
|
206
362
|
},
|
207
363
|
|
208
364
|
/**
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
365
|
+
Set the belongsTo association for the REST type models
|
366
|
+
with a hasMany association
|
367
|
+
|
368
|
+
For example if a user hasMany projects, then set the user
|
369
|
+
on each project that the user hasMany of, so that the project
|
370
|
+
now has the belongsTo user association setup
|
371
|
+
|
372
|
+
@param modelType model type like 'App.User'
|
373
|
+
@param modelName model name like 'user'
|
374
|
+
@param parent model to check for hasMany
|
219
375
|
*/
|
220
|
-
|
376
|
+
setBelongsToRESTAdapter: function (modelType, modelName, parent) {
|
221
377
|
var relationShips = Ember.get(modelType, 'relationshipNames');
|
222
378
|
|
223
379
|
if (relationShips.hasMany) {
|
@@ -285,8 +441,9 @@ DS.FixtureAdapter.reopen({
|
|
285
441
|
})
|
286
442
|
|
287
443
|
|
288
|
-
|
444
|
+
FactoryGuyTestMixin = Em.Mixin.create({
|
289
445
|
|
446
|
+
// Pass in the app root, which typically is App.
|
290
447
|
setup: function(app) {
|
291
448
|
this.set('container', app.__container__);
|
292
449
|
return this;
|