Skip to content

Commit

Permalink
Configure intervals for numbers generated
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jul 6, 2024
1 parent 9e63c25 commit 5a5c1b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class DictionaryContainer {
private char padChar = ' ';
private int padSize = 0;

ConstraintInteger intConstraint = null;

public void reset() {
lastIndex = -1;
lastCycle = 0;
Expand Down Expand Up @@ -112,6 +114,17 @@ public DictionaryAttrGen padLeft(char padChar, int padSize) {
return this;
}

/**
* Sets an interval to generated values (applicable to numbers),
* even if the schema does not specify min/max
*/
public DictionaryAttrGen setInterval(int min, int max) {
currentConfiguringContainer.intConstraint = new ConstraintInteger();
currentConfiguringContainer.intConstraint.add(">=", String.valueOf(min));
currentConfiguringContainer.intConstraint.add("<=", String.valueOf(max));
return this;
}

// Internal methods to manage the dictionary

// returns null if no dictionary container found for the coordinates
Expand Down Expand Up @@ -163,6 +176,9 @@ public String generateNumber(IConstraint constraints, String entityName, String
DictionaryContainer container = getDictionary(entityName, attrName);
String value = super.generateNumber(constraints, entityName, attrName);

if (container != null && container.intConstraint != null)
value = container.intConstraint.apply(value);

if (container != null)
value = container.padValue(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import giis.tdrules.store.loader.IAttrGen;
import giis.tdrules.store.loader.gen.DeterministicAttrGen;
import giis.tdrules.store.loader.gen.DictionaryAttrGen;

/**
* How optional settings of the Attribute Generators influence the generated values
Expand Down Expand Up @@ -37,5 +38,24 @@ public void testGenerateDatesMinYear() {
gen.incrementAttrCount();
assertEquals("2054-01-04", gen.generateDate());
}

// Limits to int values (when the schema does not specify any) must
// be set through a dictionary because are attribute specific
@Test
public void testGenerateNumberInInterval() {
IAttrGen gen= new DictionaryAttrGen().with("entity", "attr").setInterval(2, 4);
assertEquals("4", gen.generateNumber(null, "entity", "attr")); // first is 1, out of rang, produces 4
gen.incrementAttrCount();
assertEquals("2", gen.generateNumber(null, "entity", "attr")); //2
gen.incrementAttrCount();
assertEquals("3", gen.generateNumber(null, "entity", "attr")); //3
gen.incrementAttrCount();
assertEquals("4", gen.generateNumber(null, "entity", "attr")); //4
gen.incrementAttrCount();
assertEquals("2", gen.generateNumber(null, "entity", "attr")); //5 out of range, produces 2
gen.incrementAttrCount();
assertEquals("3", gen.generateNumber(null, "entity", "attr"));

}

}

0 comments on commit 5a5c1b0

Please sign in to comment.