pusewicz-rails_sequel 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -17,28 +17,25 @@ rescue LoadError
17
17
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
18
  end
19
19
 
20
- # require 'rake/testtask'
21
- # Rake::TestTask.new(:test) do |test|
22
- # test.libs << 'lib' << 'test'
23
- # test.pattern = 'test/**/*_test.rb'
24
- # test.verbose = true
25
- # end
26
- #
27
- # begin
28
- # require 'rcov/rcovtask'
29
- # Rcov::RcovTask.new do |test|
30
- # test.libs << 'test'
31
- # test.pattern = 'test/**/*_test.rb'
32
- # test.verbose = true
33
- # end
34
- # rescue LoadError
35
- # task :rcov do
36
- # abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
- # end
38
- # end
39
-
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ spec.spec_opts = %w("--color")
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ spec.rcov_opts = if PLATFORM =~ /darwin/
32
+ ['--exclude "gems/*,spec_helper.rb"']
33
+ else
34
+ ['--exclude "rubygems/*,spec_helper.rb"']
35
+ end
36
+ end
40
37
 
41
- # task :default => :test
38
+ task :default => :spec
42
39
 
43
40
  require 'rake/rdoctask'
44
41
  Rake::RDocTask.new do |rdoc|
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 1
4
- :patch: 4
3
+ :minor: 2
4
+ :patch: 0
@@ -1,50 +1,50 @@
1
- module Rails
2
- module SequelConnection
3
- # Load configuration for current environment
4
- CONFIG = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[Rails.env].with_indifferent_access unless defined?(CONFIG)
5
-
6
- # Connects to database using constructed Database Connection URI
7
- def self.connect
8
- options = self.prepare_options
9
- connection = Sequel.connect(options)
10
- if options[:adapter] == 'mysql'
11
- connection.execute("SET SQL_AUTO_IS_NULL=0")
12
- end
13
- connection
1
+ module RailsSequel
2
+
3
+ # Connects to database using constructed Database Connection URI
4
+ def self.connect
5
+ connection = Sequel.connect(options = self.prepare_options)
6
+ if options[:adapter] == 'mysql'
7
+ connection.execute("SET SQL_AUTO_IS_NULL=0")
14
8
  end
9
+ connection
10
+ end
11
+
12
+ # Returns loaded database.yml configuration for current environment
13
+ def self.config
14
+ @config ||= YAML::load(ERB.new(IO.read(File.join(Rails.root, "config", "database.yml"))).result)[Rails.env].with_indifferent_access
15
+ end
16
+
17
+ # Resets config
18
+ def self.reset_config!
19
+ @config = nil
20
+ end
21
+
22
+ # Constructs Database Connection URI
23
+ def self.prepare_options
24
+ options = {}
25
+
26
+ # Use SQLite by default
27
+ options[:adapter] = config[:adapter] || "sqlite"
15
28
 
16
- # Returns loaded database.yml configuration for current environment
17
- def self.config
18
- CONFIG
19
- end
29
+ # Use localhost as default host
30
+ options[:host] = config[:host] || config[:hostname] || "localhost"
20
31
 
21
- # Constructs Database Connection URI
22
- def self.prepare_options
23
- options = {}
24
-
25
- # Use SQLite by default
26
- options[:adapter] = config[:adapter] || "sqlite"
27
-
28
- # Use localhost as default host
29
- options[:host] = config[:host] || "localhost"
30
-
31
- # Default user is an empty string. Both username and user keys are supported.
32
- options[:user] = config[:username] || config[:user] || ""
33
- options[:password] = config[:password] || ""
34
-
35
- # Both encoding and charset options are supported, default is utf8
36
- options[:encoding] = config[:encoding] || config[:charset] || "utf8"
37
-
38
- # Default database is hey_dude_configure_your_database
39
- options[:database] = config[:database] || "hey_dude_configure_your_database"
40
-
41
- # MSSQL support
42
- options[:db_type] = config[:db_type] if config[:db_type]
43
- options[:socket] = config[:socket] if config[:socket]
44
- options[:charset] = config[:charset] if config[:charset]
45
- options[:encoding] = config[:encoding] if config[:encoding]
46
- options[:loggers] = [Rails.logger]
47
- options
32
+ # Default user is an empty string. Both username and user keys are supported.
33
+ options[:user] = config[:username] || config[:user] || ""
34
+ options[:password] = config[:password] || ""
35
+
36
+ # Both encoding and charset options are supported, default is utf8
37
+ options[:encoding] = config[:encoding] || config[:charset] || "utf8"
38
+
39
+ # Default database is hey_dude_configure_your_database
40
+ options[:database] = config[:database] || "hey_dude_configure_your_database"
41
+
42
+ # MSSQL support
43
+ [:db_type, :socket, :charset, :encoding].each do |var|
44
+ options[var] = config[var] if config[var]
48
45
  end
46
+
47
+ options[:loggers] = [Rails.logger]
48
+ options
49
49
  end
50
50
  end
@@ -1,10 +1,8 @@
1
- module Rails
2
- module SequelConnection
1
+ module RailsSequel
2
+ # Returns current plugin version
3
+ def self.version
4
+ return @version if @version
3
5
  config = YAML.load(File.read(File.join(File.dirname(__FILE__), '../../VERSION.yml')))
4
- VERSION = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
5
-
6
- def self.version
7
- VERSION
8
- end
6
+ @version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
9
7
  end
10
8
  end
data/lib/rails_sequel.rb CHANGED
@@ -5,4 +5,4 @@ require 'rails_sequel/rails_sequel'
5
5
  require 'rails_sequel/version'
6
6
  require 'rails_sequel/sequel_ext'
7
7
 
8
- Rails::SequelConnection.connect
8
+ RailsSequel.connect
@@ -0,0 +1,21 @@
1
+ test:
2
+ adapter: sqlite
3
+ database: db/test.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ database: henry
8
+ hostname: john
9
+ username: piotr
10
+ password: usewicz
11
+ charset: brilliant
12
+ encoding: ascii
13
+ socket: /var/run/my.socket
14
+
15
+ mssql:
16
+ db_type: sometype
17
+
18
+ test_mysql:
19
+ adapter: mysql
20
+ database: mysql
21
+ user: root
@@ -0,0 +1,74 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel'
3
+
4
+ describe RailsSequel do
5
+ it "can reset config" do
6
+ RailsSequel.config.should_not be_nil
7
+ RailsSequel.config.should == RailsSequel.instance_variable_get(:@config)
8
+ RailsSequel.reset_config!
9
+ RailsSequel.instance_variable_get(:@config).should be_nil
10
+ end
11
+ end
12
+
13
+ describe RailsSequel, '#config' do
14
+ before(:each) do
15
+ RailsSequel.reset_config!
16
+ end
17
+
18
+ it "reads values for current environment" do
19
+ RailsSequel.config.should == {
20
+ "adapter" => 'sqlite',
21
+ "database" => 'db/test.sqlite3'
22
+ }
23
+ end
24
+
25
+ describe "MySQL" do
26
+ it "recognizes database options" do
27
+ Rails.stub!(:env).and_return("mysql")
28
+ RailsSequel.config.should == {
29
+ "adapter" => "mysql",
30
+ "database" => "henry",
31
+ "hostname" => "john",
32
+ "username" => "piotr",
33
+ "password" => "usewicz",
34
+ "charset" =>"brilliant",
35
+ "encoding" => "ascii",
36
+ "socket" => "/var/run/my.socket"
37
+ }
38
+ end
39
+ end
40
+
41
+ describe "MSSQL" do
42
+ it "recognizes database options" do
43
+ Rails.stub!(:env).and_return("mssql")
44
+ RailsSequel.config.should == {
45
+ "db_type" => "sometype"
46
+ }
47
+ end
48
+ end
49
+ end
50
+
51
+ describe RailsSequel, "#connect" do
52
+ before(:each) do
53
+ RailsSequel.reset_config!
54
+ end
55
+
56
+ it "returns connection" do
57
+ Sequel.stub!(:connect).and_return(MODEL_DB)
58
+ RailsSequel.connect.should == MODEL_DB
59
+ end
60
+
61
+ it "connects with configuration loaded from file" do
62
+ options = RailsSequel.prepare_options
63
+ RailsSequel.should_receive(:prepare_options).and_return(options)
64
+ Sequel.should_receive(:connect).with(options)
65
+ RailsSequel.connect
66
+ end
67
+
68
+ it "sets SQL_AUTO_IS_NULL if adapter is MySQL" do
69
+ Rails.stub!(:env).and_return('test_mysql')
70
+ Sequel.stub!(:connect).and_return(MODEL_DB)
71
+ RailsSequel.connect
72
+ MODEL_DB.sqls.should include("SET SQL_AUTO_IS_NULL=0")
73
+ end
74
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel/sequel_ext'
3
+
4
+ class TestModel < Sequel::Model
5
+ end
6
+
7
+ describe Sequel::Model, '#id' do
8
+ before(:all) do
9
+ @model = TestModel.create
10
+ end
11
+
12
+ it "to_params returns id as a string" do
13
+ @model.to_param.should == '1'
14
+ end
15
+ end
16
+
17
+ describe Sequel::Model, '#new_record?' do
18
+ before(:all) do
19
+ @model = TestModel.new
20
+ end
21
+
22
+ it "behaves like new?" do
23
+ @model.new_record?.should === @model.new?
24
+ @model.save
25
+ @model.new_record?.should === @model.new?
26
+ end
27
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format specdoc
3
+ --format failing_examples:previous_failures.txt
4
+ --example previous_failures.txt
5
+ --loadby mtime
6
+ --reverse
7
+ --timeout 20
8
+ --diff
@@ -0,0 +1,115 @@
1
+ require 'rubygems'
2
+ require 'erb'
3
+ require 'rr'
4
+ require 'spec'
5
+ require 'sequel'
6
+ require 'active_support/core_ext' # HashWithIndifferentAccess
7
+
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+
11
+ Spec::Runner.configure do |config|
12
+ # config.mock_with :rr
13
+ end
14
+
15
+ class MockDataset < Sequel::Dataset
16
+ def insert(*args)
17
+ @db.execute insert_sql(*args)
18
+ end
19
+
20
+ def update(*args)
21
+ @db.execute update_sql(*args)
22
+ end
23
+
24
+ def delete(*args)
25
+ @db.execute delete_sql(*args)
26
+ end
27
+
28
+ def fetch_rows(sql)
29
+ return if sql =~ /information_schema/
30
+ @db.execute(sql)
31
+ yield({:id => 1, :x => 1})
32
+ end
33
+
34
+ def quoted_identifier(c)
35
+ "\"#{c}\""
36
+ end
37
+ end
38
+
39
+ class MockDatabase < Sequel::Database
40
+ @@quote_identifiers = false
41
+ self.identifier_input_method = nil
42
+ self.identifier_output_method = nil
43
+ attr_reader :sqls
44
+
45
+ def execute(sql, opts={})
46
+ @sqls ||= []
47
+ @sqls << sql
48
+ end
49
+
50
+ def reset
51
+ @sqls = []
52
+ end
53
+
54
+ def schema(table_name, opts)
55
+ if table_name
56
+ [[:id, {:primary_key=>true}]]
57
+ else
58
+ {table_name=>[[:id, {:primary_key=>true}]]}
59
+ end
60
+ end
61
+
62
+ def transaction(opts={})
63
+ return yield if @transactions.include?(Thread.current)
64
+ execute('BEGIN')
65
+ begin
66
+ @transactions << Thread.current
67
+ yield
68
+ rescue Exception => e
69
+ execute('ROLLBACK')
70
+ transaction_error(e)
71
+ ensure
72
+ unless e
73
+ execute('COMMIT')
74
+ end
75
+ @transactions.delete(Thread.current)
76
+ end
77
+ end
78
+
79
+ def dataset(opts=nil); MockDataset.new(self, opts); end
80
+ end
81
+
82
+ class << Sequel::Model
83
+ alias orig_columns columns
84
+ alias orig_str_columns str_columns
85
+ def columns(*cols)
86
+ return if cols.empty?
87
+ define_method(:columns){cols}
88
+ @dataset.instance_variable_set(:@columns, cols) if @dataset
89
+ define_method(:str_columns){cols.map{|x|x.to_s.freeze}}
90
+ def_column_accessor(*cols)
91
+ @columns = cols
92
+ @db_schema = {}
93
+ cols.each{|c| @db_schema[c] = {}}
94
+ end
95
+ def simple_table
96
+ nil
97
+ end
98
+ end
99
+
100
+ module Rails
101
+ def self.root
102
+ File.dirname(__FILE__)
103
+ end
104
+
105
+ def self.env
106
+ 'test'
107
+ end
108
+
109
+ def self.logger
110
+ Logger.new(STDOUT)
111
+ end
112
+ end
113
+
114
+ Sequel::Model.use_transactions = false
115
+ Sequel::Model.db = MODEL_DB = MockDatabase.new
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require 'rails_sequel/version'
3
+
4
+ describe RailsSequel, '#version' do
5
+ it "returns correct version" do
6
+ RailsSequel.version.should_not be_empty
7
+ RailsSequel.version.should =~ /^\d.\d.\d$/
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusewicz-rails_sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Usewicz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-06 00:00:00 -07:00
12
+ date: 2009-04-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,6 +31,12 @@ files:
31
31
  - lib/rails_sequel/rails_sequel.rb
32
32
  - lib/rails_sequel/sequel_ext.rb
33
33
  - lib/rails_sequel/version.rb
34
+ - spec/config/database.yml
35
+ - spec/rails_sequel_spec.rb
36
+ - spec/sequel_ext_spec.rb
37
+ - spec/spec.opts
38
+ - spec/spec_helper.rb
39
+ - spec/version_spec.rb
34
40
  has_rdoc: true
35
41
  homepage: http://github.com/pusewicz/rails_sequel
36
42
  post_install_message:
@@ -57,5 +63,8 @@ rubygems_version: 1.2.0
57
63
  signing_key:
58
64
  specification_version: 2
59
65
  summary: Sequel plugin for Ruby on Rails
60
- test_files: []
61
-
66
+ test_files:
67
+ - spec/rails_sequel_spec.rb
68
+ - spec/sequel_ext_spec.rb
69
+ - spec/spec_helper.rb
70
+ - spec/version_spec.rb