Permalink
Browse files

Added specs.

Fixed compatibility with Rails 2.3. Rails.root returns Pathname object instead of string (Julien L.)
  • Loading branch information...
1 parent d939123 commit 987cfc77c4a5a2d8f580256f4584d6d90f4c7d97 @pusewicz committed Apr 10, 2009
View
1 .gitignore
@@ -3,3 +3,4 @@
coverage
rdoc
pkg
+previous_failures.txt
View
39 Rakefile
@@ -17,28 +17,25 @@ rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end
-# require 'rake/testtask'
-# Rake::TestTask.new(:test) do |test|
-# test.libs << 'lib' << 'test'
-# test.pattern = 'test/**/*_test.rb'
-# test.verbose = true
-# end
-#
-# begin
-# require 'rcov/rcovtask'
-# Rcov::RcovTask.new do |test|
-# test.libs << 'test'
-# test.pattern = 'test/**/*_test.rb'
-# test.verbose = true
-# end
-# rescue LoadError
-# task :rcov do
-# abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
-# end
-# end
-
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+ spec.spec_opts = %w("--color")
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+ spec.rcov_opts = if PLATFORM =~ /darwin/
+ ['--exclude "gems/*,spec_helper.rb"']
+ else
+ ['--exclude "rubygems/*,spec_helper.rb"']
+ end
+end
-# task :default => :test
+task :default => :spec
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
View
2 lib/rails_sequel.rb
@@ -5,4 +5,4 @@
require 'rails_sequel/version'
require 'rails_sequel/sequel_ext'
-Rails::SequelConnection.connect
+RailsSequel.connect
View
88 lib/rails_sequel/rails_sequel.rb
@@ -1,50 +1,50 @@
-module Rails
- module SequelConnection
- # Load configuration for current environment
- CONFIG = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[Rails.env].with_indifferent_access unless defined?(CONFIG)
-
- # Connects to database using constructed Database Connection URI
- def self.connect
- options = self.prepare_options
- connection = Sequel.connect(options)
- if options[:adapter] == 'mysql'
- connection.execute("SET SQL_AUTO_IS_NULL=0")
- end
- connection
+module RailsSequel
+
+ # Connects to database using constructed Database Connection URI
+ def self.connect
+ connection = Sequel.connect(options = self.prepare_options)
+ if options[:adapter] == 'mysql'
+ connection.execute("SET SQL_AUTO_IS_NULL=0")
end
+ connection
+ end
+
+ # Returns loaded database.yml configuration for current environment
+ def self.config
+ @config ||= YAML::load(ERB.new(IO.read(File.join(Rails.root, "config", "database.yml"))).result)[Rails.env].with_indifferent_access
+ end
+
+ # Resets config
+ def self.reset_config!
+ @config = nil
+ end
+
+ # Constructs Database Connection URI
+ def self.prepare_options
+ options = {}
+
+ # Use SQLite by default
+ options[:adapter] = config[:adapter] || "sqlite"
- # Returns loaded database.yml configuration for current environment
- def self.config
- CONFIG
- end
+ # Use localhost as default host
+ options[:host] = config[:host] || config[:hostname] || "localhost"
- # Constructs Database Connection URI
- def self.prepare_options
- options = {}
-
- # Use SQLite by default
- options[:adapter] = config[:adapter] || "sqlite"
-
- # Use localhost as default host
- options[:host] = config[:host] || "localhost"
-
- # Default user is an empty string. Both username and user keys are supported.
- options[:user] = config[:username] || config[:user] || ""
- options[:password] = config[:password] || ""
-
- # Both encoding and charset options are supported, default is utf8
- options[:encoding] = config[:encoding] || config[:charset] || "utf8"
-
- # Default database is hey_dude_configure_your_database
- options[:database] = config[:database] || "hey_dude_configure_your_database"
-
- # MSSQL support
- options[:db_type] = config[:db_type] if config[:db_type]
- options[:socket] = config[:socket] if config[:socket]
- options[:charset] = config[:charset] if config[:charset]
- options[:encoding] = config[:encoding] if config[:encoding]
- options[:loggers] = [Rails.logger]
- options
+ # Default user is an empty string. Both username and user keys are supported.
+ options[:user] = config[:username] || config[:user] || ""
+ options[:password] = config[:password] || ""
+
+ # Both encoding and charset options are supported, default is utf8
+ options[:encoding] = config[:encoding] || config[:charset] || "utf8"
+
+ # Default database is hey_dude_configure_your_database
+ options[:database] = config[:database] || "hey_dude_configure_your_database"
+
+ # MSSQL support
+ [:db_type, :socket, :charset, :encoding].each do |var|
+ options[var] = config[var] if config[var]
end
+
+ options[:loggers] = [Rails.logger]
+ options
end
end
View
12 lib/rails_sequel/version.rb
@@ -1,10 +1,8 @@
-module Rails
- module SequelConnection
+module RailsSequel
+ # Returns current plugin version
+ def self.version
+ return @version if @version
config = YAML.load(File.read(File.join(File.dirname(__FILE__), '../../VERSION.yml')))
- VERSION = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
-
- def self.version
- VERSION
- end
+ @version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
end
end
View
21 spec/config/database.yml
@@ -0,0 +1,21 @@
+test:
+ adapter: sqlite
+ database: db/test.sqlite3
+
+mysql:
+ adapter: mysql
+ database: henry
+ hostname: john
+ username: piotr
+ password: usewicz
+ charset: brilliant
+ encoding: ascii
+ socket: /var/run/my.socket
+
+mssql:
+ db_type: sometype
+
+test_mysql:
+ adapter: mysql
+ database: mysql
+ user: root
View
74 spec/rails_sequel_spec.rb
@@ -0,0 +1,74 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+require 'rails_sequel'
+
+describe RailsSequel do
+ it "can reset config" do
+ RailsSequel.config.should_not be_nil
+ RailsSequel.config.should == RailsSequel.instance_variable_get(:@config)
+ RailsSequel.reset_config!
+ RailsSequel.instance_variable_get(:@config).should be_nil
+ end
+end
+
+describe RailsSequel, '#config' do
+ before(:each) do
+ RailsSequel.reset_config!
+ end
+
+ it "reads values for current environment" do
+ RailsSequel.config.should == {
+ "adapter" => 'sqlite',
+ "database" => 'db/test.sqlite3'
+ }
+ end
+
+ describe "MySQL" do
+ it "recognizes database options" do
+ Rails.stub!(:env).and_return("mysql")
+ RailsSequel.config.should == {
+ "adapter" => "mysql",
+ "database" => "henry",
+ "hostname" => "john",
+ "username" => "piotr",
+ "password" => "usewicz",
+ "charset" =>"brilliant",
+ "encoding" => "ascii",
+ "socket" => "/var/run/my.socket"
+ }
+ end
+ end
+
+ describe "MSSQL" do
+ it "recognizes database options" do
+ Rails.stub!(:env).and_return("mssql")
+ RailsSequel.config.should == {
+ "db_type" => "sometype"
+ }
+ end
+ end
+end
+
+describe RailsSequel, "#connect" do
+ before(:each) do
+ RailsSequel.reset_config!
+ end
+
+ it "returns connection" do
+ Sequel.stub!(:connect).and_return(MODEL_DB)
+ RailsSequel.connect.should == MODEL_DB
+ end
+
+ it "connects with configuration loaded from file" do
+ options = RailsSequel.prepare_options
+ RailsSequel.should_receive(:prepare_options).and_return(options)
+ Sequel.should_receive(:connect).with(options)
+ RailsSequel.connect
+ end
+
+ it "sets SQL_AUTO_IS_NULL if adapter is MySQL" do
+ Rails.stub!(:env).and_return('test_mysql')
+ Sequel.stub!(:connect).and_return(MODEL_DB)
+ RailsSequel.connect
+ MODEL_DB.sqls.should include("SET SQL_AUTO_IS_NULL=0")
+ end
+end
View
27 spec/sequel_ext_spec.rb
@@ -0,0 +1,27 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+require 'rails_sequel/sequel_ext'
+
+class TestModel < Sequel::Model
+end
+
+describe Sequel::Model, '#id' do
+ before(:all) do
+ @model = TestModel.create
+ end
+
+ it "to_params returns id as a string" do
+ @model.to_param.should == '1'
+ end
+end
+
+describe Sequel::Model, '#new_record?' do
+ before(:all) do
+ @model = TestModel.new
+ end
+
+ it "behaves like new?" do
+ @model.new_record?.should === @model.new?
+ @model.save
+ @model.new_record?.should === @model.new?
+ end
+end
View
8 spec/spec.opts
@@ -0,0 +1,8 @@
+--colour
+--format specdoc
+--format failing_examples:previous_failures.txt
+--example previous_failures.txt
+--loadby mtime
+--reverse
+--timeout 20
+--diff
View
115 spec/spec_helper.rb
@@ -0,0 +1,115 @@
+require 'rubygems'
+require 'erb'
+require 'rr'
+require 'spec'
+require 'sequel'
+require 'active_support/core_ext' # HashWithIndifferentAccess
+
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+Spec::Runner.configure do |config|
+ # config.mock_with :rr
+end
+
+class MockDataset < Sequel::Dataset
+ def insert(*args)
+ @db.execute insert_sql(*args)
+ end
+
+ def update(*args)
+ @db.execute update_sql(*args)
+ end
+
+ def delete(*args)
+ @db.execute delete_sql(*args)
+ end
+
+ def fetch_rows(sql)
+ return if sql =~ /information_schema/
+ @db.execute(sql)
+ yield({:id => 1, :x => 1})
+ end
+
+ def quoted_identifier(c)
+ "\"#{c}\""
+ end
+end
+
+class MockDatabase < Sequel::Database
+ @@quote_identifiers = false
+ self.identifier_input_method = nil
+ self.identifier_output_method = nil
+ attr_reader :sqls
+
+ def execute(sql, opts={})
+ @sqls ||= []
+ @sqls << sql
+ end
+
+ def reset
+ @sqls = []
+ end
+
+ def schema(table_name, opts)
+ if table_name
+ [[:id, {:primary_key=>true}]]
+ else
+ {table_name=>[[:id, {:primary_key=>true}]]}
+ end
+ end
+
+ def transaction(opts={})
+ return yield if @transactions.include?(Thread.current)
+ execute('BEGIN')
+ begin
+ @transactions << Thread.current
+ yield
+ rescue Exception => e
+ execute('ROLLBACK')
+ transaction_error(e)
+ ensure
+ unless e
+ execute('COMMIT')
+ end
+ @transactions.delete(Thread.current)
+ end
+ end
+
+ def dataset(opts=nil); MockDataset.new(self, opts); end
+end
+
+class << Sequel::Model
+ alias orig_columns columns
+ alias orig_str_columns str_columns
+ def columns(*cols)
+ return if cols.empty?
+ define_method(:columns){cols}
+ @dataset.instance_variable_set(:@columns, cols) if @dataset
+ define_method(:str_columns){cols.map{|x|x.to_s.freeze}}
+ def_column_accessor(*cols)
+ @columns = cols
+ @db_schema = {}
+ cols.each{|c| @db_schema[c] = {}}
+ end
+ def simple_table
+ nil
+ end
+end
+
+module Rails
+ def self.root
+ File.dirname(__FILE__)
+ end
+
+ def self.env
+ 'test'
+ end
+
+ def self.logger
+ Logger.new(STDOUT)
+ end
+end
+
+Sequel::Model.use_transactions = false
+Sequel::Model.db = MODEL_DB = MockDatabase.new
View
9 spec/version_spec.rb
@@ -0,0 +1,9 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+require 'rails_sequel/version'
+
+describe RailsSequel, '#version' do
+ it "returns correct version" do
+ RailsSequel.version.should_not be_empty
+ RailsSequel.version.should =~ /^\d.\d.\d$/
+ end
+end

0 comments on commit 987cfc7

Please sign in to comment.