Java Code Examples for com.yahoo.squidb.sql.Query

The following examples show how to use com.yahoo.squidb.sql.Query. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRawSelection() {
    String selection = COL_LUCKY_NUMBER + " > ? AND " + COL_IS_HAPPY + " != ?";
    String[] selectionArgs = new String[]{"50", "0"};
    ContentProviderQueryBuilder builder = getBuilder();
    Query query = builder.setDataSource(TestModel.TABLE).build(null, selection, selectionArgs, null);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 2, "50", "0");

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(1, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model2, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 2
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRawOrderBy() {
    String sortOrder = COL_GIVEN_NAME + " ASC";
    ContentProviderQueryBuilder builder = getBuilder();
    Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, sortOrder);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 0);

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(3, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model3, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model2, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model1, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 3
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testDefaultOrderBy() {
    ContentProviderQueryBuilder builder = getBuilder();
    builder.setDefaultOrder(TestModel.LUCKY_NUMBER.desc());
    Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, null);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 0);

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(3, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model2, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model1, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model3, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 4
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 6 votes vote down vote up
private void testJsonInsertReplaceSetInternal(int type, String path, Object value, String expectedResult) {
    String jsonString = "{\"a\":2,\"c\":4}";
    Function<String> func = null;
    switch (type) {
        case 0:
            func = JSONFunctions.jsonInsert(jsonString, path, value);
            break;
        case 1:
            func = JSONFunctions.jsonReplace(jsonString, path, value);
            break;
        case 2:
            func = JSONFunctions.jsonSet(jsonString, path, value);
            break;
        default:
            fail("Unsupported insert/replace/set type " + type);
    }
    String result = database.simpleQueryForString(Query.select(func));
    assertEquals(expectedResult, result);
}
 
Example 5
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testJsonGroupArray() {
    testJsonFunction(new Runnable() {
        @Override
        public void run() {
            Thing thing = new Thing();
            for (int i = 0; i < 5; i++) {
                thing.setFoo(Integer.toString(i));
                database.createNew(thing);
            }
            Function<String> groupArray = JSONFunctions.jsonGroupArray(Thing.FOO);
            String result = database.simpleQueryForString(Query.select(groupArray).from(Thing.TABLE));
            try {
                JSONArray resultArray = new JSONArray(result);
                Set<String> resultValues = new HashSet<>();
                for (int i = 0; i < resultArray.length(); i++) {
                    resultValues.add(resultArray.getString(i));
                }
                assertEquals(5, resultValues.size());
                assertTrue(resultValues.containsAll(Arrays.asList("0", "1", "2", "3", "4")));
            } catch (JSONException e) {
                fail("JSONException: " + e.getMessage());
            }
        }
    }, JSONFunctions.JSON1_GROUP_FUNCTIONS_VERSION);
}
 
Example 6
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testJsonGroupObject() {
    testJsonFunction(new Runnable() {
        @Override
        public void run() {
            Thing thing = new Thing();
            for (int i = 0; i < 5; i++) {
                thing.setFoo(Integer.toString(i))
                        .setBar(i * 2);
                database.createNew(thing);
            }
            Function<String> groupObject = JSONFunctions.jsonGroupObject(Thing.FOO, Thing.BAR);
            String result = database.simpleQueryForString(Query.select(groupObject).from(Thing.TABLE));
            try {
                JSONObject resultObject = new JSONObject(result);
                assertEquals(5, resultObject.length());
                for (int i = 0; i < 5; i++) {
                    assertEquals(i * 2, resultObject.getInt(Integer.toString(i)));
                }
            } catch (JSONException e) {
                fail("JSONException: " + e.getMessage());
            }
        }
    }, JSONFunctions.JSON1_GROUP_FUNCTIONS_VERSION);
}
 
Example 7
Source Project: squidb   Source File: ModelTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testEnumProperties() {
    final TestEnum enumValue = TestEnum.APPLE;
    final String enumAsString = enumValue.name();
    TestModel model = new TestModel()
            .setFirstName("A")
            .setLastName("Z")
            .setBirthday(System.currentTimeMillis())
            .setSomeEnum(enumValue);

    ValuesStorage setValues = model.getSetValues();
    assertEquals(enumAsString, setValues.get(TestModel.SOME_ENUM.getName()));

    database.persist(model);

    SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select()
            .where(TestModel.SOME_ENUM.eq(TestEnum.APPLE)));
    assertEquals(1, cursor.getCount());
    assertTrue(cursor.moveToFirst());
    assertEquals(enumAsString, cursor.get(TestModel.SOME_ENUM));

    TestModel fromDatabase = new TestModel(cursor);
    assertEquals(enumValue, fromDatabase.getSomeEnum());
}
 
Example 8
Source Project: squidb   Source File: SquidRecyclerAdapterTest.java    License: Apache License 2.0 6 votes vote down vote up
private void testRecyclerAdapterInternal(LongProperty idProperty, RecyclerAdapterTest test) {
    Query query = Query.select(TestModel.PROPERTIES)
            .orderBy(TestModel.BIRTHDAY.asc())
            .limit(2);
    if (idProperty != null) {
        query.selectMore(idProperty);
    }
    SquidCursor<TestModel> cursor = database.query(TestModel.class, query);

    TestRecyclerAdapter adapter = new TestRecyclerAdapter(idProperty);
    adapter.changeCursor(cursor);
    try {
        test.testRecyclerAdapter(adapter);
    } finally {
        cursor.close();
    }
}
 
Example 9
Source Project: squidb   Source File: SquidCursorAdapterTest.java    License: Apache License 2.0 6 votes vote down vote up
private void testCursorAdapterInternal(AbstractModel model, LongProperty idColumn, Query query,
        CursorAdapterTest test) {
    TestAdapter adapter;
    if (idColumn == null) {
        adapter = new TestAdapter(model);
    } else {
        adapter = new TestAdapter(model, idColumn);
    }

    SquidCursor<? extends AbstractModel> cursor = database.query(model.getClass(), query);
    try {
        adapter.swapCursor(cursor);
        test.testCursorAdapter(adapter);
    } finally {
        cursor.close();
    }
}
 
Example 10
Source Project: squidb   Source File: SquidCursorAdapterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testSwapCursorDoesNotCloseOldCursor() {
    TestAdapter adapter = new TestAdapter(new TestModel());

    SquidCursor<TestModel> cursor1 = database.query(TestModel.class, Query.select());
    try {
        adapter.swapCursor(cursor1);
        SquidCursor<TestModel> cursor2 = database.query(TestModel.class, Query.select().where(TestModel.ID.eq(1)));
        try {
            SquidCursor<?> swappedCursor = adapter.swapCursor(cursor2);
            assertFalse(swappedCursor.isClosed());
        } finally {
            adapter.swapCursor(null);
            cursor2.close();
        }
    } finally {
        cursor1.close();
    }
}
 
Example 11
Source Project: squidb   Source File: ContentProviderQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Build a {@link Query} combining this object's internal state with the arguments passed. If a
 * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the
 * sortOrder is null or empty, the default order will be used (if one was set).
 *
 * @param projection the raw column names to be selected
 * @param selection a raw selection string
 * @param selectionArgs array of strings which substitute replaceable arguments in the selection string
 * @param sortOrder a raw ordering clause
 * @return a {@link Query} using the projection, selection, selection args, and sort order
 */
public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Query query = Query.select(computeProjection(projection)).from(dataSource);
    boolean hasUserSelection = !SqlUtils.isEmpty(selection);
    if (hasUserSelection) {
        query.where(Criterion.fromRawSelection(selection, selectionArgs));
    }
    if (!SqlUtils.isEmpty(sortOrder)) {
        query.orderBy(Order.fromExpression(sortOrder));
    } else if (defaultOrder != null && defaultOrder.length > 0) {
        query.orderBy(defaultOrder);
    }
    if (strictMode && hasUserSelection) {
        query.requestValidation();
    }
    return query;
}
 
Example 12
Source Project: squidb   Source File: SquidDatabase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Count the number of rows matching a given {@link Criterion}. Use null to count all rows.
 *
 * @param modelClass the model class corresponding to the table
 * @param criterion the criterion to match
 * @return the number of rows matching the given criterion
 */
public int count(Class<? extends AbstractModel> modelClass, Criterion criterion) {
    Property.IntegerProperty countProperty = Property.IntegerProperty.countProperty();
    Query query = Query.select(countProperty);
    if (criterion != null) {
        query.where(criterion);
    }
    query = inferTableForQuery(modelClass, query);
    CompiledStatement compiled = query.compile(getCompileContext());
    acquireNonExclusiveLock();
    try {
        return (int) getDatabase().simpleQueryForLong(compiled.sql, compiled.sqlArgs);
    } finally {
        releaseNonExclusiveLock();
    }
}
 
Example 13
@Override
public long readWholeData() throws SQLException
{
    long start = System.nanoTime();
    SquidCursor<Message> query = myDatabase.query(Message.class, Query.select());
    Message message = new Message();
    while(query.moveToNext())
    {
        message.readPropertiesFromCursor(query);
    }
    return System.nanoTime() - start;
}
 
Example 14
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testEmptyProjectionWithMapUsesDefault() {
    final Field<?>[] expectedProjection = new Field<?>[]{
            TestModel.ID,
            TestModel.FIRST_NAME.as(COL_GIVEN_NAME),
            TestModel.LAST_NAME.as(COL_SURNAME),
            TestModel.LUCKY_NUMBER,
            TestModel.IS_HAPPY
    };

    ContentProviderQueryBuilder builder = getBuilder();
    Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, null);
    assertEquals(Arrays.asList(expectedProjection), query.getFields());
}
 
Example 15
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testInvalidProjectionIgnored() {
    ContentProviderQueryBuilder builder = getBuilder();
    final String IGNORE = "foo";
    String[] projection = {IGNORE, COL_GIVEN_NAME, COL_SURNAME, COL_LUCKY_NUMBER};
    Query query = builder.setDataSource(TestModel.TABLE).build(projection, null, null, null);
    List<Field<?>> fields = query.getFields();
    assertEquals(3, fields.size());
    for (int i = 0; i < fields.size(); i++) {
        if (IGNORE.equals(fields.get(i).getName())) {
            fail("Invalid projection not ignored!");
        }
    }
}
 
Example 16
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testQueryFailsWithException(String selection, String order) {
    ContentProviderQueryBuilder builder = getBuilder();
    builder.setStrict(true);
    final Query query = builder.setDataSource(TestModel.TABLE).build(null, selection, null, order);

    testThrowsRuntimeException(new Runnable() {
        @Override
        public void run() {
            database.query(TestModel.class, query);
        }
    });
}
 
Example 17
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testBuilderFromModel() {
    ContentProviderQueryBuilder builder = new ContentProviderQueryBuilder(TestSubqueryModel.PROPERTIES,
            TestSubqueryModel.SUBQUERY);
    Query query = builder.build(null, null, null, null);
    assertEquals(Arrays.asList(TestSubqueryModel.PROPERTIES), query.getFields());
    assertEquals(TestSubqueryModel.SUBQUERY, query.getTable());
}
 
Example 18
Source Project: squidb   Source File: ContentProviderQueryBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testQueryUsingSubqueryModel() {
    Employee employee1 = new Employee().setName("Big bird");
    Employee employee2 = new Employee().setName("Elmo");
    database.persist(employee1);
    database.persist(employee2);

    ContentProviderQueryBuilder builder = new ContentProviderQueryBuilder(TestSubqueryModel.PROPERTIES,
            TestSubqueryModel.SUBQUERY);
    Query query = builder.build(null, null, null, null);

    SquidCursor<TestSubqueryModel> cursor = null;
    try {
        cursor = database.query(TestSubqueryModel.class, query);
        assertEquals(2, cursor.getCount());

        cursor.moveToFirst();
        TestSubqueryModel model = new TestSubqueryModel(cursor);
        assertEquals(model1.getRowId(), model.getTestModelId().longValue());
        assertEquals(employee1.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(model1.getFirstName(), model.getTestName());
        assertEquals(employee1.getName(), model.getEmployeeName());
        assertEquals(employee1.getName().toUpperCase(), model.getUppercaseName());

        cursor.moveToNext();
        model.readPropertiesFromCursor(cursor);
        assertEquals(model2.getRowId(), model.getTestModelId().longValue());
        assertEquals(employee2.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(model2.getFirstName(), model.getTestName());
        assertEquals(employee2.getName(), model.getEmployeeName());
        assertEquals(employee2.getName().toUpperCase(), model.getUppercaseName());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 19
Source Project: squidb   Source File: SquidCursorTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testTypesafeGetter() {
    StringProperty literalString = StringProperty.literal("literalString", "testStr");
    IntegerProperty literalInteger = IntegerProperty.literal(2, "testInt");
    BooleanProperty literalBoolean = BooleanProperty.literal(true, "testBool");

    // Test casting Integer to Boolean
    BooleanProperty castBool = BooleanProperty.literal(false, literalInteger.getName());

    // Test casting Boolean to Integer
    IntegerProperty castInt = IntegerProperty.literal(0, literalBoolean.getName());

    // Test casting Integer to String
    StringProperty castString = StringProperty.literal("", literalInteger.getName());

    Query query = Query.select(literalString, literalInteger, literalBoolean);
    SquidCursor<?> cursor = database.query(null, query);
    try {
        assertTrue(cursor.moveToFirst());

        assertEquals("literalString", cursor.get(literalString));
        assertEquals(2, cursor.get(literalInteger).intValue());
        assertTrue(cursor.get(literalInteger) instanceof Integer);
        assertTrue(cursor.get(literalBoolean));

        assertTrue(cursor.get(castBool));
        assertEquals(1, cursor.get(castInt).intValue());
        assertEquals("2", cursor.get(castString));
    } finally {
        cursor.close();
    }
}
 
Example 20
Source Project: squidb   Source File: SquidCursorTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testMultiWindowCursor() {
    // Create a cursor that should contain more than 2MB of data (the default CursorWindow size)
    // to make sure that windowing is working correctly
    Thing thing = new Thing();
    int numDigits = 500;
    String formatString = "%0" + numDigits + "d";
    int numRowsToInsert = 5 * 1024 * 1024 / numDigits;

    database.beginTransaction();
    try {
        for (int i = 0; i < numRowsToInsert; i++) {
            thing.setFoo(String.format(formatString, i));
            database.createNew(thing);
        }
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }

    assertEquals(numRowsToInsert, database.countAll(Thing.class));
    SquidCursor<Thing> cursor = database.query(Thing.class, Query.select(Thing.FOO));
    try {
        assertEquals(numRowsToInsert, cursor.getCount());

        // Scan cursor twice so we know it can correctly jump back to the first window
        scanCursor(cursor, formatString);
        scanCursor(cursor, formatString);
    } finally {
        cursor.close();
    }
}
 
Example 21
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testBasicSelectFromView() {
    SquidCursor<TestViewModel> cursor = null;
    try {
        cursor = database.query(TestViewModel.class, Query.select());
        assertEquals(2, cursor.getCount());
        cursor.moveToFirst();
        TestViewModel model = new TestViewModel(cursor);
        assertEquals(t1.getRowId(), model.getTestModelId().longValue());
        assertEquals(e1.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t1.getFirstName(), model.getTestName());
        assertEquals(t1.getSomeEnum(), model.getTestEnum());
        assertEquals(e1.getName(), model.getEmployeeName());
        assertEquals(e1.getName().toUpperCase(), model.getUppercaseName());

        cursor.moveToNext();
        model.readPropertiesFromCursor(cursor);

        assertEquals(t2.getRowId(), model.getTestModelId().longValue());
        assertEquals(e2.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t2.getFirstName(), model.getTestName());
        assertEquals(t2.getSomeEnum(), model.getTestEnum());
        assertEquals(e2.getName(), model.getEmployeeName());
        assertEquals(e2.getName().toUpperCase(), model.getUppercaseName());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 22
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testBasicSelectFromSubquery() {
    SquidCursor<TestSubqueryModel> cursor = null;
    try {
        cursor = database.query(TestSubqueryModel.class, Query.select().from(TestSubqueryModel.SUBQUERY));
        assertEquals(2, cursor.getCount());
        cursor.moveToFirst();
        TestSubqueryModel model = new TestSubqueryModel(cursor);
        assertEquals(t1.getRowId(), model.getTestModelId().longValue());
        assertEquals(e1.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t1.getFirstName(), model.getTestName());
        assertEquals(t1.getSomeEnum(), model.getTestEnum());
        assertEquals(e1.getName(), model.getEmployeeName());
        assertEquals(e1.getName().toUpperCase(), model.getUppercaseName());

        cursor.moveToNext();
        model.readPropertiesFromCursor(cursor);

        assertEquals(t2.getRowId(), model.getTestModelId().longValue());
        assertEquals(e2.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t2.getFirstName(), model.getTestName());
        assertEquals(e2.getName(), model.getEmployeeName());
        assertEquals(e2.getName().toUpperCase(), model.getUppercaseName());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 23
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testProjectionMapAliasing() {
    assertEquals("blahTestName", TestSubqueryModel.TEST_NAME.getName());
    assertEquals("blahName", TestSubqueryModel.EMPLOYEE_NAME.getName());
    assertEquals("luckyNumber", TestSubqueryModel.TEST_LUCKY_NUMBER.getName());
    assertEquals("blahEnum", TestSubqueryModel.TEST_ENUM.getName());
    assertEquals("uppercase_name", TestSubqueryModel.UPPERCASE_NAME.getName());

    SquidCursor<TestSubqueryModel> cursor = null;
    try {
        cursor = database.query(TestSubqueryModel.class, Query.select().from(TestSubqueryModel.SUBQUERY));
        cursor.moveToFirst();
        TestSubqueryModel model = new TestSubqueryModel(cursor);
        // queried model should have "uppercase_name"
        assertEquals(e1.getName().toUpperCase(), model.getUppercaseName());

        TestModel testModel = model.mapToModel(new TestModel());
        Employee employeeModel = model.mapToModel(new Employee());
        assertEquals(t1.getFirstName(), testModel.getFirstName());
        assertEquals(e1.getName(), employeeModel.getName());
        assertEquals(t1.getLuckyNumber(), testModel.getLuckyNumber());
        assertEquals(t1.getSomeEnum(), testModel.getSomeEnum());
        // neither mapped model should have "uppercase_name"
        assertFalse(t1.containsValue(TestSubqueryModel.UPPERCASE_NAME));
        assertFalse(e1.containsValue(TestSubqueryModel.UPPERCASE_NAME));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 24
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testModelMapping() {
    SquidCursor<TestViewModel> cursor = null;
    try {
        cursor = database.query(TestViewModel.class, Query.select());
        cursor.moveToFirst();
        TestViewModel model = new TestViewModel(cursor);

        TestModel testModel = new TestModel();
        model.mapToModel(testModel);

        assertEquals(t1.getRowId(), testModel.getRowId());
        assertEquals(t1.getFirstName(), testModel.getFirstName());
        assertEquals(t1.getSomeEnum(), testModel.getSomeEnum());
        assertFalse(testModel.containsValue(Employee.NAME));
        assertFalse(testModel.containsValue(TestViewModel.UPPERCASE_NAME));

        Employee employee = new Employee();
        model.mapToModel(employee);

        assertEquals(e1.getRowId(), employee.getRowId());
        assertEquals(e1.getName(), employee.getName());
        assertFalse(employee.containsValue(TestModel.FIRST_NAME));
        assertFalse(employee.containsValue(TestViewModel.UPPERCASE_NAME));
        assertFalse(employee.containsValue(TestModel.SOME_ENUM));

        List<AbstractModel> allSources = model.mapToSourceModels();
        assertEquals(2, allSources.size());
        AbstractModel source1 = allSources.get(0);
        AbstractModel source2 = allSources.get(1);
        assertTrue(source1 instanceof TestModel || source2 instanceof TestModel);
        assertTrue(source1 instanceof Employee || source2 instanceof Employee);
        assertFalse(source1.getClass().equals(source2.getClass()));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 25
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testViewlessViewModel() {
    SquidCursor<ViewlessViewModel> cursor = null;
    try {
        cursor = database.query(ViewlessViewModel.class, Query.select(ViewlessViewModel.PROPERTIES)
                .from(TestModel.TABLE)
                .join(Join.left(Employee.TABLE, TestModel.ID.eq(Employee.ID)))
                .where(TestModel.FIRST_NAME.gt("S"))
                .orderBy(TestModel.FIRST_NAME.asc()));

        assertEquals(2, cursor.getCount());
        cursor.moveToFirst();

        ViewlessViewModel model = new ViewlessViewModel(cursor);
        assertEquals(t1.getRowId(), model.getTestModelId().longValue());
        assertEquals(e1.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t1.getFirstName(), model.getTestName());
        assertEquals(e1.getName(), model.getEmployeeName());
        assertEquals(e1.getName().toUpperCase(), model.getUppercaseName());

        cursor.moveToNext();
        model.readPropertiesFromCursor(cursor);
        assertEquals(t2.getRowId(), model.getTestModelId().longValue());
        assertEquals(e2.getRowId(), model.getEmployeeModelId().longValue());
        assertEquals(t2.getFirstName(), model.getTestName());
        assertEquals(e2.getName(), model.getEmployeeName());
        assertEquals(e2.getName().toUpperCase(), model.getUppercaseName());

    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 26
Source Project: squidb   Source File: ViewModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testViewlessViewModelMapping() {
    SquidCursor<ViewlessViewModel> cursor = null;
    try {
        cursor = database.query(ViewlessViewModel.class, Query.select(ViewlessViewModel.PROPERTIES)
                .from(TestModel.TABLE)
                .join(Join.left(Employee.TABLE, TestModel.ID.eq(Employee.ID)))
                .where(TestModel.FIRST_NAME.gt("S"))
                .orderBy(TestModel.FIRST_NAME.asc()));
        cursor.moveToFirst();
        ViewlessViewModel model = new ViewlessViewModel(cursor);

        TestModel testModel = new TestModel();
        model.mapToModel(testModel);

        assertEquals(t1.getRowId(), testModel.getRowId());
        assertEquals(t1.getFirstName(), testModel.getFirstName());
        assertFalse(testModel.containsValue(Employee.NAME));
        assertFalse(testModel.containsValue(TestViewModel.UPPERCASE_NAME));

        Employee employee = new Employee();
        model.mapToModel(employee);

        assertEquals(e1.getRowId(), employee.getRowId());
        assertEquals(e1.getName(), employee.getName());
        assertFalse(employee.containsValue(TestModel.FIRST_NAME));
        assertFalse(employee.containsValue(TestViewModel.UPPERCASE_NAME));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 27
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isJson1ExtensionEnabled() {
    try {
        return database.simpleQueryForLong(Query.select(JSONFunctions.jsonValid("{ \"a\" : \"b\" }"))) != 0;
    } catch (RuntimeException e) {
        Logger.d("JSONFunctionTest", "JSON1 extension not available", e);
        return false;
    }
}
 
Example 28
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testJson() {
    testJsonFunction(new Runnable() {
        @Override
        public void run() {
            Function<String> json = JSONFunctions.json(" { \"this\" : \"is\", \"a\": [ \"test\" ] } ");
            String minified = database.simpleQueryForString(Query.select(json));
            assertEquals("{\"this\":\"is\",\"a\":[\"test\"]}", minified);
        }
    });
}
 
Example 29
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testMalformedJsonThrowsError() {
    testJsonFunction(new Runnable() {
        @Override
        public void run() {
            testThrowsRuntimeException(new Runnable() {
                @Override
                public void run() {
                    Function<String> json = JSONFunctions.json(" { \"this\" : \"is\", \"a\": [ \"test\"  } ");
                    String minified = database.simpleQueryForString(Query.select(json));
                    assertNull(minified);
                }
            });
        }
    });
}
 
Example 30
Source Project: squidb   Source File: JSONFunctionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testJsonArray() {
    testJsonFunction(new Runnable() {
        @Override
        public void run() {
            Function<String> jsonArray = JSONFunctions.jsonArray(1, 1, 2, 3, 5, 8, 13);
            String result = database.simpleQueryForString(Query.select(jsonArray));
            assertEquals("[1,1,2,3,5,8,13]", result);
        }
    });
}