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

Commit f361f02

Browse files
author
Evgenii Kanivets
committed
#17[30m]. Add tests for CategoryRepo.
1 parent 811807c commit f361f02

File tree

4 files changed

+95
-10
lines changed

4 files changed

+95
-10
lines changed

app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/AccountRepoTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ public class AccountRepoTest extends TestCase {
2323
private AccountRepo repo;
2424

2525
public void setUp() throws Exception {
26+
super.setUp();
2627
DbHelper mock = Mockito.mock(DbHelper.class);
2728
repo = new AccountRepo(mock);
2829
}
2930

3031
public void tearDown() throws Exception {
32+
super.tearDown();
3133
repo = null;
3234
}
3335

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.model.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+
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
63+
Mockito.when(mockCursor.getString(2)).thenReturn("category");
64+
65+
List<Category> expected = new ArrayList<>();
66+
expected.add(new Category(1, "category"));
67+
68+
assertEquals(expected, repo.getListFromCursor(mockCursor));
69+
70+
assertEquals(new ArrayList<>(), repo.getListFromCursor(null));
71+
}
72+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Account.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public void take(int amount) {
5959
curSum -= amount;
6060
}
6161

62+
@SuppressWarnings("SimplifiableIfStatement")
63+
@Override
64+
public boolean equals(Object o) {
65+
if (o instanceof Account) {
66+
Account account = (Account) o;
67+
return this.id == account.getId()
68+
&& this.title.equals(account.getTitle())
69+
&& this.curSum == account.getCurSum()
70+
&& this.currency.equals(account.getCurrency());
71+
} else return false;
72+
}
73+
6274
@SuppressWarnings("StringBufferReplaceableByString")
6375
@Override
6476
public String toString() {
@@ -72,14 +84,4 @@ public String toString() {
7284

7385
return sb.toString();
7486
}
75-
76-
@SuppressWarnings("SimplifiableIfStatement")
77-
@Override
78-
public boolean equals(Object o) {
79-
if (o instanceof Account) {
80-
return ((Account) o).getId() == getId();
81-
} else {
82-
return false;
83-
}
84-
}
8587
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Category.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public String getName() {
3030
return name;
3131
}
3232

33+
@Override
34+
public boolean equals(Object o) {
35+
if (o instanceof Category) {
36+
Category category = (Category) o;
37+
return this.id == category.getId()
38+
&& this.name.equals(category.getName());
39+
} else return false;
40+
}
41+
3342
@SuppressWarnings("StringBufferReplaceableByString")
3443
@Override
3544
public String toString() {

0 commit comments

Comments
 (0)