Skip to content

Commit

Permalink
feat(custom-object): added custom-object and custom-object-draft models
Browse files Browse the repository at this point in the history
  • Loading branch information
kterry1 committed Nov 30, 2023
1 parent b1abc67 commit c06ba41
Show file tree
Hide file tree
Showing 18 changed files with 370 additions and 19 deletions.
21 changes: 21 additions & 0 deletions models/custom-object/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) commercetools GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions models/custom-object/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @commercetools-test-data/custom-object

This package provides the data model for the commercetools platform `CustomObject` representations

https://docs.commercetools.com/api/projects/custom-objects#representations

# Install

```bash
$ pnpm add -D @commercetools-test-data/custom-object
```

# Usage

```ts
import {
CustomObject,
CustomObjectDraft,
type TCustomObject,
type TCustomObjectDraft,
} from '@commercetools-test-data/custom-object';

const customObject = CustomObject.random().build<TCustomObject>();
const customObjectDraft =
CustomObjectDraft.random().build<TCustomObjectDraft>();
```
28 changes: 28 additions & 0 deletions models/custom-object/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@commercetools-test-data/custom-object",
"version": "6.7.0",
"description": "Data model for commercetools API CustomObject",
"bugs": "https://github.com/commercetools/test-data/issues",
"repository": {
"type": "git",
"url": "https://github.com/commercetools/test-data.git",
"directory": "models/custom-object"
},
"keywords": ["javascript", "typescript", "test-data"],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "dist/commercetools-test-data-custom-object.cjs.js",
"module": "dist/commercetools-test-data-custom-object.esm.js",
"files": ["dist", "package.json", "LICENSE", "README.md"],
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime-corejs3": "^7.17.9",
"@commercetools-test-data/commons": "6.6.0",
"@commercetools-test-data/core": "6.6.0",
"@commercetools-test-data/utils": "6.6.0",
"@commercetools/platform-sdk": "^6.0.0",
"@faker-js/faker": "^8.0.0"
}
}
74 changes: 74 additions & 0 deletions models/custom-object/src/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type { TCustomObject, TCustomObjectGraphql } from './types';
import * as CustomObject from './index';

describe('builder', () => {
it(
...createBuilderSpec<TCustomObject, TCustomObject>(
'default',
CustomObject.random(),
expect.objectContaining({
id: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);

it(
...createBuilderSpec<TCustomObject, TCustomObject>(
'rest',
CustomObject.random(),
expect.objectContaining({
id: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);

it(
...createBuilderSpec<TCustomObject, TCustomObjectGraphql>(
'graphql',
CustomObject.random(),
expect.objectContaining({
__typename: 'CustomObject',
id: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customerRef: expect.objectContaining({ typeId: 'customer' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customerRef: expect.objectContaining({ typeId: 'customer' }),
}),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);
});
12 changes: 12 additions & 0 deletions models/custom-object/src/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import { TCustomObject, TCreateCustomObjectBuilder } from './types';

Check failure on line 4 in models/custom-object/src/builder.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

'"./types"' has no exported member named 'TCreateCustomObjectBuilder'. Did you mean 'TCreateCustomObjectDraftBuilder'?

const Model: TCreateCustomObjectBuilder = () =>
Builder<TCustomObject>({
generator,
transformers,
});

export default Model;
Empty file.
46 changes: 46 additions & 0 deletions models/custom-object/src/custom-object-draft/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type { TCustomObjectDraft } from '../types';
import * as AssociateRoleDraft from './index';

describe('builder', () => {
it(
...createBuilderSpec<TCustomObjectDraft, TCustomObjectDraft>(
'default',
AssociateRoleDraft.random(),

Check failure on line 11 in models/custom-object/src/custom-object-draft/builder.spec.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

This expression is not callable.
expect.objectContaining({
version: expect.any(Number),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);

it(
...createBuilderSpec<TCustomObjectDraft, TCustomObjectDraft>(
'rest',
AssociateRoleDraft.random(),

Check failure on line 24 in models/custom-object/src/custom-object-draft/builder.spec.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

This expression is not callable.
expect.objectContaining({
version: expect.any(Number),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);

it(
...createBuilderSpec<TCustomObjectDraft, TCustomObjectDraft>(
'graphql',
AssociateRoleDraft.random(),

Check failure on line 37 in models/custom-object/src/custom-object-draft/builder.spec.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

This expression is not callable.
expect.objectContaining({
version: expect.any(Number),
key: expect.any(String),
container: expect.any(String),
value: expect.any(Object),
})
)
);
});
10 changes: 10 additions & 0 deletions models/custom-object/src/custom-object-draft/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Builder } from '@commercetools-test-data/core';
import { TCustomObjectDraft, TCustomObjectDraftBuilder } from '../types';
import generator from './generator';

const CustomObjectDraft: TCustomObjectDraftBuilder = () =>

Check failure on line 5 in models/custom-object/src/custom-object-draft/builder.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

Type '() => TBuilder<CustomObjectDraft>' is not assignable to type 'TCustomObjectDraftBuilder'.
Builder<TCustomObjectDraft>({
generator,
});

export default CustomObjectDraft;
17 changes: 17 additions & 0 deletions models/custom-object/src/custom-object-draft/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fake, Generator, sequence } from '@commercetools-test-data/core';
import { TCustomObjectDraft } from '../types';

const generator = Generator<TCustomObjectDraft>({
fields: {
version: sequence(),
key: fake((f) => f.string.alphanumeric(10)),
container: fake((f) => f.string.alphanumeric(10)),
value: {
[fake((f) => f.string.alpha(5))]: {

Check failure on line 10 in models/custom-object/src/custom-object-draft/generator.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
[fake((f) => f.string.alpha(6))]: fake((f) => f.string.alpha(7)),

Check failure on line 11 in models/custom-object/src/custom-object-draft/generator.ts

View workflow job for this annotation

GitHub Actions / build_lint_and_test

A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
},
},
},
});

export default generator;
2 changes: 2 additions & 0 deletions models/custom-object/src/custom-object-draft/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as random } from './builder';
export { default as presets } from './presets';
3 changes: 3 additions & 0 deletions models/custom-object/src/custom-object-draft/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
26 changes: 26 additions & 0 deletions models/custom-object/src/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ClientLogging } from '@commercetools-test-data/commons';
import { fake, Generator, sequence } from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import { TCustomObject } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

const generator = Generator<TCustomObject>({
fields: {
id: fake((f) => f.string.uuid()),
version: sequence(),
key: fake((f) => f.string.alphanumeric(10)),
container: fake((f) => f.string.alphanumeric(10)),
value: {
[fake((f) => f.string.alpha(5))]: {
[fake((f) => f.string.alpha(6))]: fake((f) => f.string.alpha(7)),
},
},
createdAt: fake(getOlderDate),
createdBy: fake(() => ClientLogging.random()),
lastModifiedAt: fake(getNewerDate),
lastModifiedBy: fake(() => ClientLogging.random()),
},
});

export default generator;
7 changes: 7 additions & 0 deletions models/custom-object/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * as CustomObjectDraft from './custom-object-draft';
export * as CustomObject from '.';

export { default as random } from './builder';
export * as presets from './presets';
export * as constants from './constants';
export * from './types';
3 changes: 3 additions & 0 deletions models/custom-object/src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
19 changes: 19 additions & 0 deletions models/custom-object/src/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Transformer } from '@commercetools-test-data/core';
import type { TCustomObject, TCustomObjectGraphql } from './types';

const transformers = {
default: Transformer<TCustomObject, TCustomObject>('default', {
buildFields: ['createdBy', 'lastModifiedBy'],
}),
rest: Transformer<TCustomObject, TCustomObject>('rest', {
buildFields: ['createdBy', 'lastModifiedBy'],
}),
graphql: Transformer<TCustomObject, TCustomObjectGraphql>('graphql', {
buildFields: ['createdBy', 'lastModifiedBy'],
addFields: () => ({
__typename: 'CustomObject',
}),
}),
};

export default transformers;
21 changes: 21 additions & 0 deletions models/custom-object/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {
CustomObject,
CustomObjectDraft,
} from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';

//CustomObjectDraft
export type TCustomObjectDraft = CustomObjectDraft;
export type TCustomObjectDraftBuilder = TBuilder<TCustomObjectDraft>;
export type TCreateCustomObjectDraftBuilder = () => TCustomObjectDraftBuilder;
export type TCustomObjectDraftGraphql = TCustomObjectDraft & {
__typename: 'CustomObjectDraft';
};

//CustomObject
export type TCustomObject = CustomObject;
export type TCustomObjectBuilder = TBuilder<TCustomObject>;
export type TCreateAssociateRoleBuilder = () => TCustomObjectBuilder;
export type TCustomObjectGraphql = TCustomObject & {
__typename: 'CustomObject';
};
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"description": "Modules to generate test data for commercetools APIs",
"private": true,
"preconstruct": {
"packages": ["core", "graphql-types", "models/*", "utils"]
"packages": [
"core",
"graphql-types",
"models/*",
"utils"
]
},
"scripts": {
"preinstall": "npx only-allow pnpm",
Expand Down Expand Up @@ -57,5 +62,8 @@
"tsc-files": "^1.1.3",
"typescript": "4.9.5"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"devDependencies": {
"@commercetools-test-data/core": "workspace:6.6.0"
}
}
Loading

0 comments on commit c06ba41

Please sign in to comment.