Permalink
Browse files

Testing more errors and handle a bit differently.

  • Loading branch information...
metaskills committed Sep 30, 2010
1 parent 2a8b8e9 commit 9a8100baeddd23e49ec850d259063015cefc9cb2
Showing with 32 additions and 13 deletions.
  1. +2 −6 ext/tiny_tds/client.c
  2. +30 −7 test/result_test.rb
View
@@ -50,12 +50,8 @@ int tinytds_err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, c
int tinytds_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line) {
static char *source = "message";
- /* TODO: Why Does FreeTDS Not Cope With Errors So Well
- 208: SELECT * FROM [foobar] - TinyTds::Error - {:severity=>16, :db_error_number=>208, :message=>"Invalid object name 'foobar'.", :os_error_number=>1, :source=>"message"} */
- if (msgno == 208)
- rb_tinytds_raise_error(dbproc, 1, msgtext, source, severity, msgno, msgstate);
if (severity)
- rb_tinytds_raise_error(dbproc, 0, msgtext, source, severity, msgno, msgstate);
+ rb_tinytds_raise_error(dbproc, 1, msgtext, source, severity, msgno, msgstate);
return 0;
}
@@ -111,7 +107,7 @@ static VALUE rb_tinytds_closed(VALUE self) {
static VALUE rb_tinytds_execute(VALUE self, VALUE sql) {
GET_CLIENT_WRAPPER(self);
REQUIRE_OPEN_CLIENT(cwrap);
- dbcmd(cwrap->client, StringValuePtr(sql));
+ dbcmd(cwrap->client, StringValuePtr(sql));
if (dbsqlexec(cwrap->client) == FAIL) {
printf("\nTODO: Account for dbsqlexec() returned FAIL.\n");
return Qfalse;
View
@@ -103,7 +103,12 @@ class ResultTest < TinyTds::TestCase
end
context 'when shit happens' do
-
+
+ should 'cope with nil or empty buffer' do
+ assert_raise(TypeError) { @client.execute(nil) }
+ assert_equal [], @client.execute('').each
+ end
+
should 'throw an error when you execute another query with other results pending' do
result1 = @client.execute(@query1)
action = lambda { @client.execute(@query1) }
@@ -114,19 +119,37 @@ class ResultTest < TinyTds::TestCase
end
end
- should 'raise error for bad sql' do
- assert_raise(TinyTds::Error) { @client.execute('SELECT * FROM [foobar]') }
+ should 'error gracefully with bad table name' do
+ assert_raise_tinytds_error(lambda{ @client.execute('SELECT * FROM [foobar]') }) do |e|
+ assert_match %r|invalid object name.*foobar|i, e.message
+ assert_equal 16, e.severity
+ assert_equal 208, e.db_error_number
+ end
+ assert_followup_query
end
-
- should 'allow good query after bad table name' do
- assert_raise(TinyTds::Error) { @client.execute('SELECT * FROM [foobar]') }
- assert_nothing_raised() { @client.execute(@query1).each }
+
+ should 'error gracefully with invalid syntax' do
+ assert_raise_tinytds_error(lambda{ @client.execute('this will not work') }) do |e|
+ assert_match %r|incorrect syntax|i, e.message
+ assert_equal 15, e.severity
+ assert_equal 156, e.db_error_number
+ end
+ assert_followup_query
end
end
end
+ protected
+
+ def assert_followup_query
+ assert_nothing_raised do
+ result = @client.execute(@query1)
+ assert_equal 1, result.first['one']
+ end
+ end
+
end

0 comments on commit 9a8100b

Please sign in to comment.