Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 4a1a86b

Browse files
author
Evgenii
committed
Merge pull request #35 from evgenii-kanivets/test-repo
[5h 30m] Test repo layer
2 parents d7f766d + 7e07da4 commit 4a1a86b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+921
-287
lines changed

app/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ android {
4444
}
4545

4646
dependencies {
47-
compile fileTree(include: ['*.jar'], dir: 'libs')
47+
compile fileTree(dir: 'libs', include: ['*.jar'])
4848
compile 'com.android.support:support-v4:23.0.0'
4949
compile 'com.android.support:appcompat-v7:23.0.0'
50-
compile 'junit:junit:4.12'
51-
compile 'org.mockito:mockito-core:2.0.2-beta'
5250
compile 'com.jakewharton:butterknife:7.0.1'
53-
5451
compile 'com.google.dagger:dagger:2.0.1'
52+
5553
apt 'com.google.dagger:dagger-compiler:2.0.1'
5654
provided 'org.glassfish:javax.annotation:10.0-b28'
5755

5856
testCompile 'junit:junit:4.12'
57+
testCompile 'org.mockito:mockito-core:2.0.43-beta'
58+
59+
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
60+
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
61+
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"
5962
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.blogspot.e_kanivets.moneytracker.repo;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
6+
import com.blogspot.e_kanivets.moneytracker.DbHelper;
7+
import com.blogspot.e_kanivets.moneytracker.entity.Account;
8+
9+
import junit.framework.TestCase;
10+
11+
import org.mockito.Mockito;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
/**
17+
* Android Test case.
18+
* Created on 3/1/16.
19+
*
20+
* @author Evgenii Kanivets
21+
*/
22+
public class AccountRepoTest extends TestCase {
23+
private AccountRepo repo;
24+
25+
@Override
26+
public void setUp() throws Exception {
27+
super.setUp();
28+
DbHelper mock = Mockito.mock(DbHelper.class);
29+
repo = new AccountRepo(mock);
30+
}
31+
32+
@Override
33+
public void tearDown() throws Exception {
34+
super.tearDown();
35+
repo = null;
36+
}
37+
38+
public void testGetTable() throws Exception {
39+
assertEquals(DbHelper.TABLE_ACCOUNTS, repo.getTable());
40+
}
41+
42+
public void testContentValues() throws Exception {
43+
Account account = new Account(-1, "title1", 100, "NON");
44+
45+
ContentValues expected = new ContentValues();
46+
expected.put(DbHelper.TITLE_COLUMN, "title1");
47+
expected.put(DbHelper.CUR_SUM_COLUMN, 100);
48+
expected.put(DbHelper.CURRENCY_COLUMN, "NON");
49+
50+
ContentValues actual = repo.contentValues(account);
51+
52+
assertEquals(expected, actual);
53+
54+
assertNull(repo.contentValues(null));
55+
}
56+
57+
public void testGetListFromCursor() throws Exception {
58+
assertEquals(new ArrayList<Account>(), repo.getListFromCursor(Mockito.mock(Cursor.class)));
59+
60+
Cursor mockCursor = Mockito.mock(Cursor.class);
61+
Mockito.when(mockCursor.moveToFirst()).thenReturn(true);
62+
Mockito.when(mockCursor.getColumnIndex(DbHelper.ID_COLUMN)).thenReturn(1);
63+
Mockito.when(mockCursor.getColumnIndex(DbHelper.TITLE_COLUMN)).thenReturn(2);
64+
Mockito.when(mockCursor.getColumnIndex(DbHelper.CUR_SUM_COLUMN)).thenReturn(3);
65+
Mockito.when(mockCursor.getColumnIndex(DbHelper.CURRENCY_COLUMN)).thenReturn(4);
66+
67+
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
68+
Mockito.when(mockCursor.getString(2)).thenReturn("title");
69+
Mockito.when(mockCursor.getInt(3)).thenReturn(100);
70+
Mockito.when(mockCursor.getString(4)).thenReturn("NON");
71+
72+
List<Account> expected = new ArrayList<>();
73+
expected.add(new Account(1, "title", 100, "NON"));
74+
75+
assertEquals(expected, repo.getListFromCursor(mockCursor));
76+
77+
assertEquals(new ArrayList<>(), repo.getListFromCursor(null));
78+
}
79+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.blogspot.e_kanivets.moneytracker.repo;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
6+
import com.blogspot.e_kanivets.moneytracker.DbHelper;
7+
import com.blogspot.e_kanivets.moneytracker.entity.Category;
8+
9+
import junit.framework.TestCase;
10+
11+
import org.mockito.Mockito;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
/**
17+
* Android Test case.
18+
* Created on 3/1/16.
19+
*
20+
* @author Evgenii Kanivets
21+
*/
22+
public class CategoryRepoTest extends TestCase {
23+
private CategoryRepo repo;
24+
25+
@Override
26+
public void setUp() throws Exception {
27+
super.setUp();
28+
DbHelper mock = Mockito.mock(DbHelper.class);
29+
repo = new CategoryRepo(mock);
30+
}
31+
32+
@Override
33+
public void tearDown() throws Exception {
34+
super.tearDown();
35+
repo = null;
36+
}
37+
38+
public void testGetTable() throws Exception {
39+
assertEquals(DbHelper.TABLE_CATEGORIES, repo.getTable());
40+
}
41+
42+
public void testContentValues() throws Exception {
43+
Category category = new Category(1, "category");
44+
45+
ContentValues expected = new ContentValues();
46+
expected.put(DbHelper.NAME_COLUMN, "category");
47+
48+
ContentValues actual = repo.contentValues(category);
49+
50+
assertEquals(expected, actual);
51+
52+
assertNull(repo.contentValues(null));
53+
}
54+
55+
public void testGetListFromCursor() throws Exception {
56+
assertEquals(new ArrayList<Category>(), repo.getListFromCursor(Mockito.mock(Cursor.class)));
57+
58+
Cursor mockCursor = Mockito.mock(Cursor.class);
59+
Mockito.when(mockCursor.moveToFirst()).thenReturn(true);
60+
Mockito.when(mockCursor.getColumnIndex(DbHelper.ID_COLUMN)).thenReturn(1);
61+
Mockito.when(mockCursor.getColumnIndex(DbHelper.NAME_COLUMN)).thenReturn(2);
62+
63+
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
64+
Mockito.when(mockCursor.getString(2)).thenReturn("category");
65+
66+
List<Category> expected = new ArrayList<>();
67+
expected.add(new Category(1, "category"));
68+
69+
assertEquals(expected, repo.getListFromCursor(mockCursor));
70+
71+
assertEquals(new ArrayList<>(), repo.getListFromCursor(null));
72+
}
73+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.blogspot.e_kanivets.moneytracker.repo;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
6+
import com.blogspot.e_kanivets.moneytracker.DbHelper;
7+
import com.blogspot.e_kanivets.moneytracker.entity.ExchangeRate;
8+
9+
import junit.framework.TestCase;
10+
11+
import org.mockito.Mockito;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
/**
17+
* Android Test case.
18+
* Created on 3/1/16.
19+
*
20+
* @author Evgenii Kanivets
21+
*/
22+
public class ExchangeRateRepoTest extends TestCase {
23+
private ExchangeRateRepo repo;
24+
25+
@Override
26+
public void setUp() throws Exception {
27+
super.setUp();
28+
DbHelper mock = Mockito.mock(DbHelper.class);
29+
repo = new ExchangeRateRepo(mock);
30+
}
31+
32+
@Override
33+
public void tearDown() throws Exception {
34+
super.tearDown();
35+
repo = null;
36+
}
37+
38+
public void testGetTable() throws Exception {
39+
assertEquals(DbHelper.TABLE_RATES, repo.getTable());
40+
}
41+
42+
public void testContentValues() throws Exception {
43+
ExchangeRate rate = new ExchangeRate(1, 1, "NON", "USD", 100);
44+
45+
ContentValues expected = new ContentValues();
46+
expected.put(DbHelper.CREATED_AT_COLUMN, 1L);
47+
expected.put(DbHelper.FROM_CURRENCY_COLUMN, "NON");
48+
expected.put(DbHelper.TO_CURRENCY_COLUMN, "USD");
49+
expected.put(DbHelper.AMOUNT_COLUMN, 100.0);
50+
51+
ContentValues actual = repo.contentValues(rate);
52+
53+
assertEquals(expected, actual);
54+
55+
assertNull(repo.contentValues(null));
56+
}
57+
58+
public void testGetListFromCursor() throws Exception {
59+
assertEquals(new ArrayList<ExchangeRate>(), repo.getListFromCursor(Mockito.mock(Cursor.class)));
60+
61+
Cursor mockCursor = Mockito.mock(Cursor.class);
62+
Mockito.when(mockCursor.moveToFirst()).thenReturn(true);
63+
Mockito.when(mockCursor.getColumnIndex(DbHelper.ID_COLUMN)).thenReturn(1);
64+
Mockito.when(mockCursor.getColumnIndex(DbHelper.CREATED_AT_COLUMN)).thenReturn(2);
65+
Mockito.when(mockCursor.getColumnIndex(DbHelper.FROM_CURRENCY_COLUMN)).thenReturn(3);
66+
Mockito.when(mockCursor.getColumnIndex(DbHelper.TO_CURRENCY_COLUMN)).thenReturn(4);
67+
Mockito.when(mockCursor.getColumnIndex(DbHelper.AMOUNT_COLUMN)).thenReturn(5);
68+
69+
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
70+
Mockito.when(mockCursor.getLong(2)).thenReturn(1L);
71+
Mockito.when(mockCursor.getString(3)).thenReturn("NON");
72+
Mockito.when(mockCursor.getString(4)).thenReturn("USD");
73+
Mockito.when(mockCursor.getDouble(5)).thenReturn(100.0);
74+
75+
List<ExchangeRate> expected = new ArrayList<>();
76+
expected.add(new ExchangeRate(1, 1, "NON", "USD", 100.0));
77+
78+
assertEquals(expected, repo.getListFromCursor(mockCursor));
79+
80+
assertEquals(new ArrayList<>(), repo.getListFromCursor(null));
81+
}
82+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.blogspot.e_kanivets.moneytracker.repo;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
6+
import com.blogspot.e_kanivets.moneytracker.DbHelper;
7+
import com.blogspot.e_kanivets.moneytracker.entity.Record;
8+
9+
import junit.framework.TestCase;
10+
11+
import org.mockito.Mockito;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
/**
17+
* Android Test case.
18+
* Created on 3/1/16.
19+
*
20+
* @author Evgenii Kanivets
21+
*/
22+
public class RecordRepoTest extends TestCase {
23+
private RecordRepo repo;
24+
25+
@Override
26+
public void setUp() throws Exception {
27+
super.setUp();
28+
DbHelper mock = Mockito.mock(DbHelper.class);
29+
repo = new RecordRepo(mock);
30+
}
31+
32+
@Override
33+
public void tearDown() throws Exception {
34+
super.tearDown();
35+
repo = null;
36+
}
37+
38+
public void testGetTable() throws Exception {
39+
assertEquals(DbHelper.TABLE_RECORDS, repo.getTable());
40+
}
41+
42+
public void testContentValues() throws Exception {
43+
Record record = new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON");
44+
45+
ContentValues expected = new ContentValues();
46+
expected.put(DbHelper.TIME_COLUMN, 1L);
47+
expected.put(DbHelper.TYPE_COLUMN, Record.TYPE_EXPENSE);
48+
expected.put(DbHelper.TITLE_COLUMN, "title");
49+
expected.put(DbHelper.CATEGORY_ID_COLUMN, 1L);
50+
expected.put(DbHelper.PRICE_COLUMN, 100);
51+
expected.put(DbHelper.ACCOUNT_ID_COLUMN, 1L);
52+
expected.put(DbHelper.CURRENCY_COLUMN, "NON");
53+
54+
ContentValues actual = repo.contentValues(record);
55+
56+
assertEquals(expected, actual);
57+
58+
assertNull(repo.contentValues(null));
59+
}
60+
61+
public void testGetListFromCursor() throws Exception {
62+
assertEquals(new ArrayList<Record>(), repo.getListFromCursor(Mockito.mock(Cursor.class)));
63+
64+
Cursor mockCursor = Mockito.mock(Cursor.class);
65+
Mockito.when(mockCursor.moveToFirst()).thenReturn(true);
66+
Mockito.when(mockCursor.getColumnIndex(DbHelper.ID_COLUMN)).thenReturn(1);
67+
Mockito.when(mockCursor.getColumnIndex(DbHelper.TIME_COLUMN)).thenReturn(2);
68+
Mockito.when(mockCursor.getColumnIndex(DbHelper.TYPE_COLUMN)).thenReturn(3);
69+
Mockito.when(mockCursor.getColumnIndex(DbHelper.TITLE_COLUMN)).thenReturn(4);
70+
Mockito.when(mockCursor.getColumnIndex(DbHelper.CATEGORY_ID_COLUMN)).thenReturn(5);
71+
Mockito.when(mockCursor.getColumnIndex(DbHelper.PRICE_COLUMN)).thenReturn(6);
72+
Mockito.when(mockCursor.getColumnIndex(DbHelper.ACCOUNT_ID_COLUMN)).thenReturn(7);
73+
Mockito.when(mockCursor.getColumnIndex(DbHelper.CURRENCY_COLUMN)).thenReturn(8);
74+
75+
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
76+
Mockito.when(mockCursor.getLong(2)).thenReturn(1L);
77+
Mockito.when(mockCursor.getInt(3)).thenReturn(Record.TYPE_EXPENSE);
78+
Mockito.when(mockCursor.getString(4)).thenReturn("title");
79+
Mockito.when(mockCursor.getLong(5)).thenReturn(1L);
80+
Mockito.when(mockCursor.getInt(6)).thenReturn(100);
81+
Mockito.when(mockCursor.getLong(7)).thenReturn(1L);
82+
Mockito.when(mockCursor.getString(8)).thenReturn("NON");
83+
84+
List<Record> expected = new ArrayList<>();
85+
expected.add(new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON"));
86+
87+
assertEquals(expected, repo.getListFromCursor(mockCursor));
88+
89+
assertEquals(new ArrayList<>(), repo.getListFromCursor(null));
90+
}
91+
}

0 commit comments

Comments
 (0)