Search in sources :

Example 6 with Context

use of com.rdbcache.helpers.Context in project rdbcache by rdbcache.

the class QueryTest method insertTest.

@Test
public void insertTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        String json = "{\n" + "    \"email\" : \"sam@example.com\",\n" + "    \"name\" : \"Sam W.\",\n" + "    \"dob\" : \"1975-08-12\"\n" + "  }";
        Map<String, Object> map1 = Utils.toMap(json);
        KvPair pair = new KvPair("*", map1);
        pairs.setPair(pair);
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifInsertOk());
        assertTrue(query.executeInsert(false, false));
        // System.out.println(Utils.toJsonMap(pairs));
        Map<String, Object> map2 = pair.getData();
        assertEquals("4", map2.get("id"));
        map2.remove("id");
        assertEquals(map1, map2);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Example 7 with Context

use of com.rdbcache.helpers.Context in project rdbcache by rdbcache.

the class QueryTest method deleteTest.

@Test
public void deleteTest() {
    try {
        Context context = new Context();
        KvPairs pairs = new KvPairs();
        AnyKey anyKey = new AnyKey();
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setExpire("100");
        keyInfo.setTable("user_table");
        String json1 = "{\"table\":\"user_table\",\"conditions\":{\"id\":{\"=\":[\"3\"]}}}";
        QueryInfo queryInfo1 = Utils.toPojo(Utils.toMap(json1), QueryInfo.class);
        keyInfo.setQuery(queryInfo1);
        anyKey.setKeyInfo(keyInfo);
        Query query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertTrue(query.executeSelect());
        // System.out.println(Utils.toJsonMap(pairs));
        assertEquals(1, pairs.size());
        String key = pairs.getPair().getId();
        KvPair pair = new KvPair(key);
        pairs.setPair(pair);
        KeyInfo keyInfo2 = new KeyInfo();
        keyInfo2.setExpire("100");
        keyInfo2.setTable("user_table");
        keyInfo2.setClause("id = ?");
        keyInfo2.setParams(Arrays.asList("3"));
        anyKey.setKeyInfo(keyInfo2);
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifDeleteOk());
        assertTrue(query.executeDelete());
        pairs.clear();
        query = new Query(context, jdbcTemplate, pairs, anyKey);
        assertTrue(query.ifSelectOk());
        assertFalse(query.executeSelect());
        assertEquals(0, pairs.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : Context(com.rdbcache.helpers.Context) AnyKey(com.rdbcache.helpers.AnyKey) KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) KvPairs(com.rdbcache.helpers.KvPairs) Test(org.junit.Test)

Aggregations

Context (com.rdbcache.helpers.Context)7 KeyInfo (com.rdbcache.models.KeyInfo)7 KvPair (com.rdbcache.models.KvPair)6 Test (org.junit.Test)6 AnyKey (com.rdbcache.helpers.AnyKey)5 KvPairs (com.rdbcache.helpers.KvPairs)5 DbaseOps (com.rdbcache.services.DbaseOps)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1