testMarkAllAsRead, testFetchUnreadCommits
[gitter-test.git] / src / ch / codelabs / gitter / test / CommitDbAdapterTest.java
1 /*
2  * Copyright (C) 2012 Martin Kempf <mkempf@hsr.ch>
3  * Copyright (C) 2012 Reto Buerki <reet@codelabs.ch>
4  * Copyright (C) 2012 Adrian-Ken Rueegsegger <ken@codelabs.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  */
16
17 package ch.codelabs.gitter.test;
18
19 import android.database.Cursor;
20 import android.test.AndroidTestCase;
21
22 import ch.codelabs.gitter.Commit;
23 import ch.codelabs.gitter.CommitDbAdapter;
24
25 public class CommitDbAdapterTest extends AndroidTestCase {
26
27         /*
28          * Register tests.
29          */
30         public CommitDbAdapterTest() {
31                 super();
32         }
33
34         /*
35          * Insert commit into database.
36          */
37         public void testInsertCommit() {
38                 final String testHash1 = "c669e1928dec2a9251a819ca40f32beed9e280ec";
39                 final String testMail = "doe@example.com";
40                 final String testDate = "Thu Mar 22 21:04:37 CET 2012";
41                 final CommitDbAdapter commitDbAdapter = new CommitDbAdapter(
42                                 getContext());
43                 commitDbAdapter.open();
44
45                 Commit commit = new Commit();
46                 commit.mTitle = "Update TODO";
47                 commit.mCommitId = testHash1;
48                 commit.mAuthor = "John Doe";
49                 commit.mDate = testDate;
50                 commit.mEmail = testMail;
51                 commit.mDiffLink = "http://1.com/abc";
52                 commit.mContent = "testdata";
53
54                 long id = commitDbAdapter.insertCommit(commit);
55
56                 final Cursor c = commitDbAdapter.fetchAllCommits();
57                 assertTrue(c.getCount() > 0);
58
59                 c.moveToFirst();
60                 assertEquals(testHash1,
61                                 c.getString(c.getColumnIndexOrThrow(CommitDbAdapter.KEY_HASH)));
62                 assertEquals(0,
63                                 c.getInt(c.getColumnIndexOrThrow(CommitDbAdapter.KEY_READ)));
64                 assertEquals("Update TODO",
65                                 c.getString(c.getColumnIndexOrThrow(CommitDbAdapter.KEY_TITLE)));
66                 assertEquals("John Doe", c.getString(c
67                                 .getColumnIndexOrThrow(CommitDbAdapter.KEY_AUTHOR)));
68                 assertEquals(testDate,
69                                 c.getString(c.getColumnIndexOrThrow(CommitDbAdapter.KEY_DATE)));
70                 assertEquals(testMail,
71                                 c.getString(c.getColumnIndexOrThrow(CommitDbAdapter.KEY_EMAIL)));
72                 assertEquals("http://1.com/abc", c.getString(c
73                                 .getColumnIndexOrThrow(CommitDbAdapter.KEY_DIFFLINK)));
74                 assertEquals("testdata", c.getString(c
75                                 .getColumnIndexOrThrow(CommitDbAdapter.KEY_CONTENT)));
76
77                 /* Test 'read' status */
78                 commitDbAdapter.setReadStatus(id, true);
79
80                 Cursor i = commitDbAdapter.fetchCommit(id);
81                 assertEquals(1,
82                                 i.getInt(i.getColumnIndexOrThrow(CommitDbAdapter.KEY_READ)));
83
84                 commitDbAdapter.setReadStatus(id, false);
85                 i = commitDbAdapter.fetchCommit(id);
86                 assertEquals(0,
87                                 i.getInt(i.getColumnIndexOrThrow(CommitDbAdapter.KEY_READ)));
88
89                 /* Test latest commit getter, insert another commit first */
90                 final String testHash2 = "abca3c29b985d90f271c7b9ea9f095729759799c";
91                 commit.mCommitId = testHash2;
92                 id = commitDbAdapter.insertCommit(commit);
93                 i = commitDbAdapter.getLatestCommit();
94                 assertTrue(i.getCount() == 1);
95                 assertEquals(testHash2,
96                                 i.getString(i.getColumnIndexOrThrow(CommitDbAdapter.KEY_HASH)));
97
98                 commitDbAdapter.clearDatabase();
99                 i = commitDbAdapter.fetchAllCommits();
100                 assertTrue(i.getCount() == 0);
101
102                 c.close();
103                 i.close();
104                 commitDbAdapter.close();
105         }
106
107         public void testFetchUnreadCommits() {
108                 final String testHash1 = "c669e1928dec2a9251a819ca40f32beed9e280ec";
109                 final String testMail = "doe@example.com";
110                 final String testDate = "Thu Mar 22 21:04:37 CET 2012";
111                 final CommitDbAdapter commitDbAdapter = new CommitDbAdapter(
112                                 getContext());
113                 commitDbAdapter.open();
114                 commitDbAdapter.clearDatabase();
115
116                 Commit commit = new Commit();
117                 commit.mTitle = "Update TODO";
118                 commit.mCommitId = testHash1;
119                 commit.mAuthor = "John Doe";
120                 commit.mDate = testDate;
121                 commit.mEmail = testMail;
122                 commit.mDiffLink = "http://1.com/abc";
123                 commit.mContent = "testdata";
124
125                 Commit commit2 = new Commit();
126                 commit2.mTitle = "Update TODO";
127                 commit2.mCommitId = testHash1;
128                 commit2.mAuthor = "John Doe";
129                 commit2.mDate = testDate;
130                 commit2.mEmail = testMail;
131                 commit2.mDiffLink = "http://1.com/abc";
132                 commit2.mContent = "testdata";
133
134                 long id = commitDbAdapter.insertCommit(commit);
135                 id = commitDbAdapter.insertCommit(commit2);
136                 commitDbAdapter.setReadStatus(id, true);
137
138                 final Cursor c = commitDbAdapter.fetchUnreadCommits();
139                 assertTrue(c.getCount() == 1); // first commit is unread
140                 c.close();
141                 commitDbAdapter.close();
142         }
143
144         public void testMarkAllAsRead() {
145                 final String testHash1 = "c669e1928dec2a9251a819ca40f32beed9e280ec";
146                 final String testMail = "doe@example.com";
147                 final String testDate = "Thu Mar 22 21:04:37 CET 2012";
148                 final CommitDbAdapter commitDbAdapter = new CommitDbAdapter(
149                                 getContext());
150                 commitDbAdapter.open();
151                 commitDbAdapter.clearDatabase();
152
153                 Commit commit = new Commit();
154                 commit.mTitle = "Update TODO";
155                 commit.mCommitId = testHash1;
156                 commit.mAuthor = "John Doe";
157                 commit.mDate = testDate;
158                 commit.mEmail = testMail;
159                 commit.mDiffLink = "http://1.com/abc";
160                 commit.mContent = "testdata";
161
162                 Commit commit2 = new Commit();
163                 commit2.mTitle = "Update TODO";
164                 commit2.mCommitId = testHash1;
165                 commit2.mAuthor = "John Doe";
166                 commit2.mDate = testDate;
167                 commit2.mEmail = testMail;
168                 commit2.mDiffLink = "http://1.com/abc";
169                 commit2.mContent = "testdata";
170
171                 long id = commitDbAdapter.insertCommit(commit);
172                 id = commitDbAdapter.insertCommit(commit2);
173                 commitDbAdapter.setReadStatus(id, true);
174
175                 int markedAsRead = commitDbAdapter.markAllAsRead();
176
177                 final Cursor c = commitDbAdapter.fetchUnreadCommits();
178                 assertTrue(c.getCount() == 0); // no unread commits anymore
179                 assertTrue(markedAsRead == 1);
180                 c.close();
181                 commitDbAdapter.close();
182         }
183
184 }