diff --git a/Meadowlark-js/packages/meadowlark-core/src/extraction/DocumentReferenceExtractor.ts b/Meadowlark-js/packages/meadowlark-core/src/extraction/DocumentReferenceExtractor.ts index 50bf393b..5c661910 100644 --- a/Meadowlark-js/packages/meadowlark-core/src/extraction/DocumentReferenceExtractor.ts +++ b/Meadowlark-js/packages/meadowlark-core/src/extraction/DocumentReferenceExtractor.ts @@ -2,13 +2,54 @@ // Licensed to the Ed-Fi Alliance under one or more agreements. // The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. // See the LICENSE and NOTICES files in the project root for more information. +import invariant from 'ts-invariant'; import { JSONPath as jsonPath } from 'jsonpath-plus'; -import { DocumentIdentity } from '../model/DocumentIdentity'; import { DocumentReference } from '../model/DocumentReference'; import { DocumentObjectKey } from '../model/api-schema/DocumentObjectKey'; import { DocumentPaths } from '../model/api-schema/DocumentPaths'; -import { JsonPath } from '../model/api-schema/JsonPath'; import { ResourceSchema } from '../model/api-schema/ResourceSchema'; +import { DocumentIdentity } from '../model/DocumentIdentity'; + +/** + * In extracting DocumentReferences, there is an intermediate step where document values are resolved + * from a JsonPath. JsonPaths return arrays of values when the path goes into an array. + * This is the case for collections of document references. + * + * This means that each path resolves to one document value in *each* document reference in the collection. + * For each DocumentObjectKey of a reference, IntermediateDocumentReferences holds the array of resolved document values + * for a path. + * + * For example, given a document with a collection of ClassPeriod references: + * + * classPeriods: [ + * { + * classPeriodReference: { + * schoolId: '24', + * classPeriodName: 'z1', + * }, + * }, + * { + * classPeriodReference: { + * schoolId: '25', + * classPeriodName: 'z2', + * }, + * }, + * ] + * + * With JsonPaths for ClassPeriod references: + * "* $.classPeriods[*].classPeriodReference.schoolId" for schoolId and + * "$.classPeriods[*].classPeriodReference.classPeriodName" for classPeriodName, + * the IntermediateDocumentReferences would be: + * + * { + * schoolId: ['24', '25'], + * classPeriodName: ['z1', 'z2'] + * } + * + * IntermediateDocumentReferences here contains information for two DocumentReferences, but as "slices" in the wrong + * orientation. + */ +type IntermediateDocumentReferences = { [key: DocumentObjectKey]: any[] }; /** * Takes a resource schema and an API document for that resource and @@ -24,24 +65,55 @@ export function extractDocumentReferences(resourceSchema: ResourceSchema, docume // Only applies to non-descriptor references if (documentPaths.isDescriptor) return; - const documentIdentity: DocumentIdentity = []; - // Build up documentIdentity in order - documentPaths.pathOrder.forEach((documentKey: DocumentObjectKey) => { - const documentJsonPath: JsonPath = documentPaths.paths[documentKey]; - const documentValue: any = jsonPath({ + // Build up intermediateDocumentReferences + const intermediateDocumentReferences: IntermediateDocumentReferences = {}; + Object.entries(documentPaths.paths).forEach(([documentKey, documentJsonPath]) => { + const documentValuesSlice: any[] = jsonPath({ path: documentJsonPath, json: documentBody, flatten: true, }); - documentIdentity.push({ documentKey, documentValue }); - }); - result.push({ - documentIdentity, - isDescriptor: documentPaths.isDescriptor, - projectName: documentPaths.projectName, - resourceName: documentPaths.resourceName, + invariant( + Array.isArray(documentValuesSlice), + `JsonPath ${documentJsonPath} should have returned an array but instead was ${documentValuesSlice}`, + ); + + // Path can be empty if reference is optional + if (documentValuesSlice.length === 0) return; + + intermediateDocumentReferences[documentKey] = documentValuesSlice; }); + + const documentValuesSlices = Object.values(intermediateDocumentReferences); + + // Empty if reference is optional and no values + if (documentValuesSlices.length === 0) return; + + // Number of document values from resolved JsonPaths should all be the same, otherwise something is very wrong + invariant( + documentValuesSlices.every( + (documentValuesSlice) => documentValuesSlice.length === documentValuesSlices[0].length, + `Length of document value slices are not equal`, + ), + ); + + // Reorient intermediateDocumentReferences into actual references + for (let index = 0; index < documentValuesSlices[0].length; index += 1) { + const documentIdentity: DocumentIdentity = []; + + // Build the document identity in the correct path order + documentPaths.pathOrder.forEach((documentKey: DocumentObjectKey) => { + documentIdentity.push({ documentKey, documentValue: intermediateDocumentReferences[documentKey][index] }); + }); + + result.push({ + documentIdentity, + isDescriptor: documentPaths.isDescriptor, + projectName: documentPaths.projectName, + resourceName: documentPaths.resourceName, + }); + } }); return result; diff --git a/Meadowlark-js/packages/meadowlark-core/test/extraction/DocumentReferenceExtractor.test.ts b/Meadowlark-js/packages/meadowlark-core/test/extraction/DocumentReferenceExtractor.test.ts index 35c3b497..3f82af0c 100644 --- a/Meadowlark-js/packages/meadowlark-core/test/extraction/DocumentReferenceExtractor.test.ts +++ b/Meadowlark-js/packages/meadowlark-core/test/extraction/DocumentReferenceExtractor.test.ts @@ -11,146 +11,142 @@ import { DomainEntityBuilder, MetaEdTextBuilder, NamespaceBuilder, + EnumerationBuilder, + DescriptorBuilder, } from '@edfi/metaed-core'; -import { domainEntityReferenceEnhancer } from '@edfi/metaed-plugin-edfi-unified'; import { - entityPropertyApiSchemaDataSetupEnhancer, - apiEntityMappingEnhancer, - entityApiSchemaDataSetupEnhancer, - referenceComponentEnhancer, - apiPropertyMappingEnhancer, - propertyCollectingEnhancer, -} from '@edfi/metaed-plugin-edfi-api-schema'; + descriptorReferenceEnhancer, + domainEntityReferenceEnhancer, + enumerationReferenceEnhancer, +} from '@edfi/metaed-plugin-edfi-unified'; import { extractDocumentReferences } from '../../src/extraction/DocumentReferenceExtractor'; import { DocumentReference } from '../../src/model/DocumentReference'; import { apiSchemaFrom } from '../TestHelper'; import { ApiSchema } from '../../src/model/api-schema/ApiSchema'; import { ResourceSchema } from '../../src/model/api-schema/ResourceSchema'; -describe('when extracting document references from domain entity referencing one as identity and another as collection', () => { - const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); - let result: DocumentReference[] = []; +// describe('when extracting document references from domain entity referencing one as identity and another as collection', () => { +// const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); +// let result: DocumentReference[] = []; - const body = { - notImportant: false, - sectionIdentifier: 'Bob', - courseOfferingReference: { - localCourseCode: 'abc', - schoolId: '23', - }, - classPeriods: [ - { - classPeriodReference: { - schoolId: '24', - classPeriodName: 'z1', - }, - }, - { - classPeriodReference: { - schoolId: '25', - classPeriodName: 'z2', - }, - }, - ], - }; +// const body = { +// notImportant: false, +// sectionIdentifier: 'Bob', +// courseOfferingReference: { +// localCourseCode: 'abc', +// schoolId: '23', +// }, +// classPeriods: [ +// { +// classPeriodReference: { +// schoolId: '24', +// classPeriodName: 'z1', +// }, +// }, +// { +// classPeriodReference: { +// schoolId: '25', +// classPeriodName: 'z2', +// }, +// }, +// ], +// }; - beforeAll(() => { - MetaEdTextBuilder.build() - .withBeginNamespace('EdFi') - .withStartDomainEntity('Section') - .withDocumentation('doc') - .withStringIdentity('SectionIdentifier', 'doc', '30') - .withDomainEntityIdentity('CourseOffering', 'doc') - .withDomainEntityProperty('ClassPeriod', 'doc', true, true) - .withBooleanProperty('NotImportant', 'doc', true, false) - .withEndDomainEntity() +// beforeAll(() => { +// MetaEdTextBuilder.build() +// .withBeginNamespace('EdFi') +// .withStartDomainEntity('Section') +// .withDocumentation('doc') +// .withStringIdentity('SectionIdentifier', 'doc', '30') +// .withDomainEntityIdentity('CourseOffering', 'doc') +// .withDomainEntityProperty('ClassPeriod', 'doc', true, true) +// .withBooleanProperty('NotImportant', 'doc', true, false) +// .withEndDomainEntity() - .withStartDomainEntity('CourseOffering') - .withDocumentation('doc') - .withStringIdentity('LocalCourseCode', 'doc', '30') - .withDomainEntityIdentity('School', 'doc') - .withBooleanProperty('AlsoNotImportant', 'doc', true, true) - .withEndDomainEntity() +// .withStartDomainEntity('CourseOffering') +// .withDocumentation('doc') +// .withStringIdentity('LocalCourseCode', 'doc', '30') +// .withDomainEntityIdentity('School', 'doc') +// .withBooleanProperty('AlsoNotImportant', 'doc', true, true) +// .withEndDomainEntity() - .withStartDomainEntity('ClassPeriod') - .withDocumentation('doc') - .withStringIdentity('ClassPeriodName', 'doc', '30') - .withDomainEntityIdentity('School', 'doc') - .withBooleanProperty('AndAlsoNotImportant', 'doc', true, true) - .withEndDomainEntity() +// .withStartDomainEntity('ClassPeriod') +// .withDocumentation('doc') +// .withStringIdentity('ClassPeriodName', 'doc', '30') +// .withDomainEntityIdentity('School', 'doc') +// .withBooleanProperty('AndAlsoNotImportant', 'doc', true, true) +// .withEndDomainEntity() - .withStartDomainEntity('School') - .withDocumentation('doc') - .withStringIdentity('SchoolId', 'doc', '30') - .withEndDomainEntity() - .withEndNamespace() - .sendToListener(new NamespaceBuilder(metaEd, [])) - .sendToListener(new DomainEntityBuilder(metaEd, [])); +// .withStartDomainEntity('School') +// .withDocumentation('doc') +// .withStringIdentity('SchoolId', 'doc', '30') +// .withEndDomainEntity() +// .withEndNamespace() +// .sendToListener(new NamespaceBuilder(metaEd, [])) +// .sendToListener(new DomainEntityBuilder(metaEd, [])); - domainEntityReferenceEnhancer(metaEd); - entityPropertyApiSchemaDataSetupEnhancer(metaEd); - entityApiSchemaDataSetupEnhancer(metaEd); - referenceComponentEnhancer(metaEd); - apiPropertyMappingEnhancer(metaEd); - propertyCollectingEnhancer(metaEd); - apiEntityMappingEnhancer(metaEd); +// domainEntityReferenceEnhancer(metaEd); - const apiSchema: ApiSchema = apiSchemaFrom(metaEd); - const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; - result = extractDocumentReferences(resourceSchema, body); - }); +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); +// }); - it('should have references', () => { - expect(result).toMatchInlineSnapshot(` - [ - { - "documentIdentity": [ - { - "documentKey": "localCourseCode", - "documentValue": [ - "abc", - ], - }, - { - "documentKey": "schoolId", - "documentValue": [ - "23", - ], - }, - ], - "isDescriptor": false, - "projectName": "EdFi", - "resourceName": "CourseOffering", - }, - { - "documentIdentity": [ - { - "documentKey": "classPeriodName", - "documentValue": [ - "z1", - "z2", - ], - }, - { - "documentKey": "schoolId", - "documentValue": [ - "24", - "25", - ], - }, - ], - "isDescriptor": false, - "projectName": "EdFi", - "resourceName": "ClassPeriod", - }, - ] - `); - }); -}); +// it('should have references', () => { +// expect(result).toMatchInlineSnapshot(` +// [ +// { +// "documentIdentity": [ +// { +// "documentKey": "localCourseCode", +// "documentValue": "abc", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "23", +// }, +// ], +// "isDescriptor": false, +// "projectName": "EdFi", +// "resourceName": "CourseOffering", +// }, +// { +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "z1", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "24", +// }, +// ], +// "isDescriptor": false, +// "projectName": "EdFi", +// "resourceName": "ClassPeriod", +// }, +// { +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "z2", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "25", +// }, +// ], +// "isDescriptor": false, +// "projectName": "EdFi", +// "resourceName": "ClassPeriod", +// }, +// ] +// `); +// }); +// }); // describe('when extracting with optional reference in body', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -180,25 +176,22 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have references', () => { // expect(result).toMatchInlineSnapshot(` // [ // { -// "documentIdentity": { -// "localCourseCode": "abc", -// }, +// "documentIdentity": [ +// { +// "documentKey": "localCourseCode", +// "documentValue": "abc", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "CourseOffering", @@ -210,7 +203,6 @@ describe('when extracting document references from domain entity referencing one // describe('when extracting with optional reference not in body', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -237,16 +229,10 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have no references', () => { @@ -254,9 +240,71 @@ describe('when extracting document references from domain entity referencing one // }); // }); +// describe('when extracting with one optional reference in body and one not', () => { +// const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); +// let result: DocumentReference[] = []; + +// const body = { +// sectionIdentifier: 'Bob', +// courseOfferingReference: { +// localCourseCode: 'abc', +// }, +// }; + +// beforeAll(() => { +// MetaEdTextBuilder.build() +// .withBeginNamespace('EdFi') +// .withStartDomainEntity('Section') +// .withDocumentation('doc') +// .withStringIdentity('SectionIdentifier', 'doc', '30') +// .withDomainEntityProperty('CourseOffering', 'doc', false, false) +// .withDomainEntityProperty('Course', 'doc', false, false) +// .withEndDomainEntity() + +// .withStartDomainEntity('CourseOffering') +// .withDocumentation('doc') +// .withStringIdentity('LocalCourseCode', 'doc', '30') +// .withBooleanProperty('AlsoNotImportant', 'doc', true, true) +// .withEndDomainEntity() + +// .withStartDomainEntity('Course') +// .withDocumentation('doc') +// .withStringIdentity('LocalCourse', 'doc', '30') +// .withBooleanProperty('AlsoNotImportant', 'doc', true, true) +// .withEndDomainEntity() + +// .withEndNamespace() +// .sendToListener(new NamespaceBuilder(metaEd, [])) +// .sendToListener(new DomainEntityBuilder(metaEd, [])); + +// domainEntityReferenceEnhancer(metaEd); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); +// }); + +// it('should have no references', () => { +// expect(result).toMatchInlineSnapshot(` +// [ +// { +// "documentIdentity": [ +// { +// "documentKey": "localCourseCode", +// "documentValue": "abc", +// }, +// ], +// "isDescriptor": false, +// "projectName": "EdFi", +// "resourceName": "CourseOffering", +// }, +// ] +// `); +// }); +// }); + // describe('when extracting optional collection in body', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -294,33 +342,33 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have references', () => { // expect(result).toMatchInlineSnapshot(` // [ // { -// "documentIdentity": { -// "classPeriodName": "z1", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "z1", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", // }, // { -// "documentIdentity": { -// "classPeriodName": "z2", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "z2", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", @@ -332,7 +380,6 @@ describe('when extracting document references from domain entity referencing one // describe('when extracting optional collection not in body', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -358,16 +405,10 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have no references', () => { @@ -377,7 +418,6 @@ describe('when extracting document references from domain entity referencing one // describe('when extracting document references with two levels of identities on a collection reference', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -436,39 +476,49 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have two references down to "schoolId"', () => { // expect(result).toMatchInlineSnapshot(` // [ // { -// "documentIdentity": { -// "classPeriodName": "c1", -// "schoolReference.schoolId": "24", -// "sessionReference.schoolId": "24", -// "sessionReference.sessionName": "s1", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "c1", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "24", +// }, +// { +// "documentKey": "sessionName", +// "documentValue": "s1", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", // }, // { -// "documentIdentity": { -// "classPeriodName": "c2", -// "schoolReference.schoolId": "25", -// "sessionReference.schoolId": "25", -// "sessionReference.sessionName": "s2", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "c2", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "25", +// }, +// { +// "documentKey": "sessionName", +// "documentValue": "s2", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", @@ -480,7 +530,6 @@ describe('when extracting document references from domain entity referencing one // describe('when extracting document references with three levels of identities on a collection reference', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -550,41 +599,65 @@ describe('when extracting document references from domain entity referencing one // .sendToListener(new DomainEntityBuilder(metaEd, [])); // domainEntityReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const section = namespace.entity.domainEntity.get('Section'); -// result = extractDocumentReferences(section, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['sections']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have ClassPeriod references down to "thirdLevelName"', () => { // expect(result).toMatchInlineSnapshot(` // [ // { -// "documentIdentity": { -// "classPeriodName": "c1", -// "schoolReference.schoolId": "24", -// "sessionReference.secondLevelName": "e1", -// "sessionReference.sessionName": "s1", -// "sessionReference.thirdLevelName": "t1", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "c1", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "24", +// }, +// { +// "documentKey": "secondLevelName", +// "documentValue": "e1", +// }, +// { +// "documentKey": "sessionName", +// "documentValue": "s1", +// }, +// { +// "documentKey": "thirdLevelName", +// "documentValue": "t1", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", // }, // { -// "documentIdentity": { -// "classPeriodName": "c2", -// "schoolReference.schoolId": "25", -// "sessionReference.secondLevelName": "e2", -// "sessionReference.sessionName": "s2", -// "sessionReference.thirdLevelName": "t2", -// }, +// "documentIdentity": [ +// { +// "documentKey": "classPeriodName", +// "documentValue": "c2", +// }, +// { +// "documentKey": "schoolId", +// "documentValue": "25", +// }, +// { +// "documentKey": "secondLevelName", +// "documentValue": "e2", +// }, +// { +// "documentKey": "sessionName", +// "documentValue": "s2", +// }, +// { +// "documentKey": "thirdLevelName", +// "documentValue": "t2", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "ClassPeriod", @@ -596,7 +669,6 @@ describe('when extracting document references from domain entity referencing one // describe('when extracting with school year reference in body', () => { // const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; // let result: DocumentReference[] = []; // const body = { @@ -636,26 +708,26 @@ describe('when extracting document references from domain entity referencing one // domainEntityReferenceEnhancer(metaEd); // enumerationReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const courseOffering = namespace.entity.domainEntity.get('CourseOffering'); -// result = extractDocumentReferences(courseOffering, body); + +// const apiSchema: ApiSchema = apiSchemaFrom(metaEd); +// const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['courseOfferings']; +// result = extractDocumentReferences(resourceSchema, body); // }); // it('should have references', () => { // expect(result).toMatchInlineSnapshot(` // [ // { -// "documentIdentity": { -// "schoolYearTypeReference.schoolYear": 2022, -// "sessionName": "def", -// }, +// "documentIdentity": [ +// { +// "documentKey": "schoolYear", +// "documentValue": 2022, +// }, +// { +// "documentKey": "sessionName", +// "documentValue": "def", +// }, +// ], // "isDescriptor": false, // "projectName": "EdFi", // "resourceName": "Session", @@ -665,104 +737,133 @@ describe('when extracting document references from domain entity referencing one // }); // }); -// describe('when extracting with school year in a reference document', () => { -// const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); -// let namespace: any = null; -// let result: DocumentReference[] = []; +describe('when extracting with school year in a reference document', () => { + const metaEd: MetaEdEnvironment = newMetaEdEnvironment(); + let result: DocumentReference[] = []; -// const body = { -// studentReference: { -// studentUniqueId: 's0zf6d1123d3e', -// }, -// schoolReference: { -// schoolId: 123, -// }, -// entryDate: '2020-01-01', -// entryGradeLevelDescriptor: 'uri://ed-fi.org/GradeLevelDescriptor#10', -// graduationPlanReference: { -// educationOrganizationId: 123, -// graduationPlanTypeDescriptor: 'uri://ed-fi.org/GraduationPlanTypeDescriptor#Minimum', -// graduationSchoolYear: 2024, -// }, -// }; + const body = { + studentReference: { + studentUniqueId: 's0zf6d1123d3e', + }, + schoolReference: { + schoolId: 123, + }, + entryDate: '2020-01-01', + entryGradeLevelDescriptor: 'uri://ed-fi.org/GradeLevelDescriptor#10', + graduationPlanReference: { + educationOrganizationId: 123, + graduationPlanTypeDescriptor: 'uri://ed-fi.org/GraduationPlanTypeDescriptor#Minimum', + graduationSchoolYear: 2024, + }, + }; -// beforeAll(() => { -// MetaEdTextBuilder.build() -// .withBeginNamespace('EdFi') -// .withStartDomainEntity('StudentSchoolAssociation') -// .withDocumentation('doc') -// .withDomainEntityIdentity('Student', 'doc') -// .withDomainEntityIdentity('School', 'doc') -// .withDateIdentity('EntryDate', 'doc') -// .withDescriptorIdentity('EntryGradeLevelDescriptor', 'doc') -// .withDomainEntityProperty('GraduationPlan', 'doc', false, false) -// .withEndDomainEntity() + beforeAll(() => { + MetaEdTextBuilder.build() + .withBeginNamespace('EdFi') -// .withStartDomainEntity('School') -// .withDocumentation('doc') -// .withStringIdentity('SchoolId', 'doc', '30') -// .withEndDomainEntity() + .withStartDomainEntity('StudentSchoolAssociation') + .withDocumentation('doc') + .withDomainEntityIdentity('Student', 'doc') + .withDomainEntityIdentity('School', 'doc') + .withDateIdentity('EntryDate', 'doc') + .withDescriptorIdentity('EntryGradeLevelDescriptor', 'doc') + .withDomainEntityProperty('GraduationPlan', 'doc', false, false) + .withEndDomainEntity() -// .withStartDomainEntity('Student') -// .withDocumentation('doc') -// .withStringIdentity('StudentUniqueId', 'doc', '60') -// .withEndDomainEntity() + .withStartDomainEntity('EducationOrganization') + .withDocumentation('doc') + .withStringIdentity('EducationOrganizationId', 'doc', '30') + .withEndDomainEntity() -// .withStartDomainEntity('GraduationPlan') -// .withDocumentation('doc') -// .withDescriptorIdentity('GraduationPlanType', 'doc') -// .withDomainEntityIdentity('EducationOrganization', 'doc') -// .withEnumerationIdentity('SchoolYear', 'doc', 'Graduation') -// .withEndDomainEntity() + .withStartDomainEntity('School') + .withDocumentation('doc') + .withStringIdentity('SchoolId', 'doc', '30') + .withEndDomainEntity() -// .withEndNamespace() -// .sendToListener(new NamespaceBuilder(metaEd, [])) -// .sendToListener(new EnumerationBuilder(metaEd, [])) -// .sendToListener(new DomainEntityBuilder(metaEd, [])); + .withStartDomainEntity('Student') + .withDocumentation('doc') + .withStringIdentity('StudentUniqueId', 'doc', '60') + .withEndDomainEntity() -// domainEntityReferenceEnhancer(metaEd); -// enumerationReferenceEnhancer(metaEd); -// entityPropertyApiSchemaDataSetupEnhancer(metaEd); -// entityApiSchemaDataSetupEnhancer(metaEd); -// referenceComponentEnhancer(metaEd); -// apiPropertyMappingEnhancer(metaEd); -// propertyCollectingEnhancer(metaEd); -// apiEntityMappingEnhancer(metaEd); - -// namespace = metaEd.namespace.get('EdFi'); -// const studentSchoolAssociation = namespace.entity.domainEntity.get('StudentSchoolAssociation'); -// result = extractDocumentReferences(studentSchoolAssociation, body); -// }); + .withStartDomainEntity('GraduationPlan') + .withDocumentation('doc') + .withDescriptorIdentity('GraduationPlanType', 'doc') + .withDomainEntityIdentity('EducationOrganization', 'doc') + .withEnumerationIdentity('SchoolYear', 'doc', 'Graduation') + .withEndDomainEntity() -// it('should have references and schoolYear reference should respect the role name', () => { -// expect(result).toMatchInlineSnapshot(` -// [ -// { -// "documentIdentity": { -// "graduationPlanTypeDescriptor": "uri://ed-fi.org/GraduationPlanTypeDescriptor#Minimum", -// "graduationSchoolYearTypeReference.schoolYear": 2024, -// }, -// "isDescriptor": false, -// "projectName": "EdFi", -// "resourceName": "GraduationPlan", -// }, -// { -// "documentIdentity": { -// "schoolId": 123, -// }, -// "isDescriptor": false, -// "projectName": "EdFi", -// "resourceName": "School", -// }, -// { -// "documentIdentity": { -// "studentUniqueId": "s0zf6d1123d3e", -// }, -// "isDescriptor": false, -// "projectName": "EdFi", -// "resourceName": "Student", -// }, -// ] -// `); -// }); -// }); + .withStartEnumeration('SchoolYear') + .withDocumentation('doc') + .withEndEnumeration() + + .withStartDescriptor('EntryGradeLevelDescriptor') + .withDocumentation('doc') + .withEndDescriptor() + + .withStartDescriptor('GraduationPlanType') + .withDocumentation('doc') + .withEndDescriptor() + + .withEndNamespace() + .sendToListener(new NamespaceBuilder(metaEd, [])) + .sendToListener(new DescriptorBuilder(metaEd, [])) + .sendToListener(new EnumerationBuilder(metaEd, [])) + .sendToListener(new DomainEntityBuilder(metaEd, [])); + + domainEntityReferenceEnhancer(metaEd); + enumerationReferenceEnhancer(metaEd); + descriptorReferenceEnhancer(metaEd); + + const apiSchema: ApiSchema = apiSchemaFrom(metaEd); + const resourceSchema: ResourceSchema = apiSchema.projectSchemas['edfi'].resourceSchemas['studentSchoolAssociations']; + result = extractDocumentReferences(resourceSchema, body); + }); + + it('should have references and schoolYear reference should respect the role name', () => { + expect(result).toMatchInlineSnapshot(` + [ + { + "documentIdentity": [ + { + "documentKey": "studentUniqueId", + "documentValue": "s0zf6d1123d3e", + }, + ], + "isDescriptor": false, + "projectName": "EdFi", + "resourceName": "Student", + }, + { + "documentIdentity": [ + { + "documentKey": "schoolId", + "documentValue": 123, + }, + ], + "isDescriptor": false, + "projectName": "EdFi", + "resourceName": "School", + }, + { + "documentIdentity": [ + { + "documentKey": "educationOrganizationId", + "documentValue": 123, + }, + { + "documentKey": "graduationPlanTypeDescriptor", + "documentValue": "uri://ed-fi.org/GraduationPlanTypeDescriptor#Minimum", + }, + { + "documentKey": "graduationSchoolYear", + "documentValue": 2024, + }, + ], + "isDescriptor": false, + "projectName": "EdFi", + "resourceName": "GraduationPlan", + }, + ] + `); + }); +});