Skip to content

Instantly share code, notes, and snippets.

@abhilater
Last active June 21, 2019 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhilater/e4d3938be7617d50bf734c9e07f4367c to your computer and use it in GitHub Desktop.
Save abhilater/e4d3938be7617d50bf734c9e07f4367c to your computer and use it in GitHub Desktop.
public class DataAccessorTest {
@Test
public void issueMetricsPaginateTest() throws Exception {
DataAccessor<IssueMetrics> da = new DataAccessor<IssueMetrics>(IssueMetrics.class)
.withFilter(new String[]{"domain", "eq"}, "hscustomer")
.withFilter(new String[]{"created_at", "gte"}, 1001)
.withFilter(new String[]{"created_at", "lt"}, 1003)
.build();
Assert.assertNotNull(da.getGeneratedQuery());
Assert.assertEquals("select domain, " +
"issue_id, " +
"created_at, " +
"inbound_count, " +
"outbound_count, " +
"csat " +
"from issue_metrics_table " +
"where domain = ? AND " +
"created_at >= ? AND created_at < ? " +
"LIMIT ? OFFSET ?",
da.getGeneratedQuery());
PagedResult<IssueMetrics> result = da.paginate();
Assert.assertNull(result.nextKey);
Assert.assertEquals(2, result.records.size());
//PagedResult{records=
//[IssueMetrics{domain='hscustomer', issueId='2', createdAt=1001, inboundMessages=100, outboundMessages=50, csat=1},
// IssueMetrics{domain='hscustomer', issueId='3', createdAt=1002, inboundMessages=200, outboundMessages=150, csat=3}], nextKey='null'}
da = new DataAccessor<IssueMetrics>(IssueMetrics.class)
.withFilter(new String[]{"domain", "eq"}, "hscustomer")
.withFilter(new String[]{"issue_id", "eq"}, "3")
.withFilter(new String[]{"created_at", "gte"}, 1001)
.withFilter(new String[]{"created_at", "lt"}, 1003)
.withLimit(10)
.withOffset(0)
.build();
Assert.assertNotNull(da.getGeneratedQuery());
Assert.assertEquals("select domain, " +
"issue_id, " +
"created_at, " +
"inbound_count, " +
"outbound_count, " +
"csat " +
"from issue_metrics_table " +
"where domain = ? AND " +
"issue_id = ? AND " +
"created_at >= ? AND created_at < ? " +
"LIMIT ? OFFSET ?",
da.getGeneratedQuery());
result = da.paginate();
Assert.assertNull(result.nextKey);
Assert.assertEquals(1, result.records.size());
// PagedResult{records=
// [IssueMetrics{domain='hscustomer', issueId='3', createdAt=1002, inboundMessages=200, outboundMessages=150, csat=3}], nextKey='null'}
Assert.assertEquals("3", ((IssueMetrics)result.records.get(0)).issueId);
}
@Test
public void agentMetricsPaginateTest() throws Exception {
DataAccessor<AgentMetrics> da = new DataAccessor<AgentMetrics>(AgentMetrics.class)
.withGroupBy("domain")
.withGroupBy("time_bucket")
.withGroupBy("agent_id")
.withFilter(new String[]{"domain", "eq"}, "hscustomer")
.withFilter(new String[]{"time_bucket", "gte"}, "201901010130")
.withFilter(new String[]{"time_bucket", "lt"}, "201901010230")
.build();
Assert.assertNotNull(da.getGeneratedQuery());
Assert.assertEquals("select SUM(ttfr_sum) AS ttfrSum, " +
"SUM(ttfr_count) AS ttfrCount, " +
"AVG(csat) AS csatAvg, " +
"domain, " +
"TO_CHAR(CONVERT_TZ(TO_DATE(time_bucket,'yyyyMMddHHmm'), 'UTC', 'Asia/Kolkata'),'yyyy-MM-dd\\'T\\'HH:00:00') AS time, " +
"agent_id " +
"from agent_metrics_table " +
"where domain = ? AND " +
"time_bucket >= ? AND time_bucket < ? " +
"group by domain, time, agent_id " +
"LIMIT ? OFFSET ?",
da.getGeneratedQuery());
PagedResult<AgentMetrics> result = da.paginate();
Assert.assertNull(result.nextKey);
Assert.assertEquals(2, result.records.size());
da = new DataAccessor<AgentMetrics>(AgentMetrics.class)
.withGroupBy("domain")
.withGroupBy("time_bucket")
.withGroupBy("agent_id")
.withFilter(new String[]{"domain", "eq"}, "hscustomer")
.withFilter(new String[]{"agent_id", "eq"}, "a1")
.withFilter(new String[]{"time_bucket", "gte"}, "201901010130")
.withFilter(new String[]{"time_bucket", "lt"}, "201901010230")
.withLimit(10)
.withOffset(0)
.build();
Assert.assertNotNull(da.getGeneratedQuery());
Assert.assertEquals("select SUM(ttfr_sum) AS ttfrSum, " +
"SUM(ttfr_count) AS ttfrCount, " +
"AVG(csat) AS csatAvg, " +
"domain, " +
"TO_CHAR(CONVERT_TZ(TO_DATE(time_bucket,'yyyyMMddHHmm'), 'UTC', 'Asia/Kolkata'),'yyyy-MM-dd\\'T\\'HH:00:00') AS time, " +
"agent_id " +
"from agent_metrics_table " +
"where domain = ? AND " +
"agent_id = ? AND " +
"time_bucket >= ? AND time_bucket < ? " +
"group by domain, time, agent_id " +
"LIMIT ? OFFSET ?",
da.getGeneratedQuery());
result = da.paginate();
Assert.assertNull(result.nextKey);
Assert.assertEquals(1, result.records.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment