Skip to content

Commit c73924b

Browse files
committed
add faker for numeric ranges
1 parent 506c549 commit c73924b

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/main/java/it/aboutbits/springboot/testing/testdata/base/FakerExtended.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package it.aboutbits.springboot.testing.testdata.base;
22

3+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
34
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
45
import lombok.NonNull;
56
import net.datafaker.Faker;
@@ -60,4 +61,52 @@ public <T extends EntityId<String>> T randomEntityId(@NonNull Function<String, T
6061
public String unique(@NonNull String value) {
6162
return value + "_" + super.random().nextInt(9999999);
6263
}
64+
65+
public RandomNumericRange numericRange() {
66+
return new RandomNumericRange(
67+
super.getFaker()
68+
);
69+
}
70+
71+
public static final class RandomNumericRange {
72+
private final Faker parent;
73+
74+
private RandomNumericRange(Faker parent) {
75+
this.parent = parent;
76+
}
77+
78+
public NumericRange random() {
79+
return random(-999999999, 999999999);
80+
}
81+
82+
public NumericRange positive() {
83+
return random(1, 999999999);
84+
}
85+
86+
public NumericRange positiveOrZero() {
87+
return random(0, 999999999);
88+
}
89+
90+
public NumericRange negative() {
91+
return random(-999999999, -1);
92+
}
93+
94+
public NumericRange negativeOrZero() {
95+
return random(-999999999, 0);
96+
}
97+
98+
public NumericRange random(double min, double max) {
99+
var lower = parent.random().nextDouble(min, max - 1);
100+
var upper = parent.random().nextDouble(lower, max);
101+
102+
return new NumericRange(
103+
ScaledBigDecimal.valueOf(lower),
104+
ScaledBigDecimal.valueOf(upper)
105+
);
106+
}
107+
108+
public record NumericRange(ScaledBigDecimal lower, ScaledBigDecimal upper) {
109+
110+
}
111+
}
63112
}

0 commit comments

Comments
 (0)