sequel 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +1551 -4
- data/README +306 -19
- data/Rakefile +84 -56
- data/bin/sequel +106 -0
- data/doc/cheat_sheet.rdoc +225 -0
- data/doc/dataset_filtering.rdoc +182 -0
- data/lib/sequel_core.rb +136 -0
- data/lib/sequel_core/adapters/adapter_skeleton.rb +54 -0
- data/lib/sequel_core/adapters/ado.rb +80 -0
- data/lib/sequel_core/adapters/db2.rb +148 -0
- data/lib/sequel_core/adapters/dbi.rb +117 -0
- data/lib/sequel_core/adapters/informix.rb +78 -0
- data/lib/sequel_core/adapters/jdbc.rb +186 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +55 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +47 -0
- data/lib/sequel_core/adapters/mysql.rb +231 -0
- data/lib/sequel_core/adapters/odbc.rb +155 -0
- data/lib/sequel_core/adapters/odbc_mssql.rb +106 -0
- data/lib/sequel_core/adapters/openbase.rb +64 -0
- data/lib/sequel_core/adapters/oracle.rb +170 -0
- data/lib/sequel_core/adapters/postgres.rb +199 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +275 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +351 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +146 -0
- data/lib/sequel_core/adapters/sqlite.rb +138 -0
- data/lib/sequel_core/connection_pool.rb +194 -0
- data/lib/sequel_core/core_ext.rb +203 -0
- data/lib/sequel_core/core_sql.rb +184 -0
- data/lib/sequel_core/database.rb +471 -0
- data/lib/sequel_core/database/schema.rb +156 -0
- data/lib/sequel_core/dataset.rb +457 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +245 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +889 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +42 -0
- data/lib/sequel_core/migration.rb +187 -0
- data/lib/sequel_core/object_graph.rb +216 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/schema/generator.rb +239 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/sql.rb +812 -0
- data/lib/sequel_model.rb +5 -1
- data/lib/sequel_model/association_reflection.rb +3 -8
- data/lib/sequel_model/base.rb +15 -10
- data/lib/sequel_model/inflector.rb +3 -5
- data/lib/sequel_model/plugins.rb +1 -1
- data/lib/sequel_model/record.rb +11 -3
- data/lib/sequel_model/schema.rb +4 -4
- data/lib/sequel_model/validations.rb +6 -1
- data/spec/adapters/ado_spec.rb +17 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +764 -0
- data/spec/adapters/oracle_spec.rb +222 -0
- data/spec/adapters/postgres_spec.rb +441 -0
- data/spec/adapters/spec_helper.rb +7 -0
- data/spec/adapters/sqlite_spec.rb +400 -0
- data/spec/integration/dataset_test.rb +51 -0
- data/spec/integration/eager_loader_test.rb +702 -0
- data/spec/integration/schema_test.rb +102 -0
- data/spec/integration/spec_helper.rb +44 -0
- data/spec/integration/type_test.rb +43 -0
- data/spec/rcov.opts +2 -0
- data/spec/sequel_core/connection_pool_spec.rb +363 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +427 -0
- data/spec/sequel_core/database_spec.rb +964 -0
- data/spec/sequel_core/dataset_spec.rb +2977 -0
- data/spec/sequel_core/expression_filters_spec.rb +346 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +234 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +122 -0
- data/spec/sequel_core/schema_spec.rb +497 -0
- data/spec/sequel_core/spec_helper.rb +51 -0
- data/spec/{association_reflection_spec.rb → sequel_model/association_reflection_spec.rb} +6 -6
- data/spec/{associations_spec.rb → sequel_model/associations_spec.rb} +47 -18
- data/spec/{base_spec.rb → sequel_model/base_spec.rb} +2 -1
- data/spec/{caching_spec.rb → sequel_model/caching_spec.rb} +0 -0
- data/spec/{dataset_methods_spec.rb → sequel_model/dataset_methods_spec.rb} +13 -1
- data/spec/{eager_loading_spec.rb → sequel_model/eager_loading_spec.rb} +75 -14
- data/spec/{hooks_spec.rb → sequel_model/hooks_spec.rb} +4 -4
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/{model_spec.rb → sequel_model/model_spec.rb} +30 -11
- data/spec/{plugins_spec.rb → sequel_model/plugins_spec.rb} +0 -0
- data/spec/{record_spec.rb → sequel_model/record_spec.rb} +47 -6
- data/spec/{schema_spec.rb → sequel_model/schema_spec.rb} +18 -4
- data/spec/{spec_helper.rb → sequel_model/spec_helper.rb} +3 -2
- data/spec/{validations_spec.rb → sequel_model/validations_spec.rb} +37 -17
- data/spec/spec_config.rb +9 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +110 -37
- data/spec/inflector_spec.rb +0 -34
|
@@ -349,24 +349,24 @@ describe "Model#before_validation && Model#after_validation" do
|
|
|
349
349
|
|
|
350
350
|
specify "should be called around validation" do
|
|
351
351
|
@c.before_validation{MODEL_DB << "BLAH before"}
|
|
352
|
-
m = @c.
|
|
352
|
+
m = @c.load(:id => 2233)
|
|
353
353
|
m.should be_valid
|
|
354
354
|
MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
355
355
|
|
|
356
356
|
MODEL_DB.sqls.clear
|
|
357
|
-
m = @c.
|
|
357
|
+
m = @c.load(:id => 22)
|
|
358
358
|
m.should_not be_valid
|
|
359
359
|
MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
360
360
|
end
|
|
361
361
|
|
|
362
362
|
specify "should be called when calling save" do
|
|
363
363
|
@c.before_validation{MODEL_DB << "BLAH before"}
|
|
364
|
-
m = @c.
|
|
364
|
+
m = @c.load(:id => 2233)
|
|
365
365
|
m.save.should == m
|
|
366
366
|
MODEL_DB.sqls.should == ['BLAH before', 'BLAH after', 'CREATE BLAH']
|
|
367
367
|
|
|
368
368
|
MODEL_DB.sqls.clear
|
|
369
|
-
m = @c.
|
|
369
|
+
m = @c.load(:id => 22)
|
|
370
370
|
m.raise_on_save_failure = false
|
|
371
371
|
m.save.should == nil
|
|
372
372
|
MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
|
+
|
|
3
|
+
describe String do
|
|
4
|
+
it "#camelize and #camelcase should transform the word to CamelCase" do
|
|
5
|
+
"egg_and_hams".camelize.should == "EggAndHams"
|
|
6
|
+
"egg_and_hams".camelize(false).should == "eggAndHams"
|
|
7
|
+
"post".camelize.should == "Post"
|
|
8
|
+
"post".camelcase.should == "Post"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "#constantize should eval the string to get a constant" do
|
|
12
|
+
"String".constantize.should == String
|
|
13
|
+
"String::Inflections".constantize.should == String::Inflections
|
|
14
|
+
proc{"BKSDDF".constantize}.should raise_error
|
|
15
|
+
proc{"++A++".constantize}.should raise_error
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "#dasherize should transform underscores to dashes" do
|
|
19
|
+
"egg_and_hams".dasherize.should == "egg-and-hams"
|
|
20
|
+
"post".dasherize.should == "post"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "#demodulize should remove any preceding modules" do
|
|
24
|
+
"String::Inflections::Blah".demodulize.should == "Blah"
|
|
25
|
+
"String::Inflections".demodulize.should == "Inflections"
|
|
26
|
+
"String".demodulize.should == "String"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "#humanize should remove _i, transform underscore to spaces, and capitalize" do
|
|
30
|
+
"egg_and_hams".humanize.should == "Egg and hams"
|
|
31
|
+
"post".humanize.should == "Post"
|
|
32
|
+
"post_id".humanize.should == "Post"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "#titleize and #titlecase should underscore, humanize, and capitalize all words" do
|
|
36
|
+
"egg-and: hams".titleize.should == "Egg And: Hams"
|
|
37
|
+
"post".titleize.should == "Post"
|
|
38
|
+
"post".titlecase.should == "Post"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "#underscore should add underscores between CamelCased words, change :: to / and - to _, and downcase" do
|
|
42
|
+
"EggAndHams".underscore.should == "egg_and_hams"
|
|
43
|
+
"EGGAndHams".underscore.should == "egg_and_hams"
|
|
44
|
+
"Egg::And::Hams".underscore.should == "egg/and/hams"
|
|
45
|
+
"post".underscore.should == "post"
|
|
46
|
+
"post-id".underscore.should == "post_id"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "#pluralize should transform words from singular to plural" do
|
|
50
|
+
"post".pluralize.should == "posts"
|
|
51
|
+
"octopus".pluralize.should =="octopi"
|
|
52
|
+
"the blue mailman".pluralize.should == "the blue mailmen"
|
|
53
|
+
"CamelOctopus".pluralize.should == "CamelOctopi"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "#singularize should transform words from plural to singular" do
|
|
57
|
+
"posts".singularize.should == "post"
|
|
58
|
+
"octopi".singularize.should == "octopus"
|
|
59
|
+
"the blue mailmen".singularize.should == "the blue mailman"
|
|
60
|
+
"CamelOctopi".singularize.should == "CamelOctopus"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "#tableize should transform class names to table names" do
|
|
64
|
+
"RawScaledScorer".tableize.should == "raw_scaled_scorers"
|
|
65
|
+
"egg_and_ham".tableize.should == "egg_and_hams"
|
|
66
|
+
"fancyCategory".tableize.should == "fancy_categories"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "#classify should tranform table names to class names" do
|
|
70
|
+
"egg_and_hams".classify.should == "EggAndHam"
|
|
71
|
+
"post".classify.should == "Post"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "#foreign_key should create a foreign key name from a class name" do
|
|
75
|
+
"Message".foreign_key.should == "message_id"
|
|
76
|
+
"Message".foreign_key(false).should == "messageid"
|
|
77
|
+
"Admin::Post".foreign_key.should == "post_id"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
describe String::Inflections do
|
|
82
|
+
before do
|
|
83
|
+
@plurals, @singulars, @uncountables = String.inflections.plurals.dup, String.inflections.singulars.dup, String.inflections.uncountables.dup
|
|
84
|
+
end
|
|
85
|
+
after do
|
|
86
|
+
String.inflections.plurals.replace(@plurals)
|
|
87
|
+
String.inflections.singulars.replace(@singulars)
|
|
88
|
+
String.inflections.uncountables.replace(@uncountables)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "should be possible to clear the list of singulars, plurals, and uncountables" do
|
|
92
|
+
String.inflections.clear(:plurals)
|
|
93
|
+
String.inflections.plurals.should == []
|
|
94
|
+
String.inflections.plural('blah', 'blahs')
|
|
95
|
+
String.inflections.clear
|
|
96
|
+
String.inflections.plurals.should == []
|
|
97
|
+
String.inflections.singulars.should == []
|
|
98
|
+
String.inflections.uncountables.should == []
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it "should be able to specify new inflection rules" do
|
|
102
|
+
String.inflections do |i|
|
|
103
|
+
i.plural(/xx$/i, 'xxx')
|
|
104
|
+
i.singular(/ttt$/i, 'tt')
|
|
105
|
+
i.irregular('yy', 'yyy')
|
|
106
|
+
i.uncountable(%w'zz')
|
|
107
|
+
end
|
|
108
|
+
'roxx'.pluralize.should == 'roxxx'
|
|
109
|
+
'rottt'.singularize.should == 'rott'
|
|
110
|
+
'yy'.pluralize.should == 'yyy'
|
|
111
|
+
'yyy'.singularize.should == 'yy'
|
|
112
|
+
'zz'.pluralize.should == 'zz'
|
|
113
|
+
'zz'.singularize.should == 'zz'
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "should be yielded and returned by String.inflections" do
|
|
117
|
+
String.inflections{|i| i.should == String::Inflections}.should == String::Inflections
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), "spec_helper")
|
|
|
2
2
|
|
|
3
3
|
describe Sequel::Model do
|
|
4
4
|
it "should have class method aliased as model" do
|
|
5
|
-
Sequel::Model.instance_methods.should include("model")
|
|
5
|
+
Sequel::Model.instance_methods.collect{|x| x.to_s}.should include("model")
|
|
6
6
|
|
|
7
7
|
model_a = Class.new(Sequel::Model(:items))
|
|
8
8
|
model_a.new.model.should be(model_a)
|
|
@@ -79,18 +79,28 @@ describe Sequel::Model, "dataset & schema" do
|
|
|
79
79
|
@model.primary_key.should == :id
|
|
80
80
|
@model.table_name.should == :foo
|
|
81
81
|
end
|
|
82
|
+
|
|
83
|
+
it "doesn't raise an error on set_dataset if there is an error raised getting the schema" do
|
|
84
|
+
@model.meta_def(:get_db_schema){raise Sequel::Error}
|
|
85
|
+
proc{@model.set_dataset(MODEL_DB[:foo])}.should_not raise_error
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "doesn't raise an error on inherited if there is an error setting the dataset" do
|
|
89
|
+
@model.meta_def(:set_dataset){raise Sequel::Error}
|
|
90
|
+
proc{Class.new(@model)}.should_not raise_error
|
|
91
|
+
end
|
|
82
92
|
end
|
|
83
93
|
|
|
84
94
|
describe Sequel::Model, "#sti_key" do
|
|
85
95
|
before do
|
|
86
|
-
class StiTest < Sequel::Model
|
|
96
|
+
class ::StiTest < Sequel::Model
|
|
87
97
|
def kind=(x); self[:kind] = x; end
|
|
88
98
|
def refresh; end
|
|
89
99
|
set_sti_key :kind
|
|
90
100
|
end
|
|
91
|
-
class StiTestSub1 < StiTest
|
|
101
|
+
class ::StiTestSub1 < StiTest
|
|
92
102
|
end
|
|
93
|
-
class StiTestSub2 < StiTest
|
|
103
|
+
class ::StiTestSub2 < StiTest
|
|
94
104
|
end
|
|
95
105
|
@ds = StiTest.dataset
|
|
96
106
|
MODEL_DB.reset
|
|
@@ -435,15 +445,15 @@ describe Sequel::Model, "attribute accessors" do
|
|
|
435
445
|
|
|
436
446
|
it "should be created on set_dataset unless lazy loading schema" do
|
|
437
447
|
%w'x y x= y='.each do |x|
|
|
438
|
-
@c.instance_methods.include
|
|
448
|
+
@c.instance_methods.collect{|y| y.to_s}.should_not include(x)
|
|
439
449
|
end
|
|
440
450
|
@c.set_dataset(@dataset)
|
|
441
451
|
%w'x y x= y='.each do |x|
|
|
442
|
-
@c.instance_methods.include
|
|
452
|
+
@c.instance_methods.collect{|y| y.to_s}.should include(x)
|
|
443
453
|
end
|
|
444
454
|
o = @c.new
|
|
445
455
|
%w'x y x= y='.each do |x|
|
|
446
|
-
o.methods.include
|
|
456
|
+
o.methods.collect{|y| y.to_s}.should include(x)
|
|
447
457
|
end
|
|
448
458
|
|
|
449
459
|
o.x.should be_nil
|
|
@@ -454,18 +464,18 @@ describe Sequel::Model, "attribute accessors" do
|
|
|
454
464
|
it "should be created on first initialization if lazy loading schema" do
|
|
455
465
|
Sequel::Model.lazy_load_schema = true
|
|
456
466
|
%w'x y x= y='.each do |x|
|
|
457
|
-
@c.instance_methods.include
|
|
467
|
+
@c.instance_methods.collect{|y| y.to_s}.should_not include(x)
|
|
458
468
|
end
|
|
459
469
|
@c.set_dataset(@dataset)
|
|
460
470
|
%w'x y x= y='.each do |x|
|
|
461
|
-
@c.instance_methods.include
|
|
471
|
+
@c.instance_methods.collect{|y| y.to_s}.should_not include(x)
|
|
462
472
|
end
|
|
463
473
|
o = @c.new
|
|
464
474
|
%w'x y x= y='.each do |x|
|
|
465
|
-
@c.instance_methods.include
|
|
475
|
+
@c.instance_methods.collect{|y| y.to_s}.should include(x)
|
|
466
476
|
end
|
|
467
477
|
%w'x y x= y='.each do |x|
|
|
468
|
-
o.methods.include
|
|
478
|
+
o.methods.collect{|y| y.to_s}.should include(x)
|
|
469
479
|
end
|
|
470
480
|
|
|
471
481
|
o.x.should be_nil
|
|
@@ -596,3 +606,12 @@ context "Model.db_schema" do
|
|
|
596
606
|
@c.db_schema.should == {:x=>{}}
|
|
597
607
|
end
|
|
598
608
|
end
|
|
609
|
+
|
|
610
|
+
context "Model.str_columns" do
|
|
611
|
+
specify "should return the columns as frozen strings" do
|
|
612
|
+
c = Class.new(Sequel::Model)
|
|
613
|
+
c.meta_def(:columns){[:a, :b]}
|
|
614
|
+
c.orig_str_columns.should == %w'a b'
|
|
615
|
+
proc{c.orig_str_columns.first << 'a'}.should raise_error
|
|
616
|
+
end
|
|
617
|
+
end
|
|
File without changes
|
|
@@ -33,7 +33,7 @@ describe "Model#save" do
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should mark saved columns as not changed" do
|
|
36
|
-
o = @c.
|
|
36
|
+
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
37
37
|
o[:y] = 4
|
|
38
38
|
o.changed_columns.should == [:y]
|
|
39
39
|
o.save(:x)
|
|
@@ -50,6 +50,7 @@ describe "Model#save_changes" do
|
|
|
50
50
|
MODEL_DB.reset
|
|
51
51
|
|
|
52
52
|
@c = Class.new(Sequel::Model(:items)) do
|
|
53
|
+
unrestrict_primary_key
|
|
53
54
|
columns :id, :x, :y
|
|
54
55
|
end
|
|
55
56
|
end
|
|
@@ -110,6 +111,7 @@ describe "Model#update_values" do
|
|
|
110
111
|
MODEL_DB.reset
|
|
111
112
|
|
|
112
113
|
@c = Class.new(Sequel::Model(:items)) do
|
|
114
|
+
unrestrict_primary_key
|
|
113
115
|
columns :id, :x, :y
|
|
114
116
|
end
|
|
115
117
|
end
|
|
@@ -141,6 +143,7 @@ describe "Model#set_values" do
|
|
|
141
143
|
MODEL_DB.reset
|
|
142
144
|
|
|
143
145
|
@c = Class.new(Sequel::Model(:items)) do
|
|
146
|
+
unrestrict_primary_key
|
|
144
147
|
columns :id, :x, :y
|
|
145
148
|
end
|
|
146
149
|
end
|
|
@@ -164,6 +167,10 @@ describe "Model#set_values" do
|
|
|
164
167
|
o.set_values('x' => 1)
|
|
165
168
|
o.x.should == 1
|
|
166
169
|
end
|
|
170
|
+
|
|
171
|
+
it "should raise an error if used with a non-String, non-Symbol key" do
|
|
172
|
+
proc{@c.new.set_values(1=>2)}.should raise_error(Sequel::Error)
|
|
173
|
+
end
|
|
167
174
|
end
|
|
168
175
|
|
|
169
176
|
describe "Model#new?" do
|
|
@@ -172,6 +179,7 @@ describe "Model#new?" do
|
|
|
172
179
|
MODEL_DB.reset
|
|
173
180
|
|
|
174
181
|
@c = Class.new(Sequel::Model(:items)) do
|
|
182
|
+
unrestrict_primary_key
|
|
175
183
|
end
|
|
176
184
|
end
|
|
177
185
|
|
|
@@ -380,6 +388,12 @@ describe Sequel::Model, "#set" do
|
|
|
380
388
|
@o1.values.should == {:x => 1}
|
|
381
389
|
MODEL_DB.sqls.should == []
|
|
382
390
|
end
|
|
391
|
+
|
|
392
|
+
it "should return self" do
|
|
393
|
+
returned_value = @o1.set_with_params(:x => 1, :z => 2)
|
|
394
|
+
returned_value.should == @o1
|
|
395
|
+
MODEL_DB.sqls.should == []
|
|
396
|
+
end
|
|
383
397
|
end
|
|
384
398
|
|
|
385
399
|
describe Sequel::Model, "#update" do
|
|
@@ -712,6 +726,7 @@ describe Sequel::Model, ".create" do
|
|
|
712
726
|
before(:each) do
|
|
713
727
|
MODEL_DB.reset
|
|
714
728
|
@c = Class.new(Sequel::Model(:items)) do
|
|
729
|
+
unrestrict_primary_key
|
|
715
730
|
columns :x
|
|
716
731
|
end
|
|
717
732
|
end
|
|
@@ -730,7 +745,7 @@ describe Sequel::Model, ".create" do
|
|
|
730
745
|
|
|
731
746
|
it "should accept a block and run it" do
|
|
732
747
|
o1, o2, o3 = nil, nil, nil
|
|
733
|
-
o = @c.create {|
|
|
748
|
+
o = @c.create {|o4| o1 = o4; o3 = o4; o2 = :blah; o3.x = 333}
|
|
734
749
|
o.class.should == @c
|
|
735
750
|
o1.should === o
|
|
736
751
|
o3.should === o
|
|
@@ -750,7 +765,8 @@ describe Sequel::Model, "#refresh" do
|
|
|
750
765
|
setup do
|
|
751
766
|
MODEL_DB.reset
|
|
752
767
|
@c = Class.new(Sequel::Model(:items)) do
|
|
753
|
-
|
|
768
|
+
unrestrict_primary_key
|
|
769
|
+
columns :id, :x
|
|
754
770
|
end
|
|
755
771
|
end
|
|
756
772
|
|
|
@@ -815,6 +831,30 @@ describe Sequel::Model, "typecasting" do
|
|
|
815
831
|
m.x.should == 1
|
|
816
832
|
end
|
|
817
833
|
|
|
834
|
+
specify "should typecast '' to nil unless type is string or blob" do
|
|
835
|
+
[:integer, :float, :decimal, :boolean, :date, :time, :datetime].each do |x|
|
|
836
|
+
@c.instance_variable_set(:@db_schema, {:x=>{:type=>x}})
|
|
837
|
+
m = @c.new
|
|
838
|
+
m.x = ''
|
|
839
|
+
m.x.should == nil
|
|
840
|
+
end
|
|
841
|
+
[:string, :blob].each do |x|
|
|
842
|
+
@c.instance_variable_set(:@db_schema, {:x=>{:type=>x}})
|
|
843
|
+
m = @c.new
|
|
844
|
+
m.x = ''
|
|
845
|
+
m.x.should == ''
|
|
846
|
+
end
|
|
847
|
+
end
|
|
848
|
+
|
|
849
|
+
specify "should not typecast '' to nil if typecast_empty_string_to_nil is false" do
|
|
850
|
+
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer}})
|
|
851
|
+
m = @c.new
|
|
852
|
+
m.typecast_empty_string_to_nil = false
|
|
853
|
+
proc{m.x = ''}.should raise_error
|
|
854
|
+
@c.typecast_empty_string_to_nil = false
|
|
855
|
+
proc{@c.new.x = ''}.should raise_error
|
|
856
|
+
end
|
|
857
|
+
|
|
818
858
|
specify "should not typecast nil if NULLs are allowed" do
|
|
819
859
|
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer,:allow_null=>true}})
|
|
820
860
|
m = @c.new
|
|
@@ -825,6 +865,7 @@ describe Sequel::Model, "typecasting" do
|
|
|
825
865
|
specify "should raise an error if attempting to typecast nil and NULLs are not allowed" do
|
|
826
866
|
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer,:allow_null=>false}})
|
|
827
867
|
proc{@c.new.x = nil}.should raise_error(Sequel::Error)
|
|
868
|
+
proc{@c.new.x = ''}.should raise_error(Sequel::Error)
|
|
828
869
|
end
|
|
829
870
|
|
|
830
871
|
specify "should not raise an error if NULLs are not allowed and typecasting is turned off" do
|
|
@@ -940,6 +981,7 @@ describe Sequel::Model, "typecasting" do
|
|
|
940
981
|
specify "should raise an error if invalid data is used in a date field" do
|
|
941
982
|
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:date}})
|
|
942
983
|
proc{@c.new.x = 'a'}.should raise_error
|
|
984
|
+
proc{@c.new.x = 100}.should raise_error
|
|
943
985
|
end
|
|
944
986
|
|
|
945
987
|
specify "should convert to time for a time field" do
|
|
@@ -989,10 +1031,9 @@ describe Sequel::Model, "typecasting" do
|
|
|
989
1031
|
specify "should raise an error if invalid data is used in a datetime field" do
|
|
990
1032
|
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:datetime}})
|
|
991
1033
|
proc{@c.new.x = '0000'}.should raise_error
|
|
992
|
-
proc{@c.new.x = ''}.should_not raise_error # Valid Time
|
|
1034
|
+
proc{@c.new.x = 'a'}.should_not raise_error # Valid Time
|
|
993
1035
|
Sequel.datetime_class = DateTime
|
|
994
1036
|
proc{@c.new.x = '0000'}.should raise_error
|
|
995
|
-
proc{@c.new.x = ''}.should raise_error
|
|
1037
|
+
proc{@c.new.x = 'a'}.should raise_error
|
|
996
1038
|
end
|
|
997
|
-
|
|
998
1039
|
end
|
|
@@ -15,13 +15,12 @@ describe Sequel::Model, "table_exists?" do
|
|
|
15
15
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
describe Sequel::Model, "create_table" do
|
|
18
|
+
describe Sequel::Model, "create_table and schema" do
|
|
19
19
|
|
|
20
20
|
before(:each) do
|
|
21
21
|
MODEL_DB.reset
|
|
22
22
|
@model = Class.new(Sequel::Model) do
|
|
23
|
-
|
|
24
|
-
set_schema do
|
|
23
|
+
set_schema(:items) do
|
|
25
24
|
text :name
|
|
26
25
|
float :price, :null => false
|
|
27
26
|
end
|
|
@@ -40,6 +39,22 @@ describe Sequel::Model, "create_table" do
|
|
|
40
39
|
@model.db_schema.should == schem
|
|
41
40
|
@model.instance_variable_get(:@columns).should == [:name, :price]
|
|
42
41
|
end
|
|
42
|
+
|
|
43
|
+
it "should return the schema generator via schema" do
|
|
44
|
+
@model.schema.should be_a_kind_of(Sequel::Schema::Generator)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "should use the superclasses schema if it exists" do
|
|
48
|
+
@submodel = Class.new(@model)
|
|
49
|
+
@submodel.schema.should be_a_kind_of(Sequel::Schema::Generator)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should return nil if no schema is present" do
|
|
53
|
+
@model = Class.new(Sequel::Model)
|
|
54
|
+
@model.schema.should == nil
|
|
55
|
+
@submodel = Class.new(@model)
|
|
56
|
+
@submodel.schema.should == nil
|
|
57
|
+
end
|
|
43
58
|
end
|
|
44
59
|
|
|
45
60
|
describe Sequel::Model, "drop_table" do
|
|
@@ -66,7 +81,6 @@ describe Sequel::Model, "create_table!" do
|
|
|
66
81
|
end
|
|
67
82
|
|
|
68
83
|
it "should drop table if it exists and then create the table" do
|
|
69
|
-
@model.should_receive(:table_exists?).and_return(true)
|
|
70
84
|
@model.should_receive(:drop_table).and_return(true)
|
|
71
85
|
@model.should_receive(:create_table).and_return(true)
|
|
72
86
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
unless Object.const_defined?('Sequel')
|
|
3
|
-
$:.unshift(File.join(File.dirname(__FILE__), "../../
|
|
3
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../../lib/"))
|
|
4
4
|
require 'sequel_core'
|
|
5
5
|
end
|
|
6
6
|
unless Sequel.const_defined?('Model')
|
|
7
|
-
$:.unshift(File.join(File.dirname(__FILE__), "
|
|
7
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../../lib/"))
|
|
8
8
|
require 'sequel_model'
|
|
9
9
|
end
|
|
10
10
|
|
|
@@ -52,6 +52,7 @@ end
|
|
|
52
52
|
|
|
53
53
|
class << Sequel::Model
|
|
54
54
|
alias orig_columns columns
|
|
55
|
+
alias orig_str_columns str_columns
|
|
55
56
|
def columns(*cols)
|
|
56
57
|
return if cols.empty?
|
|
57
58
|
define_method(:columns){cols}
|