@Test public void getCategoriesCount() { when(database.query( anyString(), any(String[].class), anyString(), any(String[].class), anyString(), eq((String) null), anyString())) .thenReturn(cursor); assertNotNull(source.getCategoryCounts(1)); }
public CategoryCounts getCategoryCounts(long timestamp) { Cursor cursor = database.query( ArticleModel.TABLE + " a join " + SourceModel.TABLE + " s on a." + ArticleModel.COLUMN_SOURCE_ID + " = s." + SourceModel.COLUMN_REMOTE_ID + " join " + CategoryModel.TABLE + " c on s." + SourceModel.COLUMN_CATEGORY_ID + " = c." + CategoryModel.COLUMN_ID, new String[] { "c." + CategoryModel.COLUMN_NAME + " as " + CategoryModel.COLUMN_NAME, "count(c." + CategoryModel.COLUMN_ID + ") as count" }, "a." + ArticleModel.COLUMN_INSERTED_AT + " > ? and " + "a." + ArticleModel.COLUMN_SOURCE_ID + " not null", new String[] { Long.toString(timestamp) }, "c." + CategoryModel.COLUMN_ID, null, "count desc" ); List<Category> categories = new ArrayList<>(); int total = 0; if (cursor != null & cursor.moveToFirst()) { do { Category category = new Category(cursor); total += category.numberArticles; categories.add(category); } while (cursor.moveToNext()); cursor.close(); } return new CategoryCounts(categories, total); }
DataSource { public CategoryCounts getCategoryCounts(long timestamp) { Cursor cursor = database.query( ArticleModel.TABLE + " a join " + SourceModel.TABLE + " s on a." + ArticleModel.COLUMN_SOURCE_ID + " = s." + SourceModel.COLUMN_REMOTE_ID + " join " + CategoryModel.TABLE + " c on s." + SourceModel.COLUMN_CATEGORY_ID + " = c." + CategoryModel.COLUMN_ID, new String[] { "c." + CategoryModel.COLUMN_NAME + " as " + CategoryModel.COLUMN_NAME, "count(c." + CategoryModel.COLUMN_ID + ") as count" }, "a." + ArticleModel.COLUMN_INSERTED_AT + " > ? and " + "a." + ArticleModel.COLUMN_SOURCE_ID + " not null", new String[] { Long.toString(timestamp) }, "c." + CategoryModel.COLUMN_ID, null, "count desc" ); List<Category> categories = new ArrayList<>(); int total = 0; if (cursor != null & cursor.moveToFirst()) { do { Category category = new Category(cursor); total += category.numberArticles; categories.add(category); } while (cursor.moveToNext()); cursor.close(); } return new CategoryCounts(categories, total); } }
DataSource { public CategoryCounts getCategoryCounts(long timestamp) { Cursor cursor = database.query( ArticleModel.TABLE + " a join " + SourceModel.TABLE + " s on a." + ArticleModel.COLUMN_SOURCE_ID + " = s." + SourceModel.COLUMN_REMOTE_ID + " join " + CategoryModel.TABLE + " c on s." + SourceModel.COLUMN_CATEGORY_ID + " = c." + CategoryModel.COLUMN_ID, new String[] { "c." + CategoryModel.COLUMN_NAME + " as " + CategoryModel.COLUMN_NAME, "count(c." + CategoryModel.COLUMN_ID + ") as count" }, "a." + ArticleModel.COLUMN_INSERTED_AT + " > ? and " + "a." + ArticleModel.COLUMN_SOURCE_ID + " not null", new String[] { Long.toString(timestamp) }, "c." + CategoryModel.COLUMN_ID, null, "count desc" ); List<Category> categories = new ArrayList<>(); int total = 0; if (cursor != null & cursor.moveToFirst()) { do { Category category = new Category(cursor); total += category.numberArticles; categories.add(category); } while (cursor.moveToNext()); cursor.close(); } return new CategoryCounts(categories, total); } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); }
DataSource { public CategoryCounts getCategoryCounts(long timestamp) { Cursor cursor = database.query( ArticleModel.TABLE + " a join " + SourceModel.TABLE + " s on a." + ArticleModel.COLUMN_SOURCE_ID + " = s." + SourceModel.COLUMN_REMOTE_ID + " join " + CategoryModel.TABLE + " c on s." + SourceModel.COLUMN_CATEGORY_ID + " = c." + CategoryModel.COLUMN_ID, new String[] { "c." + CategoryModel.COLUMN_NAME + " as " + CategoryModel.COLUMN_NAME, "count(c." + CategoryModel.COLUMN_ID + ") as count" }, "a." + ArticleModel.COLUMN_INSERTED_AT + " > ? and " + "a." + ArticleModel.COLUMN_SOURCE_ID + " not null", new String[] { Long.toString(timestamp) }, "c." + CategoryModel.COLUMN_ID, null, "count desc" ); List<Category> categories = new ArrayList<>(); int total = 0; if (cursor != null & cursor.moveToFirst()) { do { Category category = new Category(cursor); total += category.numberArticles; categories.add(category); } while (cursor.moveToNext()); cursor.close(); } return new CategoryCounts(categories, total); } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); static DataSource get(Context context); synchronized void open(); boolean isOpen(); synchronized void close(); @VisibleForTesting SQLiteDatabase getDatabase(); void clearTables(); void beginTransaction(); void execSql(String sql); Cursor rawQuery(String sql); void setTransactionSuccessful(); void endTransaction(); long insertArticle(Article article); int updateSavedArticleState(Article article); int updateArticleInsertedAtTimestamp(Article article); void updateArticleContent(Article article); Article getArticle(String url); Cursor getAllArticles(); Cursor getSavedArticles(); Cursor getArticlesForSource(long remoteSourceId); void deleteArticle(Article article); void deleteArticle(long articleId); long insertCategory(String categoryName); void insertSource(Source source); void deleteSource(String name); List<Source> getSources(); Source getSource(long remoteId); CategoryCounts getCategoryCounts(long timestamp); }
DataSource { public CategoryCounts getCategoryCounts(long timestamp) { Cursor cursor = database.query( ArticleModel.TABLE + " a join " + SourceModel.TABLE + " s on a." + ArticleModel.COLUMN_SOURCE_ID + " = s." + SourceModel.COLUMN_REMOTE_ID + " join " + CategoryModel.TABLE + " c on s." + SourceModel.COLUMN_CATEGORY_ID + " = c." + CategoryModel.COLUMN_ID, new String[] { "c." + CategoryModel.COLUMN_NAME + " as " + CategoryModel.COLUMN_NAME, "count(c." + CategoryModel.COLUMN_ID + ") as count" }, "a." + ArticleModel.COLUMN_INSERTED_AT + " > ? and " + "a." + ArticleModel.COLUMN_SOURCE_ID + " not null", new String[] { Long.toString(timestamp) }, "c." + CategoryModel.COLUMN_ID, null, "count desc" ); List<Category> categories = new ArrayList<>(); int total = 0; if (cursor != null & cursor.moveToFirst()) { do { Category category = new Category(cursor); total += category.numberArticles; categories.add(category); } while (cursor.moveToNext()); cursor.close(); } return new CategoryCounts(categories, total); } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); static DataSource get(Context context); synchronized void open(); boolean isOpen(); synchronized void close(); @VisibleForTesting SQLiteDatabase getDatabase(); void clearTables(); void beginTransaction(); void execSql(String sql); Cursor rawQuery(String sql); void setTransactionSuccessful(); void endTransaction(); long insertArticle(Article article); int updateSavedArticleState(Article article); int updateArticleInsertedAtTimestamp(Article article); void updateArticleContent(Article article); Article getArticle(String url); Cursor getAllArticles(); Cursor getSavedArticles(); Cursor getArticlesForSource(long remoteSourceId); void deleteArticle(Article article); void deleteArticle(long articleId); long insertCategory(String categoryName); void insertSource(Source source); void deleteSource(String name); List<Source> getSources(); Source getSource(long remoteId); CategoryCounts getCategoryCounts(long timestamp);  }