Skip to content

Commit

Permalink
A simple DB client test
Browse files Browse the repository at this point in the history
  • Loading branch information
rchodava committed Oct 6, 2016
1 parent 841b39e commit a2cc032
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@
<artifactId>jackson-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package foundation.stack.datamill.db.impl;

import foundation.stack.datamill.db.DatabaseClient;
import foundation.stack.datamill.reflection.Outline;
import foundation.stack.datamill.reflection.OutlineBuilder;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class DatabaseClientTest {
private static class Quark {
private String name;
private int spin;

public String getName() {
return name;
}

public Quark setName(String name) {
this.name = name;
return this;
}

public int getSpin() {
return spin;
}

public Quark setSpin(int spin) {
this.spin = spin;
return this;
}
}

@Test
public void queries() {
DatabaseClient client = new DatabaseClient("jdbc:hsqldb:mem:test");
Outline<Quark> outline = new OutlineBuilder().build(Quark.class);

client.update("create table quarks(name varchar(64), spin integer)", 0)
.count()
.toBlocking()
.last();

client.insertInto(outline).row(rb -> rb
.put(outline.member(m -> m.getName()), "up")
.put(outline.member(m -> m.getSpin()), 1)
.build())
.count()
.toBlocking()
.last();

List<Quark> quarks = client.selectAll().from(outline).all().getAs(r -> outline.wrap(new Quark())
.set(p -> p.getName(), r.column(outline.member(m -> m.getName())))
.set(p -> p.getSpin(), r.column(outline.member(m -> m.getSpin())))
.unwrap())
.toBlocking().last();

assertEquals(1, quarks.size());
assertEquals("up", quarks.get(0).getName());
assertEquals(1, quarks.get(0).getSpin());

Quark quark = client.selectAll().from(outline).all().firstAs(r -> outline.wrap(new Quark())
.set(p -> p.getName(), r.column(outline.member(m -> m.getName())))
.set(p -> p.getSpin(), r.column(outline.member(m -> m.getSpin())))
.unwrap())
.toBlocking().last();

assertEquals(1, quarks.size());
assertEquals("up", quarks.get(0).getName());
assertEquals(1, quarks.get(0).getSpin());
}
}

0 comments on commit a2cc032

Please sign in to comment.