Changeset: dc8551d51e59 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=dc8551d51e59
Modified Files:
        clients/ruby/lib/MonetDB.rb
        clients/ruby/lib/MonetDBConnection.rb
        clients/ruby/lib/demo.rb
        clients/ruby/lib/test/test_capabilities.rb
Branch: default
Log Message:

Refactored unit tests in lib/test/test_capabilities.rb
Added protocol and server identifiers as constant values in MonetDBConnection.
Added a new connection method MonetDB::conn() that supports named parameters 
passing (MonetDB::connection()
has been retained for API compatibility).


diffs (truncated from 647 to 300 lines):

diff -r fb63972843a0 -r dc8551d51e59 clients/ruby/lib/MonetDB.rb
--- a/clients/ruby/lib/MonetDB.rb       Mon Jan 17 10:40:47 2011 +0100
+++ b/clients/ruby/lib/MonetDB.rb       Sun Jan 16 00:29:08 2011 +0100
@@ -186,11 +186,42 @@
     # demo.rb contains usage example of the above mentioned methods.
 
   class MonetDB
+    DEFAULT_USERNAME = "monetdb"
+    DEFAULT_PASSWORD = "monetdb"
+    DEFAULT_LANG     = LANG_SQL
+    DEFAULT_HOST     = "127.0.0.1"
+    DEFAULT_PORT     = 50000
+    DEFAULT_DATABASE = "test"
+    DEFAULT_AUTHTYPE = "SHA1"
+    
     def initalize()
       @connection = nil 
     end
   
     # Establish a new connection.
+    #                * username: username (default is monetdb)
+    #                * password: password (default is monetdb)
+    #                * lang: language (default is sql) 
+    #                * host: server hostanme or ip  (default is localhost)
+    #                * port: server port (default is 50000)
+    #                * db_name: name of the database to connect to
+    #                * auth_type: hashing function to use during 
authentication (default is SHA1)
+    def connect(username=DEFAULT_USERNAME, password=DEFAULT_PASSWORD, 
lang=DEFAULT_LANG, host=DEFAULT_HOST, port=DEFAULT_PORT, 
db_name=DEFAULT_DATABASE, auth_type=DEFAULT_AUTHTYPE)
+      # TODO: handle pools of connections
+      
+      @username = username
+      @password = password
+      @lang = lang
+      @host = host
+      @port = port
+      @db_name = db_name
+      @auth_type = auth_type
+      
+      @connection = MonetDBConnection.new(user = @username, passwd = 
@password, lang = @lang, host = @host, port = @port)
+      @connection.connect(@db_name, @auth_type)
+    end
+  
+    # Establish a new connection using named parameters.
     #                * user: username (default is monetdb)
     #                * passwd: password (default is monetdb)
     #                * lang: language (default is sql) 
@@ -198,22 +229,29 @@
     #                * port: server port (default is 50000)
     #                * db_name: name of the database to connect to
     #                * auth_type: hashing function to use during 
authentication (default is SHA1)
-    def connect(username = "monetdb", password = "monetdb", lang = "sql", 
host="127.0.0.1", port = "50000", db_name = "test", auth_type = "SHA1")
-      # TODO: handle pools of connections
-      @username = username
-      @password = password
-      @lang = lang
-      @host = host
-      @port = port
-      @db_name = db_name
-      @auth_type = auth_type
-      @connection = MonetDBConnection.new(user = @username, passwd = 
@password, lang = @lang, host = @host, port = @port)
-      @connection.connect(@db_name, @auth_type)
+    #
+    # Conventionally named parameters are passed as an hash.
+    #
+    # Ruby 1.8:
+    # MonetDB::conn({ :user => "username", :passwd => "password", :db_name => 
"database"})
+    #
+    # Ruby 1.9:
+    # MonetDB::conn(user: "username", passwd: "password", db_name: "database")
+    def conn(options)      
+      user      = options[:user] || DEFAULT_USERNAME
+      passwd    = options[:passwd] || DEFAULT_PASSWORD
+      lang      = options[:lang] || DEFAULT_LANG
+      host      = options[:host] || DEFAULT_HOST
+      port      = options[:port] || DEFAULT_PORT
+      db_name   = options[:db_name] || DEFAULT_DATABASE
+      auth_type = options[:auth_type] || DEFAULT_AUTHTYPE
+      
+      connect(user, passwd, lang, host, port, db_name, auth_type)
     end
   
     # Send a <b> user submitted </b> query to the server and store the 
response.
     # Returns and instance of MonetDBData.
-    def query(q = "")
+    def query(q="")
       if  @connection != nil 
         @data = MonetDBData.new(@connection)
         @data.execute(q)    
diff -r fb63972843a0 -r dc8551d51e59 clients/ruby/lib/MonetDBConnection.rb
--- a/clients/ruby/lib/MonetDBConnection.rb     Mon Jan 17 10:40:47 2011 +0100
+++ b/clients/ruby/lib/MonetDBConnection.rb     Sun Jan 16 00:29:08 2011 +0100
@@ -52,9 +52,16 @@
 LANG_XQUERY = "xquery"
 XQUERY_OUTPUT_SEQ = true # use monetdb xquery's output seq
 
+# Protocols
+MAPIv8 = 8
+MAPIv9 = 9
+
+MONETDB_MEROVINGIAN = "merovingian"
+MONETDB_MSERVER     = "monetdb"
+
+MEROVINGIAN_MAX_ITERATIONS = 10
+
 class MonetDBConnection
- 
-  
   # enable debug output
   @@DEBUG               = false
 
@@ -68,7 +75,7 @@
   @@CLIENT_ENDIANNESS   = "BIG"
   
   # MAPI protocols supported by the driver
-  @@SUPPORTED_PROTOCOLS = [ 8, 9 ]
+  @@SUPPORTED_PROTOCOLS = [ MAPIv8, MAPIv9 ]
   
   attr_reader :socket, :auto_commit, :transactions, :lang
   
@@ -123,14 +130,13 @@
   def real_connect
     
     server_challenge = retrieve_server_challenge()
-    print "CHALLENGE: " + server_challenge.to_s
     if server_challenge != nil
       salt = server_challenge.split(':')[0]
       @server_name = server_challenge.split(':')[1]
       @protocol = server_challenge.split(':')[2].to_i
       @supported_auth_types = server_challenge.split(':')[3].split(',')
       @server_endianness = server_challenge.split(':')[4]
-      if @protocol == 9
+      if @protocol == MAPIv9
         @pwhash = server_challenge.split(':')[5]
       end
     else
@@ -208,14 +214,14 @@
             end
           end
           
-          if server_name == "merovingian"
-            if @auth_iteration <= 10
+          if server_name == MONETDB_MEROVINGIAN
+            if @auth_iteration <= MEROVINGIAN_MAX_ITERATIONS
               @auth_iteration += 1
               real_connect
             else
               raise MonetDBConnectionError, "Merovingian: too many iterations 
while proxying."
             end
-          elsif server_name == "monetdb"
+          elsif server_name == MONETDB_MSERVER
             begin
               @socket.close
             rescue
@@ -486,7 +492,7 @@
   
   # Check if monetdb is running behind the merovingian proxy and forward the 
connection in case
   def merovingian?
-    if @server_name.downcase == 'merovingian'
+    if @server_name.downcase == MONETDB_MEROVINGIAN
       true
     else
       false
@@ -494,7 +500,7 @@
   end
   
   def mserver?
-    if @server_name.downcase == 'monetdb'
+    if @server_name.downcase == MONETDB_MSERVER
       true
     else
       false
@@ -503,7 +509,7 @@
   
   # Check which protocol is spoken by the server
   def mapi_proto_v8?
-    if @protocol == 8
+    if @protocol == MAPIv8
       true
     else
       false
@@ -511,7 +517,7 @@
   end
   
   def mapi_proto_v9?
-    if @protocol == 9
+    if @protocol == MAPIv9
       true
     else
       false
@@ -521,13 +527,15 @@
 
 # handles transactions and savepoints. Can be used to simulate nested 
transactions.
 class MonetDBTransaction  
+  SAVEPOINT_STRING = "monetdbsp"
+  
   def initialize
     @id = 0
     @savepoint = ""
   end
   
   def savepoint
-    @savepoint = "monetdbsp" + @id.to_s
+    @savepoint = SAVEPOINT_STRING + @id.to_s
   end
   
   def release
diff -r fb63972843a0 -r dc8551d51e59 clients/ruby/lib/demo.rb
--- a/clients/ruby/lib/demo.rb  Mon Jan 17 10:40:47 2011 +0100
+++ b/clients/ruby/lib/demo.rb  Sun Jan 16 00:29:08 2011 +0100
@@ -19,7 +19,7 @@
 
 db = MonetDB.new
 
-db.connect(user = "monetdb", passwd = "monetdb", lang = "sql", 
host="127.0.0.1", port = 50000, db_name = "ruby", auth_type = "SHA1")
+db.conn({ :user => "monetdb", :passwd => "monetdb", :port => 50000, :lang => 
"sql", :host => "localhost", :db_name => "ruby_test", :auth_type => "SHA1" })
 
 # set type_cast=true to enable MonetDB to Ruby type mapping
 #res = db.query("select * from tables, tables, tables;")
diff -r fb63972843a0 -r dc8551d51e59 clients/ruby/lib/test/test_capabilities.rb
--- a/clients/ruby/lib/test/test_capabilities.rb        Mon Jan 17 10:40:47 
2011 +0100
+++ b/clients/ruby/lib/test/test_capabilities.rb        Sun Jan 16 00:29:08 
2011 +0100
@@ -12,8 +12,9 @@
 require 'time'
 require 'date'
 
+
+
 class TC_MonetDBCapabilities < Test::Unit::TestCase
-  
   # ruby rand function does not support MIN..MAX bounds.
   # This alias adds that feature.
   alias original_rand rand
@@ -28,9 +29,23 @@
     end
   end
   
+  # check the existance of a table
+  def table_exists?(table='test_ruby')    
+    begin
+      res = @db.query("select * from #{table} where 1=0")
+      return true
+    rescue
+      return false
+    end
+  end
+  
+  def drop_table(table='test_ruby')
+    res = @db.query("DROP TABLE #{table}")
+  end
+  
   def setup
     @db = MonetDB.new
-    @db.connect(user = "monetdb", passwd = "monetdb", lang = "sql", 
host="localhost", port = 50000, db_name = "ruby", auth_type = "SHA1")
+    @db.connect(user = "monetdb", passwd = "monetdb", lang = "sql", 
host="localhost", port = 50000, db_name = "ruby_test", auth_type = "SHA1")
     
   end
 
@@ -40,26 +55,25 @@
   
   # CREATE TABLE test
   def test_create_table(table='test_ruby', cols = [ "First_Name varchar(255)", 
"Second_Name varchar(255)"])
-    cols_def =  ""
-    cols.each do |c| cols_def += c + ',' end
-    cols_def = cols_def.chop # remove last ',' character
     
-    res = @db.query('CREATE TABLE ' + table + ' (' + cols_def + ')') 
+    if table_exists?(table)
+      drop_table(table)
+    end
+    
+    colsdef =  ""
+    cols.each do |c| colsdef += c + ',' end
+      
+    colsdef = colsdef.chop # remove last ',' character
+    
+    
+    res = @db.query('CREATE TABLE ' + table + ' (' + colsdef + ')') 
   end
   
-  # check the existance of a table
-  def test_table_exists(table='test_ruby')
-    test_create_table(table)
-    
-    res = @db.query('select * from ' + table + ' where 1=0')
-    assert_equal([], res.fetch_all)
-    
-    test_drop_table(table)
-  end
   
   # perform an inserstion of 'data' into 'table' and check the resulting 
   # length
-  def test_data_integrity(table = 'test_ruby', data = ["Gabriele", 'MODENA'])
+  def test_data_integrity(table='test_ruby', data=["Gabriele", "MODENA"])
+    test_create_table
         
     values = ""
     data.each do |d| values += '\'' + d.to_s + '\'' + ',' end
@@ -69,22 +83,14 @@
     
     @db.query(insert)
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to