Refactor to use string IDs for executables #262
This commit unifies the concepts of executables having same ID structure. It paves the way for more complex ID structure and using IDs in collection files as part of new ID solution (#262). Using string IDs also leads to more expressive test code. This commit also refactors the rest of the code to adopt to the changes. This commit: - Separate concerns from entities for data access (in repositories) and executables. Executables use `Identifiable` meanwhile repositories use `RepositoryEntity`. - Refactor unnecessary generic parameters for enttities and ids, enforcing string gtype everwyhere. - Changes numeric IDs to string IDs for categories to unify the retrieval and construction for executables, using pseudo-ids (their names) just like scripts. - Remove `BaseEntity` for simplicity. - Simplify usage and construction of executable objects. Move factories responsible for creation of category/scripts to domain layer. Do not longer export `CollectionCategorY` and `CollectionScript`. - Use named typed for string IDs for better differentation of different ID contexts in code.
This commit is contained in:
316
tests/unit/domain/Executables/Category/CategoryFactory.spec.ts
Normal file
316
tests/unit/domain/Executables/Category/CategoryFactory.spec.ts
Normal file
@@ -0,0 +1,316 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import type { Category } from '@/domain/Executables/Category/Category';
|
||||
import type { Script } from '@/domain/Executables/Script/Script';
|
||||
import { createCategory } from '@/domain/Executables/Category/CategoryFactory';
|
||||
import type { ExecutableId } from '@/domain/Executables/Identifiable';
|
||||
|
||||
describe('CategoryFactory', () => {
|
||||
describe('createCategory', () => {
|
||||
describe('id', () => {
|
||||
it('assigns id correctly', () => {
|
||||
// arrange
|
||||
const expectedId: ExecutableId = 'expected category id';
|
||||
// act
|
||||
const category = new TestContext()
|
||||
.withId(expectedId)
|
||||
.build();
|
||||
// assert
|
||||
const actualId = category.executableId;
|
||||
expect(actualId).to.equal(expectedId);
|
||||
});
|
||||
describe('throws error if id is absent', () => {
|
||||
itEachAbsentStringValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing ID';
|
||||
const id = absentValue;
|
||||
// act
|
||||
const construct = () => new TestContext()
|
||||
.withId(id)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
}, { excludeNull: true, excludeUndefined: true });
|
||||
});
|
||||
});
|
||||
describe('name', () => {
|
||||
it('assigns name correctly', () => {
|
||||
// arrange
|
||||
const expectedName = 'expected category name';
|
||||
// act
|
||||
const category = new TestContext()
|
||||
.withName(expectedName)
|
||||
.build();
|
||||
// assert
|
||||
const actualName = category.name;
|
||||
expect(actualName).to.equal(expectedName);
|
||||
});
|
||||
describe('throws error if name is absent', () => {
|
||||
itEachAbsentStringValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing name';
|
||||
const name = absentValue;
|
||||
// act
|
||||
const construct = () => new TestContext()
|
||||
.withName(name)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
}, { excludeNull: true, excludeUndefined: true });
|
||||
});
|
||||
});
|
||||
describe('docs', () => {
|
||||
it('assigns docs correctly', () => {
|
||||
// arrange
|
||||
const expectedDocs = ['expected', 'docs'];
|
||||
// act
|
||||
const category = new TestContext()
|
||||
.withDocs(expectedDocs)
|
||||
.build();
|
||||
// assert
|
||||
const actualDocs = category.docs;
|
||||
expect(actualDocs).to.equal(expectedDocs);
|
||||
});
|
||||
});
|
||||
describe('children', () => {
|
||||
it('assigns scripts correctly', () => {
|
||||
// arrange
|
||||
const expectedScripts = [
|
||||
new ScriptStub('expected-script-1'),
|
||||
new ScriptStub('expected-script-2'),
|
||||
];
|
||||
// act
|
||||
const category = new TestContext()
|
||||
.withScripts(expectedScripts)
|
||||
.build();
|
||||
// assert
|
||||
const actualScripts = category.docs;
|
||||
expect(actualScripts).to.equal(expectedScripts);
|
||||
});
|
||||
it('assigns categories correctly', () => {
|
||||
// arrange
|
||||
const expectedCategories = [
|
||||
new CategoryStub('expected-subcategory-1'),
|
||||
new CategoryStub('expected-subcategory-2'),
|
||||
];
|
||||
// act
|
||||
const category = new TestContext()
|
||||
.withSubcategories(expectedCategories)
|
||||
.build();
|
||||
// assert
|
||||
const actualCategories = category.subcategories;
|
||||
expect(actualCategories).to.equal(expectedCategories);
|
||||
});
|
||||
it('throws error if no children are present', () => {
|
||||
// arrange
|
||||
const expectedError = 'A category must have at least one sub-category or script';
|
||||
const scriptChildren: readonly Script[] = [];
|
||||
const categoryChildren: readonly Category[] = [];
|
||||
// act
|
||||
const construct = () => new TestContext()
|
||||
.withSubcategories(categoryChildren)
|
||||
.withScripts(scriptChildren)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('getAllScriptsRecursively', () => {
|
||||
it('retrieves direct child scripts', () => {
|
||||
// arrange
|
||||
const expectedScripts: readonly Script[] = [
|
||||
new ScriptStub('expected-script-1'),
|
||||
new ScriptStub('expected-script-2'),
|
||||
];
|
||||
const category = new TestContext()
|
||||
.withScripts(expectedScripts)
|
||||
.build();
|
||||
// act
|
||||
const actual = category.getAllScriptsRecursively();
|
||||
// assert
|
||||
expect(actual).to.have.deep.members(expectedScripts);
|
||||
});
|
||||
it('retrieves scripts from direct child categories', () => {
|
||||
// arrange
|
||||
const expectedScriptIds: readonly string[] = [
|
||||
'1', '2', '3', '4',
|
||||
];
|
||||
const subcategories: readonly Category[] = [
|
||||
new CategoryStub('subcategory-1').withScriptIds('1', '2'),
|
||||
new CategoryStub('subcategory-2').withScriptIds('3', '4'),
|
||||
];
|
||||
const category = new TestContext()
|
||||
.withScripts([])
|
||||
.withSubcategories(subcategories)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = category
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.executableId);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
it('retrieves scripts from both direct children and child categories', () => {
|
||||
// arrange
|
||||
const expectedScriptIds: readonly string[] = [
|
||||
'1', '2', '3', '4', '5', '6',
|
||||
];
|
||||
const subcategories: readonly Category[] = [
|
||||
new CategoryStub('subcategory-1').withScriptIds('1', '2'),
|
||||
new CategoryStub('subcategory-2').withScriptIds('3', '4'),
|
||||
];
|
||||
const scripts: readonly Script[] = [
|
||||
new ScriptStub('1'),
|
||||
new ScriptStub('2'),
|
||||
];
|
||||
const category = new TestContext()
|
||||
.withSubcategories(subcategories)
|
||||
.withScripts(scripts)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = category
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.executableId);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
it('retrieves scripts from nested categories recursively', () => {
|
||||
// arrange
|
||||
const expectedScriptIds: readonly string[] = [
|
||||
'1', '2', '3', '4', '5', '6',
|
||||
];
|
||||
const subcategories: readonly Category[] = [
|
||||
new CategoryStub('subcategory-1')
|
||||
.withScriptIds('1', '2')
|
||||
.withCategory(
|
||||
new CategoryStub('subcategory-1-subcategory-1')
|
||||
.withScriptIds('3', '4'),
|
||||
),
|
||||
new CategoryStub('subcategory-2')
|
||||
.withCategories(
|
||||
new CategoryStub('subcategory-2-subcategory-1')
|
||||
.withScriptIds('5')
|
||||
.withCategory(
|
||||
new CategoryStub('subcategory-2-subcategory-1-subcategory-1')
|
||||
.withCategory(
|
||||
new CategoryStub('subcategory-2-subcategory-1-subcategory-1-subcategory-1')
|
||||
.withScriptIds('6'),
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
// assert
|
||||
const category = new TestContext()
|
||||
.withScripts([])
|
||||
.withSubcategories(subcategories)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = category
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.executableId);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
});
|
||||
describe('includes', () => {
|
||||
it('returns false for scripts not included', () => {
|
||||
// assert
|
||||
const expectedResult = false;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub('subcategory')
|
||||
.withScriptIds('1', '2');
|
||||
const category = new TestContext()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = category.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
it('returns true for scripts directly included', () => {
|
||||
// assert
|
||||
const expectedResult = true;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub('subcategory')
|
||||
.withScript(script)
|
||||
.withScriptIds('non-related');
|
||||
const category = new TestContext()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = category.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
it('returns true for scripts included in nested categories', () => {
|
||||
// assert
|
||||
const expectedResult = true;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub('subcategory')
|
||||
.withScriptIds('non-related')
|
||||
.withCategory(
|
||||
new CategoryStub('nested-subcategory')
|
||||
.withScript(script),
|
||||
);
|
||||
const category = new TestContext()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = category.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private id = `[${TestContext.name}] test category`;
|
||||
|
||||
private name = 'test-category';
|
||||
|
||||
private docs: ReadonlyArray<string> = [];
|
||||
|
||||
private subcategories: ReadonlyArray<Category> = [];
|
||||
|
||||
private scripts: ReadonlyArray<Script> = [
|
||||
new ScriptStub(`[${TestContext.name}] script`),
|
||||
];
|
||||
|
||||
public withId(id: string): this {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withName(name: string): this {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withDocs(docs: ReadonlyArray<string>): this {
|
||||
this.docs = docs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withScripts(scripts: ReadonlyArray<Script>): this {
|
||||
this.scripts = scripts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withSubcategories(subcategories: ReadonlyArray<Category>): this {
|
||||
this.subcategories = subcategories;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): ReturnType<typeof createCategory> {
|
||||
return createCategory({
|
||||
executableId: this.id,
|
||||
name: this.name,
|
||||
docs: this.docs,
|
||||
subcategories: this.subcategories,
|
||||
scripts: this.scripts,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,217 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { CollectionCategory } from '@/domain/Executables/Category/CollectionCategory';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import type { Category } from '@/domain/Executables/Category/Category';
|
||||
import type { Script } from '@/domain/Executables/Script/Script';
|
||||
|
||||
describe('CollectionCategory', () => {
|
||||
describe('ctor', () => {
|
||||
describe('throws error if name is absent', () => {
|
||||
itEachAbsentStringValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing name';
|
||||
const name = absentValue;
|
||||
// act
|
||||
const construct = () => new CategoryBuilder()
|
||||
.withName(name)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
}, { excludeNull: true, excludeUndefined: true });
|
||||
});
|
||||
it('throws error if no children are present', () => {
|
||||
// arrange
|
||||
const expectedError = 'A category must have at least one sub-category or script';
|
||||
const scriptChildren: readonly Script[] = [];
|
||||
const categoryChildren: readonly Category[] = [];
|
||||
// act
|
||||
const construct = () => new CategoryBuilder()
|
||||
.withSubcategories(categoryChildren)
|
||||
.withScripts(scriptChildren)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('getAllScriptsRecursively', () => {
|
||||
it('retrieves direct child scripts', () => {
|
||||
// arrange
|
||||
const expectedScripts = [new ScriptStub('1'), new ScriptStub('2')];
|
||||
const sut = new CategoryBuilder()
|
||||
.withScripts(expectedScripts)
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.getAllScriptsRecursively();
|
||||
// assert
|
||||
expect(actual).to.have.deep.members(expectedScripts);
|
||||
});
|
||||
it('retrieves scripts from direct child categories', () => {
|
||||
// arrange
|
||||
const expectedScriptIds = ['1', '2', '3', '4'];
|
||||
const categories = [
|
||||
new CategoryStub(31).withScriptIds('1', '2'),
|
||||
new CategoryStub(32).withScriptIds('3', '4'),
|
||||
];
|
||||
const sut = new CategoryBuilder()
|
||||
.withScripts([])
|
||||
.withSubcategories(categories)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = sut
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.id);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
it('retrieves scripts from both direct children and child categories', () => {
|
||||
// arrange
|
||||
const expectedScriptIds = ['1', '2', '3', '4', '5', '6'];
|
||||
const categories = [
|
||||
new CategoryStub(31).withScriptIds('1', '2'),
|
||||
new CategoryStub(32).withScriptIds('3', '4'),
|
||||
];
|
||||
const scripts = [new ScriptStub('5'), new ScriptStub('6')];
|
||||
const sut = new CategoryBuilder()
|
||||
.withSubcategories(categories)
|
||||
.withScripts(scripts)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = sut
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.id);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
it('retrieves scripts from nested categories recursively', () => {
|
||||
// arrange
|
||||
const expectedScriptIds = ['1', '2', '3', '4', '5', '6'];
|
||||
const categories = [
|
||||
new CategoryStub(31)
|
||||
.withScriptIds('1', '2')
|
||||
.withCategory(
|
||||
new CategoryStub(32)
|
||||
.withScriptIds('3', '4'),
|
||||
),
|
||||
new CategoryStub(33)
|
||||
.withCategories(
|
||||
new CategoryStub(34)
|
||||
.withScriptIds('5')
|
||||
.withCategory(
|
||||
new CategoryStub(35)
|
||||
.withCategory(
|
||||
new CategoryStub(35).withScriptIds('6'),
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
// assert
|
||||
const sut = new CategoryBuilder()
|
||||
.withScripts([])
|
||||
.withSubcategories(categories)
|
||||
.build();
|
||||
// act
|
||||
const actualIds = sut
|
||||
.getAllScriptsRecursively()
|
||||
.map((s) => s.id);
|
||||
// assert
|
||||
expect(actualIds).to.have.deep.members(expectedScriptIds);
|
||||
});
|
||||
});
|
||||
describe('includes', () => {
|
||||
it('returns false for scripts not included', () => {
|
||||
// assert
|
||||
const expectedResult = false;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub(33)
|
||||
.withScriptIds('1', '2');
|
||||
const sut = new CategoryBuilder()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
it('returns true for scripts directly included', () => {
|
||||
// assert
|
||||
const expectedResult = true;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub(33)
|
||||
.withScript(script)
|
||||
.withScriptIds('non-related');
|
||||
const sut = new CategoryBuilder()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
it('returns true for scripts included in nested categories', () => {
|
||||
// assert
|
||||
const expectedResult = true;
|
||||
const script = new ScriptStub('3');
|
||||
const childCategory = new CategoryStub(22)
|
||||
.withScriptIds('non-related')
|
||||
.withCategory(new CategoryStub(33).withScript(script));
|
||||
const sut = new CategoryBuilder()
|
||||
.withSubcategories([childCategory])
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.includes(script);
|
||||
// assert
|
||||
expect(actual).to.equal(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class CategoryBuilder {
|
||||
private id = 3264;
|
||||
|
||||
private name = 'test-script';
|
||||
|
||||
private docs: ReadonlyArray<string> = [];
|
||||
|
||||
private subcategories: ReadonlyArray<Category> = [];
|
||||
|
||||
private scripts: ReadonlyArray<Script> = [
|
||||
new ScriptStub(`[${CategoryBuilder.name}] script`),
|
||||
];
|
||||
|
||||
public withId(id: number): this {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withName(name: string): this {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withDocs(docs: ReadonlyArray<string>): this {
|
||||
this.docs = docs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withScripts(scripts: ReadonlyArray<Script>): this {
|
||||
this.scripts = scripts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withSubcategories(subcategories: ReadonlyArray<Category>): this {
|
||||
this.subcategories = subcategories;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): CollectionCategory {
|
||||
return new CollectionCategory({
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
docs: this.docs,
|
||||
subcategories: this.subcategories,
|
||||
scripts: this.scripts,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user