package dovetaildb.apiservice;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import dovetaildb.bagindex.TrivialBagIndex;
import dovetaildb.dbservice.BagEntry;
import dovetaildb.dbservice.BagEntryFactory;
import dovetaildb.dbservice.BagIndexBridge;
import dovetaildb.dbservice.DbService;
import dovetaildb.dbservice.DbServiceTest;
import dovetaildb.dbservice.DefaultTermEncoder;
import dovetaildb.dbservice.ProcessTransactionMapper;
import dovetaildb.iter.Iter;
import dovetaildb.util.Util;
public abstract class ApiServiceTest extends TestCase {
protected DbService createDbService() {
BagIndexBridge b = new BagIndexBridge();
b.setBagIndexFactory(new BagEntryFactory() {
public BagEntry makeBagEntry(String bagName) {
return new BagEntry(new TrivialBagIndex(), new DefaultTermEncoder(), 0);
}
});
b.setTxnMapper(new ProcessTransactionMapper());
return b;
}
protected abstract ApiService createApi(DbService dbService);
public static Object[] yank(Iter iter) { return DbServiceTest.yank(iter); }
public void checkEmpty(ApiService api) {
assertEquals(0, yank(api.query("nobag", null, null)).length);
assertEquals(0, yank(api.query("people", null, null)).length);
assertEquals(0, yank(api.query("people", Util.literalMap().p("name", Util.literalList().a("!").a("Joe")), null)).length);
}
public void checkTwoInserts(ApiService api) {
assertEquals(2, yank(api.query("people", Util.literalMap(), null)).length);
Object[] rets = yank(api.query("people", Util.literalMap().p("name","Joe"), null));
assertEquals(1, rets.length);
assertEquals("Joe", ((Map)rets[0]).get("name"));
}
@Test
public void testAll() throws Exception {
DbService db = createDbService();
ApiService api1 = createApi(db);
ApiService api2 = createApi(db);
checkEmpty(api1);
Map<String,Object> phil = Util.literalMap().p("name", "Phil").p("age", new Long(31)).p("id", "p1");
Map<String,Object> joe = Util.literalMap().p("name", "Joe") .p("age", new Integer(28)).p("id", "p2");
api1.insert("people", phil);
api1.insert("people", joe);
checkTwoInserts(api1);
checkEmpty(api2);
ApiService api3 = createApi(db);
api1.commit();
checkTwoInserts(api1);
ApiService api4 = createApi(db);
checkTwoInserts(api4);
checkEmpty(api2);
checkEmpty(api3);
}
}