sequel 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/dataset_spec.rb CHANGED
@@ -109,13 +109,13 @@ context "A simple dataset" do
109
109
  @dataset.insert_sql({}).should == "INSERT INTO test DEFAULT VALUES;"
110
110
  end
111
111
 
112
- specify "should format an insert statement with array fields" do
112
+ specify "should format an insert statement with array with keys" do
113
113
  v = [1, 2, 3]
114
- v.fields = [:a, :b, :c]
114
+ v.keys = [:a, :b, :c]
115
115
  @dataset.insert_sql(v).should == "INSERT INTO test (a, b, c) VALUES (1, 2, 3);"
116
116
 
117
117
  v = []
118
- v.fields = [:a, :b]
118
+ v.keys = [:a, :b]
119
119
  @dataset.insert_sql(v).should == "INSERT INTO test DEFAULT VALUES;"
120
120
  end
121
121
 
@@ -155,9 +155,9 @@ context "A simple dataset" do
155
155
  "UPDATE test SET name = 'abc'"
156
156
  end
157
157
 
158
- specify "should format an update statement with array fields" do
158
+ specify "should format an update statement with array with keys" do
159
159
  v = ['abc']
160
- v.fields = [:name]
160
+ v.keys = [:name]
161
161
 
162
162
  @dataset.update_sql(v).should == "UPDATE test SET name = 'abc'"
163
163
  end
@@ -458,7 +458,7 @@ context "Dataset#having" do
458
458
  @grouped = @dataset.group(:region).select(:region, :population.SUM, :gdp.AVG)
459
459
  @d1 = @grouped.having('sum(population) > 10')
460
460
  @d2 = @grouped.having(:region => 'Asia')
461
- @fields = "region, sum(population), avg(gdp)"
461
+ @columns = "region, sum(population), avg(gdp)"
462
462
  end
463
463
 
464
464
  specify "should raise if the dataset is not grouped" do
@@ -467,12 +467,12 @@ context "Dataset#having" do
467
467
 
468
468
  specify "should affect select statements" do
469
469
  @d1.select_sql.should ==
470
- "SELECT #{@fields} FROM test GROUP BY region HAVING sum(population) > 10"
470
+ "SELECT #{@columns} FROM test GROUP BY region HAVING sum(population) > 10"
471
471
  end
472
472
 
473
473
  specify "should support proc expressions" do
474
474
  @grouped.having {:sum[:population] > 10}.sql.should ==
475
- "SELECT #{@fields} FROM test GROUP BY region HAVING (sum(population) > 10)"
475
+ "SELECT #{@columns} FROM test GROUP BY region HAVING (sum(population) > 10)"
476
476
  end
477
477
  end
478
478
 
@@ -789,19 +789,19 @@ context "Dataset#naked" do
789
789
  end
790
790
  end
791
791
 
792
- context "Dataset#qualified_field_name" do
792
+ context "Dataset#qualified_column_name" do
793
793
  setup do
794
794
  @dataset = Sequel::Dataset.new(nil).from(:test)
795
795
  end
796
796
 
797
797
  specify "should return the same if already qualified" do
798
- @dataset.qualified_field_name('test.a', :items).should == 'test.a'
799
- @dataset.qualified_field_name(:ccc__b, :items).should == 'ccc.b'
798
+ @dataset.qualified_column_name('test.a', :items).should == 'test.a'
799
+ @dataset.qualified_column_name(:ccc__b, :items).should == 'ccc.b'
800
800
  end
801
801
 
802
- specify "should qualify the field with the supplied table name" do
803
- @dataset.qualified_field_name('a', :items).should == 'items.a'
804
- @dataset.qualified_field_name(:b1, :items).should == 'items.b1'
802
+ specify "should qualify the column with the supplied table name" do
803
+ @dataset.qualified_column_name('a', :items).should == 'items.a'
804
+ @dataset.qualified_column_name(:b1, :items).should == 'items.b1'
805
805
  end
806
806
  end
807
807
 
@@ -825,7 +825,7 @@ context "Dataset#map" do
825
825
  @d.map {|n| n[:a] + n[:b]}.should == [3, 7, 11]
826
826
  end
827
827
 
828
- specify "should map using #[fieldname] if fieldname is given" do
828
+ specify "should map using #[column name] if column name is given" do
829
829
  @d.map(:a).should == [1, 3, 5]
830
830
  end
831
831
 
@@ -839,7 +839,7 @@ context "Dataset#to_hash" do
839
839
  @d = DummyDataset.new(nil).from(:items)
840
840
  end
841
841
 
842
- specify "should provide a hash with the first field as key and the second as value" do
842
+ specify "should provide a hash with the first column as key and the second as value" do
843
843
  @d.to_hash(:a, :b).should == {1 => 2, 3 => 4, 5 => 6}
844
844
  @d.to_hash(:b, :a).should == {2 => 1, 4 => 3, 6 => 5}
845
845
  end
@@ -889,6 +889,26 @@ context "Dataset#count" do
889
889
  end
890
890
  end
891
891
 
892
+
893
+ context "Dataset#group_and_count" do
894
+ setup do
895
+ @c = Class.new(Sequel::Dataset) do
896
+ def self.sql
897
+ @@sql
898
+ end
899
+
900
+ def fetch_rows(sql)
901
+ @@sql = sql
902
+ yield({1 => 1})
903
+ end
904
+ end
905
+ @ds = @c.new(nil).from(:test)
906
+ end
907
+
908
+ specify "should format SQL properly" do
909
+ @ds.group_and_count(:name).sql.should == "SELECT name, count(name) AS count FROM test GROUP BY name ORDER BY count"
910
+ end
911
+ end
892
912
  context "Dataset#empty?" do
893
913
  specify "should return true if #count == 0" do
894
914
  @c = Class.new(Sequel::Dataset) do
@@ -1025,6 +1045,28 @@ context "Dataset#[]=" do
1025
1045
  end
1026
1046
  end
1027
1047
 
1048
+ context "Dataset#set" do
1049
+ setup do
1050
+ c = Class.new(Sequel::Dataset) do
1051
+ def last_sql
1052
+ @@last_sql
1053
+ end
1054
+
1055
+ def update(*args)
1056
+ @@last_sql = update_sql(*args)
1057
+ end
1058
+ end
1059
+
1060
+ @d = c.new(nil).from(:items)
1061
+ end
1062
+
1063
+ specify "should act as alis to #update" do
1064
+ @d.set({:x => 3})
1065
+ @d.last_sql.should == 'UPDATE items SET x = 3'
1066
+ end
1067
+ end
1068
+
1069
+
1028
1070
  context "Dataset#insert_multiple" do
1029
1071
  setup do
1030
1072
  c = Class.new(Sequel::Dataset) do
@@ -1076,11 +1118,69 @@ context "Dataset aggregate methods" do
1076
1118
  @d.avg(:d).should == 'SELECT avg(d) AS v FROM test'
1077
1119
  end
1078
1120
 
1079
- specify "should accept qualified fields" do
1121
+ specify "should accept qualified columns" do
1080
1122
  @d.avg(:test__bc).should == 'SELECT avg(test.bc) AS v FROM test'
1081
1123
  end
1082
1124
  end
1083
1125
 
1126
+ context "Dataset#range" do
1127
+ setup do
1128
+ c = Class.new(Sequel::Dataset) do
1129
+ @@sql = nil
1130
+
1131
+ def last_sql; @@sql; end
1132
+
1133
+ def fetch_rows(sql)
1134
+ @@sql = sql
1135
+ yield(:v1 => 1, :v2 => 10)
1136
+ end
1137
+ end
1138
+ @d = c.new(nil).from(:test)
1139
+ end
1140
+
1141
+ specify "should generate a correct SQL statement" do
1142
+ @d.range(:stamp)
1143
+ @d.last_sql.should == "SELECT min(stamp) AS v1, max(stamp) AS v2 FROM test LIMIT 1"
1144
+
1145
+ @d.filter {:price > 100}.range(:stamp)
1146
+ @d.last_sql.should == "SELECT min(stamp) AS v1, max(stamp) AS v2 FROM test WHERE (price > 100) LIMIT 1"
1147
+ end
1148
+
1149
+ specify "should return a range object" do
1150
+ @d.range(:tryme).should == (1..10)
1151
+ @d.last_sql.should == "SELECT min(tryme) AS v1, max(tryme) AS v2 FROM test LIMIT 1"
1152
+ end
1153
+ end
1154
+
1155
+ context "Dataset#range" do
1156
+ setup do
1157
+ c = Class.new(Sequel::Dataset) do
1158
+ @@sql = nil
1159
+
1160
+ def last_sql; @@sql; end
1161
+
1162
+ def fetch_rows(sql)
1163
+ @@sql = sql
1164
+ yield(:v => 1234)
1165
+ end
1166
+ end
1167
+ @d = c.new(nil).from(:test)
1168
+ end
1169
+
1170
+ specify "should generate a correct SQL statement" do
1171
+ @d.interval(:stamp)
1172
+ @d.last_sql.should == "SELECT (max(stamp) - min(stamp)) AS v FROM test LIMIT 1"
1173
+
1174
+ @d.filter {:price > 100}.interval(:stamp)
1175
+ @d.last_sql.should == "SELECT (max(stamp) - min(stamp)) AS v FROM test WHERE (price > 100) LIMIT 1"
1176
+ end
1177
+
1178
+ specify "should return a range object" do
1179
+ @d.interval(:tryme).should == 1234
1180
+ @d.last_sql.should == "SELECT (max(tryme) - min(tryme)) AS v FROM test LIMIT 1"
1181
+ end
1182
+ end
1183
+
1084
1184
  context "Dataset#first" do
1085
1185
  setup do
1086
1186
  @c = Class.new(Sequel::Dataset) do
@@ -2023,6 +2123,10 @@ context "Dataset#transform" do
2023
2123
  @ds.each(:naked => true) {|r| f = r}
2024
2124
  f.should == {:x => "wow", :y => 'hello'}
2025
2125
  end
2126
+
2127
+ specify "should return self" do
2128
+ @ds.transform(:x => :marshal).should be(@ds)
2129
+ end
2026
2130
  end
2027
2131
 
2028
2132
  context "Dataset#to_csv" do
@@ -2033,7 +2137,7 @@ context "Dataset#to_csv" do
2033
2137
 
2034
2138
  def fetch_rows(sql, &block)
2035
2139
  @columns = @cols
2036
- @data.each {|r| r.fields = @columns; block[r]}
2140
+ @data.each {|r| r.keys = @columns; block[r]}
2037
2141
  end
2038
2142
 
2039
2143
  # naked should return self here because to_csv wants a naked result set.
@@ -2066,7 +2170,7 @@ context "Dataset#each_hash" do
2066
2170
  @c = Class.new(Sequel::Dataset) do
2067
2171
  def each(&block)
2068
2172
  a = [[1, 2, 3], [4, 5, 6]]
2069
- a.each {|r| r.fields = [:a, :b, :c]; block[r]}
2173
+ a.each {|r| r.keys = [:a, :b, :c]; block[r]}
2070
2174
  end
2071
2175
  end
2072
2176
 
@@ -2078,4 +2182,85 @@ context "Dataset#each_hash" do
2078
2182
  @ds.each_hash {|h| hashes << h}
2079
2183
  hashes.should == [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
2080
2184
  end
2185
+ end
2186
+
2187
+ context "Dataset magic methods" do
2188
+ setup do
2189
+ @c = Class.new(Sequel::Dataset) do
2190
+ @@sqls = []
2191
+
2192
+ def self.sqls; @@sqls; end
2193
+
2194
+ def fetch_rows(sql)
2195
+ @@sqls << sql
2196
+ yield({:a => 1, :b => 2})
2197
+ end
2198
+ end
2199
+
2200
+ @ds = @c.new(nil).from(:items)
2201
+ end
2202
+
2203
+ specify "should support order_by_xxx" do
2204
+ @ds.should_not respond_to(:order_by_name)
2205
+ proc {@ds.order_by_name}.should_not raise_error
2206
+ @ds.should respond_to(:order_by_name)
2207
+ @ds.order_by_name.should be_a_kind_of(@c)
2208
+ @ds.order_by_name.sql.should == "SELECT * FROM items ORDER BY name"
2209
+ end
2210
+
2211
+ specify "should support group_by_xxx" do
2212
+ @ds.should_not respond_to(:group_by_name)
2213
+ proc {@ds.group_by_name}.should_not raise_error
2214
+ @ds.should respond_to(:group_by_name)
2215
+ @ds.group_by_name.should be_a_kind_of(@c)
2216
+ @ds.group_by_name.sql.should == "SELECT * FROM items GROUP BY name"
2217
+ end
2218
+
2219
+ specify "should support count_by_xxx" do
2220
+ @ds.should_not respond_to(:count_by_name)
2221
+ proc {@ds.count_by_name}.should_not raise_error
2222
+ @ds.should respond_to(:count_by_name)
2223
+ @ds.count_by_name.should be_a_kind_of(@c)
2224
+ @ds.count_by_name.sql.should == "SELECT name, count(name) AS count FROM items GROUP BY name ORDER BY count"
2225
+ end
2226
+
2227
+ specify "should support filter_by_xxx" do
2228
+ @ds.should_not respond_to(:filter_by_name)
2229
+ proc {@ds.filter_by_name('sharon')}.should_not raise_error
2230
+ @ds.should respond_to(:filter_by_name)
2231
+ @ds.filter_by_name('sharon').should be_a_kind_of(@c)
2232
+ @ds.filter_by_name('sharon').sql.should == "SELECT * FROM items WHERE (name = 'sharon')"
2233
+ end
2234
+
2235
+ specify "should support all_by_xxx" do
2236
+ @ds.should_not respond_to(:all_by_name)
2237
+ proc {@ds.all_by_name('sharon')}.should_not raise_error
2238
+ @ds.should respond_to(:all_by_name)
2239
+ @ds.all_by_name('sharon').should == [{:a => 1, :b => 2}]
2240
+ @c.sqls.should == ["SELECT * FROM items WHERE (name = 'sharon')"] * 2
2241
+ end
2242
+
2243
+ specify "should support find_by_xxx" do
2244
+ @ds.should_not respond_to(:find_by_name)
2245
+ proc {@ds.find_by_name('sharon')}.should_not raise_error
2246
+ @ds.should respond_to(:find_by_name)
2247
+ @ds.find_by_name('sharon').should == {:a => 1, :b => 2}
2248
+ @c.sqls.should == ["SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"] * 2
2249
+ end
2250
+
2251
+ specify "should support first_by_xxx" do
2252
+ @ds.should_not respond_to(:first_by_name)
2253
+ proc {@ds.first_by_name('sharon')}.should_not raise_error
2254
+ @ds.should respond_to(:first_by_name)
2255
+ @ds.first_by_name('sharon').should == {:a => 1, :b => 2}
2256
+ @c.sqls.should == ["SELECT * FROM items ORDER BY name LIMIT 1"] * 2
2257
+ end
2258
+
2259
+ specify "should support last_by_xxx" do
2260
+ @ds.should_not respond_to(:last_by_name)
2261
+ proc {@ds.last_by_name('sharon')}.should_not raise_error
2262
+ @ds.should respond_to(:last_by_name)
2263
+ @ds.last_by_name('sharon').should == {:a => 1, :b => 2}
2264
+ @c.sqls.should == ["SELECT * FROM items ORDER BY name DESC LIMIT 1"] * 2
2265
+ end
2081
2266
  end
data/spec/model_spec.rb CHANGED
@@ -968,5 +968,75 @@ context "A Model constructor" do
968
968
  m.should be_new
969
969
  end
970
970
 
971
- specify "should "
971
+ specify "should accept a block and yield itself to the block" do
972
+ block_called = false
973
+ m = @m.new {|i| block_called = true; i.should be_a_kind_of(@m); i.values[:a] = 1}
974
+
975
+ block_called.should be_true
976
+ m.values[:a].should == 1
977
+ end
978
+ end
979
+
980
+ context "Model magic methods" do
981
+ setup do
982
+ @c = Class.new(Sequel::Dataset) do
983
+ @@sqls = []
984
+
985
+ def self.sqls; @@sqls; end
986
+
987
+ def fetch_rows(sql)
988
+ @@sqls << sql
989
+ yield({:id => 123, :name => 'hey'})
990
+ end
991
+ end
992
+
993
+ @m = Class.new(Sequel::Model(@c.new(nil).from(:items)))
994
+ end
995
+
996
+ specify "should support order_by_xxx" do
997
+ @m.order_by_name.should be_a_kind_of(@c)
998
+ @m.order_by_name.sql.should == "SELECT * FROM items ORDER BY name"
999
+ end
1000
+
1001
+ specify "should support group_by_xxx" do
1002
+ @m.group_by_name.should be_a_kind_of(@c)
1003
+ @m.group_by_name.sql.should == "SELECT * FROM items GROUP BY name"
1004
+ end
1005
+
1006
+ specify "should support count_by_xxx" do
1007
+ @m.count_by_name.should be_a_kind_of(@c)
1008
+ @m.count_by_name.sql.should == "SELECT name, count(name) AS count FROM items GROUP BY name ORDER BY count"
1009
+ end
1010
+
1011
+ specify "should support filter_by_xxx" do
1012
+ @m.filter_by_name('sharon').should be_a_kind_of(@c)
1013
+ @m.filter_by_name('sharon').sql.should == "SELECT * FROM items WHERE (name = 'sharon')"
1014
+ end
1015
+
1016
+ specify "should support all_by_xxx" do
1017
+ all = @m.all_by_name('sharon')
1018
+ all.class.should == Array
1019
+ all.size.should == 1
1020
+ all.first.should be_a_kind_of(@m)
1021
+ all.first.values.should == {:id => 123, :name => 'hey'}
1022
+ @c.sqls.should == ["SELECT * FROM items WHERE (name = 'sharon')"]
1023
+ end
1024
+
1025
+ specify "should support find_by_xxx" do
1026
+ @m.find_by_name('sharon').should be_a_kind_of(@m)
1027
+ @m.find_by_name('sharon').values.should == {:id => 123, :name => 'hey'}
1028
+ @c.sqls.should == ["SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"] * 2
1029
+ end
1030
+
1031
+ specify "should support first_by_xxx" do
1032
+ @m.first_by_name('sharon').should be_a_kind_of(@m)
1033
+ @m.first_by_name('sharon').values.should == {:id => 123, :name => 'hey'}
1034
+ @c.sqls.should == ["SELECT * FROM items ORDER BY name LIMIT 1"] * 2
1035
+ end
1036
+
1037
+ specify "should support last_by_xxx" do
1038
+ @m.last_by_name('sharon').should be_a_kind_of(@m)
1039
+ @m.last_by_name('sharon').values.should == {:id => 123, :name => 'hey'}
1040
+ @c.sqls.should == ["SELECT * FROM items ORDER BY name DESC LIMIT 1"] * 2
1041
+ end
972
1042
  end
@@ -13,9 +13,9 @@ describe Sequel::Schema::Generator do
13
13
  @table_name, @columns, @indexes = @generator.create_info
14
14
  end
15
15
 
16
- {:name => :id, :primary_key => true}.each do |field, expected|
17
- it "uses default primary key #{field}" do
18
- @columns.first[field].should == expected
16
+ {:name => :id, :primary_key => true}.each do |column, expected|
17
+ it "uses default primary key #{column}" do
18
+ @columns.first[column].should == expected
19
19
  end
20
20
  end
21
21
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sequel
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-10-30 00:00:00 +02:00
6
+ version: 0.3.2
7
+ date: 2007-11-01 00:00:00 +02:00
8
8
  summary: Lightweight ORM library for Ruby
9
9
  require_paths:
10
10
  - lib
@@ -33,6 +33,7 @@ files:
33
33
  - README
34
34
  - Rakefile
35
35
  - bin/sequel
36
+ - doc/rdoc
36
37
  - spec/adapters
37
38
  - spec/adapters/mysql_spec.rb
38
39
  - spec/adapters/postgres_spec.rb
@@ -60,9 +61,9 @@ files:
60
61
  - lib/sequel/dataset/sequelizer.rb
61
62
  - lib/sequel/dataset/sql.rb
62
63
  - lib/sequel/dataset.rb
64
+ - lib/sequel/db2.rb
63
65
  - lib/sequel/dbi.rb
64
66
  - lib/sequel/error.rb
65
- - lib/sequel/expressions.rb
66
67
  - lib/sequel/migration.rb
67
68
  - lib/sequel/model
68
69
  - lib/sequel/model/base.rb