Skip to content

Commit

Permalink
Merge branch 'feat-legacy-models-import' of github.com:adobe/spacecat…
Browse files Browse the repository at this point in the history
…-shared into feat-legacy-models-import
  • Loading branch information
alinarublea committed Dec 10, 2024
2 parents f8b9c20 + 6cab613 commit 9f5a3c9
Show file tree
Hide file tree
Showing 27 changed files with 1,842 additions and 504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@ class BaseCollection {
items.forEach((item) => {
try {
const { Item } = this.entity.put(item).params();
validatedItems.push({
...removeElectroProperties(Item),
...item,
});
validatedItems.push({ ...removeElectroProperties(Item), ...item });
} catch (error) {
if (error instanceof ElectroValidationError) {
errorItems.push({ item, error: new ValidationError(error) });
Expand Down
9 changes: 6 additions & 3 deletions packages/spacecat-shared-data-access/src/v2/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
* governing permissions and limitations under the License.
*/

export * from './base/index.js';

export * from './api-key/index.js';
export * from './audit/index.js';
export * from './base/index.js';
export * from './configuration/index.js';
export * from './experiment/index.js';
export * from './import-job/index.js';
export * from './import-url/index.js';
export * from './key-event/index.js';
export * from './opportunity/index.js';
export * from './organization/index.js';
export * from './site/index.js';
export * from './site-candidate/index.js';
export * from './site-top-page/index.js';
export * from './site/index.js';
export * from './suggestion/index.js';
50 changes: 20 additions & 30 deletions packages/spacecat-shared-data-access/src/v2/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,47 @@
* governing permissions and limitations under the License.
*/

import { hasText, isInteger } from '@adobe/spacecat-shared-utils';
import pluralize from 'pluralize';
import { isInteger } from '@adobe/spacecat-shared-utils';

const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
const capitalize = (str) => (hasText(str) ? str[0].toUpperCase() + str.slice(1) : '');
const decapitalize = (str) => (hasText(str) ? str[0].toLowerCase() + str.slice(1) : '');

const collectionNameToEntityName = (collectionName) => collectionName.replace('Collection', '');
const decapitalize = (str) => str.charAt(0).toLowerCase() + str.slice(1);

const entityNameToCollectionName = (entityName) => `${pluralize.singular(entityName)}Collection`;
const entityNameToIdName = (collectionName) => `${decapitalize(collectionName)}Id`;
const entityNameToReferenceMethodName = (target, type) => {
let baseName = capitalize(target);
baseName = type === 'has_many'
? pluralize.plural(baseName)
: pluralize.singular(baseName);

const entityNameToIdName = (entityName) => `${decapitalize(entityName)}Id`;

const entityNameToReferenceMethodName = (target, type) => {
const baseName = type === 'has_many'
? pluralize.plural(capitalize(target))
: pluralize.singular(capitalize(target));
return `get${baseName}`;
};

const entityNameToAllPKValue = (entityName) => `ALL_${pluralize.plural(entityName.toUpperCase())}`;

const idNameToEntityName = (idName) => capitalize(pluralize.singular(idName.replace('Id', '')));

const keyNamesToIndexName = (keyNames) => {
const capitalizedKeyNames = keyNames.map((keyName) => capitalize(keyName));
return `by${capitalizedKeyNames.join('And')}`;
};
const keyNamesToIndexName = (keyNames) => `by${keyNames.map(capitalize).join('And')}`;

const modelNameToEntityName = (modelName) => decapitalize(modelName);

const sanitizeTimestamps = (data) => {
const sanitizedData = { ...data };

delete sanitizedData.createdAt;
delete sanitizedData.updatedAt;

return sanitizedData;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { createdAt, updatedAt, ...rest } = data;
return rest;
};

const sanitizeIdAndAuditFields = (entityName, data) => {
const idName = entityNameToIdName(entityName);
const sanitizedData = { ...data };

delete sanitizedData[idName];

return sanitizeTimestamps(sanitizedData);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [idName]: _, ...rest } = data;
return sanitizeTimestamps(rest);
};

function incrementVersion(version) {
if (!isInteger(version)) return 1;

const versionNumber = parseInt(version, 10);
return versionNumber + 1;
}
const incrementVersion = (version) => (isInteger(version) ? parseInt(version, 10) + 1 : 1);

export {
capitalize,
Expand Down
6 changes: 5 additions & 1 deletion packages/spacecat-shared-data-access/test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('Data Access Wrapper Tests', () => {
mockFn = sinon.stub().resolves('function response');
mockContext = {
env: {},
log: sinon.spy(),
log: {
info: sinon.spy(),
debug: sinon.spy(),
error: sinon.spy(),
},
};
mockRequest = {};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,18 @@ describe('Data Access Object Tests', () => {
];

const electroServiceFunctions = [
'ApiKey',
'Audit',
'Configuration',
'Experiment',
'ImportJob',
'ImportUrl',
'KeyEvent',
'Opportunity',
'Organization',
'Site',
'SiteCandidate',
'SiteTopPage',
'Suggestion',
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

import { expect, use as chaiUse } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { Entity } from 'electrodb';
import { spy, stub } from 'sinon';
import sinonChai from 'sinon-chai';

import ApiKeyCollection from '../../../../../src/v2/models/api-key/api-key.collection.js';
import ApiKey from '../../../../../src/v2/models/api-key/api-key.model.js';
import ApiKeySchema from '../../../../../src/v2/models/api-key/api-key.schema.js';

chaiUse(chaiAsPromised);
chaiUse(sinonChai);

const { attributes } = new Entity(ApiKeySchema).model.schema;

let mockElectroService;

describe('ApiKeyCollection', () => {
let instance;
let mockApiKeyModel;
let mockLogger;
let mockEntityRegistry;

const mockRecord = {
apiKeyId: 's12345',
};

beforeEach(() => {
mockLogger = {
error: spy(),
warn: spy(),
};

mockEntityRegistry = {
getCollection: stub(),
};

mockElectroService = {
entities: {
apiKey: {
model: {
name: 'apiKey',
schema: { attributes },
original: {
references: {},
},
indexes: {
primary: {
pk: {
field: 'pk',
composite: ['apiKeyId'],
},
},
},
},
},
},
};

mockApiKeyModel = new ApiKey(
mockElectroService,
mockEntityRegistry,
mockRecord,
mockLogger,
);

instance = new ApiKeyCollection(
mockElectroService,
mockEntityRegistry,
mockLogger,
);
});

describe('constructor', () => {
it('initializes the ApiKeyCollection instance correctly', () => {
expect(instance).to.be.an('object');
expect(instance.electroService).to.equal(mockElectroService);
expect(instance.entityRegistry).to.equal(mockEntityRegistry);
expect(instance.log).to.equal(mockLogger);

expect(mockApiKeyModel).to.be.an('object');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

import { expect, use as chaiUse } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { Entity } from 'electrodb';
import { spy, stub } from 'sinon';
import sinonChai from 'sinon-chai';

import AuditCollection from '../../../../../src/v2/models/audit/audit.collection.js';
import Audit from '../../../../../src/v2/models/audit/audit.model.js';
import AuditSchema from '../../../../../src/v2/models/audit/audit.schema.js';

chaiUse(chaiAsPromised);
chaiUse(sinonChai);

const { attributes } = new Entity(AuditSchema).model.schema;

let mockElectroService;

describe('AuditCollection', () => {
let instance;
let mockAuditModel;
let mockLogger;
let mockEntityRegistry;

const mockRecord = {
auditId: 's12345',
};

beforeEach(() => {
mockLogger = {
error: spy(),
warn: spy(),
};

mockEntityRegistry = {
getCollection: stub(),
};

mockElectroService = {
entities: {
audit: {
model: {
name: 'audit',
schema: { attributes },
original: {
references: {},
},
indexes: {
primary: {
pk: {
field: 'pk',
composite: ['auditId'],
},
},
},
},
},
},
};

mockAuditModel = new Audit(
mockElectroService,
mockEntityRegistry,
mockRecord,
mockLogger,
);

instance = new AuditCollection(
mockElectroService,
mockEntityRegistry,
mockLogger,
);
});

describe('constructor', () => {
it('initializes the AuditCollection instance correctly', () => {
expect(instance).to.be.an('object');
expect(instance.electroService).to.equal(mockElectroService);
expect(instance.entityRegistry).to.equal(mockEntityRegistry);
expect(instance.log).to.equal(mockLogger);

expect(mockAuditModel).to.be.an('object');
});
});
});
Loading

0 comments on commit 9f5a3c9

Please sign in to comment.