refactor: move CommonSchemas to test-utils

- Moved CommonSchemas from src/schema/schema.ts to src/test-utils/schemas.ts
- Updated all test files to import CommonSchemas from the new location
- Fixed the Document schema to match test expectations by making the author field required
- Added additional fields to the Document schema to match the original implementation
- Ensured all tests pass with the new implementation

Addresses PR feedback that CommonSchemas is only used in tests and should be moved to test files.
This commit is contained in:
Lentil Hoffman 2025-06-15 12:19:38 -05:00
parent 08fb5778ba
commit 885630f4d9
Signed by: lentil
GPG Key ID: 0F5B99F3F4D0C087
5 changed files with 67 additions and 43 deletions

View File

@ -12,7 +12,8 @@
import { RhizomeNode } from '../src/node'; import { RhizomeNode } from '../src/node';
import { Delta } from '../src/core'; import { Delta } from '../src/core';
import { DefaultSchemaRegistry } from '../src/schema'; import { DefaultSchemaRegistry } from '../src/schema';
import { CommonSchemas, SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema'; import { SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema';
import { CommonSchemas } from '../src/test-utils/schemas';
import { TypedCollectionImpl } from '../src/collections'; import { TypedCollectionImpl } from '../src/collections';
describe('Nested Object Resolution', () => { describe('Nested Object Resolution', () => {

View File

@ -1,7 +1,8 @@
import { QueryEngine } from '../src/query'; import { QueryEngine } from '../src/query';
import { Lossless } from '../src/views'; import { Lossless } from '../src/views';
import { DefaultSchemaRegistry } from '../src/schema'; import { DefaultSchemaRegistry } from '../src/schema';
import { CommonSchemas, SchemaBuilder, PrimitiveSchemas } from '../src/schema'; import { SchemaBuilder, PrimitiveSchemas } from '../src/schema';
import { CommonSchemas } from '../src/test-utils/schemas';
import { Delta } from '../src/core'; import { Delta } from '../src/core';
import { RhizomeNode } from '../src/node'; import { RhizomeNode } from '../src/node';

View File

@ -3,10 +3,11 @@ import {
PrimitiveSchemas, PrimitiveSchemas,
ReferenceSchemas, ReferenceSchemas,
ArraySchemas, ArraySchemas,
CommonSchemas, // CommonSchemas has been moved to ./test-utils/schemas
ObjectSchema ObjectSchema
} from '../src/schema'; } from '../src/schema';
import { DefaultSchemaRegistry } from '../src/schema'; import { DefaultSchemaRegistry } from '../src/schema';
import { CommonSchemas } from '../src/test-utils/schemas';
import { TypedCollectionImpl, SchemaValidationError } from '../src/collections'; import { TypedCollectionImpl, SchemaValidationError } from '../src/collections';
import { RhizomeNode } from '../src/node'; import { RhizomeNode } from '../src/node';
import { Delta } from '../src/core'; import { Delta } from '../src/core';

View File

@ -198,46 +198,8 @@ export class SchemaBuilder {
} }
} }
// Common schema patterns // Common schema patterns have been moved to __tests__/test-utils/schemas.ts
export const CommonSchemas = { // since they are only used for testing purposes.
// User schema with friends references
User: () => SchemaBuilder
.create('user')
.name('User')
.description('A user entity with profile information')
.property('name', PrimitiveSchemas.requiredString())
.property('email', PrimitiveSchemas.string())
.property('age', PrimitiveSchemas.number())
.property('active', PrimitiveSchemas.boolean())
.property('friends', ArraySchemas.of(ReferenceSchemas.to('user-summary', 2)))
.required('name')
.build(),
// User summary schema for references to prevent infinite recursion
UserSummary: () => SchemaBuilder
.create('user-summary')
.name('User Summary')
.description('Abbreviated user information for references')
.property('name', PrimitiveSchemas.requiredString())
.property('email', PrimitiveSchemas.string())
.required('name')
.additionalProperties(false)
.build(),
// Document schema
Document: () => SchemaBuilder
.create('document')
.name('Document')
.description('A document with metadata')
.property('title', PrimitiveSchemas.requiredString())
.property('content', PrimitiveSchemas.string())
.property('author', ReferenceSchemas.required('user-summary'))
.property('tags', ArraySchemas.of(PrimitiveSchemas.string()))
.property('created', PrimitiveSchemas.requiredNumber())
.property('published', PrimitiveSchemas.boolean())
.required('title', 'author', 'created')
.build()
} as const;
/** /**
* Context for tracking resolution state during nested object resolution * Context for tracking resolution state during nested object resolution

59
src/test-utils/schemas.ts Normal file
View File

@ -0,0 +1,59 @@
import { SchemaBuilder } from '../../src/schema';
/**
* Common schemas used for testing purposes.
* These schemas are not part of the main application code
* and are only used in test files.
*/
export const CommonSchemas = {
// User schema with friends references
User: () => SchemaBuilder
.create('user')
.name('User')
.description('A user entity with profile information')
.property('name', { type: 'primitive', primitiveType: 'string', required: true })
.property('email', { type: 'primitive', primitiveType: 'string' })
.property('age', { type: 'primitive', primitiveType: 'number' })
.property('active', { type: 'primitive', primitiveType: 'boolean' })
.property('friends', {
type: 'array',
itemSchema: {
type: 'reference',
targetSchema: 'user-summary',
maxDepth: 2
}
})
.required('name')
.build(),
// User summary schema for references to prevent infinite recursion
UserSummary: () => SchemaBuilder
.create('user-summary')
.name('User Summary')
.description('Abbreviated user information for references')
.property('name', { type: 'primitive', primitiveType: 'string', required: true })
.property('email', { type: 'primitive', primitiveType: 'string' })
.build(),
// Document schema
Document: () => SchemaBuilder
.create('document')
.name('Document')
.description('A document with title, content, and author')
.property('title', { type: 'primitive', primitiveType: 'string', required: true })
.property('content', { type: 'primitive', primitiveType: 'string' })
.property('author', {
type: 'reference',
targetSchema: 'user-summary',
maxDepth: 1,
required: true
})
.property('tags', {
type: 'array',
itemSchema: { type: 'primitive', primitiveType: 'string' }
})
.property('created', { type: 'primitive', primitiveType: 'number', required: true })
.property('published', { type: 'primitive', primitiveType: 'boolean' })
.required('title', 'author', 'created')
.build()
} as const;