-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d45ff5
commit 39bd90b
Showing
6 changed files
with
228 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
packages/spacecat-shared-data-access/test/unit/v2/models/base/schema.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* 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 */ | ||
|
||
// eslint-disable-next-line max-classes-per-file | ||
import { expect, use as chaiUse } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
import BaseModel from '../../../../../src/v2/models/base/base.model.js'; | ||
import BaseCollection from '../../../../../src/v2/models/base/base.collection.js'; | ||
import Schema from '../../../../../src/v2/models/base/schema.js'; | ||
import Reference from '../../../../../src/v2/models/base/reference.js'; | ||
|
||
chaiUse(chaiAsPromised); | ||
chaiUse(sinonChai); | ||
|
||
const MockModel = class MockEntityModel extends BaseModel {}; | ||
const MockCollection = class MockEntityCollection extends BaseCollection {}; | ||
|
||
describe('Schema', () => { | ||
const rawSchema = { | ||
serviceName: 'service', | ||
schemaVersion: '1.0', | ||
attributes: { | ||
id: { type: 'string' }, | ||
}, | ||
indexes: { | ||
primary: { pk: { composite: ['id'] } }, | ||
byOrganizationId: { sk: { facets: ['organizationId'] } }, | ||
}, | ||
references: [new Reference('belongs_to', 'Organization')], | ||
}; | ||
|
||
let instance; | ||
|
||
beforeEach(() => { | ||
instance = new Schema(MockModel, MockCollection, rawSchema); | ||
}); | ||
|
||
describe('constructor', () => { | ||
it('constructs a new Schema instance', () => { | ||
const schema = new Schema(MockModel, MockCollection, rawSchema); | ||
|
||
expect(schema.modelClass).to.equal(MockModel); | ||
expect(schema.collectionClass).to.equal(MockCollection); | ||
expect(schema.serviceName).to.equal('service'); | ||
expect(schema.schemaVersion).to.equal('1.0'); | ||
expect(schema.attributes).to.deep.equal({ id: { type: 'string' } }); | ||
expect(schema.indexes).to.deep.equal(rawSchema.indexes); | ||
expect(schema.references).to.deep.equal([{ | ||
options: {}, | ||
target: 'Organization', | ||
type: 'belongs_to', | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('accessors', () => { | ||
it('getAttribute', () => { | ||
expect(instance.getAttribute('id')).to.deep.equal({ type: 'string' }); | ||
}); | ||
|
||
it('getAttributes', () => { | ||
expect(instance.getAttributes()).to.deep.equal({ id: { type: 'string' } }); | ||
}); | ||
|
||
it('getCollectionName', () => { | ||
expect(instance.getCollectionName()).to.equal('MockEntityCollection'); | ||
}); | ||
|
||
it('getEntityName', () => { | ||
expect(instance.getEntityName()).to.equal('mockEntityModel'); | ||
}); | ||
|
||
it('getIdName', () => { | ||
expect(instance.getIdName()).to.equal('mockEntityModelId'); | ||
}); | ||
|
||
it('getIndexByName', () => { | ||
expect(instance.getIndexByName('primary')).to.deep.equal({ pk: { composite: ['id'] } }); | ||
}); | ||
|
||
it('getIndexes', () => { | ||
expect(instance.getIndexes()).to.deep.equal(rawSchema.indexes); | ||
}); | ||
|
||
it('getIndexes with exclusion', () => { | ||
expect(instance.getIndexes(['primary'])).to.deep.equal({ | ||
byOrganizationId: { sk: { facets: ['organizationId'] } }, | ||
}); | ||
}); | ||
|
||
it('getIndexKeys', () => { | ||
expect(instance.getIndexKeys('byOrganizationId')).to.deep.equal(['organizationId']); | ||
}); | ||
|
||
it('getIndexKeys with non-existent index', () => { | ||
expect(instance.getIndexKeys('non-existent')).to.deep.equal([]); | ||
}); | ||
|
||
it('getModelClass', () => { | ||
expect(instance.getModelClass()).to.equal(MockModel); | ||
}); | ||
|
||
it('getModelName', () => { | ||
expect(instance.getModelName()).to.equal('MockEntityModel'); | ||
}); | ||
|
||
it('getReferences', () => { | ||
expect(instance.getReferences()).to.deep.equal([{ | ||
options: {}, | ||
target: 'Organization', | ||
type: 'belongs_to', | ||
}]); | ||
}); | ||
|
||
it('getReferencesByType', () => { | ||
expect(instance.getReferencesByType('belongs_to')).to.deep.equal([{ | ||
options: {}, | ||
target: 'Organization', | ||
type: 'belongs_to', | ||
}]); | ||
}); | ||
|
||
it('getServiceName', () => { | ||
expect(instance.getServiceName()).to.equal('service'); | ||
}); | ||
|
||
it('getVersion', () => { | ||
expect(instance.getVersion()).to.equal('1.0'); | ||
}); | ||
}); | ||
|
||
describe('toElectroDBSchema', () => { | ||
it('returns an ElectroDB-compatible schema', () => { | ||
expect(instance.toElectroDBSchema()).to.deep.equal({ | ||
model: { | ||
entity: 'MockEntityModel', | ||
version: '1.0', | ||
service: 'service', | ||
}, | ||
attributes: { id: { type: 'string' } }, | ||
indexes: rawSchema.indexes, | ||
}); | ||
}); | ||
}); | ||
}); |