Skip to content

Commit

Permalink
ui: Adds ember-data blueprints for Consul specific HTTP adapter etc (#…
Browse files Browse the repository at this point in the history
…6461)

* ui: Adds ember-data blueprints for Consul specific HTTP adapter etc

These are currently quite Consul specific, but we also overwrite the
default model-test blueprint to keep the names consistent (dasherized)
for easy test filtering.

```
ember generate [adapter|serializer|model|repository|route] <name>
```
  • Loading branch information
johncowen authored Nov 19, 2019
1 parent d5b5203 commit 4e183a7
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ui-v2/blueprints/adapter-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*eslint node/no-extraneous-require: "off"*/
'use strict';

const useTestFrameworkDetector = require('@ember-data/-build-infra/src/utilities/test-framework-detector');
const path = require('path');

module.exports = useTestFrameworkDetector({
description: 'Generates a Consul HTTP ember-data adapter unit and integration tests',

root: __dirname,

fileMapTokens(options) {
return {
__root__() {
return 'tests';
},
__path__() {
return '';
}
};
},

locals(options) {
return {
};
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Integration | Adapter | <%= dasherizedModuleName %>', function(hooks) {
setupTest(hooks);
const dc = 'dc-1';
const id = 'slug';
test('requestForQuery returns the correct url/method', function(assert) {
const adapter = this.owner.lookup('adapter:<%= dasherizedModuleName %>');
const client = this.owner.lookup('service:client/http');
const expected = `GET /v1/<%= dasherizedModuleName %>?dc=${dc}`;
const actual = adapter.requestForQuery(client.url, {
dc: dc,
});
assert.equal(actual, expected);
});
test('requestForQueryRecord returns the correct url/method', function(assert) {
const adapter = this.owner.lookup('adapter:<%= dasherizedModuleName %>');
const client = this.owner.lookup('service:client/http');
const expected = `GET /v1/<%= dasherizedModuleName %>/${id}?dc=${dc}`;
const actual = adapter.requestForQueryRecord(client.url, {
dc: dc,
id: id,
});
assert.equal(actual, expected);
});
test("requestForQueryRecord throws if you don't specify an id", function(assert) {
const adapter = this.owner.lookup('adapter:<%= dasherizedModuleName %>');
const client = this.owner.lookup('service:client/http');
assert.throws(function() {
adapter.requestForQueryRecord(client.url, {
dc: dc,
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Adapter | <%= dasherizedModuleName %>', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.owner.lookup('adapter:<%= dasherizedModuleName %>');
assert.ok(adapter);
});
});
21 changes: 21 additions & 0 deletions ui-v2/blueprints/adapter/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Adapter from './application';

export default Adapter.extend({
requestForQuery: function(request, { dc, index }) {
return request`
GET /v1/<%= dasherizedModuleName %>?${{ dc }}
${{ index }}
`;
},
requestForQueryRecord: function(request, { dc, index, id }) {
if (typeof id === 'undefined') {
throw new Error('You must specify an id');
}
return request`
GET /v1/<%= dasherizedModuleName %>/${id}?${{ dc }}
${{ index }}
`;
},
});
37 changes: 37 additions & 0 deletions ui-v2/blueprints/adapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*eslint node/no-extraneous-require: "off"*/
'use strict';

const path = require('path');
const isModuleUnificationProject = require('@ember-data/-build-infra/src/utilities/module-unification').isModuleUnificationProject;
module.exports = {
description: 'Generates a Consul HTTP ember-data adapter',

availableOptions: [{ name: 'base-class', type: String }],

root: __dirname,

fileMapTokens(options) {
if (isModuleUnificationProject(this.project)) {
return {
__root__() {
return 'src';
},
__path__(options) {
return path.join('data', 'models', options.dasherizedModuleName);
},
__name__() {
return 'adapter';
},
};
}
},
locals(options) {
// Return custom template variables here.
return {
};
}

// afterInstall(options) {
// // Perform extra work here.
// }
};
27 changes: 27 additions & 0 deletions ui-v2/blueprints/model-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*eslint node/no-extraneous-require: "off"*/
'use strict';

const useTestFrameworkDetector = require('@ember-data/-build-infra/src/utilities/test-framework-detector');
const path = require('path');

module.exports = useTestFrameworkDetector({
description: 'Generates a Consul ember-data model unit tests',

root: __dirname,

fileMapTokens(options) {
return {
__root__() {
return 'tests';
},
__path__() {
return '';
}
};
},

locals(options) {
return {
};
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Model | <%= dasherizedModuleName %>', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('<%= dasherizedModuleName %>', {});
assert.ok(model);
});
});
10 changes: 10 additions & 0 deletions ui-v2/blueprints/model/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export const PRIMARY_KEY = 'uid';
export const SLUG_KEY = 'ID';
export default Model.extend({
[PRIMARY_KEY]: attr('string'),
[SLUG_KEY]: attr('string'),
Datacenter: attr('string'),
});
37 changes: 37 additions & 0 deletions ui-v2/blueprints/model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*eslint node/no-extraneous-require: "off"*/
'use strict';

const path = require('path');
const isModuleUnificationProject = require('@ember-data/-build-infra/src/utilities/module-unification').isModuleUnificationProject;
module.exports = {
description: 'Generates a Consul HTTP ember-data model',

availableOptions: [],

root: __dirname,

fileMapTokens(options) {
if (isModuleUnificationProject(this.project)) {
return {
__root__() {
return 'src';
},
__path__(options) {
return path.join('data', 'models', options.dasherizedModuleName);
},
__name__() {
return 'model';
},
};
}
},
locals(options) {
// Return custom template variables here.
return {
};
}

// afterInstall(options) {
// // Perform extra work here.
// }
};
28 changes: 28 additions & 0 deletions ui-v2/blueprints/repository-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*eslint node/no-extraneous-require: "off"*/
'use strict';

const useTestFrameworkDetector = require('@ember-data/-build-infra/src/utilities/test-framework-detector');
const path = require('path');

module.exports = useTestFrameworkDetector({
description: 'Generates a Consul HTTP ember-data serializer unit and integration tests',

root: __dirname,

fileMapTokens(options) {
return {
__root__() {
return 'tests';
},
__path__() {
return '';
}
};
},

locals(options) {
return {
screamingSnakeCaseModuleName: options.entity.name.replace('-', '_').toUpperCase()
};
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { moduleFor, test } from 'ember-qunit';
import repo from 'consul-ui/tests/helpers/repo';

moduleFor('service:repository/<%= dasherizedModuleName %>', 'Integration | Repository | <%= dasherizedModuleName %>', {
// Specify the other units that are required for this test.
integration: true,
});
const dc = 'dc-1';
const id = 'slug';
const now = new Date().getTime();
test('findByDatacenter returns the correct data for list endpoint', function(assert) {
get(this.subject(), 'store').serializerFor('<%= dasherizedModuleName %>').timestamp = function() {
return now;
};
return repo(
'Service',
'findAllByDatacenter',
this.subject(),
function retrieveStub(stub) {
return stub(`/v1/<%= dasherizedModuleName %>?dc=${dc}`, {
CONSUL_<%= screamingSnakeCaseModuleName %>_COUNT: '100',
});
},
function performTest(service) {
return service.findAllByDatacenter(dc);
},
function performAssertion(actual, expected) {
assert.deepEqual(
actual,
expected(function(payload) {
return payload.map(item =>
Object.assign({}, item, {
SyncTime: now,
Datacenter: dc,
uid: `["${dc}","${item.Name}"]`,
})
);
})
);
}
);
});
test('findBySlug returns the correct data for item endpoint', function(assert) {
return repo(
'Service',
'findBySlug',
this.subject(),
function retrieveStub(stub) {
return stub(`/v1/<%= dasherizedModuleName %>/${id}?dc=${dc}`, {
CONSUL_<%= screamingSnakeCaseModuleName %>_COUNT: 1,
});
},
function performTest(service) {
return service.findBySlug(id, dc);
},
function performAssertion(actual, expected) {
assert.deepEqual(
actual,
expected(function(payload) {
return Object.assign(
{},
{
Datacenter: dc,
uid: `["${dc}","${id}"]`,
meta: {
cursor: undefined
}
},
payload
);
})
);
}
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Repository | <%= dasherizedModuleName %>', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let repo = this.owner.lookup('service:repository/<%= dasherizedModuleName %>');
assert.ok(repo);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import RepositoryService from 'consul-ui/services/repository';

const modelName = '<%= dasherizedModuleName %>';
export default RepositoryService.extend({
getModelName: function() {
return modelName;
},
});
27 changes: 27 additions & 0 deletions ui-v2/blueprints/repository/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const path = require('path');
module.exports = {
description: 'Generates a Consul repository',

availableOptions: [],

root: __dirname,

fileMapTokens(options) {
return {
__path__() {
return path.join('services', 'repository');
}
};
},
locals(options) {
// Return custom template variables here.
return {
};
}

// afterInstall(options) {
// // Perform extra work here.
// }
};
4 changes: 4 additions & 0 deletions ui-v2/blueprints/route/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Route from '@ember/routing/route';

export default Route.extend({
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{outlet}}
Loading

0 comments on commit 4e183a7

Please sign in to comment.