sequel 3.48.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +114 -0
  3. data/Rakefile +10 -7
  4. data/doc/association_basics.rdoc +25 -23
  5. data/doc/code_order.rdoc +7 -0
  6. data/doc/core_extensions.rdoc +0 -10
  7. data/doc/object_model.rdoc +4 -1
  8. data/doc/querying.rdoc +3 -3
  9. data/doc/release_notes/4.0.0.txt +262 -0
  10. data/doc/security.rdoc +0 -28
  11. data/doc/testing.rdoc +8 -14
  12. data/lib/sequel/adapters/ado.rb +7 -11
  13. data/lib/sequel/adapters/ado/access.rb +8 -8
  14. data/lib/sequel/adapters/ado/mssql.rb +4 -4
  15. data/lib/sequel/adapters/amalgalite.rb +6 -6
  16. data/lib/sequel/adapters/cubrid.rb +7 -7
  17. data/lib/sequel/adapters/db2.rb +5 -9
  18. data/lib/sequel/adapters/dbi.rb +2 -6
  19. data/lib/sequel/adapters/do.rb +4 -4
  20. data/lib/sequel/adapters/firebird.rb +4 -4
  21. data/lib/sequel/adapters/ibmdb.rb +8 -8
  22. data/lib/sequel/adapters/informix.rb +2 -10
  23. data/lib/sequel/adapters/jdbc.rb +17 -17
  24. data/lib/sequel/adapters/jdbc/as400.rb +2 -2
  25. data/lib/sequel/adapters/jdbc/cubrid.rb +1 -1
  26. data/lib/sequel/adapters/jdbc/db2.rb +1 -1
  27. data/lib/sequel/adapters/jdbc/derby.rb +1 -1
  28. data/lib/sequel/adapters/jdbc/h2.rb +2 -2
  29. data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -1
  30. data/lib/sequel/adapters/jdbc/informix.rb +1 -1
  31. data/lib/sequel/adapters/jdbc/mssql.rb +2 -2
  32. data/lib/sequel/adapters/jdbc/mysql.rb +1 -1
  33. data/lib/sequel/adapters/jdbc/oracle.rb +5 -1
  34. data/lib/sequel/adapters/jdbc/postgresql.rb +3 -3
  35. data/lib/sequel/adapters/jdbc/sqlite.rb +3 -3
  36. data/lib/sequel/adapters/jdbc/transactions.rb +3 -3
  37. data/lib/sequel/adapters/mock.rb +7 -7
  38. data/lib/sequel/adapters/mysql.rb +3 -3
  39. data/lib/sequel/adapters/mysql2.rb +4 -4
  40. data/lib/sequel/adapters/odbc.rb +2 -6
  41. data/lib/sequel/adapters/odbc/mssql.rb +1 -1
  42. data/lib/sequel/adapters/openbase.rb +1 -5
  43. data/lib/sequel/adapters/oracle.rb +13 -17
  44. data/lib/sequel/adapters/postgres.rb +20 -25
  45. data/lib/sequel/adapters/shared/cubrid.rb +3 -3
  46. data/lib/sequel/adapters/shared/db2.rb +2 -2
  47. data/lib/sequel/adapters/shared/firebird.rb +7 -7
  48. data/lib/sequel/adapters/shared/mssql.rb +9 -9
  49. data/lib/sequel/adapters/shared/mysql.rb +29 -13
  50. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +7 -7
  51. data/lib/sequel/adapters/shared/oracle.rb +22 -13
  52. data/lib/sequel/adapters/shared/postgres.rb +61 -46
  53. data/lib/sequel/adapters/shared/sqlite.rb +9 -9
  54. data/lib/sequel/adapters/sqlite.rb +17 -11
  55. data/lib/sequel/adapters/swift.rb +3 -3
  56. data/lib/sequel/adapters/swift/mysql.rb +1 -1
  57. data/lib/sequel/adapters/swift/sqlite.rb +1 -1
  58. data/lib/sequel/adapters/tinytds.rb +8 -8
  59. data/lib/sequel/ast_transformer.rb +3 -1
  60. data/lib/sequel/connection_pool.rb +4 -2
  61. data/lib/sequel/connection_pool/sharded_single.rb +2 -2
  62. data/lib/sequel/connection_pool/sharded_threaded.rb +5 -5
  63. data/lib/sequel/connection_pool/threaded.rb +7 -7
  64. data/lib/sequel/core.rb +4 -67
  65. data/lib/sequel/database.rb +1 -0
  66. data/lib/sequel/database/connecting.rb +2 -8
  67. data/lib/sequel/database/dataset.rb +2 -7
  68. data/lib/sequel/database/dataset_defaults.rb +0 -18
  69. data/lib/sequel/database/features.rb +4 -4
  70. data/lib/sequel/database/misc.rb +6 -8
  71. data/lib/sequel/database/query.rb +5 -61
  72. data/lib/sequel/database/schema_generator.rb +22 -20
  73. data/lib/sequel/database/schema_methods.rb +48 -20
  74. data/lib/sequel/database/transactions.rb +7 -17
  75. data/lib/sequel/dataset.rb +2 -0
  76. data/lib/sequel/dataset/actions.rb +23 -91
  77. data/lib/sequel/dataset/features.rb +1 -4
  78. data/lib/sequel/dataset/graph.rb +3 -47
  79. data/lib/sequel/dataset/misc.rb +4 -33
  80. data/lib/sequel/dataset/prepared_statements.rb +3 -1
  81. data/lib/sequel/dataset/query.rb +116 -240
  82. data/lib/sequel/dataset/sql.rb +19 -97
  83. data/lib/sequel/deprecated.rb +0 -16
  84. data/lib/sequel/exceptions.rb +0 -3
  85. data/lib/sequel/extensions/_pretty_table.rb +1 -1
  86. data/lib/sequel/extensions/columns_introspection.rb +1 -12
  87. data/lib/sequel/extensions/constraint_validations.rb +3 -3
  88. data/lib/sequel/extensions/core_extensions.rb +0 -9
  89. data/lib/sequel/extensions/date_arithmetic.rb +1 -2
  90. data/lib/sequel/extensions/graph_each.rb +11 -0
  91. data/lib/sequel/extensions/migration.rb +5 -5
  92. data/lib/sequel/extensions/null_dataset.rb +11 -13
  93. data/lib/sequel/extensions/pagination.rb +3 -6
  94. data/lib/sequel/extensions/pg_array.rb +6 -4
  95. data/lib/sequel/extensions/pg_array_ops.rb +35 -1
  96. data/lib/sequel/extensions/pg_json.rb +12 -2
  97. data/lib/sequel/extensions/pg_json_ops.rb +266 -0
  98. data/lib/sequel/extensions/pg_range.rb +2 -2
  99. data/lib/sequel/extensions/pg_range_ops.rb +0 -8
  100. data/lib/sequel/extensions/pg_row.rb +2 -2
  101. data/lib/sequel/extensions/pretty_table.rb +0 -4
  102. data/lib/sequel/extensions/query.rb +3 -8
  103. data/lib/sequel/extensions/schema_caching.rb +0 -7
  104. data/lib/sequel/extensions/schema_dumper.rb +10 -17
  105. data/lib/sequel/extensions/select_remove.rb +0 -4
  106. data/lib/sequel/extensions/set_overrides.rb +28 -0
  107. data/lib/sequel/extensions/to_dot.rb +6 -10
  108. data/lib/sequel/model.rb +6 -7
  109. data/lib/sequel/model/associations.rb +127 -182
  110. data/lib/sequel/model/base.rb +88 -211
  111. data/lib/sequel/model/errors.rb +0 -13
  112. data/lib/sequel/model/plugins.rb +2 -2
  113. data/lib/sequel/no_core_ext.rb +0 -1
  114. data/lib/sequel/plugins/after_initialize.rb +11 -17
  115. data/lib/sequel/plugins/association_autoreloading.rb +1 -47
  116. data/lib/sequel/plugins/association_dependencies.rb +2 -2
  117. data/lib/sequel/plugins/auto_validations.rb +2 -8
  118. data/lib/sequel/plugins/blacklist_security.rb +32 -2
  119. data/lib/sequel/plugins/caching.rb +1 -1
  120. data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
  121. data/lib/sequel/plugins/composition.rb +10 -8
  122. data/lib/sequel/plugins/constraint_validations.rb +2 -2
  123. data/lib/sequel/plugins/dataset_associations.rb +4 -0
  124. data/lib/sequel/plugins/defaults_setter.rb +8 -6
  125. data/lib/sequel/plugins/dirty.rb +6 -6
  126. data/lib/sequel/plugins/force_encoding.rb +13 -8
  127. data/lib/sequel/plugins/hook_class_methods.rb +1 -7
  128. data/lib/sequel/plugins/json_serializer.rb +13 -74
  129. data/lib/sequel/plugins/lazy_attributes.rb +2 -4
  130. data/lib/sequel/plugins/list.rb +1 -1
  131. data/lib/sequel/plugins/many_through_many.rb +4 -11
  132. data/lib/sequel/plugins/many_to_one_pk_lookup.rb +1 -49
  133. data/lib/sequel/plugins/nested_attributes.rb +1 -1
  134. data/lib/sequel/plugins/optimistic_locking.rb +3 -5
  135. data/lib/sequel/plugins/pg_array_associations.rb +453 -0
  136. data/lib/sequel/plugins/pg_typecast_on_load.rb +23 -7
  137. data/lib/sequel/plugins/prepared_statements.rb +1 -1
  138. data/lib/sequel/plugins/prepared_statements_associations.rb +20 -14
  139. data/lib/sequel/plugins/prepared_statements_safe.rb +2 -2
  140. data/lib/sequel/plugins/rcte_tree.rb +1 -1
  141. data/lib/sequel/plugins/serialization.rb +5 -4
  142. data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
  143. data/lib/sequel/plugins/sharding.rb +7 -1
  144. data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
  145. data/lib/sequel/plugins/timestamps.rb +1 -1
  146. data/lib/sequel/plugins/touch.rb +2 -2
  147. data/lib/sequel/plugins/tree.rb +1 -1
  148. data/lib/sequel/plugins/typecast_on_load.rb +19 -4
  149. data/lib/sequel/plugins/validation_class_methods.rb +0 -30
  150. data/lib/sequel/plugins/validation_helpers.rb +13 -31
  151. data/lib/sequel/plugins/xml_serializer.rb +18 -57
  152. data/lib/sequel/sql.rb +20 -22
  153. data/lib/sequel/version.rb +2 -2
  154. data/spec/adapters/db2_spec.rb +14 -23
  155. data/spec/adapters/firebird_spec.rb +25 -29
  156. data/spec/adapters/informix_spec.rb +11 -14
  157. data/spec/adapters/mssql_spec.rb +71 -77
  158. data/spec/adapters/mysql_spec.rb +165 -172
  159. data/spec/adapters/oracle_spec.rb +36 -39
  160. data/spec/adapters/postgres_spec.rb +175 -100
  161. data/spec/adapters/spec_helper.rb +13 -11
  162. data/spec/adapters/sqlite_spec.rb +36 -44
  163. data/spec/core/connection_pool_spec.rb +2 -1
  164. data/spec/core/database_spec.rb +55 -55
  165. data/spec/core/dataset_spec.rb +45 -249
  166. data/spec/core/deprecated_spec.rb +0 -8
  167. data/spec/core/expression_filters_spec.rb +23 -5
  168. data/spec/core/object_graph_spec.rb +4 -66
  169. data/spec/core/schema_spec.rb +35 -12
  170. data/spec/core/spec_helper.rb +3 -2
  171. data/spec/core_extensions_spec.rb +17 -19
  172. data/spec/extensions/arbitrary_servers_spec.rb +2 -3
  173. data/spec/extensions/association_dependencies_spec.rb +14 -14
  174. data/spec/extensions/auto_validations_spec.rb +7 -0
  175. data/spec/extensions/blacklist_security_spec.rb +5 -5
  176. data/spec/extensions/blank_spec.rb +2 -0
  177. data/spec/extensions/class_table_inheritance_spec.rb +2 -2
  178. data/spec/extensions/columns_introspection_spec.rb +2 -29
  179. data/spec/extensions/composition_spec.rb +10 -17
  180. data/spec/extensions/core_refinements_spec.rb +5 -1
  181. data/spec/extensions/dataset_associations_spec.rb +18 -0
  182. data/spec/extensions/date_arithmetic_spec.rb +2 -2
  183. data/spec/extensions/defaults_setter_spec.rb +9 -9
  184. data/spec/extensions/dirty_spec.rb +0 -5
  185. data/spec/extensions/eval_inspect_spec.rb +2 -0
  186. data/spec/extensions/force_encoding_spec.rb +2 -18
  187. data/spec/extensions/hash_aliases_spec.rb +8 -0
  188. data/spec/extensions/hook_class_methods_spec.rb +39 -58
  189. data/spec/extensions/inflector_spec.rb +2 -0
  190. data/spec/extensions/instance_filters_spec.rb +8 -8
  191. data/spec/extensions/json_serializer_spec.rb +1 -41
  192. data/spec/extensions/list_spec.rb +1 -1
  193. data/spec/extensions/many_through_many_spec.rb +106 -109
  194. data/spec/extensions/migration_spec.rb +2 -0
  195. data/spec/extensions/named_timezones_spec.rb +1 -0
  196. data/spec/extensions/pg_array_associations_spec.rb +603 -0
  197. data/spec/extensions/pg_array_ops_spec.rb +25 -0
  198. data/spec/extensions/pg_array_spec.rb +9 -1
  199. data/spec/extensions/pg_hstore_ops_spec.rb +13 -0
  200. data/spec/extensions/pg_hstore_spec.rb +1 -0
  201. data/spec/extensions/pg_json_ops_spec.rb +131 -0
  202. data/spec/extensions/pg_json_spec.rb +10 -4
  203. data/spec/extensions/pg_range_ops_spec.rb +2 -5
  204. data/spec/extensions/pg_range_spec.rb +6 -2
  205. data/spec/extensions/pg_row_ops_spec.rb +2 -0
  206. data/spec/extensions/prepared_statements_associations_spec.rb +26 -5
  207. data/spec/extensions/rcte_tree_spec.rb +15 -15
  208. data/spec/extensions/schema_dumper_spec.rb +0 -1
  209. data/spec/extensions/schema_spec.rb +9 -9
  210. data/spec/extensions/serialization_modification_detection_spec.rb +1 -1
  211. data/spec/extensions/serialization_spec.rb +18 -29
  212. data/spec/extensions/set_overrides_spec.rb +4 -0
  213. data/spec/extensions/{many_to_one_pk_lookup_spec.rb → shared_caching_spec.rb} +1 -4
  214. data/spec/extensions/single_table_inheritance_spec.rb +4 -4
  215. data/spec/extensions/spec_helper.rb +8 -9
  216. data/spec/extensions/sql_expr_spec.rb +2 -0
  217. data/spec/extensions/string_date_time_spec.rb +2 -0
  218. data/spec/extensions/string_stripper_spec.rb +2 -0
  219. data/spec/extensions/tactical_eager_loading_spec.rb +12 -12
  220. data/spec/extensions/thread_local_timezones_spec.rb +2 -0
  221. data/spec/extensions/timestamps_spec.rb +1 -1
  222. data/spec/extensions/to_dot_spec.rb +1 -1
  223. data/spec/extensions/touch_spec.rb +24 -24
  224. data/spec/extensions/tree_spec.rb +7 -7
  225. data/spec/extensions/typecast_on_load_spec.rb +8 -1
  226. data/spec/extensions/update_primary_key_spec.rb +10 -10
  227. data/spec/extensions/validation_class_methods_spec.rb +10 -39
  228. data/spec/extensions/validation_helpers_spec.rb +29 -47
  229. data/spec/extensions/xml_serializer_spec.rb +1 -23
  230. data/spec/integration/associations_test.rb +231 -40
  231. data/spec/integration/database_test.rb +1 -1
  232. data/spec/integration/dataset_test.rb +64 -64
  233. data/spec/integration/eager_loader_test.rb +28 -28
  234. data/spec/integration/migrator_test.rb +1 -1
  235. data/spec/integration/model_test.rb +2 -2
  236. data/spec/integration/plugin_test.rb +21 -21
  237. data/spec/integration/prepared_statement_test.rb +7 -7
  238. data/spec/integration/schema_test.rb +115 -110
  239. data/spec/integration/spec_helper.rb +17 -27
  240. data/spec/integration/timezone_test.rb +1 -1
  241. data/spec/integration/transaction_test.rb +10 -10
  242. data/spec/integration/type_test.rb +2 -2
  243. data/spec/model/association_reflection_spec.rb +2 -28
  244. data/spec/model/associations_spec.rb +239 -188
  245. data/spec/model/base_spec.rb +27 -68
  246. data/spec/model/dataset_methods_spec.rb +4 -4
  247. data/spec/model/eager_loading_spec.rb +160 -172
  248. data/spec/model/hooks_spec.rb +62 -79
  249. data/spec/model/model_spec.rb +36 -51
  250. data/spec/model/plugins_spec.rb +5 -19
  251. data/spec/model/record_spec.rb +125 -151
  252. data/spec/model/spec_helper.rb +8 -6
  253. data/spec/model/validations_spec.rb +4 -17
  254. data/spec/spec_config.rb +2 -10
  255. metadata +50 -56
  256. data/lib/sequel/deprecated_core_extensions.rb +0 -135
  257. data/lib/sequel/extensions/pg_auto_parameterize.rb +0 -185
  258. data/lib/sequel/extensions/pg_statement_cache.rb +0 -318
  259. data/lib/sequel/plugins/identity_map.rb +0 -260
  260. data/lib/sequel_core.rb +0 -2
  261. data/lib/sequel_model.rb +0 -2
  262. data/spec/extensions/association_autoreloading_spec.rb +0 -102
  263. data/spec/extensions/identity_map_spec.rb +0 -337
  264. data/spec/extensions/pg_auto_parameterize_spec.rb +0 -70
  265. data/spec/extensions/pg_statement_cache_spec.rb +0 -208
  266. data/spec/rcov.opts +0 -8
  267. data/spec/spec_config.rb.example +0 -10
@@ -15,7 +15,7 @@ describe "Touch plugin" do
15
15
  @Album.many_to_one :artist, :class=>@Artist
16
16
 
17
17
  @a = @Artist.load(:id=>1)
18
- MODEL_DB.reset
18
+ DB.reset
19
19
  end
20
20
 
21
21
  specify "should default to using Time.now when setting the column values for model instances" do
@@ -23,45 +23,45 @@ describe "Touch plugin" do
23
23
  c.plugin :touch
24
24
  c.columns :id, :updated_at
25
25
  c.load(:id=>1).touch
26
- MODEL_DB.sqls.first.should =~ /UPDATE a SET updated_at = '[-0-9 :.]+' WHERE \(id = 1\)/
26
+ DB.sqls.first.should =~ /UPDATE a SET updated_at = '[-0-9 :.]+' WHERE \(id = 1\)/
27
27
  end
28
28
 
29
29
  specify "should allow #touch instance method for updating the updated_at column" do
30
30
  @Artist.plugin :touch
31
31
  @a.touch
32
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
32
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
33
33
  end
34
34
 
35
35
  specify "should have #touch take an argument for the column to touch" do
36
36
  @Artist.plugin :touch
37
37
  @a.touch(:modified_on)
38
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
38
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
39
39
  end
40
40
 
41
41
  specify "should be able to specify the default column to touch in the plugin call using the :column option" do
42
42
  @Artist.plugin :touch, :column=>:modified_on
43
43
  @a.touch
44
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
44
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
45
45
  end
46
46
 
47
47
  specify "should be able to specify the default column to touch using the touch_column model accessor" do
48
48
  @Artist.plugin :touch
49
49
  @Artist.touch_column = :modified_on
50
50
  @a.touch
51
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
51
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)"]
52
52
  end
53
53
 
54
54
  specify "should be able to specify the associations to touch in the plugin call using the :associations option" do
55
55
  @Artist.plugin :touch, :associations=>:albums
56
56
  @a.touch
57
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
57
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
58
58
  "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (albums.artist_id = 1)"]
59
59
  end
60
60
 
61
61
  specify "should be able to give an array to the :associations option specifying multiple associations" do
62
62
  @Album.plugin :touch, :associations=>[:artist, :followup_albums]
63
63
  @Album.load(:id=>4, :artist_id=>1).touch
64
- sqls = MODEL_DB.sqls
64
+ sqls = DB.sqls
65
65
  sqls.shift.should == "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 4)"
66
66
  sqls.sort.should == ["UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (albums.original_album_id = 4)",
67
67
  "UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (artists.id = 1)"]
@@ -70,21 +70,21 @@ describe "Touch plugin" do
70
70
  specify "should be able to give a hash to the :associations option specifying the column to use for each association" do
71
71
  @Artist.plugin :touch, :associations=>{:albums=>:modified_on}
72
72
  @a.touch
73
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
73
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
74
74
  "UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.artist_id = 1)"]
75
75
  end
76
76
 
77
77
  specify "should default to using the touch_column as the default touch column for associations" do
78
78
  @Artist.plugin :touch, :column=>:modified_on, :associations=>:albums
79
79
  @a.touch
80
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)",
80
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)",
81
81
  "UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.artist_id = 1)"]
82
82
  end
83
83
 
84
84
  specify "should allow the mixed use of symbols and hashes inside an array for the :associations option" do
85
85
  @Album.plugin :touch, :associations=>[:artist, {:followup_albums=>:modified_on}]
86
86
  @Album.load(:id=>4, :artist_id=>1).touch
87
- sqls = MODEL_DB.sqls
87
+ sqls = DB.sqls
88
88
  sqls.shift.should == "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 4)"
89
89
  sqls.sort.should == ["UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.original_album_id = 4)",
90
90
  "UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (artists.id = 1)"]
@@ -94,7 +94,7 @@ describe "Touch plugin" do
94
94
  @Album.plugin :touch
95
95
  @Album.touch_associations(:artist, {:followup_albums=>:modified_on})
96
96
  @Album.load(:id=>4, :artist_id=>1).touch
97
- sqls = MODEL_DB.sqls
97
+ sqls = DB.sqls
98
98
  sqls.shift.should == "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 4)"
99
99
  sqls.sort.should == ["UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.original_album_id = 4)",
100
100
  "UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (artists.id = 1)"]
@@ -104,7 +104,7 @@ describe "Touch plugin" do
104
104
  @Album.plugin :touch
105
105
  @Album.touch_associations(:artist, {:followup_albums=>:modified_on})
106
106
  @Album.load(:id=>4, :artist_id=>1).destroy
107
- sqls = MODEL_DB.sqls
107
+ sqls = DB.sqls
108
108
  sqls.shift.should == "DELETE FROM albums WHERE id = 4"
109
109
  sqls.sort.should == ["UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.original_album_id = 4)",
110
110
  "UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (artists.id = 1)"]
@@ -113,7 +113,7 @@ describe "Touch plugin" do
113
113
  specify "should be able to touch many_to_one associations" do
114
114
  @Album.plugin :touch, :associations=>:artist
115
115
  @Album.load(:id=>3, :artist_id=>4).touch
116
- MODEL_DB.sqls.should == ["UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 3)",
116
+ DB.sqls.should == ["UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 3)",
117
117
  "UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (artists.id = 4)"]
118
118
  end
119
119
 
@@ -121,7 +121,7 @@ describe "Touch plugin" do
121
121
  @Artist.one_to_one :album, :class=>@Album, :key=>:artist_id
122
122
  @Artist.plugin :touch, :associations=>:album
123
123
  @a.touch
124
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
124
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
125
125
  "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (albums.artist_id = 1)"]
126
126
  end
127
127
 
@@ -129,7 +129,7 @@ describe "Touch plugin" do
129
129
  @Artist.many_to_many :albums, :class=>@Album, :left_key=>:artist_id, :join_table=>:aa
130
130
  @Artist.plugin :touch, :associations=>:albums
131
131
  @a.touch
132
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
132
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
133
133
  "SELECT albums.* FROM albums INNER JOIN aa ON ((aa.album_id = albums.id) AND (aa.artist_id = 1))",
134
134
  "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
135
135
  end
@@ -139,7 +139,7 @@ describe "Touch plugin" do
139
139
  @Artist.many_through_many :albums, [[:aa, :artist_id, :album_id]], :class=>@Album
140
140
  @Artist.plugin :touch, :associations=>:albums
141
141
  @a.touch
142
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
142
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)",
143
143
  "SELECT albums.* FROM albums INNER JOIN aa ON ((aa.album_id = albums.id) AND (aa.artist_id = 1))",
144
144
  "UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
145
145
  end
@@ -147,17 +147,17 @@ describe "Touch plugin" do
147
147
  specify "should handle touching many_to_one associations with no associated object" do
148
148
  @Album.plugin :touch, :associations=>:artist
149
149
  @Album.load(:id=>3, :artist_id=>nil).touch
150
- MODEL_DB.sqls.should == ["UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 3)"]
150
+ DB.sqls.should == ["UPDATE albums SET updated_at = CURRENT_TIMESTAMP WHERE (id = 3)"]
151
151
  end
152
152
 
153
153
  specify "should not update a column that doesn't exist" do
154
154
  @Album.plugin :touch, :column=>:x
155
155
  a = @Album.load(:id=>1)
156
156
  a.touch
157
- MODEL_DB.sqls.should == []
157
+ DB.sqls.should == []
158
158
  a.artist_id = 1
159
159
  a.touch
160
- MODEL_DB.sqls.should == ['UPDATE albums SET artist_id = 1 WHERE (id = 1)']
160
+ DB.sqls.should == ['UPDATE albums SET artist_id = 1 WHERE (id = 1)']
161
161
  end
162
162
 
163
163
  specify "should raise an error if given a column argument in touch that doesn't exist" do
@@ -174,21 +174,21 @@ describe "Touch plugin" do
174
174
  @Artist.plugin :touch
175
175
  c1 = Class.new(@Artist)
176
176
  c1.load(:id=>4).touch
177
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 4)"]
177
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 4)"]
178
178
 
179
179
  c1.touch_column = :modified_on
180
180
  c1.touch_associations :albums
181
181
  c1.load(:id=>1).touch
182
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)",
182
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 1)",
183
183
  "UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.artist_id = 1)"]
184
184
 
185
185
  @a.touch
186
- MODEL_DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
186
+ DB.sqls.should == ["UPDATE artists SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
187
187
 
188
188
  @Artist.plugin :touch, :column=>:modified_on, :associations=>:albums
189
189
  c2 = Class.new(@Artist)
190
190
  c2.load(:id=>4).touch
191
- MODEL_DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 4)",
191
+ DB.sqls.should == ["UPDATE artists SET modified_on = CURRENT_TIMESTAMP WHERE (id = 4)",
192
192
  "UPDATE albums SET modified_on = CURRENT_TIMESTAMP WHERE (albums.artist_id = 4)"]
193
193
  end
194
194
  end
@@ -2,7 +2,7 @@ require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
2
2
 
3
3
  describe Sequel::Model, "tree plugin" do
4
4
  def klass(opts={})
5
- @db = MODEL_DB
5
+ @db = DB
6
6
  c = Class.new(Sequel::Model(@db[:nodes]))
7
7
  c.class_eval do
8
8
  def self.name; 'Node'; end
@@ -68,8 +68,8 @@ describe Sequel::Model, "tree plugin" do
68
68
  it "should have ancestors return the ancestors of the current node" do
69
69
  @ds._fetch = [[{:id=>1, :parent_id=>5, :name=>'r'}], [{:id=>5, :parent_id=>nil, :name=>'r2'}]]
70
70
  @o.ancestors.should == [@c.load(:id=>1, :parent_id=>5, :name=>'r'), @c.load(:id=>5, :parent_id=>nil, :name=>'r2')]
71
- @db.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 1) LIMIT 1",
72
- "SELECT * FROM nodes WHERE (nodes.id = 5) LIMIT 1"]
71
+ @db.sqls.should == ["SELECT * FROM nodes WHERE id = 1",
72
+ "SELECT * FROM nodes WHERE id = 5"]
73
73
  end
74
74
 
75
75
  it "should have descendants return the descendants of the current node" do
@@ -84,8 +84,8 @@ describe Sequel::Model, "tree plugin" do
84
84
  it "should have root return the root of the current node" do
85
85
  @ds._fetch = [[{:id=>1, :parent_id=>5, :name=>'r'}], [{:id=>5, :parent_id=>nil, :name=>'r2'}]]
86
86
  @o.root.should == @c.load(:id=>5, :parent_id=>nil, :name=>'r2')
87
- @db.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 1) LIMIT 1",
88
- "SELECT * FROM nodes WHERE (nodes.id = 5) LIMIT 1"]
87
+ @db.sqls.should == ["SELECT * FROM nodes WHERE id = 1",
88
+ "SELECT * FROM nodes WHERE id = 5"]
89
89
  end
90
90
 
91
91
  it "should have root? return true for a root node and false for a child node" do
@@ -100,14 +100,14 @@ describe Sequel::Model, "tree plugin" do
100
100
  it "should have self_and_siblings return the children of the current node's parent" do
101
101
  @ds._fetch = [[{:id=>1, :parent_id=>3, :name=>'r'}], [{:id=>7, :parent_id=>1, :name=>'r2'}, @o.values.dup]]
102
102
  @o.self_and_siblings.should == [@c.load(:id=>7, :parent_id=>1, :name=>'r2'), @o]
103
- @db.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 1) LIMIT 1",
103
+ @db.sqls.should == ["SELECT * FROM nodes WHERE id = 1",
104
104
  "SELECT * FROM nodes WHERE (nodes.parent_id = 1)"]
105
105
  end
106
106
 
107
107
  it "should have siblings return the children of the current node's parent, except for the current node" do
108
108
  @ds._fetch = [[{:id=>1, :parent_id=>3, :name=>'r'}], [{:id=>7, :parent_id=>1, :name=>'r2'}, @o.values.dup]]
109
109
  @o.siblings.should == [@c.load(:id=>7, :parent_id=>1, :name=>'r2')]
110
- @db.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 1) LIMIT 1",
110
+ @db.sqls.should == ["SELECT * FROM nodes WHERE id = 1",
111
111
  "SELECT * FROM nodes WHERE (nodes.parent_id = 1)"]
112
112
  end
113
113
 
@@ -31,10 +31,17 @@ describe Sequel::Model, "TypecastOnLoad plugin" do
31
31
  o.bset.should == true
32
32
  end
33
33
 
34
+ specify "should call setter method with value when automatically reloading the object on creation" do
35
+ @c.plugin :typecast_on_load, :b
36
+ o = @c.new(:b=>"1", :y=>"0")
37
+ o.save.values.should == {:id=>1, :b=>1, :y=>"0"}
38
+ o.bset.should == true
39
+ end
40
+
34
41
  specify "should call setter method with value when automatically reloading the object on creation via insert_select" do
35
42
  @c.plugin :typecast_on_load, :b
36
43
  @c.dataset.meta_def(:insert_select){|h| insert(h); first}
37
- o = @c.load(:id=>1, :b=>"1", :y=>"0")
44
+ o = @c.new(:b=>"1", :y=>"0")
38
45
  o.save.values.should == {:id=>1, :b=>1, :y=>"0"}
39
46
  o.bset.should == true
40
47
  end
@@ -8,27 +8,27 @@ describe "Sequel::Plugins::UpdatePrimaryKey" do
8
8
  @c.set_primary_key :a
9
9
  @c.unrestrict_primary_key
10
10
  @ds = @c.dataset
11
- MODEL_DB.reset
11
+ DB.reset
12
12
  end
13
13
 
14
14
  specify "should handle regular updates" do
15
15
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>1, :b=>4}], [{:a=>1, :b=>4}], [{:a=>1, :b=>5}], [{:a=>1, :b=>5}], [{:a=>1, :b=>6}], [{:a=>1, :b=>6}]]
16
16
  @c.first.update(:b=>4)
17
17
  @c.all.should == [@c.load(:a=>1, :b=>4)]
18
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 4 WHERE (a = 1)", "SELECT * FROM a"]
18
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 4 WHERE (a = 1)", "SELECT * FROM a"]
19
19
  @c.first.set(:b=>5).save
20
20
  @c.all.should == [@c.load(:a=>1, :b=>5)]
21
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 5 WHERE (a = 1)", "SELECT * FROM a"]
21
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 5 WHERE (a = 1)", "SELECT * FROM a"]
22
22
  @c.first.set(:b=>6).save(:columns=>:b)
23
23
  @c.all.should == [@c.load(:a=>1, :b=>6)]
24
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 6 WHERE (a = 1)", "SELECT * FROM a"]
24
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET b = 6 WHERE (a = 1)", "SELECT * FROM a"]
25
25
  end
26
26
 
27
27
  specify "should handle updating the primary key field with another field" do
28
28
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>4}]]
29
29
  @c.first.update(:a=>2, :b=>4)
30
30
  @c.all.should == [@c.load(:a=>2, :b=>4)]
31
- sqls = MODEL_DB.sqls
31
+ sqls = DB.sqls
32
32
  ["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].should include(sqls.slice!(1))
33
33
  sqls.should == ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
34
34
  end
@@ -37,17 +37,17 @@ describe "Sequel::Plugins::UpdatePrimaryKey" do
37
37
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>3}], [{:a=>2, :b=>3}], [{:a=>3, :b=>3}]]
38
38
  @c.first.update(:a=>2)
39
39
  @c.all.should == [@c.load(:a=>2, :b=>3)]
40
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "SELECT * FROM a"]
40
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "SELECT * FROM a"]
41
41
  @c.first.set(:a=>3).save(:columns=>:a)
42
42
  @c.all.should == [@c.load(:a=>3, :b=>3)]
43
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 3 WHERE (a = 2)", "SELECT * FROM a"]
43
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 3 WHERE (a = 2)", "SELECT * FROM a"]
44
44
  end
45
45
 
46
46
  specify "should handle saving after modifying the primary key field with another field" do
47
47
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>4}]]
48
48
  @c.first.set(:a=>2, :b=>4).save
49
49
  @c.all.should == [@c.load(:a=>2, :b=>4)]
50
- sqls = MODEL_DB.sqls
50
+ sqls = DB.sqls
51
51
  ["UPDATE a SET a = 2, b = 4 WHERE (a = 1)", "UPDATE a SET b = 4, a = 2 WHERE (a = 1)"].should include(sqls.slice!(1))
52
52
  sqls.should == ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
53
53
  end
@@ -56,7 +56,7 @@ describe "Sequel::Plugins::UpdatePrimaryKey" do
56
56
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>3}]]
57
57
  @c.first.set(:a=>2).save
58
58
  @c.all.should == [@c.load(:a=>2, :b=>3)]
59
- sqls = MODEL_DB.sqls
59
+ sqls = DB.sqls
60
60
  ["UPDATE a SET a = 2, b = 3 WHERE (a = 1)", "UPDATE a SET b = 3, a = 2 WHERE (a = 1)"].should include(sqls.slice!(1))
61
61
  sqls.should == ["SELECT * FROM a LIMIT 1", "SELECT * FROM a"]
62
62
  end
@@ -65,7 +65,7 @@ describe "Sequel::Plugins::UpdatePrimaryKey" do
65
65
  @ds._fetch = [[{:a=>1, :b=>3}], [{:a=>2, :b=>5}]]
66
66
  @c.first.update(:a=>2).update(:b=>4).set(:b=>5).save
67
67
  @c.all.should == [@c.load(:a=>2, :b=>5)]
68
- MODEL_DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "UPDATE a SET b = 4 WHERE (a = 2)", "UPDATE a SET b = 5 WHERE (a = 2)", "SELECT * FROM a"]
68
+ DB.sqls.should == ["SELECT * FROM a LIMIT 1", "UPDATE a SET a = 2 WHERE (a = 1)", "UPDATE a SET b = 4 WHERE (a = 2)", "UPDATE a SET b = 5 WHERE (a = 2)", "SELECT * FROM a"]
69
69
  end
70
70
 
71
71
  specify "should clear the associations cache of non-many_to_one associations when changing the primary key" do
@@ -619,6 +619,12 @@ describe "Superclass validations" do
619
619
  o.valid?.should be_true
620
620
  end
621
621
 
622
+ specify "should have skip_superclass_validations? return whether superclass validations were skipped" do
623
+ @c2.skip_superclass_validations?.should == nil
624
+ @c2.skip_superclass_validations
625
+ @c2.skip_superclass_validations?.should == true
626
+ end
627
+
622
628
  specify "should be skipped if skip_superclass_validations is called" do
623
629
  @c2.skip_superclass_validations
624
630
 
@@ -764,41 +770,6 @@ describe Sequel::Model, "Validations" do
764
770
  @person.should be_valid
765
771
  end
766
772
 
767
- qspecify "should validate that a column doesn't have a string value" do
768
- p = model_class.call Sequel::Model do
769
- columns :age, :price, :confirmed
770
- self.raise_on_typecast_failure = false
771
- validates_not_string :age
772
- validates_not_string :confirmed
773
- validates_not_string :price, :message=>'is not valid'
774
- @db_schema = {:age=>{:type=>:integer}}
775
- end
776
-
777
- @person = p.new
778
- @person.should be_valid
779
-
780
- @person.confirmed = 't'
781
- @person.should_not be_valid
782
- @person.errors.full_messages.should == ['confirmed is a string']
783
- @person.confirmed = true
784
- @person.should be_valid
785
-
786
- @person.age = 'a'
787
- @person.should_not be_valid
788
- @person.errors.full_messages.should == ['age is not a valid integer']
789
- @person.db_schema[:age][:type] = :datetime
790
- @person.should_not be_valid
791
- @person.errors.full_messages.should == ['age is not a valid datetime']
792
- @person.age = 20
793
- @person.should be_valid
794
-
795
- @person.price = 'a'
796
- @person.should_not be_valid
797
- @person.errors.full_messages.should == ['price is not valid']
798
- @person.price = 20
799
- @person.should be_valid
800
- end
801
-
802
773
  it "should validate that a column has the correct type for the schema column" do
803
774
  p = model_class.call Sequel::Model do
804
775
  columns :age, :d
@@ -1026,26 +997,26 @@ describe "Model#save" do
1026
997
  end
1027
998
  end
1028
999
  @m = @c.load(:id => 4, :x=>6)
1029
- MODEL_DB.reset
1000
+ DB.reset
1030
1001
  end
1031
1002
 
1032
1003
  specify "should save only if validations pass" do
1033
1004
  @m.raise_on_save_failure = false
1034
1005
  @m.should_not be_valid
1035
1006
  @m.save
1036
- MODEL_DB.sqls.should be_empty
1007
+ DB.sqls.should be_empty
1037
1008
 
1038
1009
  @m.x = 7
1039
1010
  @m.should be_valid
1040
1011
  @m.save.should_not be_false
1041
- MODEL_DB.sqls.should == ['UPDATE people SET x = 7 WHERE (id = 4)']
1012
+ DB.sqls.should == ['UPDATE people SET x = 7 WHERE (id = 4)']
1042
1013
  end
1043
1014
 
1044
1015
  specify "should skip validations if the :validate=>false option is used" do
1045
1016
  @m.raise_on_save_failure = false
1046
1017
  @m.should_not be_valid
1047
1018
  @m.save(:validate=>false)
1048
- MODEL_DB.sqls.should == ['UPDATE people SET x = 6 WHERE (id = 4)']
1019
+ DB.sqls.should == ['UPDATE people SET x = 6 WHERE (id = 4)']
1049
1020
  end
1050
1021
 
1051
1022
  specify "should raise error if validations fail and raise_on_save_faiure is true" do
@@ -235,18 +235,6 @@ describe "Sequel::Plugins::ValidationHelpers" do
235
235
  @m.should_not be_valid
236
236
  end
237
237
 
238
- qspecify "should support validates_not_string" do
239
- @c.set_validations{validates_not_string(:value)}
240
- @m.value = 123
241
- @m.should be_valid
242
- @m.value = '123'
243
- @m.should_not be_valid
244
- @m.errors.full_messages.should == ['value is a string']
245
- @m.meta_def(:db_schema){{:value=>{:type=>:integer}}}
246
- @m.should_not be_valid
247
- @m.errors.full_messages.should == ['value is not a valid integer']
248
- end
249
-
250
238
  specify "should support validates_schema_types" do
251
239
  @c.set_validations{validates_schema_types}
252
240
  @m.value = 123
@@ -332,12 +320,6 @@ describe "Sequel::Plugins::ValidationHelpers" do
332
320
  @m.errors.full_messages.should == ['value is not a valid integer or float']
333
321
  end
334
322
 
335
- qspecify "should have validates_type skip nil values by default" do
336
- @c.set_validations{validates_type([Integer, Float], :value)}
337
- @m.value = nil
338
- @m.should be_valid
339
- end
340
-
341
323
  specify "should support validates_not_null" do
342
324
  @c.set_validations{validates_not_null(:value)}
343
325
  @m.should_not be_valid
@@ -374,7 +356,7 @@ describe "Sequel::Plugins::ValidationHelpers" do
374
356
 
375
357
  it "should support validates_unique with a single attribute" do
376
358
  @c.columns(:id, :username, :password)
377
- @c.set_dataset MODEL_DB[:items]
359
+ @c.set_dataset DB[:items]
378
360
  @c.set_validations{validates_unique(:username)}
379
361
  @c.dataset._fetch = proc do |sql|
380
362
  case sql
@@ -390,10 +372,10 @@ describe "Sequel::Plugins::ValidationHelpers" do
390
372
  @user = @c.load(:id=>3, :username => "0records", :password => "anothertest")
391
373
  @user.should be_valid
392
374
 
393
- MODEL_DB.sqls
375
+ DB.sqls
394
376
  @user = @c.new(:password => "anothertest")
395
377
  @user.should be_valid
396
- MODEL_DB.sqls.should == []
378
+ DB.sqls.should == []
397
379
 
398
380
  @user = @c.new(:username => "1record", :password => "anothertest")
399
381
  @user.should_not be_valid
@@ -409,15 +391,15 @@ describe "Sequel::Plugins::ValidationHelpers" do
409
391
 
410
392
  @user = @c.load(:id=>1, :username => "0records", :password => "anothertest")
411
393
  @user.should be_valid
412
- MODEL_DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (id != 1)) LIMIT 1"
394
+ DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (id != 1)) LIMIT 1"
413
395
  @user = @c.new(:username => "0records", :password => "anothertest")
414
396
  @user.should be_valid
415
- MODEL_DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE (username = '0records') LIMIT 1"
397
+ DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE (username = '0records') LIMIT 1"
416
398
  end
417
399
 
418
400
  it "should support validates_unique with multiple attributes" do
419
401
  @c.columns(:id, :username, :password)
420
- @c.set_dataset MODEL_DB[:items]
402
+ @c.set_dataset DB[:items]
421
403
  @c.set_validations{validates_unique([:username, :password])}
422
404
  @c.dataset._fetch = proc do |sql|
423
405
  case sql
@@ -433,7 +415,7 @@ describe "Sequel::Plugins::ValidationHelpers" do
433
415
  @user = @c.load(:id=>3, :username => "0records", :password => "anothertest")
434
416
  @user.should be_valid
435
417
 
436
- MODEL_DB.sqls
418
+ DB.sqls
437
419
  @user = @c.new(:password => "anothertest")
438
420
  @user.should be_valid
439
421
  @user.errors.full_messages.should == []
@@ -443,7 +425,7 @@ describe "Sequel::Plugins::ValidationHelpers" do
443
425
  @user = @c.new
444
426
  @user.should be_valid
445
427
  @user.errors.full_messages.should == []
446
- MODEL_DB.sqls.should == []
428
+ DB.sqls.should == []
447
429
 
448
430
  @user = @c.new(:username => "1record", :password => "anothertest")
449
431
  @user.should_not be_valid
@@ -459,76 +441,76 @@ describe "Sequel::Plugins::ValidationHelpers" do
459
441
 
460
442
  @user = @c.load(:id=>1, :username => "0records", :password => "anothertest")
461
443
  @user.should be_valid
462
- MODEL_DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest') AND (id != 1)) LIMIT 1"
444
+ DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest') AND (id != 1)) LIMIT 1"
463
445
  @user = @c.new(:username => "0records", :password => "anothertest")
464
446
  @user.should be_valid
465
- MODEL_DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"
447
+ DB.sqls.last.should == "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"
466
448
  end
467
449
 
468
450
  it "should support validates_unique with a block" do
469
451
  @c.columns(:id, :username, :password)
470
- @c.set_dataset MODEL_DB[:items]
452
+ @c.set_dataset DB[:items]
471
453
  @c.set_validations{validates_unique(:username){|ds| ds.filter(:active)}}
472
454
  @c.dataset._fetch = {:v=>0}
473
455
 
474
- MODEL_DB.reset
456
+ DB.reset
475
457
  @c.new(:username => "0records", :password => "anothertest").should be_valid
476
458
  @c.load(:id=>3, :username => "0records", :password => "anothertest").should be_valid
477
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND active) LIMIT 1",
459
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND active) LIMIT 1",
478
460
  "SELECT count(*) AS count FROM items WHERE ((username = '0records') AND active AND (id != 3)) LIMIT 1"]
479
461
  end
480
462
 
481
463
  it "should support validates_unique with a custom filter" do
482
464
  @c.columns(:id, :username, :password)
483
- @c.set_dataset MODEL_DB[:items]
465
+ @c.set_dataset DB[:items]
484
466
  @c.set_validations{validates_unique(:username, :where=>proc{|ds, obj, cols| ds.where(cols.map{|c| [Sequel.function(:lower, c), obj.send(c).downcase]})})}
485
467
  @c.dataset._fetch = {:v=>0}
486
468
 
487
- MODEL_DB.reset
469
+ DB.reset
488
470
  @c.new(:username => "0RECORDS", :password => "anothertest").should be_valid
489
471
  @c.load(:id=>3, :username => "0RECORDS", :password => "anothertest").should be_valid
490
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE (lower(username) = '0records') LIMIT 1",
472
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE (lower(username) = '0records') LIMIT 1",
491
473
  "SELECT count(*) AS count FROM items WHERE ((lower(username) = '0records') AND (id != 3)) LIMIT 1"]
492
474
  end
493
475
 
494
476
  it "should support :only_if_modified option for validates_unique, and not check uniqueness for existing records if values haven't changed" do
495
477
  @c.columns(:id, :username, :password)
496
- @c.set_dataset MODEL_DB[:items]
478
+ @c.set_dataset DB[:items]
497
479
  @c.set_validations{validates_unique([:username, :password], :only_if_modified=>true)}
498
480
  @c.dataset._fetch = {:v=>0}
499
481
 
500
- MODEL_DB.reset
482
+ DB.reset
501
483
  @c.new(:username => "0records", :password => "anothertest").should be_valid
502
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"]
503
- MODEL_DB.reset
484
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = 'anothertest')) LIMIT 1"]
485
+ DB.reset
504
486
  m = @c.load(:id=>3, :username => "0records", :password => "anothertest")
505
487
  m.should be_valid
506
- MODEL_DB.sqls.should == []
488
+ DB.sqls.should == []
507
489
 
508
490
  m.username = '1'
509
491
  m.should be_valid
510
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '1') AND (password = 'anothertest') AND (id != 3)) LIMIT 1"]
492
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '1') AND (password = 'anothertest') AND (id != 3)) LIMIT 1"]
511
493
 
512
494
  m = @c.load(:id=>3, :username => "0records", :password => "anothertest")
513
- MODEL_DB.reset
495
+ DB.reset
514
496
  m.password = '1'
515
497
  m.should be_valid
516
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = '1') AND (id != 3)) LIMIT 1"]
517
- MODEL_DB.reset
498
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '0records') AND (password = '1') AND (id != 3)) LIMIT 1"]
499
+ DB.reset
518
500
  m.username = '2'
519
501
  m.should be_valid
520
- MODEL_DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '2') AND (password = '1') AND (id != 3)) LIMIT 1"]
502
+ DB.sqls.should == ["SELECT count(*) AS count FROM items WHERE ((username = '2') AND (password = '1') AND (id != 3)) LIMIT 1"]
521
503
  end
522
504
 
523
505
  it "should not attempt a database query if the underlying columns have validation errors" do
524
506
  @c.columns(:id, :username, :password)
525
- @c.set_dataset MODEL_DB[:items]
507
+ @c.set_dataset DB[:items]
526
508
  @c.set_validations{errors.add(:username, 'foo'); validates_unique([:username, :password])}
527
509
  @c.dataset._fetch = {:v=>0}
528
510
 
529
- MODEL_DB.reset
511
+ DB.reset
530
512
  m = @c.new(:username => "1", :password => "anothertest")
531
513
  m.should_not be_valid
532
- MODEL_DB.sqls.should == []
514
+ DB.sqls.should == []
533
515
  end
534
516
  end