1package com.xtremelabs.robolectric.shadows;
2
3
4import android.database.sqlite.SQLiteQueryBuilder;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Before;
7import org.junit.Test;
8import org.junit.runner.RunWith;
9
10import static org.hamcrest.CoreMatchers.equalTo;
11import static org.junit.Assert.assertThat;
12
13@RunWith(WithTestDefaultsRunner.class)
14public class SQLiteQueryBuilderTest {
15
16    SQLiteQueryBuilder builder;
17
18    @Before
19    public void setUp() throws Exception {
20        builder = new SQLiteQueryBuilder();
21    }
22
23    @Test
24    public void testDistinct() {
25        String sql = SQLiteQueryBuilder.buildQueryString(
26                true,
27                "table_name",
28                new String[]{"id", "name"},
29                null, null, null, null, null);
30        assertThat(sql, equalTo("SELECT DISTINCT id, name FROM table_name"));
31
32    }
33
34    @Test
35    public void testSelectColumn() {
36        String sql = SQLiteQueryBuilder.buildQueryString(
37                false,
38                "table_name",
39                new String[]{"id"},
40                null, null, null, null, null);
41        assertThat(sql, equalTo("SELECT id FROM table_name"));
42    }
43
44    @Test
45    public void testSelectColumns() {
46        String sql = SQLiteQueryBuilder.buildQueryString(
47                false,
48                "table_name",
49                new String[]{"id", "name"},
50                null, null, null, null, null);
51        assertThat(sql, equalTo("SELECT id, name FROM table_name"));
52    }
53
54    @Test
55    public void testSelectAllColumns() {
56        String sql = SQLiteQueryBuilder.buildQueryString(
57                false,
58                "table_name",
59                null, null, null, null, null, null);
60        assertThat(sql, equalTo("SELECT * FROM table_name"));
61    }
62
63    @Test
64    public void testWhereClause() {
65        String sql = SQLiteQueryBuilder.buildQueryString(
66                false,
67                "table_name",
68                new String[]{"person", "department", "division"},
69                "(id = 2 AND name = 'Chuck')", null, null, null, null);
70        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck')"));
71    }
72
73    @Test
74    public void testEmptyWhereClause() {
75        String sql = SQLiteQueryBuilder.buildQueryString(
76                false,
77                "table_name",
78                new String[]{"person", "department", "division"},
79                null, "person", null, null, null);
80        assertThat(sql, equalTo("SELECT person, department, division FROM table_name GROUP BY person"));
81    }
82
83    @Test
84    public void testGroupBy() {
85        String sql = SQLiteQueryBuilder.buildQueryString(
86                false,
87                "table_name",
88                new String[]{"person", "department", "division"},
89                "(id = 2 AND name = 'Chuck')", "person", null, null, null);
90        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person"));
91    }
92
93    @Test
94    public void testEmptyGroupBy() {
95        String sql = SQLiteQueryBuilder.buildQueryString(
96                false,
97                "table_name",
98                new String[]{"person", "department", "division"},
99                "(id = 2 AND name = 'Chuck')", null, "SUM(hours) < 20", null, null);
100        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') HAVING SUM(hours) < 20"));
101    }
102
103    @Test
104    public void testHaving() {
105        String sql = SQLiteQueryBuilder.buildQueryString(
106                false,
107                "table_name",
108                new String[]{"person", "department", "division"},
109                "(id = 2 AND name = 'Chuck')", "person", "SUM(hours) < 20", null, null);
110        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person HAVING SUM(hours) < 20"));
111    }
112
113    @Test
114    public void testEmptyHaving() {
115        String sql = SQLiteQueryBuilder.buildQueryString(
116                false,
117                "table_name",
118                new String[]{"person", "department", "division"},
119                "(id = 2 AND name = 'Chuck')", "person", null, "id ASC", null);
120        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person ORDER BY id ASC"));
121    }
122
123    @Test
124    public void testSortOrder() {
125        String sql = SQLiteQueryBuilder.buildQueryString(
126                false,
127                "table_name",
128                new String[]{"person", "department", "division"},
129                "(id = 2 AND name = 'Chuck')", "person", "SUM(hours) < 20", "id ASC", null);
130        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person HAVING SUM(hours) < 20 ORDER BY id ASC"));
131    }
132
133    @Test
134    public void testEmptySortOrder() {
135        String sql = SQLiteQueryBuilder.buildQueryString(
136                false,
137                "table_name",
138                new String[]{"person", "department", "division"},
139                "(id = 2 AND name = 'Chuck')", "person", "SUM(hours) < 20", null, "10");
140        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person HAVING SUM(hours) < 20 LIMIT 10"));
141    }
142
143    @Test
144    public void testLimit() {
145        String sql = SQLiteQueryBuilder.buildQueryString(
146                false,
147                "table_name",
148                new String[]{"person", "department", "division"},
149                "(id = 2 AND name = 'Chuck')", "person", "SUM(hours) < 20", "id ASC", "10");
150        assertThat(sql, equalTo("SELECT person, department, division FROM table_name WHERE (id = 2 AND name = 'Chuck') GROUP BY person HAVING SUM(hours) < 20 ORDER BY id ASC LIMIT 10"));
151    }
152}
153