Skip to content

Commit 78444cc

Browse files
committed
Added multiple tests for the new parser.
1 parent ec189d9 commit 78444cc

12 files changed

+289
-0
lines changed

tests/res/raw/block_comment.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
Column1 INTEGER /* This is a block comment and should be ignored */
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
Column1 INTEGER /* This is a block comment 'with a string that doesn't matter' */
5+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, /* This is a block comment -- not a line comment */ Column1 INTEGER
4+
)

tests/res/raw/complex.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- Create table for migration
2+
CREATE TABLE Entity2
3+
(
4+
Id INTEGER AUTO_INCREMENT PRIMARY KEY,
5+
Column TEXT NOT NULL,
6+
Column2 INTEGER NULL /* this column is new */
7+
);
8+
9+
-- Migrate data
10+
INSERT INTO Entity2
11+
(
12+
Id,
13+
Column,
14+
Column2
15+
)
16+
SELECT Id,
17+
Column,
18+
0 -- there's no such value in the old table
19+
FROM Entity;
20+
21+
-- Rename Entity2 to Entity
22+
DROP TABLE Entity;
23+
ALTER TABLE Entity2 RENAME TO Entity;
24+
25+
/* Add some --sample-- data */
26+
INSERT INTO Entity2
27+
(
28+
Id,
29+
Col/*not sure if anyone would ever be insane enough to do this*/umn,
30+
Column2--,
31+
)
32+
VALUES
33+
(
34+
9001 /* not -- really */, -- almost forgot that comma
35+
42,--23, /* I don't know who messed this up
36+
'string /* string */ -- string'--,
37+
-- 'test' whoops we don't have that many columns
38+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
/* /* /* This is an invalid block comment */ */
5+
Column1 INTEGER
6+
)

tests/res/raw/line_comment.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
Column1 INTEGER -- This is a line comment and should be ignored
5+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
-- This is a line comment and should be ignored */ NonColumn STRING,
5+
Column1 INTEGER
6+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
Column1 INTEGER -- This is a line comment 'with a string that doesn't matter'
5+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO Entity
2+
(
3+
Id,
4+
Column1,
5+
Column2
6+
)
7+
VALUES
8+
(
9+
1,
10+
'/* some text',
11+
'some text */'
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO Entity
2+
(
3+
Id,
4+
Column1,
5+
Column2
6+
)
7+
VALUES
8+
(
9+
1,
10+
'-- some text',
11+
'some text'
12+
);

tests/res/raw/two_statements.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
4+
Column1 INTEGER
5+
);
6+
7+
CREATE TABLE Entity2
8+
(
9+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
10+
Column1 INTEGER
11+
)
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
2+
package com.activeandroid.test.parser;
3+
4+
import com.activeandroid.test.ActiveAndroidTestCase;
5+
import com.activeandroid.test.R;
6+
import com.activeandroid.util.SqlParser;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.List;
11+
12+
13+
public class ParserTest extends ActiveAndroidTestCase {
14+
15+
private final String sql1 = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )";
16+
private final String sql2 = "CREATE TABLE Entity2 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )";
17+
18+
private final String invalid = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, */ Column1 INTEGER )";
19+
20+
private InputStream getStream(int id) {
21+
return this.getContext().getResources().openRawResource(id);
22+
}
23+
24+
/**
25+
* Should be able to parse a script with two multi-line statments, even if the last statement
26+
* is not terminated by a semicolon.
27+
* @throws IOException
28+
*/
29+
public void testTwoStatements() throws IOException {
30+
31+
final InputStream stream = this.getStream(R.raw.two_statements);
32+
List<String> commands = SqlParser.parse(stream);
33+
34+
assertEquals(2, commands.size());
35+
assertEquals(sql1, commands.get(0));
36+
assertEquals(sql2, commands.get(1));
37+
}
38+
39+
/**
40+
* Should be able to parse a multi-line statement that has an embedded line comment.
41+
* @throws IOException
42+
*/
43+
public void testLineComment() throws IOException {
44+
45+
final InputStream stream = this.getStream(R.raw.line_comment);
46+
List<String> commands = SqlParser.parse(stream);
47+
48+
assertEquals(1, commands.size());
49+
assertEquals(sql1, commands.get(0));
50+
}
51+
52+
/**
53+
* Should be able to handle a line comment that contains string tokens.
54+
* @throws IOException
55+
*/
56+
public void testLineCommentWithString() throws IOException {
57+
58+
final InputStream stream = this.getStream(R.raw.line_comment_with_string);
59+
List<String> commands = SqlParser.parse(stream);
60+
61+
assertEquals(1, commands.size());
62+
assertEquals(sql1, commands.get(0));
63+
}
64+
65+
/**
66+
* Should ignore a block comment end token inside a line comment.
67+
* @throws IOException
68+
*/
69+
public void testLineAndBlockEndComment() throws IOException {
70+
71+
final InputStream stream = this.getStream(R.raw.line_comment_and_block_end);
72+
List<String> commands = SqlParser.parse(stream);
73+
74+
assertEquals(1, commands.size());
75+
assertEquals(sql1, commands.get(0));
76+
}
77+
78+
/**
79+
* Should be able to handle a block comment.
80+
* @throws IOException
81+
*/
82+
public void testBlockComment() throws IOException {
83+
84+
final InputStream stream = this.getStream(R.raw.block_comment);
85+
List<String> commands = SqlParser.parse(stream);
86+
87+
assertEquals(1, commands.size());
88+
assertEquals(sql1, commands.get(0));
89+
}
90+
91+
/**
92+
* Should be able to handle a block comment that contains string tokens.
93+
* @throws IOException
94+
*/
95+
public void testBlockCommentWithString() throws IOException {
96+
97+
final InputStream stream = this.getStream(R.raw.block_comment_with_string);
98+
List<String> commands = SqlParser.parse(stream);
99+
100+
assertEquals(1, commands.size());
101+
assertEquals(sql1, commands.get(0));
102+
}
103+
104+
/**
105+
* Should ignore a line comment token inside a block comment.
106+
* @throws IOException
107+
*/
108+
public void testBlockAndLineComment() throws IOException {
109+
110+
final InputStream stream = this.getStream(R.raw.block_with_line_comment);
111+
List<String> commands = SqlParser.parse(stream);
112+
113+
assertEquals(1, commands.size());
114+
assertEquals(sql1, commands.get(0));
115+
}
116+
117+
/**
118+
* Should be able to parse a script that incorrectly closes a block comment twice. The
119+
* resulting script is not expected to run, but the parser shouldn't choke on it.
120+
* @throws IOException
121+
*/
122+
public void testInvalidBlockComment() throws IOException {
123+
124+
final InputStream stream = this.getStream(R.raw.invalid_block_comment);
125+
List<String> commands = SqlParser.parse(stream);
126+
127+
assertEquals(1, commands.size());
128+
assertEquals(invalid, commands.get(0));
129+
}
130+
131+
/**
132+
* Should ignore a line comment token inside a string.
133+
* @throws IOException
134+
*/
135+
public void testStringWithLineComment() throws IOException {
136+
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '-- some text', 'some text' )";
137+
138+
final InputStream stream = this.getStream(R.raw.string_with_line_comment);
139+
List<String> commands = SqlParser.parse(stream);
140+
141+
assertEquals(1, commands.size());
142+
assertEquals(sql, commands.get(0));
143+
}
144+
145+
/**
146+
* Should ignore block comment tokens inside strings.
147+
* @throws IOException
148+
*/
149+
public void testStringWithBlockComment() throws IOException {
150+
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '/* some text', 'some text */' )";
151+
152+
final InputStream stream = this.getStream(R.raw.string_with_block_comment);
153+
List<String> commands = SqlParser.parse(stream);
154+
155+
assertEquals(1, commands.size());
156+
assertEquals(sql, commands.get(0));
157+
}
158+
159+
/**
160+
* Should be able to handle a script that contains anything nasty I can thing of right now.
161+
* @throws IOException
162+
*/
163+
public void testComplex() throws IOException {
164+
final String sql1 = "CREATE TABLE Entity2 ( Id INTEGER AUTO_INCREMENT PRIMARY KEY, Column TEXT NOT NULL, Column2 INTEGER NULL )";
165+
final String sql2 = "INSERT INTO Entity2 ( Id, Column, Column2 ) SELECT Id, Column, 0 FROM Entity";
166+
final String sql3 = "DROP TABLE Entity";
167+
final String sql4 = "ALTER TABLE Entity2 RENAME TO Entity";
168+
final String sql5 = "INSERT INTO Entity2 ( Id, Column, Column2) VALUES ( 9001 , 42, 'string /* string */ -- string' )";
169+
170+
final InputStream stream = this.getStream(R.raw.complex);
171+
List<String> commands = SqlParser.parse(stream);
172+
173+
assertEquals(5, commands.size());
174+
assertEquals(sql1, commands.get(0));
175+
assertEquals(sql2, commands.get(1));
176+
assertEquals(sql3, commands.get(2));
177+
assertEquals(sql4, commands.get(3));
178+
assertEquals(sql5, commands.get(4));
179+
}
180+
}

0 commit comments

Comments
 (0)