Skip to content

Commit

Permalink
[FEATURE] Update identifiers profile (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Jun 1, 2024
1 parent f20d7ee commit f049e84
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/docs/System-Documentation/Profiles/00_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ console.log(fabricator.generate());
{ id: '5c3c2dc9-905b-4005-4059-40ff821cd2cc' }
```

!!! tip

In this example, you can see a basic usage of [Identifiers](./01_identifiers.md)
profile for generating an object ID. Check it out!

As you can see, the profile data are generated and passed into the
Fabrication Context object into `profiles` property; the actual
profile object (with its contents) you requested is then accessible
Expand Down
101 changes: 101 additions & 0 deletions docs/docs/System-Documentation/Profiles/01_identifiers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Identifiers

Identifiers are one of the most basic metadata properties of data. Instead of
generating them on your own, you can use a predefined profile `identifiers`.

## Example Usage

At the following example you can see how could this profile:

```typescript linenums="1"
const schemaInput: SchemaInput = {
// Declaration of profiles you want to use
profiles: ['identifiers'],

fields: {
// Field being scraped from a context using the reference notation
uuid: '!ref-profiles.identifiers.uuid'

// ... other fields ...
},
};

const fabricator = new Fabricator(schemaInput);

console.log(fabricator.generate());
```

!!! abstract "Output"

```
{ uuid: '5c3c2dc9-905b-4005-4059-40ff821cd2cc' }
```


## Contents

There are more various identifiers than just uuid:

- `rowNumber` - Number of the generated instance in a sequence
(only when using `generateMany(n)` method)
- `randH` - Random number within hundreds (from interval [100 - 999])
- `randT` - Random number within thousands (from interval [1,000 - 9,999])
- `randM` - Random number within millions (from interval [1,000,000 - 9,999,999])
- `uuid` - UUID (Universally Unique Identifier) in lowercase characters;
otherwise the value is the same as in `UUID` property
- `UUID` - UUID (Universally Unique Identifier) in uppercase characters;
otherwise the value is the same as in `uuid` property


## Example

Here's an example of how does the `identifiers` profile behave:

```typescript linenums="1"
const schemaInput: SchemaInput = {
profiles: ['identifiers'],
fields: {
rowNumber: '!ref-profiles.identifiers.rowNumber',
randH: '!ref-profiles.identifiers.randH',
randT: '!ref-profiles.identifiers.randT',
randM: '!ref-profiles.identifiers.randM',
uuid: '!ref-profiles.identifiers.uuid',
UUID: '!ref-profiles.identifiers.UUID'
},
};

const fabricator = new Fabricator(schemaInput);

console.log(fabricator.generateMany(3));
```

!!! abstract "Output"

```
[
{
rowNumber: 0,
randH: 115,
randT: 5263,
randM: 3968364,
uuid: '23d8ec5f-93ea-4919-ce83-df638a958303',
UUID: '23D8EC5F-93EA-4919-CE83-DF638A958303'
},
{
rowNumber: 1,
randH: 649,
randT: 9368,
randM: 3201817,
uuid: '38360491-f11a-484c-24c8-61d00ba29aba',
UUID: '38360491-F11A-484C-24C8-61D00BA29ABA'
},
{
rowNumber: 2,
randH: 502,
randT: 2845,
randM: 5387946,
uuid: '9393d3fb-d23d-43bb-9bdb-9af99c5d942a',
UUID: '9393D3FB-D23D-43BB-9BDB-9AF99C5D942A'
}
]
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@falbricate/base",
"version": "1.3.1",
"version": "1.3.2",
"description": "Base Framework for Falbricate - tool for Falsa Fabrication",
"keywords": [
"mocking",
Expand Down
34 changes: 29 additions & 5 deletions src/profiles/generators/IdentifierProfileFabricator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,36 @@ import { SchemaInput } from '../../schema';
export class IdentifierProfileFabricator extends ProfileFabricator {
private static readonly SCHEMA_INPUT: SchemaInput = {
fields: {
rowNumber: { type: 'context-input', config: { path: 'index' } },
rowNumber: '!ref-index',
randH: {
type: 'range-integer',
config: {
min: 100,
max: 999
}
},
randT: {
type: 'range-integer',
config: {
min: 1_000,
max: 9_999
}
},
randM: {
type: 'range-integer',
config: {
min: 1_000_000,
max: 9_999_999
}
},
uuid: 'uuid',
UUID: 'UUID',
randH: { type: 'range-integer', config: { max: 100 } },
randT: { type: 'range-integer', config: { max: 1_000 } },
randM: { type: 'range-integer', config: { max: 1_000_000 } },
UUID: {
type: 'context-input',
config: {
path: 'current.uuid',
pipes: ['uppercase']
},
}
},
};

Expand Down
55 changes: 55 additions & 0 deletions test/profiles/generators/IdentifierProfileFabricator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Fabricator, SchemaInput } from '../../../src';

const schemaInput: SchemaInput = {
profiles: ['identifiers'],
fields: {
rowNumber: '!ref-profiles.identifiers.rowNumber',
randH: '!ref-profiles.identifiers.randH',
randT: '!ref-profiles.identifiers.randT',
randM: '!ref-profiles.identifiers.randM',
uuid: '!ref-profiles.identifiers.uuid',
UUID: '!ref-profiles.identifiers.UUID'
},
};

const fabricator = new Fabricator(schemaInput);

describe('rowNumber property', () => {
it('should always increase the value with index', () => {
const generated = fabricator.generateMany(5);

generated.forEach((item: any, idx) => {
expect(item.rowNumber).toBe(idx)
});
});
});

describe('randH property', () => {
it('should always be within a range of [100, 999]', () => {
const generated = fabricator.generateMany(50);
generated.forEach((item: any) => {
expect(item.randH).toBeGreaterThanOrEqual(100);
expect(item.randH).toBeLessThanOrEqual(999);
});
});
});

describe('randT property', () => {
it('should always be within a range of [1000, 9999]', () => {
const generated = fabricator.generateMany(50);
generated.forEach((item: any) => {
expect(item.randT).toBeGreaterThanOrEqual(1000);
expect(item.randT).toBeLessThanOrEqual(9999);
});
});
});

describe('randM property', () => {
it('should always be within a range of millions', () => {
const generated = fabricator.generateMany(50);
generated.forEach((item: any) => {
expect(item.randM).toBeGreaterThanOrEqual(1_000_000);
expect(item.randM).toBeLessThanOrEqual(9_999_999);
});
});
});

0 comments on commit f049e84

Please sign in to comment.