Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1495 from finos/1488-custom-generator-constraint
Browse files Browse the repository at this point in the history
feat(#1488): allow Custom generators to be used in constraints
  • Loading branch information
cdowding-sl authored Nov 12, 2019
2 parents 1378282 + c981639 commit 9392007
Show file tree
Hide file tree
Showing 95 changed files with 184 additions and 99 deletions.
8 changes: 4 additions & 4 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ We'll start by generating data for a trivial schema. Using your favourite text e

```json
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"fields": [{ "name": "firstName", "type": "string" }],
"rules": []
}
Expand Down Expand Up @@ -111,7 +111,7 @@ Update the JSON profile as follows:

```json
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"fields": [{ "name": "firstName", "type": "string" }],
"rules": [
{
Expand Down Expand Up @@ -181,7 +181,7 @@ We'll expand the example profile to add a new `age` field, a not-null integer in

```json
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"fields": [
{ "name": "firstName", "type": "string" },
{ "name": "age", "type": "integer" }
Expand Down Expand Up @@ -228,7 +228,7 @@ Finally, we'll add a field for National Insurance number. In this case, the cons

```json
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"fields": [
{ "name": "firstName", "type": "string" },
{ "name": "age", "type": "integer" },
Expand Down
24 changes: 20 additions & 4 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
7. [Custom Generators](#custom-generator)
1. [adding](#adding-custom-generator)
2. [using](#using-custom-generator)
3. [using as constraint](#using-custom-generator-as-constraint)

8. [Running a Profile](#Running-a-Profile)
1. [Command Line Arguments](#Command-Line-Arguments)
Expand Down Expand Up @@ -87,9 +88,9 @@ This section will walk you through creating basic profiles with which you can ge
Profiles are JSON documents consisting of three sections, the schema version, the list of fields and the rules.

- **Schema Version** - Dictates the method of serialisation of the profile in order for the generator to
interpret the profile fields and rules. The latest version is 0.7.
interpret the profile fields and rules. The latest version is
```
"schemaVersion": "0.7",
"schemaVersion": "0.16",
```
- **List of Fields** - An array of column headings is defined with unique "name" keys.
```
Expand Down Expand Up @@ -140,7 +141,7 @@ These three sections are combined to form the [complete profile](#Example-Profil
## Example Profile
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"fields": [
{
"name": "Column 1",
Expand Down Expand Up @@ -668,7 +669,6 @@ While it's not prohibited, wrapping conditional constraints in any other kind of




# Custom Generators
<div id="custom-generator"></div>

Expand Down Expand Up @@ -704,6 +704,22 @@ This will use the "lorem ipsum" example custom generator.

To use your own, put the name of your generator instead of "lorem ipsum"

## using Custom Generators as Constraint
<div id="using-custom-generator-as-constraint"></div>

You can also use custom generators as constraints


```javascript
{ "field": "field1", "generator": "lorem ipsum" }
```

Custom generators can be used in "anyOf" grammatical constraints, as well as in the "then" and "else" parts of conditional constraints

To combine generators with sets and equalTo, you will need to create a 'matchingFunction' when building the custom generator. Which should be a function that returns true if a value is one the custom generator could produce.

To be able negate the custom generator, or use in the 'if' section of an if then statement, you must define the 'negated Generator' when building the custom generator. Which should return values that the custom generator should not produce.

# Running a Profile
<div id="Running-a-Profile"></div>

Expand Down
2 changes: 1 addition & 1 deletion docs/user/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Sample file
```javascript
{
"schemaVersion": "0.7",
"schemaVersion": "0.16",
"description": "A dataset about financial products",
"fields":
[
Expand Down
2 changes: 1 addition & 1 deletion examples/a-valid-isin/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "isin",
Expand Down
2 changes: 1 addition & 1 deletion examples/actor-names/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "forename",
Expand Down
2 changes: 1 addition & 1 deletion examples/anyOf/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"rules": [
{
"rule": "rule 1",
Expand Down
2 changes: 1 addition & 1 deletion examples/date-after/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "date",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-after-dynamic/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "field1",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-after/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "date",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-equal-to-dynamic-offset/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-equal-to-dynamic/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-equal-to/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "date",
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime-offset-working-days/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "foo",
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-trades-smaller/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "tradeId",
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-trades/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "tradeId",
Expand Down
2 changes: 1 addition & 1 deletion examples/duplicates/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "a",
Expand Down
2 changes: 1 addition & 1 deletion examples/formatting/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "decimal",
Expand Down
2 changes: 1 addition & 1 deletion examples/from-file-weighted/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "name",
Expand Down
2 changes: 1 addition & 1 deletion examples/from-file/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "name",
Expand Down
2 changes: 1 addition & 1 deletion examples/hard-contradiction-null-validation/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "Column 1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "Column 1",
Expand Down
2 changes: 1 addition & 1 deletion examples/integer-range-with-blacklist/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "an_integer",
Expand Down
2 changes: 1 addition & 1 deletion examples/integer-range/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "an_integer",
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple-fields/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "field1",
Expand Down
2 changes: 1 addition & 1 deletion examples/names/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "forename",
Expand Down
2 changes: 1 addition & 1 deletion examples/partial-contradictions/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "Column 1",
Expand Down
2 changes: 1 addition & 1 deletion examples/partitioning/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "p1f1",
Expand Down
2 changes: 1 addition & 1 deletion examples/real-number-range/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "a_number",
Expand Down
2 changes: 1 addition & 1 deletion examples/regex-contains/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first_name",
Expand Down
2 changes: 1 addition & 1 deletion examples/regex-intersect-with-blacklist/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first_name",
Expand Down
2 changes: 1 addition & 1 deletion examples/regex-intersect/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first_name",
Expand Down
2 changes: 1 addition & 1 deletion examples/regex/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first_name",
Expand Down
2 changes: 1 addition & 1 deletion examples/setwise-combination/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "a",
Expand Down
2 changes: 1 addition & 1 deletion examples/shorter-than/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "first_name",
Expand Down
2 changes: 1 addition & 1 deletion examples/string/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"rules": [],
"fields": [
{
Expand Down
2 changes: 1 addition & 1 deletion examples/unique/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "id",
Expand Down
2 changes: 1 addition & 1 deletion examples/user-account/profile.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "user_id",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Generator use custom generators.

Scenario: custom lorem ipsum generator can output rows
Given the generation strategy is Random
And the generator can generate at most 1 rows
And there is a non nullable field foo
And foo uses custom generator "lorem ipsum"
Then the generator can generate at most 1 rows
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public void producedDataShouldContainStringValuesOutOfRangeForField(String field
value -> !isLengthBetweenInclusively(min, max).apply(value));
}

@When("^([A-z0-9]+) uses custom generator \"(.*)\"$")
public void customGenerator(String fieldName, String value) {
state.addConstraint(fieldName, ConstraintType.EQUAL_TO, value);
}

private Function<String, Boolean> isLengthBetweenInclusively(int minInclusive, int maxInclusive) {
return value -> isLongerThanOrEqual(value, minInclusive) && isShorterThanOrEqual(value, maxInclusive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ private ConstraintDTO createConstraint(String fieldName, ConstraintType type, Ob
field = fieldName;
value = (String) _value;
}};
case GENERATOR:
return new GeneratorConstraintDTO()
{{
field = fieldName;
generator = (String) _value;
}};
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"fields": [
{
"name": "foo",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"schemaVersion": "0.15",
"schemaVersion": "0.16",
"description": "Testing Profile",
"fields": [
{
Expand Down
Loading

0 comments on commit 9392007

Please sign in to comment.