Skip to content

Commit

Permalink
fix: mockdata generation now works as intended (#141)
Browse files Browse the repository at this point in the history
* fix: mockdata generation now works as intended

* fix: test coverage
  • Loading branch information
nlunets authored Jun 8, 2022
1 parent 4faf8c8 commit 8df5691
Show file tree
Hide file tree
Showing 9 changed files with 463 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-foxes-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-ux/fe-mockserver-core': patch
'@sap-ux/ui5-middleware-fe-mockserver': patch
---

Fix mock data generation and reorganize some part of the code
2 changes: 2 additions & 0 deletions packages/fe-mockserver-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface BaseServerConfig {
watch?: boolean;
noETag?: boolean;
contextBasedIsolation?: boolean;
generateMockData?: boolean;
fileLoader?: string;
/** Name of the package to use for the metadata provider **/
metadataProcessor?: {
Expand Down Expand Up @@ -91,6 +92,7 @@ export type ServiceConfigEx = ServiceConfig & {
export interface MockserverConfiguration {
debug?: boolean;
contextBasedIsolation?: boolean;
generateMockData?: boolean;
watch?: boolean;
strictKeyMode?: boolean;
annotations?: AnnotationConfig[];
Expand Down
3 changes: 3 additions & 0 deletions packages/fe-mockserver-core/src/data/dataAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class DataAccess implements DataAccessInterface {
this.generateMockData = !!service.generateMockData;
this.contextBasedIsolation = !!service.contextBasedIsolation;
this.fileLoader = fileLoader;
if (this.generateMockData && this.debug) {
this.log.info('Missing mockdata will be generated');
}
this.initializeMockData();
}

Expand Down
8 changes: 6 additions & 2 deletions packages/fe-mockserver-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { MockserverConfiguration } from './api';
import Router from 'router';
import type { IRouter } from 'router';
import { createMockMiddleware } from './middleware';
import * as path from 'path';

export interface IFileLoader {
loadFile(filePath: string): Promise<string>;
Expand All @@ -26,11 +27,14 @@ export default class FEMockserver {
}

private async initialize() {
const FileLoaderClass = (await import(this.configuration.fileLoader || './plugins/fileSystemLoader')).default;
const FileLoaderClass =
(this.configuration.fileLoader as any) || (await import('./plugins/fileSystemLoader')).default;
this.fileLoader = new FileLoaderClass() as IFileLoader;

const MetadataProviderClass = (
await import(this.configuration.metadataProcessor?.name || './plugins/metadataProvider')
await this.fileLoader.loadJS(
this.configuration.metadataProcessor?.name || path.resolve(__dirname, './plugins/metadataProvider')
)
).default;
this.metadataProvider = new MetadataProviderClass(
this.fileLoader,
Expand Down

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions packages/fe-mockserver-plugin-cds/test/cds/valid-withmorecommon.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using {
cuid,
managed
} from '@sap/cds/common';

service Service {
@odata.draft.enabled
entity RootEntity {
key ID : Integer @title : 'ID';
StringProperty : String @title : 'String';
IntegerProperty : Integer @title : 'Integer';
NumberProperty : Decimal(4, 2) @title : 'Number';
BooleanProperty : Boolean @title : 'Boolean';
DateProperty : Date @title : 'Date';
TimeProperty : Time @title : 'Time';
PropertyWithUnit : Integer64 @title : 'With Unit' @Measures.Unit : Unit;
PropertyWithCurrency : Integer64 @title : 'With Currency' @Measures.ISOCurrency : Currency;
Unit : String @title : 'UoM';
Currency : String @title : 'Currency';
TextProperty : String @title : 'Text';
TextArrangementTextOnlyProperty : String @title : 'Text Only';
TextArrangementTextLastProperty : String @title : 'Text Last';
TextArrangementTextFirstProperty : String @title : 'Text First';
TextArrangementTextSeparateProperty : String @title : 'TextSeparate';
PropertyWithValueHelp : String @title : 'With Value Help';
}

entity RootEntity2 {
key ID : Integer @title : 'ID';
StringProperty : String @title : 'String';
}

@cds.autoexpose
entity ValueHelpEntity {
key KeyProp : String(1) @title : 'Value Help Key';
@Core.Immutable
Description : String(20) @title : 'Value Help Description';
}

@cds.autoexpose
entity ValueHelpCurrencyEntity {
key Currency : String(3) @title : 'Currency Key';
@Core.Immutable
Description : String(20) @title : 'Currency Name';
}

annotate RootEntity2 with @(UI : {LineItem : [
{Value : ID},
{Value : StringProperty}
]});

annotate RootEntity with @(UI : {LineItem : [
{Value : ID},
{Value : BooleanProperty},
{Value : TextArrangementTextFirstProperty},
{Value : PropertyWithValueHelp},
{Value : PropertyWithCurrency}
]}) {
TextArrangementTextOnlyProperty @Common : {
Text : TextProperty,
TextArrangement : #TextOnly
};
TextArrangementTextLastProperty @Common : {
Text : TextProperty,
TextArrangement : #TextLast
};
TextArrangementTextFirstProperty @Common : {
Text : TextProperty,
TextArrangement : #TextFirst
};
TextArrangementTextSeparateProperty @Common : {
Text : TextProperty,
TextArrangement : #TextSeparate
};
PropertyWithValueHelp @(Common : {ValueList : {
Label : 'Value with Value Help',
CollectionPath : 'ValueHelpEntity',
Parameters : [
{
$Type : 'Common.ValueListParameterInOut',
LocalDataProperty : PropertyWithValueHelp,
ValueListProperty : 'KeyProp'
},
{
$Type : 'Common.ValueListParameterDisplayOnly',
ValueListProperty : 'Description'
}
]
}});
Currency @(Common : {
ValueListWithFixedValues,
ValueList : {
Label : 'Currency Value Help',
CollectionPath : 'ValueHelpCurrencyEntity',
Parameters : [
{
$Type : 'Common.ValueListParameterInOut',
LocalDataProperty : Currency,
ValueListProperty : 'Currency'
},
{
$Type : 'Common.ValueListParameterDisplayOnly',
ValueListProperty : 'Description'
}
]
}
});
};

annotate ValueHelpEntity with {
KeyProp @(Common : {
Text : Description,
TextArrangement : #TextFirst
},

);
};
}
4 changes: 4 additions & 0 deletions packages/fe-mockserver-plugin-cds/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('FE Mockserver CDS Plugin', () => {
const edmx = await myCDSProvider.loadMetadata(path.join(cdsDataPath, 'valid-withCommon.cds'));
expect(edmx).toMatchSnapshot();
});
it('can compile valid CDS file using more common stuff', async () => {
const edmx = await myCDSProvider.loadMetadata(path.join(cdsDataPath, 'valid-withmorecommon.cds'));
expect(edmx).toMatchSnapshot();
});
it('can also load XML files', async () => {
const edmx = await myCDSProvider.loadMetadata(path.join(xmlDataPath, 'valid.xml'));
expect(edmx).toMatchSnapshot();
Expand Down
5 changes: 5 additions & 0 deletions packages/ui5-middleware-fe-mockserver/src/configResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function processServicesConfig(
noETag: inService.noETag,
debug: inService.debug,
strictKeyMode: inService.strictKeyMode,
generateMockData: inService.generateMockData,
contextBasedIsolation: inService.contextBasedIsolation
} as any;
const metadataPath = inService.metadataPath || inService.metadataXmlPath || inService.metadataCdsPath;
Expand Down Expand Up @@ -109,6 +110,9 @@ function processServicesConfig(
if (inConfig.contextBasedIsolation && !inService.hasOwnProperty('contextBasedIsolation')) {
myServiceConfig.contextBasedIsolation = inConfig.contextBasedIsolation;
}
if (inConfig.generateMockData && !inService.hasOwnProperty('generateMockData')) {
myServiceConfig.generateMockData = inConfig.generateMockData;
}

return myServiceConfig;
});
Expand Down Expand Up @@ -138,6 +142,7 @@ export function resolveConfig(inConfig: ServerConfig, basePath: string): Mockser
watch: !!inConfig.watch,
strictKeyMode: !!inConfig.strictKeyMode,
debug: !!inConfig.debug,
generateMockData: !!inConfig.generateMockData,
annotations: annotations,
services: services,
fileLoader: inConfig.fileLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ describe('The config resolver', () => {
debug: false,
strictKeyMode: false,
contextBasedIsolation: false,
noETag: false
noETag: false,
generateMockData: false
}
],
watch: true,
debug: true,
strictKeyMode: true,
contextBasedIsolation: true,
noETag: true
noETag: true,
generateMockData: true
},
'/'
);
Expand All @@ -172,5 +174,7 @@ describe('The config resolver', () => {
expect(myBaseResolvedConfig.services[1].contextBasedIsolation).toBe(false);
expect(myBaseResolvedConfig.services[0].noETag).toBe(true);
expect(myBaseResolvedConfig.services[1].noETag).toBe(false);
expect(myBaseResolvedConfig.services[0].generateMockData).toBe(true);
expect(myBaseResolvedConfig.services[1].generateMockData).toBe(false);
});
});

0 comments on commit 8df5691

Please sign in to comment.