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 #1435 from finos/1340-fix-relations-with-null-beha…
Browse files Browse the repository at this point in the history
…viour

fix(#1340): Fix null behaviour with relational constraints
  • Loading branch information
r-stuart authored Oct 8, 2019
2 parents 7607481 + 7ac1272 commit 03dd178
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(chronoUnit, workingDay);
}

@Override
public String toString() {
return "DateTimeGranularity{" +
"chronoUnit=" + chronoUnit +
", workingDay=" + workingDay +
", temporalAdjusterGenerator=" + temporalAdjusterGenerator +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public FieldSpec reduceToRelatedFieldSpec(FieldSpec otherValue) {

@Override
public FieldSpec reduceValueToFieldSpec(DataBagValue generatedValue) {
Limit<OffsetDateTime> limit = new Limit<>((OffsetDateTime)generatedValue.getValue(), true);
OffsetDateTime value = (OffsetDateTime) generatedValue.getValue();
if (value == null) {
return FieldSpec.empty();
}
Limit<OffsetDateTime> limit = new Limit<>(value, true);
LinearRestrictions<OffsetDateTime> restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(limit,limit);
FieldSpec newSpec = FieldSpec.fromRestriction(restrictions).withNotNull();
FieldSpec newSpec = FieldSpec.fromRestriction(restrictions);
return reduceToRelatedFieldSpec(newSpec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public FieldSpec reduceToRelatedFieldSpec(FieldSpec otherValue) {

@Override
public FieldSpec reduceValueToFieldSpec(DataBagValue generatedValue) {
Limit<OffsetDateTime> limit = new Limit<>((OffsetDateTime)generatedValue.getValue(), true);
OffsetDateTime value = (OffsetDateTime) generatedValue.getValue();
if (value == null) {
return FieldSpec.empty();
}
Limit<OffsetDateTime> limit = new Limit<>(value, true);
LinearRestrictions<OffsetDateTime> restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(limit,limit);
FieldSpec newSpec = FieldSpec.fromRestriction(restrictions).withNotNull();
FieldSpec newSpec = FieldSpec.fromRestriction(restrictions);
return reduceToRelatedFieldSpec(newSpec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ public FieldSpec reduceToRelatedFieldSpec(FieldSpec otherValue) {

@Override
public FieldSpec reduceValueToFieldSpec(DataBagValue generatedValue) {
Limit<OffsetDateTime> limit = new Limit<>((OffsetDateTime)generatedValue.getValue(), true);
OffsetDateTime value = (OffsetDateTime) generatedValue.getValue();
if (value == null) {
return FieldSpec.empty();
}
Limit<OffsetDateTime> limit = new Limit<>(value, true);
LinearRestrictions<OffsetDateTime> restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(limit,limit);
return FieldSpec.fromRestriction(restrictions).withNotNull();
return FieldSpec.fromRestriction(restrictions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scottlogic.deg.generator.fieldspecs.relations;

import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.constraintdetail.DateTimeGranularity;
import com.scottlogic.deg.common.profile.constraintdetail.Granularity;
import com.scottlogic.deg.generator.fieldspecs.FieldSpec;
import com.scottlogic.deg.generator.generation.databags.DataBagValue;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

import static com.scottlogic.deg.common.util.Defaults.ISO_MAX_DATE;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AfterDateRelationTest {

private final Field a = new Field("a", Types.DATETIME, false, "", false);
private final Field b = new Field("b", Types.DATETIME, false, "", false);

@Test
public void testReduceToFieldSpec_withNotNull_reducesToSpec() {
FieldSpecRelations afterDateRelations = new AfterDateRelation(a, b, true);
OffsetDateTime value = OffsetDateTime.of(2000,
1,
1,
0,
0,
0,
0,
ZoneOffset.UTC);
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = afterDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.fromRestriction(new LinearRestrictions<>(value, ISO_MAX_DATE, new DateTimeGranularity(ChronoUnit.MILLIS)));
assertEquals(expected, result);
}

@Test
public void testReduceToFieldSpec_withNotNullExclusive_reducesToSpec() {
FieldSpecRelations afterDateRelations = new AfterDateRelation(a, b, false);
OffsetDateTime value = OffsetDateTime.of(2000,
1,
1,
0,
0,
0,
0,
ZoneOffset.UTC);
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = afterDateRelations.reduceValueToFieldSpec(generatedValue);

Granularity<OffsetDateTime> granularity = new DateTimeGranularity(ChronoUnit.MILLIS);
FieldSpec expected = FieldSpec.fromRestriction(new LinearRestrictions<>(granularity.getNext(value), ISO_MAX_DATE, new DateTimeGranularity(ChronoUnit.MILLIS)));
assertEquals(expected, result);
}

@Test
public void testReduceToFieldSpec_withNull_reducesToSpec() {
FieldSpecRelations afterDateRelations = new AfterDateRelation(a, b, true);
OffsetDateTime value = null;
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = afterDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.empty();
assertEquals(expected, result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scottlogic.deg.generator.fieldspecs.relations;

import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.constraintdetail.DateTimeGranularity;
import com.scottlogic.deg.common.profile.constraintdetail.Granularity;
import com.scottlogic.deg.generator.fieldspecs.FieldSpec;
import com.scottlogic.deg.generator.generation.databags.DataBagValue;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

import static com.scottlogic.deg.common.util.Defaults.ISO_MIN_DATE;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BeforeDateRelationTest {

private final Field a = new Field("a", Types.DATETIME, false, "", false);
private final Field b = new Field("b", Types.DATETIME, false, "", false);

@Test
public void testReduceToFieldSpec_withNotNull_reducesToSpec() {
FieldSpecRelations beforeDateRelations = new BeforeDateRelation(a, b, true);
OffsetDateTime value = OffsetDateTime.of(2000,
1,
1,
0,
0,
0,
0,
ZoneOffset.UTC);
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = beforeDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.fromRestriction(new LinearRestrictions<>(ISO_MIN_DATE, value, new DateTimeGranularity(ChronoUnit.MILLIS)));
assertEquals(expected, result);
}

@Test
public void testReduceToFieldSpec_withNotNullExclusive_reducesToSpec() {
FieldSpecRelations beforeDateRelations = new BeforeDateRelation(a, b, false);
OffsetDateTime value = OffsetDateTime.of(2000,
1,
1,
0,
0,
0,
0,
ZoneOffset.UTC);
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = beforeDateRelations.reduceValueToFieldSpec(generatedValue);

Granularity<OffsetDateTime> granularity = new DateTimeGranularity(ChronoUnit.MILLIS);
FieldSpec expected = FieldSpec.fromRestriction(new LinearRestrictions<>(ISO_MIN_DATE, granularity.getPrevious(value), new DateTimeGranularity(ChronoUnit.MILLIS)));
assertEquals(expected, result);
}

@Test
public void testReduceToFieldSpec_withNull_reducesToSpec() {
FieldSpecRelations beforeDateRelations = new BeforeDateRelation(a, b, true);
OffsetDateTime value = null;
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = beforeDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.empty();
assertEquals(expected, result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.scottlogic.deg.generator.fieldspecs.relations;

import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.constraintdetail.DateTimeGranularity;
import com.scottlogic.deg.generator.fieldspecs.FieldSpec;
import com.scottlogic.deg.generator.generation.databags.DataBagValue;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

import static org.junit.jupiter.api.Assertions.*;

class EqualToDateRelationTest {

private final Field a = new Field("a", Types.DATETIME, false ,"", false);
private final Field b = new Field("b", Types.DATETIME, false, "", false);
private final FieldSpecRelations equalToDateRelations = new EqualToDateRelation(a, b);

@Test
public void testReduceToFieldSpec_withNotNull_reducesToSpec() {
OffsetDateTime value = OffsetDateTime.of(2000,
1,
1,
0,
0,
0,
0,
ZoneOffset.UTC);
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = equalToDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.fromRestriction(new LinearRestrictions<>(value, value, new DateTimeGranularity(ChronoUnit.MILLIS)));
assertEquals(expected, result);
}

@Test
public void testReduceToFieldSpec_withNull_reducesToSpec() {
OffsetDateTime value = null;
DataBagValue generatedValue = new DataBagValue(value);

FieldSpec result = equalToDateRelations.reduceValueToFieldSpec(generatedValue);

FieldSpec expected = FieldSpec.empty();
assertEquals(expected, result);
}

}

0 comments on commit 03dd178

Please sign in to comment.