This repository was archived by the owner on Jan 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL4DriverTest.java
More file actions
136 lines (123 loc) · 4.86 KB
/
L4DriverTest.java
File metadata and controls
136 lines (123 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package io.vacco.l4zr;
import com.zaxxer.hikari.*;
import io.vacco.l4zr.dao.*;
import io.vacco.l4zr.jdbc.*;
import io.vacco.l4zr.rqlite.L4Client;
import io.vacco.l4zr.schema.*;
import io.vacco.metolithe.codegen.dao.MtDaoMapper;
import io.vacco.metolithe.core.*;
import io.vacco.shax.logging.ShOption;
import j8spec.annotation.DefinedOrder;
import j8spec.junit.J8SpecRunner;
import liquibase.Scope;
import liquibase.command.CommandScope;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.codejargon.fluentjdbc.api.FluentJdbcBuilder;
import org.codejargon.fluentjdbc.api.integration.ConnectionProvider;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.io.*;
import java.sql.DriverManager;
import java.util.List;
import java.util.stream.Stream;
import static j8spec.J8Spec.*;
@DefinedOrder
@RunWith(J8SpecRunner.class)
public class L4DriverTest {
public static final Class<?>[] schema = new Class<?>[] {
User.class, Device.class, Location.class
};
public static final String rqUrl = "jdbc:sqlite:http://localhost:4001";
public static final L4Client rq = L4Tests.localClient();
static {
ShOption.setSysProp(ShOption.IO_VACCO_SHAX_DEVMODE, "true");
ShOption.setSysProp(ShOption.IO_VACCO_SHAX_LOGLEVEL, "trace");
if (!GraphicsEnvironment.isHeadless()) {
it("Generates schema DAOs", () -> {
var daoDir = new File("./src/test/java");
var pkg = "io.vacco.l4zr.dao";
new MtDaoMapper().mapSchema(daoDir, pkg, MtCaseFormat.KEEP_CASE, schema);
});
it("Applies Liquibase changesets", () -> {
var log = LoggerFactory.getLogger(L4DriverTest.class);
L4Log.traceFn = log::trace;
var hkConfig = new HikariConfig();
hkConfig.setJdbcUrl(rqUrl);
try (var hds = new HikariDataSource(hkConfig)) {
try (var jdbcConn = new JdbcConnection(hds.getConnection())) {
var ra = new ClassLoaderResourceAccessor();
var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConn);
database.setDefaultCatalogName(L4Db.Main);
Scope.child(Scope.Attr.resourceAccessor, ra, () -> {
var commandScope = new CommandScope("update");
commandScope.addArgumentValue("changelogFile", "l4-schema.yml");
commandScope.addArgumentValue("database", database);
commandScope.execute();
});
}
}
});
it("Inserts data via object mapping", () -> {
var Fmt = MtCaseFormat.KEEP_CASE;
var idFn = new MtMurmur3IFn(1984);
var connP = (ConnectionProvider) query -> {
try (var conn = DriverManager.getConnection(rqUrl)) {
query.receive(conn);
}
};
var fj = new FluentJdbcBuilder().connectionProvider(connP).build();
var userDao = new UserDao(L4Db.Main, Fmt, fj, idFn);
var deviceDao = new DeviceDao(L4Db.Main, Fmt, fj, idFn);
var locationDao = new LocationDao(L4Db.Main, Fmt, fj, idFn);
var user = new User();
user.email = "[email protected]";
user.nickName = "Joe";
user = userDao.upsert(user);
user = new User();
user.email = "[email protected]";
user.nickName = "Jane";
user = userDao.upsert(user);
var res = userDao.sql().query()
.batch("update User set nickName = ? where email = ?")
.params(Stream.of(
List.of("JoeLol", "[email protected]"),
List.of("JaneLol", "[email protected]")
)).run();
var device = new Device();
device.number = 4567345;
device.uid = user.uid;
device = deviceDao.upsert(device);
var loc = new Location();
loc.did = device.did;
loc.geoHash8 = "9q4gu1y4";
locationDao.upsert(loc);
try (var conn = DriverManager.getConnection(rqUrl)) {
var dbm = conn.getMetaData();
try (var lol = (L4Rs) dbm.getPrimaryKeys(null, null, "User")) {
lol.result.print(System.out);
}
}
});
it("Queries table metadata", () -> {
var tables = new String[] { "User", "Device", "Location" };
try (var conn = DriverManager.getConnection(rqUrl)) {
for (var table : tables) {
var idx = (L4Rs) conn.getMetaData().getIndexInfo(null, null, table, true, false);
var cols = L4Db.dbGetColumns(table, null, rq);
var pk = L4Db.dbGetPrimaryKeys(table, rq);
var fkImp = L4Db.dbGetImportedKeys(table, rq);
var fkExp = L4Db.dbGetExportedKeys(table, rq);
idx.result.print(System.out);
cols.print(System.out);
pk.print(System.out);
fkImp.print(System.out);
fkExp.print(System.out);
}
}
});
}
}
}