Skip to content

Commit 2527d80

Browse files
committed
Unite tests for types and values
1 parent eab5938 commit 2527d80

File tree

6 files changed

+112
-182
lines changed

6 files changed

+112
-182
lines changed

table/src/test/java/tech/ydb/table/types/DecimalTypeTest.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

table/src/test/java/tech/ydb/table/types/PrimitiveTypeTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

table/src/test/java/tech/ydb/table/types/StructTypeTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

table/src/test/java/tech/ydb/table/values/DecimalValueTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.Test;
99

1010
import tech.ydb.proto.ValueProtos;
11+
import tech.ydb.table.values.proto.ProtoType;
1112
import tech.ydb.table.values.proto.ProtoValue;
1213

1314

@@ -17,6 +18,53 @@
1718
*/
1819
public class DecimalValueTest {
1920

21+
@Test
22+
public void contractType() {
23+
DecimalType t = DecimalType.of(13, 2);
24+
25+
Assert.assertEquals(Type.Kind.DECIMAL, t.getKind());
26+
Assert.assertEquals(13, t.getPrecision());
27+
Assert.assertEquals(2, t.getScale());
28+
29+
Assert.assertEquals(DecimalType.of(13, 2), t);
30+
Assert.assertNotEquals(DecimalType.of(11, 2), t);
31+
Assert.assertNotEquals(DecimalType.of(13, 1), t);
32+
33+
Assert.assertEquals(DecimalType.of(13, 2).hashCode(), t.hashCode());
34+
Assert.assertNotEquals(DecimalType.of(11, 2).hashCode(), t.hashCode());
35+
Assert.assertNotEquals(DecimalType.of(13, 1).hashCode(), t.hashCode());
36+
37+
Assert.assertEquals("Decimal(13, 2)", t.toString());
38+
}
39+
40+
@Test
41+
public void protobufType() {
42+
DecimalType type = DecimalType.of(10, 5);
43+
ValueProtos.Type typePb = type.toPb();
44+
45+
Assert.assertEquals(ProtoType.getDecimal(10, 5), typePb);
46+
47+
Type typeX = ProtoType.fromPb(typePb);
48+
Assert.assertEquals(typeX, type);
49+
}
50+
51+
@Test
52+
public void bigDecimalConv() {
53+
BigDecimal orig, dest;
54+
55+
orig = new BigDecimal("-1.0");
56+
dest = DecimalType.of(22, 9).newValue(orig).toBigDecimal();
57+
Assert.assertEquals(0, orig.compareTo(dest));
58+
59+
orig = new BigDecimal("0.023");
60+
dest = DecimalType.of(22, 9).newValue(orig).toBigDecimal();
61+
Assert.assertEquals(0, orig.compareTo(dest));
62+
63+
orig = new BigDecimal("10000.52");
64+
dest = DecimalType.of(22, 9).newValue(orig).toBigDecimal();
65+
Assert.assertEquals(0, orig.compareTo(dest));
66+
}
67+
2068
@Test
2169
public void contract() {
2270
DecimalType type = DecimalType.of(13, 2);

table/src/test/java/tech/ydb/table/values/PrimitiveValueTest.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -517,26 +517,37 @@ public void datetime() {
517517

518518
@Test
519519
public void timestamp() {
520-
long seconds = 1534728225678901L;
521-
Instant instant = Instant.parse("2018-08-20T01:23:45.678901Z");
522-
523-
Consumer<PrimitiveValue> doTest = (v) -> {
524-
Assert.assertEquals(PrimitiveValue.newTimestamp(seconds), v);
525-
Assert.assertEquals(PrimitiveValue.newTimestamp(instant), v);
526-
Assert.assertNotEquals(PrimitiveValue.newTimestamp(0), v);
527-
528-
Assert.assertEquals("2018-08-20T01:23:45.678901Z", v.toString());
529-
Assert.assertEquals(v.getTimestamp(), instant);
530-
531-
ValueProtos.Value vPb = v.toPb();
532-
Assert.assertEquals(vPb, ProtoValue.fromTimestamp(seconds));
533-
Assert.assertEquals(vPb, ProtoValue.fromTimestamp(instant));
534-
535-
Assert.assertTrue(ProtoValue.fromPb(PrimitiveType.Timestamp, vPb).equals(v));
536-
};
537-
538-
doTest.accept(PrimitiveValue.newTimestamp(seconds));
539-
doTest.accept(PrimitiveValue.newTimestamp(instant));
520+
PrimitiveValue min = PrimitiveValue.newTimestamp(Instant.EPOCH);
521+
Assert.assertEquals(min, PrimitiveValue.newTimestamp(0));
522+
ValueProtos.Value minValue = min.toPb();
523+
524+
Assert.assertEquals(0, minValue.getUint32Value());
525+
Assert.assertEquals(0, minValue.getUint64Value());
526+
Assert.assertEquals(0, minValue.getInt32Value());
527+
Assert.assertEquals(0, minValue.getInt64Value());
528+
Assert.assertEquals(0, minValue.getLow128());
529+
Assert.assertEquals(0, minValue.getHigh128());
530+
531+
PrimitiveValue max = PrimitiveValue.newTimestamp(Instant.parse("2105-12-31T23:59:59.999999Z"));
532+
Assert.assertEquals(max, PrimitiveValue.newTimestamp(4291747199999999l));
533+
ValueProtos.Value maxValue = max.toPb();
534+
535+
Assert.assertEquals(0, maxValue.getUint32Value());
536+
Assert.assertEquals(4291747199999999l, maxValue.getUint64Value());
537+
Assert.assertEquals(0, maxValue.getInt32Value());
538+
Assert.assertEquals(0, maxValue.getInt64Value());
539+
Assert.assertEquals(0, maxValue.getLow128());
540+
Assert.assertEquals(0, maxValue.getHigh128());
541+
542+
IllegalArgumentException err1 = Assert.assertThrows(
543+
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(-1)
544+
);
545+
Assert.assertEquals("Negative microsSinceEpoch: -1", err1.getMessage());
546+
547+
IllegalArgumentException err2 = Assert.assertThrows(
548+
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(Instant.EPOCH.minusNanos(1))
549+
);
550+
Assert.assertEquals("Instant before epoch: 1969-12-31T23:59:59.999999999Z", err2.getMessage());
540551
}
541552

542553
@Test

table/src/test/java/tech/ydb/table/values/StructValueTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,37 @@ public void toStr() {
9999
"c", PrimitiveValue.newText("yes"));
100100
Assert.assertEquals("Struct[1, true, \"yes\"]", value2.toString());
101101
}
102+
103+
@Test
104+
public void oneMember() {
105+
StructType s = StructType.of("a", PrimitiveType.Text);
106+
107+
Assert.assertEquals(1, s.getMembersCount());
108+
Assert.assertEquals("Struct<'a': Text>", s.toString());
109+
110+
Assert.assertEquals("a", s.getMemberName(0));
111+
Assert.assertEquals(PrimitiveType.Text, s.getMemberType(0));
112+
Assert.assertEquals(0, s.getMemberIndex("a"));
113+
}
114+
115+
@Test
116+
public void manyMembers() {
117+
// not ordered names
118+
StructType s = StructType.of(
119+
"b", PrimitiveType.Int32,
120+
"a", PrimitiveType.Text);
121+
122+
Assert.assertEquals(2, s.getMembersCount());
123+
Assert.assertEquals("Struct<'a': Text, 'b': Int32>", s.toString());
124+
125+
// member 'a'
126+
Assert.assertEquals("a", s.getMemberName(0));
127+
Assert.assertEquals(PrimitiveType.Text, s.getMemberType(0));
128+
Assert.assertEquals(0, s.getMemberIndex("a"));
129+
130+
// member 'b'
131+
Assert.assertEquals("b", s.getMemberName(1));
132+
Assert.assertEquals(PrimitiveType.Int32, s.getMemberType(1));
133+
Assert.assertEquals(1, s.getMemberIndex("b"));
134+
}
102135
}

0 commit comments

Comments
 (0)