Skip to content

Commit 871663e

Browse files
Adjust test to respect expected inaccuracies
1 parent fbeed7b commit 871663e

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.javawebstack.orm.test;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.ORM;
5+
import org.javawebstack.orm.ORMConfig;
6+
import org.javawebstack.orm.Repo;
7+
import org.javawebstack.orm.annotation.Column;
8+
import org.javawebstack.orm.exception.ORMConfigurationException;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.sql.Timestamp;
12+
import java.time.Instant;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
public class TypesTest extends ORMTestCase {
17+
18+
@Test
19+
public void testFields() throws ORMConfigurationException {
20+
ORMConfig config = new ORMConfig()
21+
.setDefaultSize(255);
22+
ORM.register(ExampleModel.class, sql(), config);
23+
ORM.autoMigrate(true);
24+
25+
Timestamp timestamp = Timestamp.from(Instant.now());
26+
ExampleModel model = new ExampleModel();
27+
model.exampleString = "Hello ;)";
28+
model.exampleEnum = ExampleModel.Type.USER;
29+
30+
model.exampleFloatPrimitive = 1.33769F;
31+
model.exampleFloat = 1.33769F;
32+
33+
model.exampleDoublePrimitive = 123456789.1234567890D;
34+
model.exampleDouble = 123456789.1234567890D;
35+
36+
model.exampleLongPrimitive = 999999999999999999L;
37+
model.exampleLong = 999999999999999999L;
38+
39+
model.exampleBooleanPrimitive = true;
40+
model.exampleBoolean = true;
41+
42+
model.timestampTest = timestamp;
43+
model.save();
44+
45+
int id = model.id;
46+
47+
model = Repo.get(ExampleModel.class).get(id);
48+
49+
assertEquals(model.id, id);
50+
assertNull(model.exampleNull);
51+
assertTrue(model.exampleBoolean);
52+
assertTrue(model.exampleBooleanPrimitive);
53+
assertEquals(model.exampleEnum, ExampleModel.Type.USER);
54+
assertEquals(model.exampleString, "Hello ;)");
55+
assertEquals(model.exampleFloatPrimitive, 1.33769f);
56+
assertEquals(model.exampleFloat, 1.33769f);
57+
assertEquals(model.exampleDoublePrimitive, 123456789.1234567890D);
58+
assertEquals(model.exampleDouble, 123456789.1234567890D);
59+
assertEquals(model.exampleLong, 999999999999999999L);
60+
assertEquals(model.exampleLongPrimitive, 999999999999999999L);
61+
62+
// TODO: Use
63+
assertNotNull(model.timestampTest);
64+
65+
model.exampleNull = "Text";
66+
model.save();
67+
model = Repo.get(ExampleModel.class).get(id);
68+
assertEquals(model.exampleNull, "Text");
69+
}
70+
71+
public static class ExampleModel extends Model {
72+
73+
@Column
74+
public int id;
75+
76+
@Column
77+
public String exampleString;
78+
79+
@Column
80+
public float exampleFloatPrimitive;
81+
82+
@Column
83+
public Float exampleFloat;
84+
85+
@Column
86+
public double exampleDoublePrimitive;
87+
88+
@Column
89+
public Double exampleDouble;
90+
91+
@Column
92+
public long exampleLongPrimitive;
93+
94+
@Column
95+
public Long exampleLong;
96+
97+
@Column
98+
public boolean exampleBooleanPrimitive;
99+
100+
@Column
101+
public Boolean exampleBoolean;
102+
103+
@Column
104+
public Type exampleEnum;
105+
106+
@Column
107+
public String exampleNull;
108+
109+
@Column
110+
public Timestamp timestampTest;
111+
112+
public enum Type {
113+
ADMIN, USER, GUEST
114+
}
115+
116+
}
117+
118+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javawebstack.orm.test.shared.util;
2+
3+
import java.sql.Timestamp;
4+
5+
/*
6+
* The original author is Aaron Digulla
7+
* https://stackoverflow.com/a/582487
8+
*/
9+
public class TimestampUtil {
10+
11+
public static Double diffInNanoseconds (java.util.Date t1, java.util.Date t2)
12+
{
13+
// Make sure the result is always > 0
14+
if (t1.compareTo (t2) < 0)
15+
{
16+
java.util.Date tmp = t1;
17+
t1 = t2;
18+
t2 = tmp;
19+
}
20+
21+
// Timestamps mix milli and nanoseconds in the API, so we have to separate the two
22+
long diffSeconds = (t1.getTime () / 1000) - (t2.getTime () / 1000);
23+
// For normals dates, we have millisecond precision
24+
int nano1 = ((int) t1.getTime () % 1000) * 1000000;
25+
// If the parameter is a Timestamp, we have additional precision in nanoseconds
26+
if (t1 instanceof Timestamp)
27+
nano1 = ((Timestamp)t1).getNanos ();
28+
int nano2 = ((int) t2.getTime () % 1000) * 1000000;
29+
if (t2 instanceof Timestamp)
30+
nano2 = ((Timestamp)t2).getNanos ();
31+
32+
int diffNanos = nano1 - nano2;
33+
if (diffNanos < 0)
34+
{
35+
// Borrow one second
36+
diffSeconds --;
37+
diffNanos += 1000000000;
38+
}
39+
40+
return diffSeconds * 1000000000.0 + diffNanos / 1000.0;
41+
42+
}
43+
}

0 commit comments

Comments
 (0)