From 12277c5557be871d5f42c503290be563bec59e08 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Tue, 8 Feb 2022 08:24:39 -0800 Subject: [PATCH] Move new fields attributes from host generated model to plain host model. Add normalization tests --- addons/api/addon/generated/models/host.js | 18 ------ addons/api/addon/models/host.js | 15 +++++ .../api/tests/unit/serializers/host-test.js | 60 +++++++++++++++++++ 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/addons/api/addon/generated/models/host.js b/addons/api/addon/generated/models/host.js index 3873070f2..08efa6397 100644 --- a/addons/api/addon/generated/models/host.js +++ b/addons/api/addon/generated/models/host.js @@ -44,27 +44,9 @@ export default class GeneratedHostModel extends BaseModel { }) version; - @attr({ - description: 'Attributes specific to the host type', - }) - attributes; - - @attr('string', { - description: 'The address (DNS or IP name) used to reach the host', - isNestedAttribute: true, - }) - address; - @attr({ description: 'Plugin information for this resource.', readOnly: true, }) plugin; - - @attr('string', { - description: '', - readOnly: true, - isNestedAttribute: true, - }) - external_id; } diff --git a/addons/api/addon/models/host.js b/addons/api/addon/models/host.js index 133464027..6ff2022e4 100644 --- a/addons/api/addon/models/host.js +++ b/addons/api/addon/models/host.js @@ -1,7 +1,9 @@ import GeneratedHostModel from '../generated/models/host'; +import { attr } from '@ember-data/model'; import { fragmentArray } from 'ember-data-model-fragments/attributes'; export default class HostModel extends GeneratedHostModel { + // =attributes (nested under attributes) @fragmentArray('fragment-string', { readOnly: true, emptyArrayIfMissing: true, @@ -9,6 +11,19 @@ export default class HostModel extends GeneratedHostModel { }) ip_addresses; + @attr('string', { + description: 'The address (DNS or IP name) used to reach the host', + isNestedAttribute: true, + }) + address; + + @attr('string', { + description: '', + readOnly: true, + isNestedAttribute: true, + }) + external_id; + // Aws specific @fragmentArray('fragment-string', { readOnly: true, diff --git a/addons/api/tests/unit/serializers/host-test.js b/addons/api/tests/unit/serializers/host-test.js index e6361984c..366ca62ad 100644 --- a/addons/api/tests/unit/serializers/host-test.js +++ b/addons/api/tests/unit/serializers/host-test.js @@ -48,4 +48,64 @@ module('Unit | Serializer | host', function (hooks) { }, }); }); + + test('it normalizes type static record', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('host'); + const host = store.createRecord('host').constructor; + const payload = { + id: '1', + name: 'host test', + type: 'static', + }; + const normalized = serializer.normalizeSingleResponse(store, host, payload); + assert.deepEqual(normalized, { + included: [], + data: { + id: '1', + type: 'host', + attributes: { + name: 'host test', + type: 'static', + authorized_actions: [], + dns_names: [], + ip_addresses: [], + }, + relationships: {}, + }, + }); + }); + + test('it normalizes type plugin (aws) record', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('host'); + const host = store.createRecord('host').constructor; + const payload = { + id: '1', + name: 'host test', + type: 'plugin', + plugin: { + name: 'aws', + }, + }; + const normalized = serializer.normalizeSingleResponse(store, host, payload); + assert.deepEqual(normalized, { + included: [], + data: { + id: '1', + type: 'host', + attributes: { + name: 'host test', + type: 'plugin', + authorized_actions: [], + dns_names: [], + ip_addresses: [], + plugin: { name: 'aws' }, + }, + relationships: {}, + }, + }); + }); });