ember-data-factory-guy 0.8.7 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  var testHelper, store;
2
2
 
3
- module('FactoryGuy', {
3
+ module('FactoryGuy with DS.RESTAdapter', {
4
4
  setup: function() {
5
- testHelper = TestHelper.setup(DS.FixtureAdapter);
5
+ testHelper = TestHelper.setup(DS.RESTAdapter);
6
6
  store = testHelper.getStore();
7
7
  },
8
8
  teardown: function() {
@@ -12,6 +12,10 @@ module('FactoryGuy', {
12
12
  }
13
13
  });
14
14
 
15
+ test("can set and get store", function() {
16
+ FactoryGuy.setStore(store);
17
+ equal(FactoryGuy.getStore(), store)
18
+ });
15
19
 
16
20
  test("Using sequences in definitions", function() {
17
21
  delete FactoryGuy.modelDefinitions['person']
@@ -191,6 +195,15 @@ test("#buildList creates list of fixtures", function() {
191
195
  });
192
196
 
193
197
 
198
+ test("#isAttributeRelationship", function() {
199
+ FactoryGuy.setStore(store);
200
+ var typeName = 'user'
201
+ equal(FactoryGuy.isAttributeRelationship(typeName,'company'),true);
202
+ equal(FactoryGuy.isAttributeRelationship(typeName,'hats'),true);
203
+ equal(FactoryGuy.isAttributeRelationship(typeName,'name'),false);
204
+ });
205
+
206
+
194
207
  test("#lookupDefinitionForFixtureName", function() {
195
208
  equal(!!FactoryGuy.lookupDefinitionForFixtureName('person'), true, 'finds definition if its the same as model name');
196
209
  equal(!!FactoryGuy.lookupDefinitionForFixtureName('funny_person'), true, 'finds definition if its a named fixture');
@@ -203,9 +216,45 @@ test("#lookupModelForFixtureName", function() {
203
216
  equal(FactoryGuy.lookupModelForFixtureName('fake'), undefined, "return nothing if can't find definition");
204
217
  });
205
218
 
219
+
220
+ test("#make returns a model instance", function() {
221
+ FactoryGuy.setStore(store);
222
+ var user = FactoryGuy.make('user');
223
+ ok(user instanceof User)
224
+ });
225
+
226
+ test("#make requires that the store is set on FactoryGuy", function(assert) {
227
+ assert.throws(FactoryGuy.make('user'))
228
+ });
229
+
230
+
231
+ module('FactoryGuy with DS.FixtureAdapter', {
232
+ setup: function() {
233
+ testHelper = TestHelper.setup(DS.FixtureAdapter);
234
+ store = testHelper.getStore();
235
+ },
236
+ teardown: function() {
237
+ Em.run(function() {
238
+ testHelper.teardown();
239
+ });
240
+ }
241
+ });
242
+
243
+
244
+ asyncTest("#make loads the fixture in the store and returns an object", function() {
245
+ FactoryGuy.setStore(store);
246
+ var user = FactoryGuy.make('user');
247
+ ok(user instanceof Object )
248
+ store.find('user', user.id).then(function(u){
249
+ ok(u instanceof User)
250
+ start()
251
+ })
252
+ });
253
+
254
+
206
255
  asyncTest("#pushFixture", function() {
207
256
  var User = store.modelFor('user'),
208
- user = store.makeFixture('user'),
257
+ user = FactoryGuy.make('user'),
209
258
  duplicateUser = FactoryGuy.build('user', { id: user.id, name: 'monkey' }),
210
259
  differentUser = FactoryGuy.build('user'),
211
260
  usersById = {};
@@ -1,4 +1,4 @@
1
- var testHelper, store;
1
+ var testHelper, store, make;
2
2
 
3
3
  module('FactoryGuyTestMixin with DS.RESTAdapter', {});
4
4
 
@@ -28,6 +28,7 @@ module('FactoryGuyTestMixin (using mockjax) with DS.RESTAdapter', {
28
28
  setup: function () {
29
29
  testHelper = TestHelper.setup(DS.RESTAdapter);
30
30
  store = testHelper.getStore();
31
+ make = function() {return testHelper.make.apply(testHelper,arguments)}
31
32
  },
32
33
  teardown: function () {
33
34
  testHelper.teardown();
@@ -102,6 +103,7 @@ module('FactoryGuyTestMixin (using mockjax) with DS.ActiveModelAdapter', {
102
103
  setup: function () {
103
104
  testHelper = TestHelper.setup(DS.ActiveModelAdapter);
104
105
  store = testHelper.getStore();
106
+ make = function() {return testHelper.make.apply(testHelper,arguments)}
105
107
  },
106
108
  teardown: function () {
107
109
  testHelper.teardown();
@@ -168,7 +170,7 @@ asyncTest("#handleCreate returns camelCase attributes", function() {
168
170
  });
169
171
 
170
172
  asyncTest("#handleCreate match belongsTo association", function() {
171
- var company = store.makeFixture('company')
173
+ var company = make('company')
172
174
  testHelper.handleCreate('profile', {match:{ company: company}})
173
175
 
174
176
  store.createRecord('profile', {company: company}).save().then(function(profile) {
@@ -178,7 +180,7 @@ asyncTest("#handleCreate match belongsTo association", function() {
178
180
  });
179
181
 
180
182
  asyncTest("#handleCreate match belongsTo polymorphic association", function() {
181
- var group = store.makeFixture('group')
183
+ var group = make('group')
182
184
  testHelper.handleCreate('profile', {match:{ group: group}})
183
185
 
184
186
  store.createRecord('profile', {group: group}).save().then(function(profile) {
@@ -191,8 +193,8 @@ asyncTest("#handleCreate match belongsTo polymorphic association", function() {
191
193
  asyncTest("#handleCreate match attributes and return attributes", function() {
192
194
  var date = new Date()
193
195
  var customDescription = "special description"
194
- var company = store.makeFixture('company')
195
- var group = store.makeFixture('big_group')
196
+ var company = make('company')
197
+ var group = make('big_group')
196
198
 
197
199
  testHelper.handleCreate('profile', {
198
200
  match: {description: customDescription, company: company, group: group},
@@ -291,8 +293,15 @@ asyncTest("#handleFindMany with traits and fixture options", function () {
291
293
 
292
294
  /////// handleUpdate //////////
293
295
 
294
- asyncTest("#handleUpdate the basic", function() {
295
- var profile = store.makeFixture('profile');
296
+ test("#handleUpdate with incorrect parameters", function(assert) {
297
+ // assert.throws(function(){testHelper.handleUpdate()},"missing everything");
298
+ // assert.throws(function(){testHelper.handleUpdate('profile')},"missing id");
299
+ // assert.throws(function(){testHelper.handleUpdate('profile', false)},"missing id");
300
+ assert.throws(function(){testHelper.handleUpdate('profile', true)},"missing id");
301
+ });
302
+
303
+ asyncTest("#handleUpdate the with modelType and id", function() {
304
+ var profile = make('profile');
296
305
  testHelper.handleUpdate('profile', profile.id);
297
306
 
298
307
  profile.set('description','new desc');
@@ -302,10 +311,36 @@ asyncTest("#handleUpdate the basic", function() {
302
311
  });
303
312
  });
304
313
 
305
- asyncTest("#handleUpdate failure", function() {
306
- var profile = store.makeFixture('profile');
314
+
315
+ asyncTest("#handleUpdate the with model", function() {
316
+ var profile = make('profile');
317
+ testHelper.handleUpdate(profile, true, {e:1});
318
+
319
+ profile.set('description','new desc');
320
+ profile.save().then(function(profile) {
321
+ ok(profile.get('description') == 'new desc');
322
+ start();
323
+ });
324
+ });
325
+
326
+ asyncTest("#handleUpdate the with modelType and id that fails", function() {
327
+ var profile = make('profile');
307
328
  testHelper.handleUpdate('profile', profile.id, false);
308
329
 
330
+ profile.set('description','new desc');
331
+ profile.save().then(
332
+ function() {},
333
+ function() {
334
+ ok(true)
335
+ start();
336
+ }
337
+ )
338
+ });
339
+
340
+ asyncTest("#handleUpdate with model that fails", function() {
341
+ var profile = make('profile');
342
+ testHelper.handleUpdate(profile, false);
343
+
309
344
  profile.set('description','new desc');
310
345
  profile.save().then(
311
346
  function() {},
@@ -320,7 +355,7 @@ asyncTest("#handleUpdate failure", function() {
320
355
  /////// handleDelete //////////
321
356
 
322
357
  asyncTest("#handleDelete the basic", function() {
323
- var profile = store.makeFixture('profile');
358
+ var profile = make('profile');
324
359
  testHelper.handleDelete('profile', profile.id);
325
360
 
326
361
  profile.destroyRecord().then(function() {
@@ -330,7 +365,7 @@ asyncTest("#handleDelete the basic", function() {
330
365
  });
331
366
 
332
367
  asyncTest("#handleDelete failure case", function() {
333
- var profile = store.makeFixture('profile');
368
+ var profile = make('profile');
334
369
  testHelper.handleDelete('profile', profile.id, false);
335
370
 
336
371
  profile.destroyRecord().then(
@@ -1,9 +1,10 @@
1
- var testHelper, store;
1
+ var testHelper, store, make;
2
2
 
3
3
  module('FactoryGuy with DS.RESTAdapter', {
4
4
  setup: function() {
5
5
  testHelper = TestHelper.setup(DS.RESTAdapter);
6
6
  store = testHelper.getStore();
7
+ make = function() {return testHelper.make.apply(testHelper,arguments)}
7
8
  },
8
9
  teardown: function() {
9
10
  Em.run(function() { testHelper.teardown(); });
@@ -12,8 +13,8 @@ module('FactoryGuy with DS.RESTAdapter', {
12
13
 
13
14
 
14
15
  test("#resetModels clears the store of models, and resets the model definition", function() {
15
- var project = store.makeFixture('project');
16
- var user = store.makeFixture('user', {projects: [project]});
16
+ var project = make('project');
17
+ var user = make('user', {projects: [project]});
17
18
 
18
19
  for (model in FactoryGuy.modelDefinitions) {
19
20
  var definition = FactoryGuy.modelDefinitions[model];
@@ -37,6 +38,7 @@ module('DS.Store#makeFixture with RestAdapter', {
37
38
  setup: function() {
38
39
  testHelper = TestHelper.setup(DS.RESTAdapter);
39
40
  store = testHelper.getStore();
41
+ make = function() {return testHelper.make.apply(testHelper,arguments)}
40
42
  },
41
43
  teardown: function() {
42
44
  Em.run(function() { testHelper.teardown(); });
@@ -45,7 +47,7 @@ module('DS.Store#makeFixture with RestAdapter', {
45
47
 
46
48
 
47
49
  asyncTest("creates records in the store", function() {
48
- var user = store.makeFixture('user');
50
+ var user = make('user');
49
51
  ok(user instanceof User);
50
52
 
51
53
  store.find('user', user.id).then(function(store_user) {
@@ -54,34 +56,40 @@ asyncTest("creates records in the store", function() {
54
56
  });
55
57
  });
56
58
 
59
+ test("handles custom attribute type attributes", function() {
60
+ var info = {first:1}
61
+ var user = make('user', {info: info});
62
+ ok(user.get('info') == info)
63
+ });
64
+
57
65
  test("makeFixture with fixture options", function() {
58
- var profile = store.makeFixture('profile', {description: 'dude'});
66
+ var profile = make('profile', {description: 'dude'});
59
67
  ok(profile.get('description') == 'dude');
60
68
  });
61
69
 
62
70
  test("makeFixture with traits", function() {
63
- var profile = store.makeFixture('profile', 'goofy_description');
71
+ var profile = make('profile', 'goofy_description');
64
72
  ok(profile.get('description') == 'goofy');
65
73
  });
66
74
 
67
75
  test("makeFixture with traits and fixture options ", function() {
68
- var profile = store.makeFixture('profile', 'goofy_description', {description: 'dude'});
76
+ var profile = make('profile', 'goofy_description', {description: 'dude'});
69
77
  ok(profile.get('description') == 'dude');
70
78
  });
71
79
 
72
80
 
73
81
 
74
82
  test("when hasMany associations assigned, belongTo parent is assigned", function() {
75
- var project = store.makeFixture('project');
76
- var user = store.makeFixture('user', {projects: [project]})
83
+ var project = make('project');
84
+ var user = make('user', {projects: [project]})
77
85
 
78
86
  ok(project.get('user') == user);
79
87
  });
80
88
 
81
89
 
82
90
  asyncTest("when hasMany ( asnyc ) associations assigned, belongTo parent is assigned", function() {
83
- var user = store.makeFixture('user');
84
- var company = store.makeFixture('company', {users: [user]});
91
+ var user = make('user');
92
+ var company = make('company', {users: [user]});
85
93
 
86
94
  user.get('company').then(function(c){
87
95
  ok(c == company);
@@ -91,9 +99,9 @@ asyncTest("when hasMany ( asnyc ) associations assigned, belongTo parent is assi
91
99
 
92
100
 
93
101
  test("when hasMany ( polymorphic ) associations are assigned, belongTo parent is assigned", function() {
94
- var bh = store.makeFixture('big_hat');
95
- var sh = store.makeFixture('small_hat');
96
- var user = store.makeFixture('user', {hats: [bh, sh]});
102
+ var bh = make('big_hat');
103
+ var sh = make('small_hat');
104
+ var user = make('user', {hats: [bh, sh]});
97
105
 
98
106
  equal(user.get('hats.length'), 2);
99
107
  ok(user.get('hats.firstObject') instanceof BigHat)
@@ -105,40 +113,40 @@ test("when hasMany ( polymorphic ) associations are assigned, belongTo parent is
105
113
 
106
114
 
107
115
  test("when hasMany ( self referential ) associations are assigned, belongsTo parent is assigned", function() {
108
- var big_group = store.makeFixture('big_group');
109
- var group = store.makeFixture('group', {versions: [big_group]});
116
+ var big_group = make('big_group');
117
+ var group = make('group', {versions: [big_group]});
110
118
  ok(big_group.get('group') == group)
111
119
  });
112
120
 
113
121
 
114
122
  test("when hasMany associations are assigned, belongsTo parent is assigned using inverse", function() {
115
- var project = store.makeFixture('project');
116
- var project2 = store.makeFixture('project', {children: [project]});
123
+ var project = make('project');
124
+ var project2 = make('project', {children: [project]});
117
125
 
118
126
  ok(project.get('parent') == project2);
119
127
  });
120
128
 
121
129
 
122
130
  test("when hasMany associations are assigned, belongsTo parent is assigned using actual belongsTo name", function() {
123
- var silk = store.makeFixture('silk');
124
- var bh = store.makeFixture('big_hat', {materials: [silk]});
131
+ var silk = make('silk');
132
+ var bh = make('big_hat', {materials: [silk]});
125
133
 
126
134
  ok(silk.get('hat') == bh)
127
135
  });
128
136
 
129
137
 
130
138
  test("when hasMany associations are assigned, belongsTo ( polymorphic ) parent is assigned", function() {
131
- var fluff = store.makeFixture('fluffy_material');
132
- var big_hat = store.makeFixture('big_hat', {fluffy_materials: [fluff]});
139
+ var fluff = make('fluffy_material');
140
+ var big_hat = make('big_hat', {fluffy_materials: [fluff]});
133
141
 
134
142
  ok(fluff.get('hat') == big_hat)
135
143
  });
136
144
 
137
145
 
138
146
  test("when belongTo parent is assigned, parent adds to hasMany records", function() {
139
- var user = store.makeFixture('user');
140
- var project1 = store.makeFixture('project', {user: user});
141
- var project2 = store.makeFixture('project', {user: user});
147
+ var user = make('user');
148
+ var project1 = make('project', {user: user});
149
+ var project2 = make('project', {user: user});
142
150
 
143
151
  equal(user.get('projects.length'), 2);
144
152
  ok(user.get('projects.firstObject') == project1);
@@ -147,9 +155,9 @@ test("when belongTo parent is assigned, parent adds to hasMany records", functio
147
155
 
148
156
 
149
157
  test("when belongTo parent is assigned, parent adds to polymorphic hasMany records", function() {
150
- var user = store.makeFixture('user');
151
- store.makeFixture('big_hat', {user: user});
152
- store.makeFixture('small_hat', {user: user});
158
+ var user = make('user');
159
+ make('big_hat', {user: user});
160
+ make('small_hat', {user: user});
153
161
 
154
162
  equal(user.get('hats.length'), 2);
155
163
  ok(user.get('hats.firstObject') instanceof BigHat)
@@ -158,9 +166,9 @@ test("when belongTo parent is assigned, parent adds to polymorphic hasMany recor
158
166
 
159
167
 
160
168
  asyncTest("when hasMany ( async ) relationship is assigned, model relationship is synced on both sides", function() {
161
- var property = store.makeFixture('property');
162
- var user1 = store.makeFixture('user', {properties: [property]});
163
- var user2 = store.makeFixture('user', {properties: [property]});
169
+ var property = make('property');
170
+ var user1 = make('user', {properties: [property]});
171
+ var user2 = make('user', {properties: [property]});
164
172
 
165
173
  equal(property.get('owners.length'), 2);
166
174
  ok(property.get('owners.firstObject') == user1);
@@ -170,9 +178,9 @@ asyncTest("when hasMany ( async ) relationship is assigned, model relationship i
170
178
 
171
179
 
172
180
  asyncTest("when belongsTo ( async ) parent is assigned, parent adds to hasMany records", function() {
173
- var user1 = store.makeFixture('user');
174
- var user2 = store.makeFixture('user');
175
- var company = store.makeFixture('company', {users: [user1, user2]});
181
+ var user1 = make('user');
182
+ var user2 = make('user');
183
+ var company = make('company', {users: [user1, user2]});
176
184
 
177
185
  equal(company.get('users.length'), 2);
178
186
  ok(company.get('users.firstObject') == user1);
@@ -182,8 +190,8 @@ asyncTest("when belongsTo ( async ) parent is assigned, parent adds to hasMany r
182
190
 
183
191
 
184
192
  test("when belongTo parent is assigned, parent adds to hasMany record using inverse", function() {
185
- var project = store.makeFixture('project');
186
- var project2 = store.makeFixture('project', {parent: project});
193
+ var project = make('project');
194
+ var project2 = make('project', {parent: project});
187
195
 
188
196
  equal(project.get('children.length'), 1);
189
197
  ok(project.get('children.firstObject') == project2);
@@ -191,43 +199,43 @@ test("when belongTo parent is assigned, parent adds to hasMany record using inve
191
199
 
192
200
 
193
201
  test("when belongTo parent is assigned, parent adds to hasMany record using actual hasMany name", function() {
194
- var bh = store.makeFixture('big_hat');
195
- var silk = store.makeFixture('silk', {hat: bh});
202
+ var bh = make('big_hat');
203
+ var silk = make('silk', {hat: bh});
196
204
 
197
205
  ok(bh.get('materials.firstObject') == silk)
198
206
  });
199
207
 
200
208
 
201
209
  test("when belongTo parent is assigned, parent adds to belongsTo record", function() {
202
- var company = store.makeFixture('company');
203
- var profile = store.makeFixture('profile', {company: company});
210
+ var company = make('company');
211
+ var profile = make('profile', {company: company});
204
212
  ok(company.get('profile') == profile);
205
213
 
206
214
  // but guard against a situation where a model can belong to itself
207
215
  // and do not want to set the belongsTo on this case.
208
- var hat1 = store.makeFixture('big_hat')
209
- var hat2 = store.makeFixture('big_hat', {hat: hat1})
216
+ var hat1 = make('big_hat')
217
+ var hat2 = make('big_hat', {hat: hat1})
210
218
  ok(hat1.get('hat') == null);
211
219
  ok(hat2.get('hat') == hat1);
212
220
  });
213
221
 
214
222
 
215
223
  test("belongsTo associations defined as attributes in fixture", function() {
216
- var project = store.makeFixture('project_with_user');
224
+ var project = make('project_with_user');
217
225
  equal(project.get('user') instanceof User, true)
218
226
  ok(project.get('user.name') == 'User1');
219
227
 
220
- var project = store.makeFixture('project_with_dude');
228
+ var project = make('project_with_dude');
221
229
  ok(project.get('user.name') == 'Dude');
222
230
 
223
- var project = store.makeFixture('project_with_admin');
231
+ var project = make('project_with_admin');
224
232
  ok(project.get('user.name') == 'Admin');
225
233
  });
226
234
 
227
235
 
228
236
 
229
237
  test("hasMany associations defined as attributes in fixture", function() {
230
- var user = store.makeFixture('user_with_projects');
238
+ var user = make('user_with_projects');
231
239
  equal(user.get('projects.length'), 2)
232
240
  ok(user.get('projects.firstObject.user') == user)
233
241
  ok(user.get('projects.lastObject.user') == user)
@@ -235,24 +243,24 @@ test("hasMany associations defined as attributes in fixture", function() {
235
243
 
236
244
 
237
245
  test("hasMany associations defined with traits", function() {
238
- var user = store.makeFixture('user', 'with_projects');
246
+ var user = make('user', 'with_projects');
239
247
  equal(user.get('projects.length'), 2)
240
248
  ok(user.get('projects.firstObject.user') == user)
241
249
  ok(user.get('projects.lastObject.user') == user)
242
250
  })
243
251
 
244
252
  test("belongsTo associations defined with traits", function() {
245
- var hat1 = store.makeFixture('hat', 'with_user');
253
+ var hat1 = make('hat', 'with_user');
246
254
  equal(hat1.get('user') instanceof User, true)
247
255
 
248
- var hat2 = store.makeFixture('hat', 'with_user', 'with_outfit');
256
+ var hat2 = make('hat', 'with_user', 'with_outfit');
249
257
  equal(hat2.get('user') instanceof User, true)
250
258
  equal(hat2.get('outfit') instanceof Outfit, true)
251
259
  })
252
260
 
253
261
 
254
262
  test("with (nested json fixture) belongsTo has a hasMany association which has a belongsTo", function() {
255
- var project = store.makeFixture('project', 'with_user_having_hats_belonging_to_outfit');
263
+ var project = make('project', 'with_user_having_hats_belonging_to_outfit');
256
264
  var user = project.get('user');
257
265
  var hats = user.get('hats');
258
266
  var firstHat = hats.get('firstObject');