Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved code coverage - now at 99.2%. Fixed String#to_time to raise …

…error correctly for invalid time stamps. Version 0.5.0.2.
  • Loading branch information...
commit ce46f77df425d61fc071a10f951069e75c5f57c4 1 parent 65a863f
@ciconia ciconia authored
View
6 core/CHANGELOG
@@ -1,3 +1,9 @@
+=== 0.5.0.2 (2007-01-01)
+
+* Fixed String#to_time to raise error correctly for invalid time stamps.
+
+* Improved code coverage - now at 99.2%.
+
=== 0.5.0.1 (2007-12-31)
* Added a stub for Sequel::Model that auto-loads sequel_model.
View
2  core/Rakefile
@@ -9,7 +9,7 @@ include FileUtils
# Configuration
##############################################################################
NAME = "sequel"
-VERS = "0.5.0.1"
+VERS = "0.5.0.2"
CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
RDOC_OPTS = [
"--quiet",
View
2  core/lib/sequel/core_sql.rb
@@ -50,7 +50,7 @@ def to_time
begin
Time.parse(self)
rescue Exception => e
- raise Error::InvalidValue, "Invalid time value '#{self}' (#{e.message})"
+ raise Sequel::Error::InvalidValue, "Invalid time value '#{self}' (#{e.message})"
end
# Why does Time.parse('0000-00-00') bork and not return nil or some such?
end
View
3  core/lib/sequel/dataset/sequelizer.rb
@@ -140,9 +140,6 @@ def call_expr(e, b, opts)
r = eval_expr(e[3][1], b, opts)
if l.is_one_of?(Symbol, Sequel::SQL::Subscript)
l|r
- elsif l.is_one_of?(Symbol, Sequel::LiteralString, Sequel::SQL::Expression) || \
- r.is_one_of?(Symbol, Sequel::LiteralString, Sequel::SQL::Expression)
- "(#{literal(l)} #{op} #{literal(r)})".lit
else
ext_expr(e, b, opts)
end
View
50 core/lib/sequel/schema/schema_generator.rb
@@ -72,57 +72,57 @@ def initialize(db, &block)
end
def add_column(name, type, opts = {})
- @operations << {
- :op => :add_column,
- :name => name,
- :type => type
+ @operations << { \
+ :op => :add_column, \
+ :name => name, \
+ :type => type \
}.merge(opts)
end
def drop_column(name)
- @operations << {
- :op => :drop_column,
- :name => name
+ @operations << { \
+ :op => :drop_column, \
+ :name => name \
}
end
def rename_column(name, new_name, opts = {})
- @operations << {
- :op => :rename_column,
- :name => name,
- :new_name => new_name
+ @operations << { \
+ :op => :rename_column, \
+ :name => name, \
+ :new_name => new_name \
}.merge(opts)
end
def set_column_type(name, type)
- @operations << {
- :op => :set_column_type,
- :name => name,
- :type => type
+ @operations << { \
+ :op => :set_column_type, \
+ :name => name, \
+ :type => type \
}
end
def set_column_default(name, default)
- @operations << {
- :op => :set_column_default,
- :name => name,
- :default => default
+ @operations << { \
+ :op => :set_column_default, \
+ :name => name, \
+ :default => default \
}
end
def add_index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
- @operations << {
- :op => :add_index,
- :columns => columns
+ @operations << { \
+ :op => :add_index, \
+ :columns => columns \
}.merge(opts)
end
def drop_index(columns)
columns = [columns] unless columns.is_a?(Array)
- @operations << {
- :op => :drop_index,
- :columns => columns
+ @operations << { \
+ :op => :drop_index, \
+ :columns => columns \
}
end
end
View
135 core/spec/array_keys_spec.rb
@@ -534,6 +534,130 @@ def fetch_rows(sql, &block)
@ds.set_model(Hash)
end
+ specify "should work correctly with dataset with transforms" do
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
+ Sequel.use_array_tuples
+
+ @ds.transform(:a => [proc {|x| x + 10}, proc {|x| x - 10}])
+ a = @ds.first
+ a.class.should == Array
+ a[:a].should == 11
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+
+ a = @ds.all[0]
+ a.class.should == Array
+ a[:a].should == 11
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+ end
+
+ specify "should work correctly with dataset with model" do
+ ccc = Class.new do
+ attr_reader :values
+ def initialize(v)
+ @values = v
+ end
+ end
+
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
+ Sequel.use_array_tuples
+
+ @ds.set_model(ccc)
+ a = @ds.first
+ a.class.should == ccc
+ a.values.class.should == Array
+ a.values[:a].should == 1
+ a.values[:b].should == 2
+ a.values[:c].should == 3
+ a.values[:d].should == nil
+
+ a = @ds.all[0]
+ a.class.should == ccc
+ a.values.class.should == Array
+ a.values[:a].should == 1
+ a.values[:b].should == 2
+ a.values[:c].should == 3
+ a.values[:d].should == nil
+
+ @ds.each(:naked => true) do |a|
+ a.class.should == Array
+ a[:a].should == 1
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+ end
+ end
+
+ specify "should work correctly with dataset with model and transform" do
+ ccc = Class.new do
+ attr_reader :values
+ def initialize(v)
+ @values = v
+ end
+ end
+
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
+ Sequel.use_array_tuples
+
+ @ds.transform(:a => [proc {|x| x + 10}, proc {|x| x - 10}])
+ @ds.set_model(ccc)
+ a = @ds.first
+ a.class.should == ccc
+ a.values.class.should == Array
+ a.values[:a].should == 11
+ a.values[:b].should == 2
+ a.values[:c].should == 3
+ a.values[:d].should == nil
+
+ a = @ds.all[0]
+ a.class.should == ccc
+ a.values.class.should == Array
+ a.values[:a].should == 11
+ a.values[:b].should == 2
+ a.values[:c].should == 3
+ a.values[:d].should == nil
+
+ @ds.each(:naked => true) do |a|
+ a.class.should == Array
+ a[:a].should == 11
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+ end
+ end
+
+ specify "should work correctly with denuded dataset" do
+ ccc = Class.new do
+ attr_reader :values
+ def initialize(v)
+ @values = v
+ end
+ end
+
+ @ds.first.should == {:a => 1, :b => 2, :c => 3}
+ Sequel.use_array_tuples
+
+ @ds.set_model(ccc)
+ @ds.set_model(nil)
+
+ a = @ds.first
+ a.class.should == Array
+ a[:a].should == 1
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+
+ a = @ds.all[0]
+ a.class.should == Array
+ a[:a].should == 1
+ a[:b].should == 2
+ a[:c].should == 3
+ a[:d].should == nil
+ end
+
specify "should be reversible using Sequel.use_hash_tuples" do
Sequel.use_array_tuples
@ds.first.class.should == Array
@@ -541,4 +665,15 @@ def fetch_rows(sql, &block)
Sequel.use_hash_tuples
@ds.first.should == {:a => 1, :b => 2, :c => 3}
end
+
+ specify "should apply and unapply correctly to dataset with array_tuples_fetch_rows" do
+ @c.class_def(:fetch_rows) {'yo hash'}
+ @c.class_def(:array_tuples_fetch_rows) {'yo array'}
+ Sequel.use_array_tuples
+
+ @ds.fetch_rows.should == 'yo array'
+
+ Sequel.use_hash_tuples
+ @ds.fetch_rows.should == 'yo hash'
+ end
end
View
7 core/spec/core_ext_spec.rb
@@ -8,13 +8,6 @@
end
end
-context "String#to_time" do
- specify "should convert the string into a Time object" do
- "2007-07-11".to_time.should == Time.parse("2007-07-11")
- "06:30".to_time.should == Time.parse("06:30")
- end
-end
-
context "Range#interval" do
specify "should return the interval between the beginning and end of the range" do
(1..10).interval.should == 9
View
11 core/spec/core_sql_spec.rb
@@ -288,3 +288,14 @@ def quote_column_ref(c); "`#{c}`"; end
end
end
+context "String#to_time" do
+ specify "should convert the string into a Time object" do
+ "2007-07-11".to_time.should == Time.parse("2007-07-11")
+ "06:30".to_time.should == Time.parse("06:30")
+ end
+
+ specify "should raise Error::InvalidValue for an invalid time" do
+ proc {'0000-00-00'.to_time}.should raise_error(Sequel::Error::InvalidValue)
+ end
+end
+
View
25 core/spec/database_spec.rb
@@ -557,6 +557,20 @@ class CCC < Sequel::Database
end
end
+context "A broken adapter (lib is there but the class is not)" do
+ setup do
+ FileUtils.touch('lib/sequel/adapters/blah.rb')
+ end
+
+ teardown do
+ FileUtils.rm('lib/sequel/adapters/blah.rb')
+ end
+
+ specify "should raise an error" do
+ proc {Sequel.connect('blah://blow')}.should raise_error(Sequel::Error::AdapterNotFound)
+ end
+end
+
context "Database#uri_to_options" do
specify "should convert a URI to an options hash" do
h = Sequel::Database.uri_to_options(URI.parse('ttt://uuu:ppp@192.168.60.1:1234/blah'))
@@ -785,4 +799,13 @@ def fetch_rows(sql); yield({:sql => sql}); end
end
end
-
+# TODO: beaf this up with specs for all supported ops
+context "Database#alter_table_sql" do
+ setup do
+ @db = DummyDatabase.new
+ end
+
+ specify "should raise error for an invalid op" do
+ proc {@db.alter_table_sql(:mau, :op => :blah)}.should raise_error(Sequel::Error)
+ end
+end
View
31 core/spec/dataset_spec.rb
@@ -1,13 +1,5 @@
require File.join(File.dirname(__FILE__), "spec_helper")
-SQLITE_DB = Sequel("sqlite:/")
-SQLITE_DB.create_table :items do
- integer :id, :primary_key => true, :auto_increment => true
- text :name
- float :value
-end
-SQLITE_DB.create_table(:time) {timestamp :t}
-
context "Dataset" do
setup do
@dataset = Sequel::Dataset.new("db")
@@ -1425,10 +1417,6 @@ def fetch_rows(sql); end
@d = @c.new(nil).from(:test)
@e = @cc.new(nil).from(:test)
-
- @d_empty = SQLITE_DB[:items]
- @d_empty.delete # remove all records
-
end
specify "should call each and return the first record" do
@@ -1440,7 +1428,7 @@ def fetch_rows(sql); end
end
specify "should return nil if no record is present" do
- @d_empty.single_record.should be_nil
+ @e.single_record.should be_nil
end
end
@@ -1451,9 +1439,12 @@ def fetch_rows(sql)
yield({1 => sql})
end
end
+ @cc = Class.new(@c) do
+ def fetch_rows(sql); end
+ end
+
@d = @c.new(nil).from(:test)
- @d_empty = SQLITE_DB[:items]
- @d_empty.delete # remove all records
+ @e = @cc.new(nil).from(:test)
end
specify "should call each and return the first value of the first record" do
@@ -1465,7 +1456,7 @@ def fetch_rows(sql)
end
specify "should return nil" do
- @d_empty.single_value.should be_nil
+ @e.single_value.should be_nil
end
end
@@ -1741,11 +1732,15 @@ def fetch_rows(sql, &block)
@d.set_model(@m)
end
- specify "should destroy raise for every model in the dataset" do
+ specify "should call destroy for every model instance in the dataset" do
count = @d.destroy
count.should == 2
DESTROYED.should == MODELS
- end
+ end
+
+ specify "should raise error if no models are associated with the dataset" do
+ proc {@d.naked.destroy}.should raise_error(Sequel::Error)
+ end
end
context "Dataset#<<" do
View
8 core/spec/pretty_table_spec.rb
@@ -35,6 +35,14 @@
/\n(\|x\|y\|)|(\|y\|x\|)\n/
end
+ specify "should infer columns from array with keys" do
+ a = [1, 2, 3]
+ a.keys = [:a, :b, :c]
+ Sequel::PrettyTable.print([a])
+ @output.rewind
+ @output.read.should =~ /\n\|a\|b\|c\|\n/
+ end
+
specify "should calculate the maximum width of each column correctly" do
Sequel::PrettyTable.print(@data2, [:a, :b])
@output.rewind
View
26 core/spec/schema_generator_spec.rb
@@ -57,4 +57,30 @@
@indexes[1][:columns].should include(:title)
@indexes[1][:columns].should include(:body)
end
+end
+
+describe Sequel::Schema::AlterTableGenerator do
+ before :all do
+ @generator = Sequel::Schema::AlterTableGenerator.new(SchemaDummyDatabase.new) do
+ add_column :aaa, :text
+ drop_column :bbb
+ rename_column :ccc, :ho
+ set_column_type :ddd, :float
+ set_column_default :eee, 1
+ add_index [:fff, :ggg]
+ drop_index :hhh
+ end
+ end
+
+ specify "should generate operation records" do
+ @generator.operations.should == [
+ {:op => :add_column, :name => :aaa, :type => :text},
+ {:op => :drop_column, :name => :bbb},
+ {:op => :rename_column, :name => :ccc, :new_name => :ho},
+ {:op => :set_column_type, :name => :ddd, :type => :float},
+ {:op => :set_column_default, :name => :eee, :default => 1},
+ {:op => :add_index, :columns => [:fff, :ggg]},
+ {:op => :drop_index, :columns => [:hhh]}
+ ]
+ end
end
View
10 core/spec/schema_spec.rb
@@ -1,14 +1,5 @@
require File.join(File.dirname(__FILE__), 'spec_helper')
-class SchemaDummyDatabase < Sequel::Database
- attr_reader :sqls
-
- def execute(sql)
- @sqls ||= []
- @sqls << sql
- end
-end
-
context "DB#create_table" do
setup do
@db = SchemaDummyDatabase.new
@@ -237,4 +228,3 @@ def execute(sql)
@db.sqls.should == ['DROP TABLE cats']
end
end
-
View
10 core/spec/sequel_spec.rb
@@ -0,0 +1,10 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe "Sequel::Model()" do
+ specify "should auto-load sequel_model and create a sequel model" do
+ db = Sequel::Database.new
+ Sequel::Model.instance_eval {@db = db}
+ c = Class.new(Sequel::Model(:items))
+ c.dataset.sql.should == "SELECT * FROM items"
+ end
+end
View
5 core/spec/sequelizer_spec.rb
@@ -327,6 +327,11 @@ def t(x)
proc {:units * :price}.to_sql.should == "(units * price)"
end
+ specify "should support | operator" do
+ proc {(:x | 1) > 0}.to_sql.should == "(x[1] > 0)"
+ proc {10 | 1}.to_sql.should == 11
+ end
+
specify "should support globals" do
$aaaa_zzzz = 400
proc {:x > $aaaa_zzzz}.to_sql.should == "(x > 400)"
View
10 core/spec/spec_helper.rb
@@ -32,3 +32,13 @@ def transaction; yield; end
def dataset; MockDataset.new(self); end
end
+
+class SchemaDummyDatabase < Sequel::Database
+ attr_reader :sqls
+
+ def execute(sql)
+ @sqls ||= []
+ @sqls << sql
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.