Skip to content

Commit

Permalink
Added multiple tests for the new parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpfeiffermway committed Apr 17, 2014
1 parent ec189d9 commit 78444cc
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/res/raw/block_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER /* This is a block comment and should be ignored */
)
5 changes: 5 additions & 0 deletions tests/res/raw/block_comment_with_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER /* This is a block comment 'with a string that doesn't matter' */
)
4 changes: 4 additions & 0 deletions tests/res/raw/block_with_line_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, /* This is a block comment -- not a line comment */ Column1 INTEGER
)
38 changes: 38 additions & 0 deletions tests/res/raw/complex.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Create table for migration
CREATE TABLE Entity2
(
Id INTEGER AUTO_INCREMENT PRIMARY KEY,
Column TEXT NOT NULL,
Column2 INTEGER NULL /* this column is new */
);

-- Migrate data
INSERT INTO Entity2
(
Id,
Column,
Column2
)
SELECT Id,
Column,
0 -- there's no such value in the old table
FROM Entity;

-- Rename Entity2 to Entity
DROP TABLE Entity;
ALTER TABLE Entity2 RENAME TO Entity;

/* Add some --sample-- data */
INSERT INTO Entity2
(
Id,
Col/*not sure if anyone would ever be insane enough to do this*/umn,
Column2--,
)
VALUES
(
9001 /* not -- really */, -- almost forgot that comma
42,--23, /* I don't know who messed this up
'string /* string */ -- string'--,
-- 'test' whoops we don't have that many columns
)
6 changes: 6 additions & 0 deletions tests/res/raw/invalid_block_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
/* /* /* This is an invalid block comment */ */
Column1 INTEGER
)
5 changes: 5 additions & 0 deletions tests/res/raw/line_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER -- This is a line comment and should be ignored
)
6 changes: 6 additions & 0 deletions tests/res/raw/line_comment_and_block_end.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
-- This is a line comment and should be ignored */ NonColumn STRING,
Column1 INTEGER
)
5 changes: 5 additions & 0 deletions tests/res/raw/line_comment_with_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER -- This is a line comment 'with a string that doesn't matter'
)
12 changes: 12 additions & 0 deletions tests/res/raw/string_with_block_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO Entity
(
Id,
Column1,
Column2
)
VALUES
(
1,
'/* some text',
'some text */'
);
12 changes: 12 additions & 0 deletions tests/res/raw/string_with_line_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO Entity
(
Id,
Column1,
Column2
)
VALUES
(
1,
'-- some text',
'some text'
);
11 changes: 11 additions & 0 deletions tests/res/raw/two_statements.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER
);

CREATE TABLE Entity2
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
Column1 INTEGER
)
180 changes: 180 additions & 0 deletions tests/src/com/activeandroid/test/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@

package com.activeandroid.test.parser;

import com.activeandroid.test.ActiveAndroidTestCase;
import com.activeandroid.test.R;
import com.activeandroid.util.SqlParser;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class ParserTest extends ActiveAndroidTestCase {

private final String sql1 = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )";
private final String sql2 = "CREATE TABLE Entity2 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )";

private final String invalid = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, */ Column1 INTEGER )";

private InputStream getStream(int id) {
return this.getContext().getResources().openRawResource(id);
}

/**
* Should be able to parse a script with two multi-line statments, even if the last statement
* is not terminated by a semicolon.
* @throws IOException
*/
public void testTwoStatements() throws IOException {

final InputStream stream = this.getStream(R.raw.two_statements);
List<String> commands = SqlParser.parse(stream);

assertEquals(2, commands.size());
assertEquals(sql1, commands.get(0));
assertEquals(sql2, commands.get(1));
}

/**
* Should be able to parse a multi-line statement that has an embedded line comment.
* @throws IOException
*/
public void testLineComment() throws IOException {

final InputStream stream = this.getStream(R.raw.line_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to handle a line comment that contains string tokens.
* @throws IOException
*/
public void testLineCommentWithString() throws IOException {

final InputStream stream = this.getStream(R.raw.line_comment_with_string);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should ignore a block comment end token inside a line comment.
* @throws IOException
*/
public void testLineAndBlockEndComment() throws IOException {

final InputStream stream = this.getStream(R.raw.line_comment_and_block_end);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to handle a block comment.
* @throws IOException
*/
public void testBlockComment() throws IOException {

final InputStream stream = this.getStream(R.raw.block_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to handle a block comment that contains string tokens.
* @throws IOException
*/
public void testBlockCommentWithString() throws IOException {

final InputStream stream = this.getStream(R.raw.block_comment_with_string);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should ignore a line comment token inside a block comment.
* @throws IOException
*/
public void testBlockAndLineComment() throws IOException {

final InputStream stream = this.getStream(R.raw.block_with_line_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to parse a script that incorrectly closes a block comment twice. The
* resulting script is not expected to run, but the parser shouldn't choke on it.
* @throws IOException
*/
public void testInvalidBlockComment() throws IOException {

final InputStream stream = this.getStream(R.raw.invalid_block_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(invalid, commands.get(0));
}

/**
* Should ignore a line comment token inside a string.
* @throws IOException
*/
public void testStringWithLineComment() throws IOException {
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '-- some text', 'some text' )";

final InputStream stream = this.getStream(R.raw.string_with_line_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql, commands.get(0));
}

/**
* Should ignore block comment tokens inside strings.
* @throws IOException
*/
public void testStringWithBlockComment() throws IOException {
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '/* some text', 'some text */' )";

final InputStream stream = this.getStream(R.raw.string_with_block_comment);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql, commands.get(0));
}

/**
* Should be able to handle a script that contains anything nasty I can thing of right now.
* @throws IOException
*/
public void testComplex() throws IOException {
final String sql1 = "CREATE TABLE Entity2 ( Id INTEGER AUTO_INCREMENT PRIMARY KEY, Column TEXT NOT NULL, Column2 INTEGER NULL )";
final String sql2 = "INSERT INTO Entity2 ( Id, Column, Column2 ) SELECT Id, Column, 0 FROM Entity";
final String sql3 = "DROP TABLE Entity";
final String sql4 = "ALTER TABLE Entity2 RENAME TO Entity";
final String sql5 = "INSERT INTO Entity2 ( Id, Column, Column2) VALUES ( 9001 , 42, 'string /* string */ -- string' )";

final InputStream stream = this.getStream(R.raw.complex);
List<String> commands = SqlParser.parse(stream);

assertEquals(5, commands.size());
assertEquals(sql1, commands.get(0));
assertEquals(sql2, commands.get(1));
assertEquals(sql3, commands.get(2));
assertEquals(sql4, commands.get(3));
assertEquals(sql5, commands.get(4));
}
}

0 comments on commit 78444cc

Please sign in to comment.