public void testCreateAndWriteData() throws Exception {
if (proceedWithUnitTest()) {
final MongoDbDataContext dc = new MongoDbDataContext(db);
final Schema defaultSchema = dc.getDefaultSchema();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
Table[] tables = defaultSchema.getTables();
for (Table table : tables) {
callback.deleteFrom(table).execute();
}
}
});
assertEquals(0, defaultSchema.getTableCount());
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
Table table = callback.createTable(defaultSchema, "some_entries").withColumn("foo").withColumn("bar")
.withColumn("baz").withColumn("list").execute();
callback.insertInto(table).value("foo", 1).value("bar", "hello").execute();
callback.insertInto(table).value("foo", 2).value("bar", "world").execute();
callback.insertInto(table).value("foo", 3).value("bar", "hi").execute();
Map<String, Object> nestedObj = new HashMap<String, Object>();
nestedObj.put("foo", "bar");
nestedObj.put("123", 456);
callback.insertInto(table).value("foo", 4).value("bar", "there").value("baz", nestedObj)
.value("list", Arrays.asList(1, 2, 3)).execute();
}
});
DataSet dataSet;
assertEquals(1, defaultSchema.getTableCount());
// "Pure" SELECT COUNT(*) query
dataSet = dc.query().from("some_entries").selectCount().execute();
dataSet.close();
assertTrue(dataSet.next());
assertEquals(1, dataSet.getSelectItems().length);
assertEquals(SelectItem.getCountAllItem(), dataSet.getSelectItems()[0]);
assertEquals(4l, dataSet.getRow().getValue(SelectItem.getCountAllItem()));
assertFalse(dataSet.next());
assertEquals(InMemoryDataSet.class, dataSet.getClass());
// A conditional SELECT COUNT(*) query
dataSet = dc.query().from("some_entries").selectCount().where("foo").greaterThan(2).execute();
dataSet.close();
assertTrue(dataSet.next());
assertEquals(1, dataSet.getSelectItems().length);
assertEquals(SelectItem.getCountAllItem(), dataSet.getSelectItems()[0]);
assertEquals(2l, dataSet.getRow().getValue(SelectItem.getCountAllItem()));
assertFalse(dataSet.next());
assertEquals(InMemoryDataSet.class, dataSet.getClass());
// Select columns
dataSet = dc.query().from("some_entries").select("foo").and("bar").and("baz").and("list").execute();
assertTrue(dataSet.next());
assertEquals("Row[values=[1, hello, null, null]]", dataSet.getRow().toString());
assertTrue(dataSet.next());
assertEquals("Row[values=[2, world, null, null]]", dataSet.getRow().toString());
assertTrue(dataSet.next());
assertEquals("Row[values=[3, hi, null, null]]", dataSet.getRow().toString());
assertTrue(dataSet.next());
assertEquals("Row[values=[4, there, {123=456, foo=bar}, [ 1 , 2 , 3]]]", dataSet.getRow().toString());
assertFalse(dataSet.next());
dataSet.close();
assertEquals(MongoDbDataSet.class, dataSet.getClass());
// delete some records
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.deleteFrom("some_entries").where("foo").greaterThan(2).where("baz").isNotNull().execute();
}
});
dataSet = dc.query().from("some_entries").select("foo").execute();
assertTrue(dataSet.next());
assertEquals("Row[values=[1]]", dataSet.getRow().toString());
assertTrue(dataSet.next());
assertEquals("Row[values=[2]]", dataSet.getRow().toString());
assertTrue(dataSet.next());
assertEquals("Row[values=[3]]", dataSet.getRow().toString());
assertFalse(dataSet.next());
dataSet.close();
assertEquals(MongoDbDataSet.class, dataSet.getClass());
// drop the collection
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.dropTable("some_entries").execute();
}
});