drift/sqlparser/test/parser/moor_file_test.dart

138 lines
3.8 KiB
Dart
Raw Normal View History

2019-09-16 13:33:36 -07:00
import 'package:sqlparser/sqlparser.dart';
import 'package:test/test.dart';
import 'utils.dart';
2019-09-16 13:33:36 -07:00
const content = r'''
import 'other.dart';
import 'another.moor';
CREATE TABLE tbl (
id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
-- this is a single-line comment
place VARCHAR REFERENCES other(location)
) AS RowName;
2019-09-16 13:33:36 -07:00
all: SELECT /* COUNT(*), */ * FROM tbl WHERE $predicate;
@special: SELECT * FROM tbl;
2019-12-25 11:24:03 -08:00
typeHints(:foo AS TEXT): SELECT :foo;
2020-04-03 09:37:33 -07:00
nested: SELECT foo.** FROM tbl foo;
2019-09-16 13:33:36 -07:00
''';
void main() {
test('parses moor files', () {
testMoorFile(
content,
2019-09-16 13:33:36 -07:00
MoorFile([
ImportStatement('other.dart'),
ImportStatement('another.moor'),
CreateTableStatement(
tableName: 'tbl',
columns: [
ColumnDefinition(
columnName: 'id',
typeName: 'INT',
constraints: [
NotNull(null),
PrimaryKeyColumn(null, autoIncrement: true),
],
),
ColumnDefinition(
columnName: 'place',
typeName: 'VARCHAR',
constraints: [
ForeignKeyColumnConstraint(
null,
ForeignKeyClause(
foreignTable: TableReference('other', null),
columnNames: [
Reference(columnName: 'location'),
],
),
),
],
),
],
overriddenDataClassName: 'RowName',
2019-09-16 13:33:36 -07:00
),
DeclaredStatement(
SimpleName('all'),
2019-09-16 13:33:36 -07:00
SelectStatement(
columns: [StarResultColumn(null)],
from: TableReference('tbl', null),
2019-09-16 13:33:36 -07:00
where: DartExpressionPlaceholder(name: 'predicate'),
),
),
DeclaredStatement(
SpecialStatementIdentifier('special'),
SelectStatement(
columns: [StarResultColumn(null)],
from: TableReference('tbl', null),
),
),
2019-12-25 11:24:03 -08:00
DeclaredStatement(
SimpleName('typeHints'),
SelectStatement(columns: [
ExpressionResultColumn(
expression: ColonNamedVariable(
ColonVariableToken(fakeSpan(':foo'), ':foo'),
),
),
]),
parameters: [
VariableTypeHint(
ColonNamedVariable(
ColonVariableToken(fakeSpan(':foo'), ':foo'),
),
'TEXT',
)
],
),
2020-04-03 09:37:33 -07:00
DeclaredStatement(
SimpleName('nested'),
SelectStatement(
columns: [NestedStarResultColumn('foo')],
from: TableReference('tbl', 'foo'),
),
),
2019-09-16 13:33:36 -07:00
]),
);
});
test("reports error when the statement can't be parsed", () {
// regression test for https://github.com/simolus3/moor/issues/280#issuecomment-570789454
final parsed = SqlEngine(EngineOptions(useMoorExtensions: true))
.parseMoorFile('name: NSERT INTO foo DEFAULT VALUES;');
expect(
parsed.errors,
contains(const TypeMatcher<ParsingError>().having(
(e) => e.message,
'message',
contains('Expected a sql statement here'),
)),
);
final root = parsed.rootNode as MoorFile;
expect(
root.allDescendants,
isNot(contains(const TypeMatcher<DeclaredStatement>())),
);
});
test('syntax errors contain correct position', () {
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
final result = engine.parseMoorFile('''
worksByComposer:
SELECT DISTINCT A.* FROM works A, works B ON A.id =
WHERE A.composer = :id OR B.composer = :id;
''');
expect(result.errors, hasLength(1));
expect(
result.errors.single,
isA<ParsingError>()
.having((e) => e.token.lexeme, 'token.lexeme', 'WHERE'));
});
2019-09-16 13:33:36 -07:00
}