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 #1440 from finos/1425-create-tests-for-multiple-fi…
Browse files Browse the repository at this point in the history
…eld-dependencies

test(#1425): added cucumber tests for multiple related date fields
  • Loading branch information
hashbyhayter authored Oct 9, 2019
2 parents 96c0d80 + 332923e commit 70914f2
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.scottlogic.deg.generator.fieldspecs.relations.InMapRelation;
import com.scottlogic.deg.generator.generation.FieldSpecValueGenerator;
import com.scottlogic.deg.generator.generation.databags.*;
import com.scottlogic.deg.generator.utils.SetUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -48,7 +50,7 @@ public FieldSpecGroupValueGenerator(FieldSpecValueGenerator underlyingGenerator,
public Stream<DataBag> generate(FieldSpecGroup group) {
Field first = getFirst(group);

if (group.fieldSpecs().size() == 1){
if (group.fieldSpecs().size() == 1) {
return underlyingGenerator.generate(first, group.fieldSpecs().get(first))
.map(val -> toDataBag(first, val));
}
Expand All @@ -64,11 +66,16 @@ public Stream<DataBag> generate(FieldSpecGroup group) {
}

private Field getFirst(FieldSpecGroup keySet) {
Map<Field, Integer> otherCount = keySet.relations().stream().collect(Collectors.toMap(
FieldSpecRelations::other,
r -> 1, Integer::sum));
return otherCount.keySet().stream().reduce((l,r)->otherCount.get(l)>otherCount.get(r)?l:r)
.orElse(keySet.fieldSpecs().keySet().iterator().next());
Stream<FieldSpecRelations> relations = Stream.concat(
keySet.relations().stream(),
keySet.relations().stream().map(FieldSpecRelations::inverse)
);
List<Map.Entry<Field, Integer>> list = new ArrayList<>(relations
.collect(Collectors.toMap(
FieldSpecRelations::other,
r -> 1, Integer::sum)).entrySet());
list.sort(Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder()));
return list.isEmpty() ? SetUtils.firstIteratorElement(keySet.fieldSpecs().keySet()) : list.get(0).getKey();
}

private FieldSpec updateFirstSpecFromRelations(Field first, FieldSpecGroup group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Feature:As a User

Background:
Given the generation strategy is full
And the combination strategy is minimal
And the combination strategy is exhaustive
And there is a field foo
And foo has type "integer"
And foo is greater than 0
Expand All @@ -17,50 +17,33 @@ Feature:As a User
###Integer
Scenario: The one where a user can specify that one number should be greater than another number
Given bar is greater than 0
And the generator can generate at most 3 rows
And there is a constraint:
"""
{
"field": "bar",
"is": "greaterThan",
"otherField": "foo"
}
"""
And bar is less than 4
And the generator can generate at most 5 rows
And bar is greater than field foo
Then the following data should be generated:
| foo| bar|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 3 | 4 |

Scenario: The one where a user can specify that one number should be greater than or equal to another number
Given bar is greater than 0
And the generator can generate at most 3 rows
And there is a constraint:
"""
{
"field": "bar",
"is": "greaterThanOrEqualTo",
"otherField": "foo"
}
"""
And bar is less than 4
And the generator can generate at most 5 rows
And bar is greater than or equal to field foo
Then the following data should be generated:
| foo| bar|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 3 | 3 |
| 2 | 3 |

Scenario: The one where a user can specify that one number should be less than another number
Given foo is less than 3
And bar is greater than 0
And the generator can generate at most 3 rows
And there is a constraint:
"""
{
"field": "bar",
"is": "lessThan",
"otherField": "foo"
}
"""
And bar is less than field foo
Then the following data should be generated:
| foo| bar|
| 2 | 1 |
Expand All @@ -69,14 +52,7 @@ Feature:As a User
Given the combination strategy is exhaustive
And foo is less than 3
And bar is greater than 0
And there is a constraint:
"""
{
"field": "bar",
"is": "lessThanOrEqualTo",
"otherField": "foo"
}
"""
And bar is less than or equal to field foo
Then the following data should be generated:
| foo| bar|
| 1 | 1 |
Expand All @@ -86,14 +62,7 @@ Feature:As a User
Scenario: The one where a user can specify that one number should be equal to another number
Given bar is greater than 0
And the generator can generate at most 3 rows
And there is a constraint:
"""
{
"field": "bar",
"is": "equalTo",
"otherField": "foo"
}
"""
And bar is equal to field foo
Then the following data should be generated:
| foo| bar|
| 1 | 1 |
Expand Down Expand Up @@ -183,14 +152,7 @@ Feature:As a User
And bar is greater than 0
And bar is less than 5
And the generator can generate at most 6 rows
And there is a constraint:
"""
{
"field": "bar",
"is": "greaterThan",
"otherField": "foo"
}
"""
And bar is greater than field foo
Then the following data should be generated:
| foo| bar|
| 1 | 2 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ Feature: User can specify that decimal fields are granular to a certain number o

Scenario: The one where a user can specify that one decimal number should be greater than another decimal number
Given foo is granular to 0.1
And the combination strategy is exhaustive
And foo is greater than or equal to 1
And foo is anything but null
And bar is anything but null
And there is a field bar
And bar has type "decimal"
And bar is granular to 0.1
And bar is less than 1.4
And bar is greater than or equal to 1
And there is a constraint:
"""
{
"field": "bar",
"is": "greaterThan",
"otherField": "foo"
}
"""
And foo is less than 1.4
And bar is greater than field foo
Then the following data should be generated:
| foo | bar |
| 1 | 1.1 |
| 1 | 1.2 |
| 1 | 1.3 |
| 1.1 | 1.2 |
| 1.1 | 1.3 |
| 1.2 | 1.3 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
Feature: running datetimes related to otherfield datetimes for multiple fields

Background:
Given the generation strategy is full
And there is a field foobar
And foobar has type "datetime"
And there is a field foo
And foo has type "datetime"
And there is a field bar
And bar has type "datetime"
And the combination strategy is exhaustive

Scenario: Running a "before" and "after" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is before field bar
And foobar is after field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.002Z |

Scenario: Running a "before" and "equalTo" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is before field bar
And foobar is equal to field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.001Z |

Scenario: Running an "after" and "equalTo" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is after field bar
And foobar is equal to field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.000Z |

Scenario: Running two "after" constraints
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is after field bar
And foobar is after field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.000Z |

Scenario: Running two "before" constraints switched other field constraints
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And bar is before field foobar
And foo is before field foobar
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.000Z |

Scenario: Running two "before" constraints
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is before field bar
And foobar is before field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.001Z |

Scenario: Running two "after" switched other field constraints
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And bar is after field foobar
And foo is after field foobar
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.001Z | 0001-01-01T00:00:00.001Z |

Scenario: Running linked "after" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is after field bar
And bar is after field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.002Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.001Z |

Scenario: Running linked "before" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is before field bar
And bar is before field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.002Z | 0001-01-01T00:00:00.001Z |

Scenario: Running linked "before" constraint with lower limit
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is before 2019-01-01T00:00:00.000Z
And foobar is before field bar
And bar is before field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.002Z | 0001-01-01T00:00:00.001Z |

Scenario: Running linked "equalTo" constraint
Given the generator can generate at most 1 rows
And foo is anything but null
And bar is anything but null
And foobar is anything but null
And foobar is equal to field bar
And bar is equal to field foo
Then the following data should be generated:
| foobar | foo | bar |
| 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.000Z | 0001-01-01T00:00:00.000Z |
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.scottlogic.deg.orchestrator.cucumber.testframework.steps;

import com.scottlogic.deg.common.profile.constraintdetail.AtomicConstraintType;
import com.scottlogic.deg.orchestrator.cucumber.testframework.utils.CucumberTestHelper;
import com.scottlogic.deg.orchestrator.cucumber.testframework.utils.CucumberTestState;
import com.scottlogic.deg.orchestrator.cucumber.testframework.utils.GeneratorTestUtilities;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

Expand Down Expand Up @@ -46,6 +48,16 @@ public void whenFieldIsNotConstrainedByDateValue(String fieldName, String constr
state.addNotConstraint(fieldName, constraintName, value);
}

@And("^(.+) is after field ([A-z0-9]+)$")
public void dateAfter(String field, String otherField){
state.addRelationConstraint(field, AtomicConstraintType.IS_AFTER_CONSTANT_DATE_TIME.getText(), otherField);
}

@And("^(.+) is before field ([A-z0-9]+)$")
public void dateBefore(String field, String otherField){
state.addRelationConstraint(field, AtomicConstraintType.IS_BEFORE_CONSTANT_DATE_TIME.getText(), otherField);
}

@Then("{fieldVar} contains only datetime data")
public void producedDataShouldContainOnlyDateTimeValuesForField(String fieldName){
helper.assertFieldContainsNullOrMatching(fieldName, OffsetDateTime.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public void uniquefieldIsUnique(String fieldName) {
this.state.setFieldUnique(fieldName);
}

@And("^(.+) is equal to field (.+)$")
public void fieldEqualTo(String field, String otherField){
state.addRelationConstraint(field, AtomicConstraintType.IS_EQUAL_TO_CONSTANT.getText(), otherField);
}



@Then("^the profile should be considered valid$")
Expand Down
Loading

0 comments on commit 70914f2

Please sign in to comment.