Skip to content

Instantly share code, notes, and snippets.

View izmajlowiczl's full-sized avatar

Łukasz Izmajłowicz izmajlowiczl

View GitHub Profile
@izmajlowiczl
izmajlowiczl / DateUtilsTest.java
Created March 29, 2015 21:30
Exploratory test for DateUtils::getRelativeTimeSpanString class from Android SDK.
package pl.izmajlowiczl.bsj.delete_me;
import android.text.format.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.Calendar;
@izmajlowiczl
izmajlowiczl / build.gradle
Created February 19, 2016 21:16
Test results loggin to console
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
exceptionFormat 'full'
}
}
package pl.expensive.db;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class DatabaseSchemaTestHelper {
public interface WalletsStorage {
/**
* List all stored {@link Wallet}s. Result is null safe
*
* @return Collection of all stored Wallets or Collections.emptyList() for empty result.
*/
Collection<Wallet> list();
/**
* Stores {@link Wallet} object.
@Test
public void storeSingleWallet() {
Wallet bankWallet = Wallet.create(randomUUID(), "bank");
storage.insert(bankWallet);
assertThat(getStoredWalletNames())
.containsExactly("bank");
}
private List<String> getStoredWalletNames() {
@Test
public void listWallets() {
Wallet bankWallet = Wallet.create(randomUUID(), "bank");
Wallet ccWallet = Wallet.create(randomUUID(), "credit card");
db.execSQL(storeWalletSql(bankWallet));
db.execSQL(storeWalletSql(ccWallet));
assertThat(storage.list()).containsExactly(bankWallet, ccWallet);
}
@Test
public void storeWallets() {
Wallet wallet = Wallet.create(UUID.randomUUID(), "Test-Walet");
model.insert(wallet);
assertThat(model.list())
.containsExactly(wallet);
}
@Test fun columnsForCategoryTable() {
val columns = getTableColumns(database.readableDatabase, "tbl_category")
assertThat(columns).containsExactly("uuid", "name", "color")
}
@Test fun createDefaultCategoriesTable() {
assertThat(database.readableDatabase.doesTableExist("tbl_category")).isTrue()
}
fun SQLiteDatabase.doesTableExist(tableName: String): Boolean {
val sql = "SELECT name FROM sqlite_master WHERE type='table';"
val c = rawQuery(sql, null)
while (c.moveToNext()) {
if (c.getString(0) == tableName) return true
else continue
}
return false
}
fun SQLiteDatabase.doesTableExistIncludingTemps(tableName: String): Boolean {
val sql = """SELECT name
FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master)
WHERE type='table';"""
val c = rawQuery(sql, null)
while (c.moveToNext()) {
if (c.getString(0) == tableName) return true else continue
}