Skip to content

Commit fb15b49

Browse files
committed
Auto merge of #2138 - Turbo87:cnt, r=locks
mirage: Calculate `crates_cnt` properties These should be consistent with the data that we have in the mock backend database. r? @locks
2 parents 7bc91b7 + a64d1fb commit fb15b49

File tree

10 files changed

+102
-31
lines changed

10 files changed

+102
-31
lines changed

mirage/factories/category.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ export default Factory.extend({
1717
},
1818

1919
created_at: '2010-06-16T21:30:45Z',
20-
crates_cnt: 0,
2120
});

mirage/factories/keyword.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ export default Factory.extend({
66
id() {
77
return this.keyword;
88
},
9-
10-
crates_cnt: 0,
119
});

mirage/fixtures/categories.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export default [
22
{
33
category: 'API bindings',
4-
crates_cnt: 0,
54
created_at: '2017-01-20T14:51:49Z',
65
description:
76
'Idiomatic wrappers of specific APIs for convenient access from Rust. Includes HTTP API wrappers as well. Non-idiomatic or unsafe bindings can be found in External FFI bindings.',
@@ -10,15 +9,13 @@ export default [
109
},
1110
{
1211
category: 'Algorithms',
13-
crates_cnt: 1,
1412
created_at: '2017-01-20T14:51:49Z',
1513
description: 'Rust implementations of core algorithms such as hashing, sorting, searching, and more.',
1614
id: 'algorithms',
1715
slug: 'algorithms',
1816
},
1917
{
2018
category: 'Asynchronous',
21-
crates_cnt: 3910,
2219
created_at: '2017-01-20T14:51:49Z',
2320
description:
2421
'Crates to help you deal with events independently of the main program flow, using techniques like futures, promises, waiting, or eventing.',

mirage/fixtures/keywords.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,70 @@
11
export default [
22
{
3-
crates_cnt: 38,
43
created_at: '2014-11-23T06:47:40Z',
54
id: 'network',
65
keyword: 'network',
76
},
87
{
9-
crates_cnt: 1,
108
created_at: '2014-11-23T06:47:40Z',
119
id: 'rust',
1210
keyword: 'rust',
1311
},
1412
{
15-
crates_cnt: 1,
1613
created_at: '2014-11-23T06:47:40Z',
1714
id: 'plugin',
1815
keyword: 'plugin',
1916
},
2017
{
21-
crates_cnt: 1,
2218
created_at: '2014-11-23T06:47:40Z',
2319
id: 'code-generation',
2420
keyword: 'code-generation',
2521
},
2622
{
27-
crates_cnt: 1,
2823
created_at: '2014-11-23T06:47:40Z',
2924
id: 'python',
3025
keyword: 'python',
3126
},
3227
{
33-
crates_cnt: 1,
3428
created_at: '2014-11-23T06:47:40Z',
3529
id: 'ruby',
3630
keyword: 'ruby',
3731
},
3832
{
39-
crates_cnt: 1,
4033
created_at: '2014-11-23T06:47:40Z',
4134
id: 'shell',
4235
keyword: 'shell',
4336
},
4437
{
45-
crates_cnt: 1,
4638
created_at: '2014-11-23T06:47:40Z',
4739
id: 'string',
4840
keyword: 'string',
4941
},
5042
{
51-
crates_cnt: 1,
5243
created_at: '2014-11-23T06:47:40Z',
5344
id: 'case',
5445
keyword: 'case',
5546
},
5647
{
57-
crates_cnt: 1,
5848
created_at: '2014-11-23T06:47:40Z',
5949
id: 'camel',
6050
keyword: 'camel',
6151
},
6252
{
63-
crates_cnt: 1,
6453
created_at: '2014-11-23T06:47:40Z',
6554
id: 'snake',
6655
keyword: 'snake',
6756
},
6857
{
69-
crates_cnt: 1,
7058
created_at: '2014-11-23T06:47:40Z',
7159
id: 'inflection',
7260
keyword: 'inflection',
7361
},
7462
{
75-
crates_cnt: 1,
7663
created_at: '2014-11-23T06:47:40Z',
7764
id: 'elastic',
7865
keyword: 'elastic',
7966
},
8067
{
81-
crates_cnt: 1,
8268
created_at: '2014-11-23T06:47:40Z',
8369
id: 'elasticsearch',
8470
keyword: 'elasticsearch',

mirage/serializers/category.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import BaseSerializer from './application';
2+
3+
export default BaseSerializer.extend({
4+
getHashForResource() {
5+
let [hash, addToIncludes] = BaseSerializer.prototype.getHashForResource.apply(this, arguments);
6+
7+
if (Array.isArray(hash)) {
8+
for (let resource of hash) {
9+
this._adjust(resource);
10+
}
11+
} else {
12+
this._adjust(hash);
13+
}
14+
15+
return [hash, addToIncludes];
16+
},
17+
18+
_adjust(hash) {
19+
let allCrates = this.schema.crates.all();
20+
let associatedCrates = allCrates.filter(it => it.categoryIds.includes(hash.id));
21+
22+
hash.crates_cnt = associatedCrates.length;
23+
},
24+
});

mirage/serializers/keyword.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import BaseSerializer from './application';
2+
3+
export default BaseSerializer.extend({
4+
getHashForResource() {
5+
let [hash, addToIncludes] = BaseSerializer.prototype.getHashForResource.apply(this, arguments);
6+
7+
if (Array.isArray(hash)) {
8+
for (let resource of hash) {
9+
this._adjust(resource);
10+
}
11+
} else {
12+
this._adjust(hash);
13+
}
14+
15+
return [hash, addToIncludes];
16+
},
17+
18+
_adjust(hash) {
19+
let allCrates = this.schema.crates.all();
20+
let associatedCrates = allCrates.filter(it => it.keywordIds.includes(hash.id));
21+
22+
hash.crates_cnt = associatedCrates.length;
23+
},
24+
});

tests/acceptance/categories-test.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module('Acceptance | categories', function(hooks) {
1313
test('is accessible', async function(assert) {
1414
assert.expect(0);
1515

16-
this.server.create('category', { category: 'API bindings', crates_cnt: 0 });
17-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
18-
this.server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });
16+
this.server.create('category', { category: 'API bindings' });
17+
this.server.create('category', { category: 'Algorithms' });
18+
this.server.create('category', { category: 'Asynchronous' });
1919

2020
await visit('/categories');
2121
percySnapshot(assert);
@@ -26,7 +26,7 @@ module('Acceptance | categories', function(hooks) {
2626
test('category/:category_id is accessible', async function(assert) {
2727
assert.expect(0);
2828

29-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
29+
this.server.create('category', { category: 'Algorithms' });
3030

3131
await visit('/categories/algorithms');
3232
percySnapshot(assert);
@@ -35,19 +35,21 @@ module('Acceptance | categories', function(hooks) {
3535
});
3636

3737
test('listing categories', async function(assert) {
38-
this.server.create('category', { category: 'API bindings', crates_cnt: 0 });
39-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
40-
this.server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });
38+
this.server.create('category', { category: 'API bindings' });
39+
this.server.create('category', { category: 'Algorithms' });
40+
this.server.createList('crate', 1, { categoryIds: ['algorithms'] });
41+
this.server.create('category', { category: 'Asynchronous' });
42+
this.server.createList('crate', 15, { categoryIds: ['asynchronous'] });
4143

4244
await visit('/categories');
4345

4446
assert.dom('[data-test-category="api-bindings"] [data-test-crate-count]').hasText('0 crates');
4547
assert.dom('[data-test-category="algorithms"] [data-test-crate-count]').hasText('1 crate');
46-
assert.dom('[data-test-category="asynchronous"] [data-test-crate-count]').hasText('3,910 crates');
48+
assert.dom('[data-test-category="asynchronous"] [data-test-crate-count]').hasText('15 crates');
4749
});
4850

4951
test('category/:category_id index default sort is recent-downloads', async function(assert) {
50-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
52+
this.server.create('category', { category: 'Algorithms' });
5153

5254
await visit('/categories/algorithms');
5355

tests/mirage/categories-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ module('Mirage | Categories', function(hooks) {
125125
},
126126
});
127127
});
128+
129+
test('calculates `crates_cnt` correctly', async function(assert) {
130+
this.server.create('category', { category: 'cli' });
131+
this.server.createList('crate', 7, { categoryIds: ['cli'] });
132+
this.server.create('category', { category: 'not-cli' });
133+
this.server.createList('crate', 3, { categoryIds: ['not-cli'] });
134+
135+
let response = await fetch('/api/v1/categories/cli');
136+
assert.equal(response.status, 200);
137+
138+
let responsePayload = await response.json();
139+
assert.deepEqual(responsePayload, {
140+
category: {
141+
category: 'cli',
142+
crates_cnt: 7,
143+
created_at: '2010-06-16T21:30:45Z',
144+
description: 'This is the description for the category called "cli"',
145+
id: 'cli',
146+
slug: 'cli',
147+
},
148+
});
149+
});
128150
});
129151

130152
module('GET /api/v1/category_slugs', function() {

tests/mirage/crates-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ module('Mirage | Crates', function(hooks) {
294294
{
295295
id: 'no-std',
296296
category: 'no-std',
297-
crates_cnt: 0,
297+
crates_cnt: 1,
298298
created_at: '2010-06-16T21:30:45Z',
299299
description: 'This is the description for the category called "no-std"',
300300
slug: 'no-std',
@@ -314,7 +314,7 @@ module('Mirage | Crates', function(hooks) {
314314
assert.deepEqual(responsePayload.crate.keywords, ['no-std']);
315315
assert.deepEqual(responsePayload.keywords, [
316316
{
317-
crates_cnt: 0,
317+
crates_cnt: 1,
318318
id: 'no-std',
319319
keyword: 'no-std',
320320
},

tests/mirage/keywords-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,24 @@ module('Mirage | Keywords', function(hooks) {
107107
},
108108
});
109109
});
110+
111+
test('calculates `crates_cnt` correctly', async function(assert) {
112+
this.server.create('keyword', { keyword: 'cli' });
113+
this.server.createList('crate', 7, { keywordIds: ['cli'] });
114+
this.server.create('keyword', { keyword: 'not-cli' });
115+
this.server.createList('crate', 3, { keywordIds: ['not-cli'] });
116+
117+
let response = await fetch('/api/v1/keywords/cli');
118+
assert.equal(response.status, 200);
119+
120+
let responsePayload = await response.json();
121+
assert.deepEqual(responsePayload, {
122+
keyword: {
123+
id: 'cli',
124+
crates_cnt: 7,
125+
keyword: 'cli',
126+
},
127+
});
128+
});
110129
});
111130
});

0 commit comments

Comments
 (0)