a2e5e9610b4552bf372b05ab6faf68bdd7eba75c
[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 }