BackupQueryBuilder { public @Nullable Query buildQueryForDataType(DataType type, @Nullable ContactGroupIds groupIds, int max) { switch (type) { case SMS: return getQueryForSMS(groupIds, max); case MMS: return getQueryForMMS(groupIds, max); case CALLLOG: return getQueryForCallLog(max); default: return null; } } BackupQueryBuilder(DataTypePreferences preferences); @Nullable Query buildQueryForDataType(DataType type, @Nullable ContactGroupIds groupIds, int max); @Nullable Query buildMostRecentQueryForDataType(DataType type);  }
@Test public void shouldBuildQueryForSMS() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(SMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content: assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND type <> ?"); assertThat(query.selectionArgs).asList().containsExactly("-1", "3"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildQueryForSMSIncludingContactGroup() throws Exception { ContactGroupIds ids = new ContactGroupIds(); ids.add(1L, 20L); BackupQueryBuilder.Query query = builder.buildQueryForDataType(SMS, ids, 200); assertThat(query.uri).isEqualTo(Uri.parse("content: assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND type <> ? AND (type = 2 OR person IN (20))"); assertThat(query.selectionArgs).asList().containsExactly("-1", "3"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildQueryForMMS() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(MMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content: assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND m_type <> ?"); assertThat(query.selectionArgs).asList().containsExactly("-1", "134"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildQueryForMMSWithSyncedDate() throws Exception { long nowInSecs = System.currentTimeMillis(); when(dataTypePreferences.getMaxSyncedDate(MMS)).thenReturn(nowInSecs); BackupQueryBuilder.Query query = builder.buildQueryForDataType(MMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content: assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND m_type <> ?"); assertThat(query.selectionArgs).asList().containsExactly(String.valueOf(nowInSecs / 1000L), "134"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildQueryForCallLog() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(CALLLOG, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content: assertThat(query.projection).asList().containsExactly("_id", "number", "duration", "date", "type"); assertThat(query.selection).isEqualTo("date > ?"); assertThat(query.selectionArgs).asList().containsExactly("-1"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }