use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testQueryWithGroupBy.
public void testQueryWithGroupBy() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
final int count = 3 * 10;
ContentValues[] values = new ContentValues[count];
assertNotNull(values);
DatabaseObject object = null;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
assertNotNull(object);
object.setValue("intValue", i);
object.setValue("doubleValue", ((double) i * 2));
object.setValue("textValue", String.format("%04d", i * 3));
values[i * 10 + j] = object.getValues();
}
}
final Template tmpl = object.getTemplate();
long serial = 0;
Uri queryUri = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
Column col = tmpl.getColumn("intValue");
ExpressionToken selection = col.gte(0).and(col.lt(2));
assertNotNull(selection);
OrderingToken groupByToken = col.groupBy();
assertNotNull(groupByToken);
Cursor c = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, object.toSQLProjection(), selection.toString(), null, SortOrderEncoder.encode(groupByToken.toString(), null, null, null));
assertNotNull(c);
assertEquals(2, c.getCount());
assertEquals(true, c.moveToFirst());
int columnIndex = -1;
for (int i = 0; i < 2; i++) {
columnIndex = c.getColumnIndex("intValue");
assertEquals(i, c.getInt(columnIndex));
columnIndex = c.getColumnIndex("doubleValue");
assertEquals(((double) i * 2), c.getDouble(columnIndex));
columnIndex = c.getColumnIndex("textValue");
assertEquals(String.format("%04d", i * 3), c.getString(columnIndex));
c.moveToNext();
}
c.close();
cr.delete(queryUri, null, null);
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testInsertDatabaseObjects.
public void testInsertDatabaseObjects() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
final int count = 10;
ContentValues[] values = new ContentValues[count];
assertNotNull(values);
DatabaseObject object = null;
for (int i = 0; i < 10; i++) {
object = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
assertNotNull(object);
object.setValue(SampleObject2.COLUMN_LAT, 0.1 * i);
object.setValue(SampleObject2.COLUMN_LON, 0.2 * i);
object.setValue(SampleObject2.COLUMN_ALT, 0.3 * i);
values[i] = object.getValues();
}
Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
assertNotNull(handler);
SQLiteDatabase sqlDB = null;
Cursor c = null;
sqlDB = handler.getReadableDatabase();
assertNotNull(sqlDB);
c = sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
assertNotNull(c);
assertEquals(10, c.getCount());
assertEquals(true, c.moveToFirst());
for (int i = 0; i < count; i++) {
assertEquals(0.1 * i, c.getDouble(c.getColumnIndex("latitude")));
assertEquals(0.2 * i, c.getDouble(c.getColumnIndex("longitude")));
assertEquals(0.3 * i, c.getDouble(c.getColumnIndex("altitude")));
c.moveToNext();
}
c.close();
sqlDB.delete(DatabaseObject.classToTable(SampleObject2.class), null, null);
sqlDB.close();
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testSimpleQuery.
public void testSimpleQuery() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
final int count = 10;
ContentValues[] values = new ContentValues[count];
assertNotNull(values);
DatabaseObject object = null;
for (int i = 0; i < count; i++) {
object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
assertNotNull(object);
object.setValue("intValue", i);
object.setValue("doubleValue", ((double) i * 2));
object.setValue("textValue", String.format("%04d", i * 3));
values[i] = object.getValues();
}
final Template tmpl = object.getTemplate();
long serial = 0;
Uri queryUri = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
Column col = tmpl.getColumn("intValue");
ExpressionToken selection = col.gt(5).and(col.lt(9));
assertNotNull(selection);
Cursor c = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, object.toSQLProjection(), selection.toString(), null, null);
assertNotNull(c);
assertEquals(3, c.getCount());
assertEquals(true, c.moveToFirst());
int columnIndex = -1;
for (int i = 6; i <= 8; i++) {
columnIndex = c.getColumnIndex("intValue");
assertEquals(i, c.getInt(columnIndex));
columnIndex = c.getColumnIndex("doubleValue");
assertEquals(((double) i * 2), c.getDouble(columnIndex));
columnIndex = c.getColumnIndex("textValue");
assertEquals(String.format("%04d", i * 3), c.getString(columnIndex));
c.moveToNext();
}
c.close();
cr.delete(queryUri, null, null);
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testQueryWithOrderBy.
public void testQueryWithOrderBy() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
final int count = 3 * 10;
ContentValues[] values = new ContentValues[count];
assertNotNull(values);
DatabaseObject object = null;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
assertNotNull(object);
object.setValue("intValue", j);
object.setValue("doubleValue", ((double) j * 2));
object.setValue("textValue", String.format("%04d", j * 3));
values[i * 10 + j] = object.getValues();
}
}
final Template tmpl = object.getTemplate();
long serial = 0;
Uri queryUri = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
Column col = tmpl.getColumn("intValue");
OrderingToken orderByToken = null;
Cursor c = null;
int columnIndex = -1;
orderByToken = col.orderByAscending();
assertNotNull(orderByToken);
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, object.toSQLProjection(), null, null, SortOrderEncoder.encode(null, null, orderByToken.toString(), null));
assertNotNull(c);
assertEquals(30, c.getCount());
assertEquals(true, c.moveToFirst());
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 3; i++) {
columnIndex = c.getColumnIndex("intValue");
assertEquals(j, c.getInt(columnIndex));
columnIndex = c.getColumnIndex("doubleValue");
assertEquals(((double) j * 2), c.getDouble(columnIndex));
columnIndex = c.getColumnIndex("textValue");
assertEquals(String.format("%04d", j * 3), c.getString(columnIndex));
c.moveToNext();
}
}
c.close();
orderByToken = col.orderByDescending();
assertNotNull(orderByToken);
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, object.toSQLProjection(), null, null, SortOrderEncoder.encode(null, null, orderByToken.toString(), null));
assertNotNull(c);
assertEquals(30, c.getCount());
assertEquals(true, c.moveToFirst());
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 3; i++) {
columnIndex = c.getColumnIndex("intValue");
assertEquals((9 - j), c.getInt(columnIndex));
columnIndex = c.getColumnIndex("doubleValue");
assertEquals(((double) (9 - j) * 2), c.getDouble(columnIndex));
columnIndex = c.getColumnIndex("textValue");
assertEquals(String.format("%04d", (9 - j) * 3), c.getString(columnIndex));
c.moveToNext();
}
}
c.close();
cr.delete(queryUri, null, null);
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testDeleteAllDatabaseObjects.
public void testDeleteAllDatabaseObjects() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
final int count = 10;
ContentValues[] values = new ContentValues[count];
assertNotNull(values);
DatabaseObject object = null;
for (int i = 0; i < count; i++) {
object = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
assertNotNull(object);
object.setValue(SampleObject2.COLUMN_LAT, 0.1 * i);
object.setValue(SampleObject2.COLUMN_LON, 0.2 * i);
object.setValue(SampleObject2.COLUMN_ALT, 0.3 * i);
values[i] = object.getValues();
}
Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
cr.delete(queryUri, null, null);
DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
assertNotNull(handler);
SQLiteDatabase sqlDB = null;
sqlDB = handler.getReadableDatabase();
assertNotNull(sqlDB);
boolean catched = false;
try {
sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
catched = false;
} catch (SQLException e) {
e.printStackTrace();
catched = true;
}
sqlDB.close();
assertTrue(catched);
}
Aggregations