sequel 2.12.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +62 -0
- data/README.rdoc +3 -3
- data/Rakefile +7 -0
- data/doc/advanced_associations.rdoc +44 -0
- data/doc/release_notes/3.0.0.txt +221 -0
- data/lib/sequel/adapters/amalgalite.rb +208 -0
- data/lib/sequel/adapters/db2.rb +3 -0
- data/lib/sequel/adapters/dbi.rb +9 -0
- data/lib/sequel/adapters/do.rb +0 -4
- data/lib/sequel/adapters/firebird.rb +16 -18
- data/lib/sequel/adapters/informix.rb +5 -3
- data/lib/sequel/adapters/jdbc.rb +24 -20
- data/lib/sequel/adapters/jdbc/h2.rb +15 -4
- data/lib/sequel/adapters/mysql.rb +4 -8
- data/lib/sequel/adapters/odbc.rb +0 -4
- data/lib/sequel/adapters/oracle.rb +0 -4
- data/lib/sequel/adapters/shared/mssql.rb +16 -5
- data/lib/sequel/adapters/shared/mysql.rb +87 -86
- data/lib/sequel/adapters/shared/oracle.rb +92 -3
- data/lib/sequel/adapters/shared/postgres.rb +85 -29
- data/lib/sequel/adapters/shared/progress.rb +8 -3
- data/lib/sequel/adapters/shared/sqlite.rb +53 -23
- data/lib/sequel/adapters/sqlite.rb +4 -7
- data/lib/sequel/adapters/utils/unsupported.rb +3 -3
- data/lib/sequel/connection_pool.rb +18 -25
- data/lib/sequel/core.rb +2 -21
- data/lib/sequel/database.rb +60 -44
- data/lib/sequel/database/schema_generator.rb +26 -31
- data/lib/sequel/database/schema_methods.rb +8 -3
- data/lib/sequel/database/schema_sql.rb +114 -28
- data/lib/sequel/dataset.rb +14 -41
- data/lib/sequel/dataset/convenience.rb +31 -54
- data/lib/sequel/dataset/graph.rb +7 -13
- data/lib/sequel/dataset/sql.rb +43 -54
- data/lib/sequel/extensions/inflector.rb +0 -5
- data/lib/sequel/extensions/schema_dumper.rb +238 -0
- data/lib/sequel/metaprogramming.rb +0 -20
- data/lib/sequel/model.rb +1 -2
- data/lib/sequel/model/base.rb +18 -16
- data/lib/sequel/model/inflections.rb +6 -9
- data/lib/sequel/plugins/caching.rb +0 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/sql.rb +2 -0
- data/lib/sequel/version.rb +2 -2
- data/spec/adapters/firebird_spec.rb +35 -8
- data/spec/adapters/mysql_spec.rb +173 -266
- data/spec/adapters/oracle_spec.rb +13 -0
- data/spec/adapters/postgres_spec.rb +127 -227
- data/spec/adapters/sqlite_spec.rb +13 -171
- data/spec/core/connection_pool_spec.rb +15 -4
- data/spec/core/core_sql_spec.rb +14 -170
- data/spec/core/database_spec.rb +50 -132
- data/spec/core/dataset_spec.rb +47 -930
- data/spec/core/expression_filters_spec.rb +12 -0
- data/spec/core/schema_generator_spec.rb +37 -45
- data/spec/core/schema_spec.rb +26 -16
- data/spec/core/spec_helper.rb +0 -25
- data/spec/extensions/inflector_spec.rb +0 -3
- data/spec/extensions/schema_dumper_spec.rb +292 -0
- data/spec/extensions/serialization_spec.rb +9 -0
- data/spec/extensions/single_table_inheritance_spec.rb +6 -1
- data/spec/extensions/spec_helper.rb +1 -3
- data/spec/extensions/validation_helpers_spec.rb +4 -4
- data/spec/integration/database_test.rb +18 -0
- data/spec/integration/dataset_test.rb +112 -1
- data/spec/integration/eager_loader_test.rb +70 -9
- data/spec/integration/prepared_statement_test.rb +2 -2
- data/spec/integration/schema_test.rb +76 -27
- data/spec/integration/spec_helper.rb +0 -14
- data/spec/integration/transaction_test.rb +27 -0
- data/spec/model/associations_spec.rb +0 -36
- data/spec/model/base_spec.rb +18 -123
- data/spec/model/hooks_spec.rb +2 -235
- data/spec/model/inflector_spec.rb +15 -115
- data/spec/model/model_spec.rb +0 -120
- data/spec/model/plugins_spec.rb +0 -70
- data/spec/model/record_spec.rb +35 -93
- data/spec/model/spec_helper.rb +0 -27
- data/spec/model/validations_spec.rb +0 -931
- metadata +9 -14
- data/lib/sequel/deprecated.rb +0 -593
- data/lib/sequel/deprecated_migration.rb +0 -91
- data/lib/sequel/model/deprecated.rb +0 -204
- data/lib/sequel/model/deprecated_hooks.rb +0 -103
- data/lib/sequel/model/deprecated_inflector.rb +0 -335
- data/lib/sequel/model/deprecated_validations.rb +0 -388
- data/spec/core/core_ext_spec.rb +0 -156
- data/spec/core/migration_spec.rb +0 -263
- data/spec/core/pretty_table_spec.rb +0 -58
- data/spec/model/caching_spec.rb +0 -217
- data/spec/model/schema_spec.rb +0 -92
data/spec/core/migration_spec.rb
DELETED
@@ -1,263 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
context "Migration classes" do
|
4
|
-
before do
|
5
|
-
deprec{Sequel::Migration.descendants.clear}
|
6
|
-
end
|
7
|
-
|
8
|
-
deprec_specify "should be registred in Migration.descendants" do
|
9
|
-
@class = Class.new(Sequel::Migration)
|
10
|
-
|
11
|
-
Sequel::Migration.descendants.should == [@class]
|
12
|
-
end
|
13
|
-
|
14
|
-
deprec_specify "should be registered in the right order" do
|
15
|
-
@c1 = Class.new(Sequel::Migration)
|
16
|
-
@c2 = Class.new(Sequel::Migration)
|
17
|
-
@c3 = Class.new(Sequel::Migration)
|
18
|
-
|
19
|
-
Sequel::Migration.descendants.should == [@c1, @c2, @c3]
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context "Migration#apply" do
|
24
|
-
before do
|
25
|
-
@c = Class.new do
|
26
|
-
define_method(:one) {|x| [1111, x]}
|
27
|
-
define_method(:two) {|x| [2222, x]}
|
28
|
-
end
|
29
|
-
@db = @c.new
|
30
|
-
|
31
|
-
deprec do
|
32
|
-
@migration = Class.new(Sequel::Migration) do
|
33
|
-
define_method(:up) {one(3333)}
|
34
|
-
define_method(:down) {two(4444)}
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
deprec_specify "should raise for an invalid direction" do
|
40
|
-
proc {@migration.apply(@db, :hahaha)}.should raise_error(ArgumentError)
|
41
|
-
end
|
42
|
-
|
43
|
-
deprec_specify "should apply the up direction correctly" do
|
44
|
-
@migration.apply(@db, :up).should == [1111, 3333]
|
45
|
-
end
|
46
|
-
|
47
|
-
deprec_specify "should apply the down direction correctly" do
|
48
|
-
@migration.apply(@db, :down).should == [2222, 4444]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class DummyMigrationDataset
|
53
|
-
attr_reader :from
|
54
|
-
|
55
|
-
def initialize(x); @from = x; end
|
56
|
-
|
57
|
-
@@version = nil
|
58
|
-
|
59
|
-
def version; @@version; end
|
60
|
-
def version=(x); @@version = x; end
|
61
|
-
def first; @@version ? {:version => @@version} : nil; end
|
62
|
-
def update(h); @@version = h[:version]; end
|
63
|
-
def <<(h); @@version = h[:version]; end
|
64
|
-
end
|
65
|
-
|
66
|
-
class DummyMigrationDB
|
67
|
-
attr_reader :creates, :drops, :table_created
|
68
|
-
|
69
|
-
def initialize
|
70
|
-
@creates = []
|
71
|
-
@drops = []
|
72
|
-
end
|
73
|
-
|
74
|
-
def create(x); @creates << x; end
|
75
|
-
def drop(x); @drops << x; end
|
76
|
-
|
77
|
-
def [](x); DummyMigrationDataset.new(x); end
|
78
|
-
|
79
|
-
def create_table(x); raise if @table_created == x; @table_created = x; end
|
80
|
-
def table_exists?(x); @table_created == x; end
|
81
|
-
|
82
|
-
def transaction; yield; end
|
83
|
-
end
|
84
|
-
|
85
|
-
MIGRATION_001 = %[
|
86
|
-
class CreateSessions < Sequel::Migration
|
87
|
-
def up
|
88
|
-
create(1111)
|
89
|
-
end
|
90
|
-
|
91
|
-
def down
|
92
|
-
drop(1111)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
]
|
96
|
-
|
97
|
-
MIGRATION_002 = %[
|
98
|
-
class CreateNodes < Sequel::Migration
|
99
|
-
def up
|
100
|
-
create(2222)
|
101
|
-
end
|
102
|
-
|
103
|
-
def down
|
104
|
-
drop(2222)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
]
|
108
|
-
|
109
|
-
MIGRATION_003 = %[
|
110
|
-
class CreateUsers < Sequel::Migration
|
111
|
-
def up
|
112
|
-
create(3333)
|
113
|
-
end
|
114
|
-
|
115
|
-
def down
|
116
|
-
drop(3333)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
]
|
120
|
-
|
121
|
-
MIGRATION_005 = %[
|
122
|
-
class CreateAttributes < Sequel::Migration
|
123
|
-
def up
|
124
|
-
create(5555)
|
125
|
-
end
|
126
|
-
|
127
|
-
def down
|
128
|
-
drop(5555)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
]
|
132
|
-
|
133
|
-
context "Sequel::Migrator" do
|
134
|
-
before do
|
135
|
-
@db = DummyMigrationDB.new
|
136
|
-
|
137
|
-
File.open('001_create_sessions.rb', 'w') {|f| f << MIGRATION_001}
|
138
|
-
File.open('002_create_nodes.rb', 'w') {|f| f << MIGRATION_002}
|
139
|
-
File.open('003_create_users.rb', 'w') {|f| f << MIGRATION_003}
|
140
|
-
File.open('005_create_attributes.rb', 'w') {|f| f << MIGRATION_005}
|
141
|
-
|
142
|
-
@db[:schema_info].version = nil
|
143
|
-
end
|
144
|
-
|
145
|
-
after do
|
146
|
-
Object.send(:remove_const, "CreateSessions") if Object.const_defined?("CreateSessions")
|
147
|
-
Object.send(:remove_const, "CreateNodes") if Object.const_defined?("CreateNodes")
|
148
|
-
Object.send(:remove_const, "CreateUsers") if Object.const_defined?("CreateUsers")
|
149
|
-
Object.send(:remove_const, "CreateAttributes") if Object.const_defined?("CreateAttributes")
|
150
|
-
|
151
|
-
File.delete('001_create_sessions.rb')
|
152
|
-
File.delete('002_create_nodes.rb')
|
153
|
-
File.delete('003_create_users.rb')
|
154
|
-
File.delete('005_create_attributes.rb')
|
155
|
-
end
|
156
|
-
|
157
|
-
deprec_specify "should return the list of files for a specified version range" do
|
158
|
-
Sequel::Migrator.migration_files('.', 1..1).should == \
|
159
|
-
['./001_create_sessions.rb']
|
160
|
-
|
161
|
-
Sequel::Migrator.migration_files('.', 1..3).should == \
|
162
|
-
['./001_create_sessions.rb', './002_create_nodes.rb', './003_create_users.rb']
|
163
|
-
|
164
|
-
Sequel::Migrator.migration_files('.', 3..5).should == \
|
165
|
-
['./003_create_users.rb', './005_create_attributes.rb']
|
166
|
-
|
167
|
-
Sequel::Migrator.migration_files('.', 7..8).should == []
|
168
|
-
end
|
169
|
-
|
170
|
-
deprec_specify "should return the latest version available" do
|
171
|
-
Sequel::Migrator.latest_migration_version('.').should == 5
|
172
|
-
end
|
173
|
-
|
174
|
-
deprec_specify "should load the migration classes for the specified range" do
|
175
|
-
Sequel::Migrator.migration_classes('.', 3, 0, :up).should == \
|
176
|
-
[CreateSessions, CreateNodes, CreateUsers]
|
177
|
-
end
|
178
|
-
|
179
|
-
deprec_specify "should load the migration classes for the specified range" do
|
180
|
-
Sequel::Migrator.migration_classes('.', 0, 5, :down).should == \
|
181
|
-
[CreateAttributes, CreateUsers, CreateNodes, CreateSessions]
|
182
|
-
end
|
183
|
-
|
184
|
-
deprec_specify "should start from current + 1 for the up direction" do
|
185
|
-
Sequel::Migrator.migration_classes('.', 3, 1, :up).should == \
|
186
|
-
[CreateNodes, CreateUsers]
|
187
|
-
end
|
188
|
-
|
189
|
-
deprec_specify "should end on current + 1 for the down direction" do
|
190
|
-
Sequel::Migrator.migration_classes('.', 2, 5, :down).should == \
|
191
|
-
[CreateAttributes, CreateUsers]
|
192
|
-
end
|
193
|
-
|
194
|
-
deprec_specify "should automatically create the schema_info table" do
|
195
|
-
@db.table_exists?(:schema_info).should be_false
|
196
|
-
Sequel::Migrator.schema_info_dataset(@db)
|
197
|
-
@db.table_exists?(:schema_info).should be_true
|
198
|
-
|
199
|
-
# should not raise if table already exists
|
200
|
-
proc {Sequel::Migrator.schema_info_dataset(@db)}.should_not raise_error
|
201
|
-
end
|
202
|
-
|
203
|
-
deprec_specify "should return a dataset for the schema_info table" do
|
204
|
-
d = Sequel::Migrator.schema_info_dataset(@db)
|
205
|
-
d.from.should == :schema_info
|
206
|
-
end
|
207
|
-
|
208
|
-
deprec_specify "should get the migration version stored in the database" do
|
209
|
-
# default is 0
|
210
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 0
|
211
|
-
|
212
|
-
Sequel::Migrator.schema_info_dataset(@db) << {:version => 4321}
|
213
|
-
|
214
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 4321
|
215
|
-
end
|
216
|
-
|
217
|
-
deprec_specify "should set the migration version stored in the database" do
|
218
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 0
|
219
|
-
Sequel::Migrator.set_current_migration_version(@db, 6666)
|
220
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 6666
|
221
|
-
end
|
222
|
-
|
223
|
-
deprec_specify "should apply migrations correctly in the up direction" do
|
224
|
-
Sequel::Migrator.apply(@db, '.', 3, 2)
|
225
|
-
@db.creates.should == [3333]
|
226
|
-
|
227
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 3
|
228
|
-
|
229
|
-
Sequel::Migrator.apply(@db, '.', 5)
|
230
|
-
@db.creates.should == [3333, 5555]
|
231
|
-
|
232
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 5
|
233
|
-
end
|
234
|
-
|
235
|
-
deprec_specify "should apply migrations correctly in the down direction" do
|
236
|
-
Sequel::Migrator.apply(@db, '.', 1, 5)
|
237
|
-
@db.drops.should == [5555, 3333, 2222]
|
238
|
-
|
239
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 1
|
240
|
-
end
|
241
|
-
|
242
|
-
deprec_specify "should apply migrations up to the latest version if no target is given" do
|
243
|
-
Sequel::Migrator.apply(@db, '.')
|
244
|
-
@db.creates.should == [1111, 2222, 3333, 5555]
|
245
|
-
|
246
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 5
|
247
|
-
end
|
248
|
-
|
249
|
-
deprec_specify "should apply migrations down to 0 version correctly" do
|
250
|
-
Sequel::Migrator.apply(@db, '.', 0, 5)
|
251
|
-
@db.drops.should == [5555, 3333, 2222, 1111]
|
252
|
-
|
253
|
-
Sequel::Migrator.get_current_migration_version(@db).should == 0
|
254
|
-
end
|
255
|
-
|
256
|
-
deprec_specify "should return the target version" do
|
257
|
-
Sequel::Migrator.apply(@db, '.', 3, 2).should == 3
|
258
|
-
|
259
|
-
Sequel::Migrator.apply(@db, '.', 0).should == 0
|
260
|
-
|
261
|
-
Sequel::Migrator.apply(@db, '.').should == 5
|
262
|
-
end
|
263
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
require 'stringio'
|
4
|
-
|
5
|
-
context "PrettyTable" do
|
6
|
-
before do
|
7
|
-
@data1 = [
|
8
|
-
{:x => 3, :y => 4}
|
9
|
-
]
|
10
|
-
|
11
|
-
@data2 = [
|
12
|
-
{:a => 23, :b => 45},
|
13
|
-
{:a => 45, :b => 2377}
|
14
|
-
]
|
15
|
-
|
16
|
-
@data3 = [
|
17
|
-
{:aaa => 1},
|
18
|
-
{:bb => 2},
|
19
|
-
{:c => 3}
|
20
|
-
]
|
21
|
-
|
22
|
-
@output = StringIO.new
|
23
|
-
@orig_stdout = $stdout
|
24
|
-
$stdout = @output
|
25
|
-
end
|
26
|
-
|
27
|
-
after do
|
28
|
-
$stdout = @orig_stdout
|
29
|
-
end
|
30
|
-
|
31
|
-
specify "should infer the columns if not given" do
|
32
|
-
Sequel::PrettyTable.print(@data1)
|
33
|
-
@output.rewind
|
34
|
-
@output.read.should =~ \
|
35
|
-
/\n(\|x\|y\|)|(\|y\|x\|)\n/
|
36
|
-
end
|
37
|
-
|
38
|
-
specify "should calculate the maximum width of each column correctly" do
|
39
|
-
Sequel::PrettyTable.print(@data2, [:a, :b])
|
40
|
-
@output.rewind
|
41
|
-
@output.read.should == \
|
42
|
-
"+--+----+\n|a |b |\n+--+----+\n|23| 45|\n|45|2377|\n+--+----+\n"
|
43
|
-
end
|
44
|
-
|
45
|
-
specify "should also take header width into account" do
|
46
|
-
Sequel::PrettyTable.print(@data3, [:aaa, :bb, :c])
|
47
|
-
@output.rewind
|
48
|
-
@output.read.should == \
|
49
|
-
"+---+--+-+\n|aaa|bb|c|\n+---+--+-+\n| 1| | |\n| | 2| |\n| | |3|\n+---+--+-+\n"
|
50
|
-
end
|
51
|
-
|
52
|
-
specify "should print only the specified columns" do
|
53
|
-
Sequel::PrettyTable.print(@data2, [:a])
|
54
|
-
@output.rewind
|
55
|
-
@output.read.should == \
|
56
|
-
"+--+\n|a |\n+--+\n|23|\n|45|\n+--+\n"
|
57
|
-
end
|
58
|
-
end
|
data/spec/model/caching_spec.rb
DELETED
@@ -1,217 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
-
|
3
|
-
describe Sequel::Model, "caching" do
|
4
|
-
before do
|
5
|
-
MODEL_DB.reset
|
6
|
-
|
7
|
-
@cache_class = Class.new(Hash) do
|
8
|
-
attr_accessor :ttl
|
9
|
-
def set(k, v, ttl); self[k] = v; @ttl = ttl; end
|
10
|
-
def get(k); self[k]; end
|
11
|
-
end
|
12
|
-
cache = @cache_class.new
|
13
|
-
@cache = cache
|
14
|
-
|
15
|
-
@c = Class.new(Sequel::Model(:items))
|
16
|
-
deprec do
|
17
|
-
@c.class_eval do
|
18
|
-
set_cache cache
|
19
|
-
def self.name; 'Item' end
|
20
|
-
|
21
|
-
columns :name, :id
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
$cache_dataset_row = {:name => 'sharon', :id => 1}
|
26
|
-
@dataset = @c.dataset
|
27
|
-
$sqls = []
|
28
|
-
@dataset.extend(Module.new {
|
29
|
-
def fetch_rows(sql)
|
30
|
-
$sqls << sql
|
31
|
-
yield $cache_dataset_row
|
32
|
-
end
|
33
|
-
|
34
|
-
def update(values)
|
35
|
-
$sqls << update_sql(values)
|
36
|
-
$cache_dataset_row.merge!(values)
|
37
|
-
end
|
38
|
-
|
39
|
-
def delete
|
40
|
-
$sqls << delete_sql
|
41
|
-
end
|
42
|
-
})
|
43
|
-
@c2 = Class.new(@c) do
|
44
|
-
def self.name; 'SubItem' end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should set the model's cache store" do
|
49
|
-
@c.cache_store.should be(@cache)
|
50
|
-
@c2.cache_store.should be(@cache)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should have a default ttl of 3600" do
|
54
|
-
@c.cache_ttl.should == 3600
|
55
|
-
@c2.cache_ttl.should == 3600
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should take a ttl option" do
|
59
|
-
@c.set_cache @cache, :ttl => 1234
|
60
|
-
@c.cache_ttl.should == 1234
|
61
|
-
Class.new(@c).cache_ttl.should == 1234
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should offer a set_cache_ttl method for setting the ttl" do
|
65
|
-
@c.cache_ttl.should == 3600
|
66
|
-
@c.set_cache_ttl 1234
|
67
|
-
@c.cache_ttl.should == 1234
|
68
|
-
Class.new(@c).cache_ttl.should == 1234
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should generate a cache key appropriate to the class" do
|
72
|
-
m = @c.new
|
73
|
-
m.values[:id] = 1
|
74
|
-
m.cache_key.should == "#{m.class}:1"
|
75
|
-
m = @c2.new
|
76
|
-
m.values[:id] = 1
|
77
|
-
m.cache_key.should == "#{m.class}:1"
|
78
|
-
|
79
|
-
# custom primary key
|
80
|
-
@c.set_primary_key :ttt
|
81
|
-
m = @c.new
|
82
|
-
m.values[:ttt] = 333
|
83
|
-
m.cache_key.should == "#{m.class}:333"
|
84
|
-
c = Class.new(@c)
|
85
|
-
m = c.new
|
86
|
-
m.values[:ttt] = 333
|
87
|
-
m.cache_key.should == "#{m.class}:333"
|
88
|
-
|
89
|
-
# composite primary key
|
90
|
-
@c.set_primary_key [:a, :b, :c]
|
91
|
-
m = @c.new
|
92
|
-
m.values[:a] = 123
|
93
|
-
m.values[:c] = 456
|
94
|
-
m.values[:b] = 789
|
95
|
-
m.cache_key.should == "#{m.class}:123,789,456"
|
96
|
-
c = Class.new(@c)
|
97
|
-
m = c.new
|
98
|
-
m.values[:a] = 123
|
99
|
-
m.values[:c] = 456
|
100
|
-
m.values[:b] = 789
|
101
|
-
m.cache_key.should == "#{m.class}:123,789,456"
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should raise error if attempting to generate cache_key and primary key value is null" do
|
105
|
-
m = @c.new
|
106
|
-
proc {m.cache_key}.should raise_error(Sequel::Error)
|
107
|
-
m.values[:id] = 1
|
108
|
-
proc {m.cache_key}.should_not raise_error(Sequel::Error)
|
109
|
-
|
110
|
-
m = @c2.new
|
111
|
-
proc {m.cache_key}.should raise_error(Sequel::Error)
|
112
|
-
m.values[:id] = 1
|
113
|
-
proc {m.cache_key}.should_not raise_error(Sequel::Error)
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should not raise error if trying to save a new record" do
|
117
|
-
proc {@c.new(:name=>'blah').save}.should_not raise_error
|
118
|
-
proc {@c.create(:name=>'blah')}.should_not raise_error
|
119
|
-
proc {@c2.new(:name=>'blah').save}.should_not raise_error
|
120
|
-
proc {@c2.create(:name=>'blah')}.should_not raise_error
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should set the cache when reading from the database" do
|
124
|
-
$sqls.should == []
|
125
|
-
@cache.should be_empty
|
126
|
-
|
127
|
-
m = @c[1]
|
128
|
-
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
129
|
-
m.values.should == $cache_dataset_row
|
130
|
-
@cache[m.cache_key].should == m
|
131
|
-
m2 = @c[1]
|
132
|
-
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
133
|
-
m2.should == m
|
134
|
-
m2.values.should == $cache_dataset_row
|
135
|
-
|
136
|
-
$sqls.clear
|
137
|
-
m = @c2[1]
|
138
|
-
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
139
|
-
m.values.should == $cache_dataset_row
|
140
|
-
@cache[m.cache_key].should == m
|
141
|
-
m2 = @c2[1]
|
142
|
-
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
143
|
-
m2.should == m
|
144
|
-
m2.values.should == $cache_dataset_row
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should delete the cache when writing to the database" do
|
148
|
-
m = @c[1]
|
149
|
-
@cache[m.cache_key].should == m
|
150
|
-
m.name = 'hey'
|
151
|
-
m.save
|
152
|
-
@cache.has_key?(m.cache_key).should be_false
|
153
|
-
$sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
|
154
|
-
|
155
|
-
m = @c2[1]
|
156
|
-
@cache[m.cache_key].should == m
|
157
|
-
m.name = 'hey'
|
158
|
-
m.save
|
159
|
-
@cache.has_key?(m.cache_key).should be_false
|
160
|
-
$sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
|
161
|
-
end
|
162
|
-
|
163
|
-
deprec_specify "should delete the cache when using update_values" do
|
164
|
-
m = @c[1]
|
165
|
-
@cache[m.cache_key].should == m
|
166
|
-
m.update_values(:name => 'tutu')
|
167
|
-
@cache.has_key?(m.cache_key).should be_false
|
168
|
-
$sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
|
169
|
-
|
170
|
-
m = @c2[1]
|
171
|
-
@cache[m.cache_key].should == m
|
172
|
-
m.update_values(:name => 'tutu')
|
173
|
-
@cache.has_key?(m.cache_key).should be_false
|
174
|
-
$sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
|
175
|
-
end
|
176
|
-
|
177
|
-
it "should delete the cache when deleting the record" do
|
178
|
-
m = @c[1]
|
179
|
-
@cache[m.cache_key].should == m
|
180
|
-
m.delete
|
181
|
-
@cache.has_key?(m.cache_key).should be_false
|
182
|
-
$sqls.last.should == "DELETE FROM items WHERE (id = 1)"
|
183
|
-
|
184
|
-
m = @c2[1]
|
185
|
-
@cache[m.cache_key].should == m
|
186
|
-
m.delete
|
187
|
-
@cache.has_key?(m.cache_key).should be_false
|
188
|
-
$sqls.last.should == "DELETE FROM items WHERE (id = 1)"
|
189
|
-
end
|
190
|
-
|
191
|
-
it "should support #[] as a shortcut to #find with hash" do
|
192
|
-
m = @c[:id => 3]
|
193
|
-
@cache[m.cache_key].should be_nil
|
194
|
-
$sqls.last.should == "SELECT * FROM items WHERE (id = 3) LIMIT 1"
|
195
|
-
m = @c[1]
|
196
|
-
@cache[m.cache_key].should == m
|
197
|
-
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
198
|
-
"SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
199
|
-
@c[:id => 4]
|
200
|
-
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
201
|
-
"SELECT * FROM items WHERE (id = 1) LIMIT 1", \
|
202
|
-
"SELECT * FROM items WHERE (id = 4) LIMIT 1"]
|
203
|
-
|
204
|
-
$sqls.clear
|
205
|
-
m = @c2[:id => 3]
|
206
|
-
@cache[m.cache_key].should be_nil
|
207
|
-
$sqls.last.should == "SELECT * FROM items WHERE (id = 3) LIMIT 1"
|
208
|
-
m = @c2[1]
|
209
|
-
@cache[m.cache_key].should == m
|
210
|
-
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
211
|
-
"SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
212
|
-
@c2[:id => 4]
|
213
|
-
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
214
|
-
"SELECT * FROM items WHERE (id = 1) LIMIT 1", \
|
215
|
-
"SELECT * FROM items WHERE (id = 4) LIMIT 1"]
|
216
|
-
end
|
217
|
-
end
|