Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Added throwSqlException matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlucas committed Aug 23, 2015
1 parent f1060d9 commit 8508fe6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 70 deletions.
42 changes: 42 additions & 0 deletions test/helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:oracle/oracle.dart' as oracle;
import 'package:matcher/matcher.dart';

Matcher throwsSqlException(int code) => new SqlExceptionThrownMatcher(code);

class SqlExceptionThrownMatcher extends Matcher {
final int _code;

const SqlExceptionThrownMatcher(this._code);

bool matches(f, Map state) {
try {
f();
addStateInfo(state, {'failure_type': 'fuction retuned normally'});
return false;
} catch (e) {
if (e is! oracle.SqlException) {
addStateInfo(state, {
'failure_type': 'expected SqlException, got ${e.runtimeType}'
});
return false;
} else if (_code != e.code) {
print('rawr');
addStateInfo(state, {
'failure_type':
'error code mismatch: expected <$_code>, got <${e.code}>'
});
return false;
}

return true;
}
}

Description describe(Description description) =>
description.add('a SqlException with error code <$_code> to be thrown');

Description describeMismatch(
item, Description mismatchDescription, Map matchState, bool verbose) {
return mismatchDescription.add(matchState['failure_type']);
}
}
3 changes: 1 addition & 2 deletions test/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ main() {
con = new oracle.Connection(username, password, connString);
con.execute(
"BEGIN EXECUTE IMMEDIATE 'DROP TABLE metadata_test'; EXCEPTION WHEN OTHERS THEN NULL; END;");
con.execute(
"""CREATE TABLE metadata_test (
con.execute("""CREATE TABLE metadata_test (
test_int int,
test_string varchar(255),
test_number NUMBER(20, 5) NOT NULL)""");
Expand Down
112 changes: 56 additions & 56 deletions test/resultset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'package:oracle/oracle.dart' as oracle;
import 'package:test/test.dart';
import 'package:collection/equality.dart';
import 'helpers.dart';

const MAX_DELTA = 0.0001;

Expand Down Expand Up @@ -109,14 +110,14 @@ main() {
con.commit();
stmt = con.createStatement("""CREATE TABLE resultset_test
(test_int int,
test_string varchar(255),
test_date DATE,
test_blob BLOB,
test_clob CLOB,
test_float FLOAT,
test_number NUMBER,
test_ts TIMESTAMP,
test_tstz TIMESTAMP WITH TIME ZONE)""");
test_string varchar(255),
test_date DATE,
test_blob BLOB,
test_clob CLOB,
test_float FLOAT,
test_number NUMBER,
test_ts TIMESTAMP,
test_tstz TIMESTAMP WITH TIME ZONE)""");
stmt.execute();
con.commit();

Expand Down Expand Up @@ -159,7 +160,6 @@ main() {
expect(f, returnsNormally);
expect(f(), isNull);
});

});

group('.getInt', () {
Expand All @@ -176,12 +176,12 @@ main() {
expect(() => queryAll().getInt(1), returnsNormally);
expect(queryAll().getInt(1), isNull);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_date from resultset_test');
rs.next();
expect(() => rs.getInt(1), throws);
expect(() => rs.getInt(1), throwsSqlException(932));
});
});

Expand All @@ -204,7 +204,7 @@ main() {
expect(() => queryAll().getDouble(1), returnsNormally);
expect(queryAll().getDouble(1), isNull);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_date from resultset_test');
Expand Down Expand Up @@ -297,7 +297,7 @@ main() {
expect(f, returnsNormally);
expect(f(), isNull);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
Expand All @@ -323,7 +323,7 @@ main() {
expect(f, returnsNormally);
expect(f(), isNull);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
Expand All @@ -332,55 +332,55 @@ main() {
});
});

group('.getBlob', () {
test('with BLOB', () {
// insert as hex string
var blob = insertIntoColType('BLOB', "010203").getBlob();
group('.getBlob', () {
test('with BLOB', () {
// insert as hex string
var blob = insertIntoColType('BLOB', "010203").getBlob();

expect(blob.length(), 3);
expect(blob.read(3, 1), [1, 2, 3]);
});
expect(blob.length(), 3);
expect(blob.read(3, 1), [1, 2, 3]);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
rs.next();
test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
rs.next();

expect(() => rs.getBlob(1), throws);
});
expect(() => rs.getBlob(1), throws);
});

test('with NULL', () {
insertNulls();
var f = () => queryAll().getBlob(1);
expect(f, returnsNormally);
expect(f(), isNull);
});
});

group('.getClob', () {
test('with CLOB', () {
var expected = "string";
var clob = insertIntoColType('CLOB', expected).getClob();

expect(clob.length(), equals(expected.length));
expect(clob.read(expected.length, 1), expected);
test('with NULL', () {
insertNulls();
var f = () => queryAll().getBlob(1);
expect(f, returnsNormally);
expect(f(), isNull);
});
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
rs.next();

expect(() => rs.getClob(1), throws);
});
group('.getClob', () {
test('with CLOB', () {
var expected = "string";
var clob = insertIntoColType('CLOB', expected).getClob();

test('with NULL', () {
insertNulls();
var f = () => queryAll().getClob(1);
expect(f, returnsNormally);
expect(f(), isNull);
});
});
expect(clob.length(), equals(expected.length));
expect(clob.read(expected.length, 1), expected);
});

test('with unconvertable type', () {
insertValidRow();
var rs = con.executeQuery('SELECT test_int from resultset_test');
rs.next();

expect(() => rs.getClob(1), throws);
});

test('with NULL', () {
insertNulls();
var f = () => queryAll().getClob(1);
expect(f, returnsNormally);
expect(f(), isNull);
});
});

test('getColumnListMetadata', () {
var f = () => con
Expand Down
28 changes: 16 additions & 12 deletions test/statement_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,49 +122,53 @@ main() {
expect(rs.getDate(1), null);
});
});

group('.setDynamic', () {
test('with String', () {
var stmt = conn.createStatement('INSERT INTO stmt_test (test_string) VALUES (:1)');
var stmt = conn
.createStatement('INSERT INTO stmt_test (test_string) VALUES (:1)');
expect(() => stmt.setDynamic(1, 'test'), returnsNormally);
});

test('with int', () {
var stmt = conn.createStatement('INSERT INTO stmt_test (test_int) VALUES (:1)');
var stmt = conn
.createStatement('INSERT INTO stmt_test (test_int) VALUES (:1)');
expect(() => stmt.setDynamic(1, int.parse('5')), returnsNormally);
});

test('with double', () {
var stmt = conn.createStatement('INSERT INTO stmt_test (test_number) VALUES (:1)');
var stmt = conn
.createStatement('INSERT INTO stmt_test (test_number) VALUES (:1)');
expect(() => stmt.setDynamic(1, double.parse('5.5')), returnsNormally);
});

test('with DateTime', () {
var stmt = conn.createStatement('INSERT INTO stmt_test (test_date) VALUES (:1)');
var stmt = conn
.createStatement('INSERT INTO stmt_test (test_date) VALUES (:1)');
expect(() => stmt.setDynamic(1, new DateTime.now()), returnsNormally);
});
});

group('.execute', () {
test('with List', () {
helper(colName, argsList) =>
conn.execute('INSERT INTO stmt_test ($colName) VALUES(:1)', argsList);
helper(colName, argsList) => conn.execute(
'INSERT INTO stmt_test ($colName) VALUES(:1)', argsList);

expect(() => helper('test_string', ['test']), returnsNormally);

expect(() => helper('test_int', [5]), returnsNormally);

expect(() => helper('test_number', [5.5]), returnsNormally);

expect(() => helper('test_date', [new DateTime.now()]), returnsNormally);

expect(
() => helper('test_date', [new DateTime.now()]), returnsNormally);

// should fail if too many args given
expect(() => helper('test_string', ['test', 5]), throws);
});
});
}); // Statement


test('Connection(username, password, connString)', () {
expect(() => new oracle.Connection(username, password, connString),
returnsNormally);
Expand Down

0 comments on commit 8508fe6

Please sign in to comment.