Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed for work for multiples, null results

git-svn-id: svn://newsprojects.nytimes.com/newsdev/gems/activerecord-dbslayer-adapter@4455 ae54da50-7589-804d-96e1-6ee4b2538e53
  • Loading branch information...
commit 102cec42ea8bbe59742e0525f67b3ee1489c4bbf 1 parent 0ad2888
harrisj authored
View
11 History.txt
@@ -1,10 +1,17 @@
-== 0.0.2 2008-05-01
+== 0.2.5 2008-05-06
+
+* 3 minor enhancements:
+ * passes test for multiple results
+ * passes test for null results
+ * rcov rake task
+
+== 0.2.0 2008-05-01
* 2 major enhancments:
* tests
* it actually works!
-== 0.0.1 2008-04-15
+== 0.1.0 2008-04-15
* 1 major enhancement:
* Initial release
View
1  Manifest.txt
@@ -16,6 +16,7 @@ script/txt2html
setup.rb
tasks/deployment.rake
tasks/environment.rake
+tasks/rcov.rake
tasks/website.rake
test/helper.rb
test/localtest.rb
View
27 lib/active_record/connection_adapters/dbslayer_connection.rb
@@ -70,16 +70,21 @@ def initialize(host='localhost', port=9090)
def sql_query(sql)
dbslay_results = cmd_execute(:db, 'SQL' => sql)
- case dbslay_results
- when Hash
- # check for an error
- if dbslay_results['MYSQL_ERROR']
- raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
- else
- DbslayerResult.new(dbslay_results['RESULT'])
+ # check for an error
+ if dbslay_results['MYSQL_ERROR']
+ raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
+ elsif dbslay_results['RESULT']
+ result = dbslay_results['RESULT']
+ case result
+ when Hash
+ DbslayerResult.new(result)
+ when Array
+ result.map {|r| DbslayerResult.new(r) }
+ else
+ raise DbslayerException, "Unknown format for SQL results from DBSlayer"
end
- when Array
- dbslay_results.map { |r| DbslayerResult.new(r['RESULT']) }
+ elsif dbslay_results['SUCCESS']
+ return dbslay_results['SUCCESS']
else
raise DbslayerException, "Unknown format for SQL results from DBSlayer"
end
@@ -106,14 +111,14 @@ def client_info
def client_version_num
if @client_version.nil?
- @client_version = cmd_execute(:db, 'CLIENT_VERSION' => true)["CLIENT_VERSION"]
+ @client_version = Integer(cmd_execute(:db, 'CLIENT_VERSION' => true)["CLIENT_VERSION"])
end
@client_version
end
def server_version_num
if @server_version.nil?
- @server_version = cmd_execute(:db, 'SERVER_VERSION' => true)["SERVER_VERSION"]
+ @server_version = Integer(cmd_execute(:db, 'SERVER_VERSION' => true)["SERVER_VERSION"])
end
@server_version
end
View
2  lib/activerecord-dbslayer-adapter.rb
@@ -27,7 +27,7 @@
module ActiveRecord
module ConnectionAdapters
class DbslayerAdapter
- VERSION = '0.2.0'
+ VERSION = '0.2.5'
end
end
end
View
9 tasks/rcov.rake
@@ -0,0 +1,9 @@
+desc 'Measures test coverage'
+task :test_coverage do
+ rm_f "coverage"
+ rm_f "coverage.data"
+ rcov = "rcov --aggregate coverage.data --text-summary -Ilib"
+ system("#{rcov} --html test/test_*.rb")
+ system("open coverage/index.html") if PLATFORM['darwin']
+end
+
View
27 test/helper.rb
@@ -10,6 +10,10 @@
STAT_REPLY = {"STAT" => "STAT"}
+CLIENT_INFO_REPLY = {"CLIENT_INFO" => "5.2.27"}
+
+VERSION_NUM_REPLY = {"SERVER_VERSION" => "50037", "CLIENT_VERSION" => "50037"}
+
# Let's mock out the DBSlayer reply
CITY_ROWS = [[123, "Mumbai (Bombay)" , "India" , 10500000] ,
[4112, "Seoul" , "South Korea" , 9981619] ,
@@ -23,7 +27,7 @@
[1, "New York" , "United States" , 8008278]
].freeze
-CITY_TYPES = ["MYSQL_TYPE_STRING", "MYSQL_TYPE_STRING" , "MYSQL_TYPE_LONG"].freeze
+CITY_TYPES = ["MYSQL_TYPE_INTEGER", "MYSQL_TYPE_STRING", "MYSQL_TYPE_STRING" , "MYSQL_TYPE_LONG"].freeze
CITY_HEADER = ["id", "city_name" , "country_name" , "population"].freeze
@@ -34,6 +38,27 @@
}
}.freeze
+COUNTRY_ROWS = [[1, 'United States'], [2, 'Canada'], [3, 'India']].freeze
+
+COUNTRY_TYPES = ["MYSQL_TYPE_INTEGER", "MYSQL_TYPE_STRING"]
+
+COUNTRY_HEADER = ["id", "name"]
+
+MULTIPLE_RESULTS = {
+ "RESULT" => [{"TYPES" => CITY_TYPES,
+ "HEADER" => CITY_HEADER,
+ "ROWS" => CITY_ROWS
+ },
+
+ {"TYPES" => COUNTRY_TYPES,
+ "HEADER" => COUNTRY_HEADER,
+ "ROWS" => COUNTRY_ROWS
+ }
+
+ ]
+}.freeze
+
+NULL_RESULT = {"SUCCESS" => true}
SHOW_TABLES_REPLY = {"RESULT"=> {"HEADER"=> ["Tables_in_Test_Database"],
"ROWS" => [["table1"], ["table2"]],
View
63 test/test_dbslayer_connection.rb
@@ -2,18 +2,28 @@
class TestDbslayerConnection < Test::Unit::TestCase
- STAT_REPLY = {
- "STAT" => "THIS IS A STAT REPLY"
- }.freeze
-
- CLIENT_INFO_REPLY = {
- "CLIENT_INFO" => "5.2.27"
- }.freeze
-
def setup
@slayer = ActiveRecord::ConnectionAdapters::DbslayerConnection.new
end
+ def test_query_string
+ query = {"foo" => "bar"}
+ assert_equal URI.encode(query.to_json), @slayer.send('query_string', query)
+ end
+
+ # def cmd_execute(endpoint, commands)
+ # 147 url = "http://#{host}:#{port}/#{endpoint.to_s}?#{query_string(commands)}"
+ # 148 open(url) do |file|
+ # 149 JSON.parse(file.read)
+ # 150 end
+
+ # def test_cmd_execute_url
+ # query = {"SQL" => "select * from cities"}
+ # test = ActiveRecord::ConnectionAdapters::DbslayerConnection.new('localhost', 9090)
+ # URI.expects(:open).with("http://localhost:9090/db?#{test.send(:query_string, query)}")
+ # test.send :cmd_execute, :db, query
+ # end
+
def test_sql_query
sql_command = "select * from cities limit 10"
@slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(CITY_RESULTS)
@@ -24,6 +34,26 @@ def test_sql_query
assert_not_nil reply.rows
end
+ def test_sql_null_return
+ sql_command = "update set posted = 1"
+ @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(NULL_RESULT)
+
+ status = @slayer.sql_query(sql_command)
+ assert_equal true, status
+ end
+
+ def test_multiple_results
+ sql_command = "select * from cities limit 10; select * from countries limit 3"
+ @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(MULTIPLE_RESULTS)
+
+ reply = @slayer.sql_query(sql_command)
+ assert_kind_of Array, reply
+ assert_equal 2, reply.size
+ reply.each {|i| assert_kind_of(ActiveRecord::ConnectionAdapters::DbslayerResult, i)}
+ assert_equal CITY_ROWS, reply[0].rows
+ assert_equal COUNTRY_ROWS, reply[1].rows
+ end
+
def test_stat
@slayer.stubs(:cmd_execute).with(:db, {"STAT" => true}).returns(STAT_REPLY)
reply = @slayer.mysql_stats
@@ -40,6 +70,21 @@ def test_client_info
def test_server_error
@slayer.stubs(:cmd_execute).returns(ERROR_REPLY)
- assert_raise(DbslayerException) { @slayer.sql_query("SELECT * FROM items") }
+ assert_raise(ActiveRecord::ConnectionAdapters::DbslayerException) { @slayer.sql_query("SELECT * FROM items") }
end
+
+ def test_client_num
+ @slayer.stubs(:cmd_execute).with(:db, {"CLIENT_VERSION" => true}).returns(VERSION_NUM_REPLY)
+ reply = @slayer.client_version_num
+
+ assert_equal 50037, reply
+ end
+
+ def test_server_num
+ @slayer.stubs(:cmd_execute).with(:db, {"SERVER_VERSION" => true}).returns(VERSION_NUM_REPLY)
+ reply = @slayer.server_version_num
+
+ assert_equal 50037, reply
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.