Skip to content

Commit 519d33b

Browse files
committed
Finished tests for nested endpoints.
1 parent 100d868 commit 519d33b

File tree

6 files changed

+153
-25
lines changed

6 files changed

+153
-25
lines changed

dist/angular-data.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,8 @@ function errorPrefix(resourceName) {
20642064
* @id DS.async_methods:create
20652065
* @name create
20662066
* @description
2067-
* Create a new resource and save it to the server.
2067+
* The "C" in "CRUD". Delegate to the `create` method of whatever adapter is being used (http by default) and inject the
2068+
* result into the data store.
20682069
*
20692070
* ## Signature:
20702071
* ```js
@@ -2074,20 +2075,19 @@ function errorPrefix(resourceName) {
20742075
* ## Example:
20752076
*
20762077
* ```js
2077-
* DS.create('document', { author: 'John Anderson' })
2078-
* .then(function (document) {
2079-
* document; // { id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson' }
2078+
* DS.create('document', {
2079+
* author: 'John Anderson'
2080+
* }).then(function (document) {
2081+
* document; // { id: 5, author: 'John Anderson' }
20802082
*
2081-
* // The new document is already in the data store
2082-
* DS.get('document', document.id); // { id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson' }
2083-
* }, function (err) {
2084-
* // handle error
2085-
* });
2083+
* // The new document is already in the data store
2084+
* DS.get('document', document.id); // { id: 5, author: 'John Anderson' }
2085+
* });
20862086
* ```
20872087
*
20882088
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
20892089
* @param {object} attrs The attributes with which to create the item of the type specified by `resourceName`.
2090-
* @param {object=} options Configuration options. Properties:
2090+
* @param {object=} options Configuration options. Passed directly to the adapter's `create` method. Properties:
20912091
*
20922092
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
20932093
* - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`.
@@ -2107,16 +2107,16 @@ function create(resourceName, attrs, options) {
21072107
var DS = this;
21082108
var deferred = DS.$q.defer();
21092109
var promise = deferred.promise;
2110+
var definition = DS.definitions[resourceName];
21102111

21112112
try {
21122113
options = options || {};
21132114

2114-
if (!DS.definitions[resourceName]) {
2115+
if (!definition) {
21152116
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
21162117
} else if (!DS.utils.isObject(attrs)) {
21172118
throw new DS.errors.IA(errorPrefix(resourceName) + 'attrs: Must be an object!');
21182119
}
2119-
var definition = DS.definitions[resourceName];
21202120
var resource = DS.store[resourceName];
21212121

21222122
if (!('cacheResponse' in options)) {

dist/angular-data.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/async_methods/create.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ function errorPrefix(resourceName) {
77
* @id DS.async_methods:create
88
* @name create
99
* @description
10-
* Create a new resource and save it to the server.
10+
* The "C" in "CRUD". Delegate to the `create` method of whatever adapter is being used (http by default) and inject the
11+
* result into the data store.
1112
*
1213
* ## Signature:
1314
* ```js
@@ -17,20 +18,19 @@ function errorPrefix(resourceName) {
1718
* ## Example:
1819
*
1920
* ```js
20-
* DS.create('document', { author: 'John Anderson' })
21-
* .then(function (document) {
22-
* document; // { id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson' }
21+
* DS.create('document', {
22+
* author: 'John Anderson'
23+
* }).then(function (document) {
24+
* document; // { id: 5, author: 'John Anderson' }
2325
*
24-
* // The new document is already in the data store
25-
* DS.get('document', document.id); // { id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson' }
26-
* }, function (err) {
27-
* // handle error
28-
* });
26+
* // The new document is already in the data store
27+
* DS.get('document', document.id); // { id: 5, author: 'John Anderson' }
28+
* });
2929
* ```
3030
*
3131
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
3232
* @param {object} attrs The attributes with which to create the item of the type specified by `resourceName`.
33-
* @param {object=} options Configuration options. Properties:
33+
* @param {object=} options Configuration options. Passed directly to the adapter's `create` method. Properties:
3434
*
3535
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
3636
* - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`.
@@ -50,16 +50,16 @@ function create(resourceName, attrs, options) {
5050
var DS = this;
5151
var deferred = DS.$q.defer();
5252
var promise = deferred.promise;
53+
var definition = DS.definitions[resourceName];
5354

5455
try {
5556
options = options || {};
5657

57-
if (!DS.definitions[resourceName]) {
58+
if (!definition) {
5859
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
5960
} else if (!DS.utils.isObject(attrs)) {
6061
throw new DS.errors.IA(errorPrefix(resourceName) + 'attrs: Must be an object!');
6162
}
62-
var definition = DS.definitions[resourceName];
6363
var resource = DS.store[resourceName];
6464

6565
if (!('cacheResponse' in options)) {

test/integration/datastore/async_methods/destroyAll.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,31 @@ describe('DS.destroyAll(resourceName, params[, options]): ', function () {
7676

7777
assert.deepEqual(DS.filter('post', {}), [], 'The posts should not be in the store yet');
7878
});
79+
it('should handle nested resources', function () {
80+
$httpBackend.expectDELETE('http://test.angular-cache.com/user/4/comment?content=test').respond(204);
81+
82+
DS.destroyAll('comment', {
83+
content: 'test'
84+
}, {
85+
parentKey: 4
86+
}).then(function () {
87+
}, function (err) {
88+
console.log(err);
89+
fail('Should not have failed!');
90+
});
91+
92+
$httpBackend.flush();
93+
94+
$httpBackend.expectDELETE('http://test.angular-cache.com/comment?content=test').respond(204);
95+
96+
DS.destroyAll('comment', {
97+
content: 'test'
98+
}).then(function () {
99+
}, function (err) {
100+
console.log(err);
101+
fail('Should not have failed!');
102+
});
103+
104+
$httpBackend.flush();
105+
});
79106
});

test/integration/datastore/async_methods/findAll.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,51 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
234234
assert.equal(lifecycle.serialize.callCount, 0, 'serialize should have been called');
235235
assert.equal(lifecycle.deserialize.callCount, 1, 'deserialize should have been called');
236236
});
237+
it('should handle nested resources', function () {
238+
var testComment = {
239+
id: 5,
240+
content: 'test',
241+
approvedBy: 4
242+
};
243+
var testComment2 = {
244+
id: 6,
245+
content: 'test',
246+
approvedBy: 4
247+
};
248+
$httpBackend.expectGET('http://test.angular-cache.com/user/4/comment?content=test').respond(200, [testComment, testComment2]);
249+
250+
DS.findAll('comment', {
251+
content: 'test'
252+
}, {
253+
parentKey: 4
254+
}).then(function (comments) {
255+
assert.deepEqual(comments, [testComment, testComment2]);
256+
assert.deepEqual(comments, DS.filter('comment', {
257+
content: 'test'
258+
}));
259+
}, function () {
260+
fail('Should not have failed!');
261+
});
262+
263+
$httpBackend.flush();
264+
265+
DS.ejectAll('comment');
266+
267+
$httpBackend.expectGET('http://test.angular-cache.com/comment?content=test').respond(200, [testComment, testComment2]);
268+
269+
DS.findAll('comment', {
270+
content: 'test'
271+
}, {
272+
bypassCache: true
273+
}).then(function (comments) {
274+
assert.deepEqual(comments, [testComment, testComment2]);
275+
assert.deepEqual(comments, DS.filter('comment', {
276+
content: 'test'
277+
}));
278+
}, function () {
279+
fail('Should not have failed!');
280+
});
281+
282+
$httpBackend.flush();
283+
});
237284
});

test/integration/datastore/async_methods/updateAll.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,58 @@ describe('DS.updateAll(resourceName, attrs, params[, options])', function () {
9696
assert.equal(lifecycle.serialize.callCount, 2, 'serialize should have been called');
9797
assert.equal(lifecycle.deserialize.callCount, 2, 'deserialize should have been called');
9898
});
99+
it('should handle nested resources', function () {
100+
var testComment = {
101+
id: 5,
102+
content: 'stuff',
103+
approvedBy: 4
104+
};
105+
var testComment2 = {
106+
id: 6,
107+
content: 'stuff',
108+
approvedBy: 4
109+
};
110+
$httpBackend.expectPUT('http://test.angular-cache.com/user/4/comment?content=test').respond(200, [testComment, testComment2]);
111+
112+
DS.inject('comment', testComment);
113+
114+
DS.updateAll('comment', {
115+
content: 'stuff'
116+
}, {
117+
content: 'test'
118+
}, {
119+
parentKey: 4
120+
}).then(function (comments) {
121+
assert.deepEqual(comments, [testComment, testComment2]);
122+
assert.deepEqual(comments, DS.filter('comment', {
123+
content: 'stuff'
124+
}));
125+
}, function () {
126+
fail('Should not have failed!');
127+
});
128+
129+
$httpBackend.flush();
130+
131+
DS.ejectAll('comment');
132+
133+
$httpBackend.expectPUT('http://test.angular-cache.com/comment?content=test').respond(200, [testComment, testComment2]);
134+
135+
DS.inject('comment', testComment2);
136+
137+
DS.updateAll('comment', {
138+
content: 'stuff'
139+
}, {
140+
content: 'test'
141+
}).then(function (comments) {
142+
assert.deepEqual(comments, [testComment, testComment2]);
143+
assert.deepEqual(comments, DS.filter('comment', {
144+
content: 'stuff',
145+
sort: 'id'
146+
}));
147+
}, function () {
148+
fail('Should not have failed!');
149+
});
150+
151+
$httpBackend.flush();
152+
});
99153
});

0 commit comments

Comments
 (0)