@Test public void testDeleteByRecords() { Connection db = getConnection(); long app = getAppId(); try { Record record; List<Record> records = new ArrayList<Record>(); List<Long> ids = insertRecords(); ResultSet rs = db.select(app, ""); if (rs.size() != 3) { fail("invalid count"); } while (rs.next()) { record = new Record(); record.setId(rs.getId()); record.setRevision(rs.getRevision()); records.add(record); } db.deleteByRecords(app, records); rs = db.select(app, ""); if (rs.size() != 0) { fail("failed to delete"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testUpdateByRecord() { Connection db = getConnection(); long app = getAppId(); try { Record record = new Record(); List<Long> ids = insertRecords(); long id = ids.get(1); ResultSet rs = db.select(app, "Record_number = " + id); if (rs.size() != 1) { fail("invalid count"); } rs.next(); record.setId(rs.getId()); record.setRevision(rs.getRevision()); record.setString("Single_line_text", "hoge"); db.updateByRecord(app, record); rs = db.select(app, "Single_line_text = \"hoge\""); if (rs.size() != 1) { fail("failed to update"); } rs.next(); if (rs.getId() != id) { fail("invalid id"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testDeleteByRecord() { Connection db = getConnection(); long app = getAppId(); try { Record record = new Record(); List<Long> ids = insertRecords(); long id = ids.get(1); ResultSet rs = db.select(app, "Record_number = " + id); if (rs.size() != 1) { fail("invalid count"); } rs.next(); record.setId(rs.getId()); record.setRevision(rs.getRevision()); db.deleteByRecord(app, record); rs = db.select(app, ""); if (rs.size() != 2) { fail("failed to delete"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testBulkRequest() { Connection db = getConnection(); long app = getAppId(); try { Record record; List<Record> records = new ArrayList<Record>(); List<Long> ids = insertRecords(); ResultSet rs = db.select(app, "order by Record_number asc"); if (rs.size() != 3) { fail("invalid count"); } int i = 0; while (rs.next()) { record = new Record(); record.setId(rs.getId()); record.setRevision(rs.getRevision()); record.setString("Single_line_text", "hoge" + i); records.add(record); i++; } BulkRequest bulk = new BulkRequest(); bulk.updateByRecords(app, records); records = new ArrayList<Record>(); record = new Record(); record.setString("Single_line_text", "fuga"); records.add(record); record = new Record(); record.setString("Single_line_text", "piyo"); records.add(record); bulk.insert(app, records); bulk.delete(app, ids.get(1)); db.bulkRequest(bulk); rs = db.select(app, "order by Record_number asc"); if (rs.size() != 4) { fail("invalid count"); } i = 0; String[] values = {"hoge0", "hoge2", "fuga", "piyo"}; while (rs.next()) { if (!rs.getString("Single_line_text").equals(values[i])) { fail("invalid value:" + values[i]); } i++; } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testSelect() { Connection db = getConnection(); long app = getAppId(); try { ArrayList<Record> records = new ArrayList<Record>(); Record record; record = new Record(); record.setString("Single_line_text", "foo"); records.add(record); record = new Record(); record.setString("Single_line_text", "bar"); records.add(record); record = new Record(); record.setString("Single_line_text", "foo"); records.add(record); record = new Record(); record.setString("Single_line_text", "bar"); records.add(record); record = new Record(); record.setString("Single_line_text", "foo"); records.add(record); db.insert(app, records); ResultSet rs = db.select(app, "Single_line_text = \"foo\""); if (rs.size() != 3) { fail("invalid count " + rs.size()); } } catch (Exception e) { fail("failed to select"); } }
@Test public void testInsertLongRecord() { Connection db = getConnection(); long app = getAppId(); try { Record record; record = new Record(); record.setString("Single_line_text", "foo"); record.setLong("Number", 999); record.setString("Number_0", "999.99"); db.insert(app, record); ResultSet rs = db.select(app, ""); if (rs.size() != 1) { fail("invalid count"); } rs.next(); assert (rs.getString("Single_line_text") == "foo"); assert (rs.getLong("Number") == 999); assert (rs.getString("Number_0") == "999.99"); } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testApiToken() { String domain = System.getenv("DOMAIN"); String apiToken = "lzrxAeavY4RI99c9146xzZq4U7gkOj3uvMTMt5QS"; Connection db = new Connection(domain, apiToken); // db.setProxy("127.0.0.1", 8888); // for fiddler2 // db.setTrustAllHosts(true); long app = getAppId(); try { db.select(app, "Record_number > 0"); } catch (Exception e) { fail("Can not use api token"); } }
@Test public void testDeleteByQuery() { Connection db = getConnection(); long app = getAppId(); try { List<Long> ids = insertRecords(); db.deleteByQuery(app, "Single_line_text = \"foo\""); ResultSet rs = db.select(app, ""); if (rs.size() != 2) { fail("failed to delete"); } } catch (Exception e) { fail("db exception"); } }
@Test public void testDeleteLongListOfLong() { Connection db = getConnection(); long app = getAppId(); try { List<Long> ids = insertRecords(); db.delete(app, ids); ResultSet rs = db.select(app, ""); if (rs.size() != 0) { fail("failed to delete"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testInsertLongListOfRecord() { Connection db = getConnection(); long app = getAppId(); try { insertRecords(); ResultSet rs = db.select(app, ""); if (rs.size() != 3) { fail("invalid count"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testUpdateByQuery() { Connection db = getConnection(); long app = getAppId(); try { Record record; record = new Record(); record.setString("Single_line_text", "hoge"); List<Long> ids = insertRecords(); db.updateByQuery(app, "Single_line_text = \"foo\"", record); ResultSet rs = db.select(app, "Single_line_text = \"hoge\""); if (rs.size() != 1) { fail("failed to update"); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }
@Test public void testDeleteByRecords2() { Connection db = getConnection(); long app = getAppId(); Record record; List<Record> records = new ArrayList<Record>(); ResultSet rs = null; try { List<Long> ids = insertRecords(); rs = db.select(app, ""); if (rs.size() != 3) { fail("invalid count"); } record = new Record(); rs.next(); record.setId(rs.getId()); record.setString("Single_line_text", "hoge"); db.updateByRecord(app, record); } catch (Exception e) { fail("db exception:" + e.getMessage()); } try { rs.first(); while (rs.next()) { record = new Record(); record.setId(rs.getId()); record.setRevision(rs.getRevision()); record.setString("Single_line_text", "hoge"); records.add(record); } db.deleteByRecords(app, records); fail("no conflict"); } catch (Exception e) { } }
@Test public void testInsertCreatorModifier() { Connection db = getConnection(); long app = getAppId(); try { Record record; DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm"); Date date = df.parse("2000-01-01 01:34"); record = new Record(); record.setUser("Created_by", "aono"); record.setUser("Updated_by", "aono"); record.setDateTime("Created_datetime", date); record.setDateTime("Updated_datetime", date); db.insert(app, record); ResultSet rs = db.select(app, ""); if (rs.size() != 1) { fail("invalid count"); } rs.first(); if (!rs.getUser("Created_by").getCode().equals("aono")) { fail("failed to update created by"); } if (!rs.getUser("Updated_by").getCode().equals("aono")) { fail("failed to update updated by"); } if (!rs.getDateTime("Created_datetime").equals(date)) { fail("failed to update created datetime" + rs.getDate("Created_datetime").toString()); } if (!rs.getDateTime("Updated_datetime").equals(date)) { fail("failed to update updated datetime" + rs.getDate("Updated_datetime").toString()); } } catch (Exception e) { fail("db exception:" + e.getMessage()); } }