viking-sequel 3.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. data/CHANGELOG +3134 -0
  2. data/COPYING +19 -0
  3. data/README.rdoc +723 -0
  4. data/Rakefile +193 -0
  5. data/bin/sequel +196 -0
  6. data/doc/advanced_associations.rdoc +644 -0
  7. data/doc/cheat_sheet.rdoc +218 -0
  8. data/doc/dataset_basics.rdoc +106 -0
  9. data/doc/dataset_filtering.rdoc +158 -0
  10. data/doc/opening_databases.rdoc +296 -0
  11. data/doc/prepared_statements.rdoc +104 -0
  12. data/doc/reflection.rdoc +84 -0
  13. data/doc/release_notes/1.0.txt +38 -0
  14. data/doc/release_notes/1.1.txt +143 -0
  15. data/doc/release_notes/1.3.txt +101 -0
  16. data/doc/release_notes/1.4.0.txt +53 -0
  17. data/doc/release_notes/1.5.0.txt +155 -0
  18. data/doc/release_notes/2.0.0.txt +298 -0
  19. data/doc/release_notes/2.1.0.txt +271 -0
  20. data/doc/release_notes/2.10.0.txt +328 -0
  21. data/doc/release_notes/2.11.0.txt +215 -0
  22. data/doc/release_notes/2.12.0.txt +534 -0
  23. data/doc/release_notes/2.2.0.txt +253 -0
  24. data/doc/release_notes/2.3.0.txt +88 -0
  25. data/doc/release_notes/2.4.0.txt +106 -0
  26. data/doc/release_notes/2.5.0.txt +137 -0
  27. data/doc/release_notes/2.6.0.txt +157 -0
  28. data/doc/release_notes/2.7.0.txt +166 -0
  29. data/doc/release_notes/2.8.0.txt +171 -0
  30. data/doc/release_notes/2.9.0.txt +97 -0
  31. data/doc/release_notes/3.0.0.txt +221 -0
  32. data/doc/release_notes/3.1.0.txt +406 -0
  33. data/doc/release_notes/3.10.0.txt +286 -0
  34. data/doc/release_notes/3.2.0.txt +268 -0
  35. data/doc/release_notes/3.3.0.txt +192 -0
  36. data/doc/release_notes/3.4.0.txt +325 -0
  37. data/doc/release_notes/3.5.0.txt +510 -0
  38. data/doc/release_notes/3.6.0.txt +366 -0
  39. data/doc/release_notes/3.7.0.txt +179 -0
  40. data/doc/release_notes/3.8.0.txt +151 -0
  41. data/doc/release_notes/3.9.0.txt +233 -0
  42. data/doc/schema.rdoc +36 -0
  43. data/doc/sharding.rdoc +113 -0
  44. data/doc/virtual_rows.rdoc +205 -0
  45. data/lib/sequel.rb +1 -0
  46. data/lib/sequel/adapters/ado.rb +90 -0
  47. data/lib/sequel/adapters/ado/mssql.rb +30 -0
  48. data/lib/sequel/adapters/amalgalite.rb +176 -0
  49. data/lib/sequel/adapters/db2.rb +139 -0
  50. data/lib/sequel/adapters/dbi.rb +113 -0
  51. data/lib/sequel/adapters/do.rb +188 -0
  52. data/lib/sequel/adapters/do/mysql.rb +49 -0
  53. data/lib/sequel/adapters/do/postgres.rb +91 -0
  54. data/lib/sequel/adapters/do/sqlite.rb +40 -0
  55. data/lib/sequel/adapters/firebird.rb +283 -0
  56. data/lib/sequel/adapters/informix.rb +77 -0
  57. data/lib/sequel/adapters/jdbc.rb +587 -0
  58. data/lib/sequel/adapters/jdbc/as400.rb +58 -0
  59. data/lib/sequel/adapters/jdbc/h2.rb +133 -0
  60. data/lib/sequel/adapters/jdbc/mssql.rb +57 -0
  61. data/lib/sequel/adapters/jdbc/mysql.rb +78 -0
  62. data/lib/sequel/adapters/jdbc/oracle.rb +50 -0
  63. data/lib/sequel/adapters/jdbc/postgresql.rb +108 -0
  64. data/lib/sequel/adapters/jdbc/sqlite.rb +55 -0
  65. data/lib/sequel/adapters/mysql.rb +421 -0
  66. data/lib/sequel/adapters/odbc.rb +143 -0
  67. data/lib/sequel/adapters/odbc/mssql.rb +42 -0
  68. data/lib/sequel/adapters/openbase.rb +64 -0
  69. data/lib/sequel/adapters/oracle.rb +131 -0
  70. data/lib/sequel/adapters/postgres.rb +504 -0
  71. data/lib/sequel/adapters/shared/mssql.rb +490 -0
  72. data/lib/sequel/adapters/shared/mysql.rb +498 -0
  73. data/lib/sequel/adapters/shared/oracle.rb +195 -0
  74. data/lib/sequel/adapters/shared/postgres.rb +830 -0
  75. data/lib/sequel/adapters/shared/progress.rb +44 -0
  76. data/lib/sequel/adapters/shared/sqlite.rb +389 -0
  77. data/lib/sequel/adapters/sqlite.rb +224 -0
  78. data/lib/sequel/adapters/utils/stored_procedures.rb +84 -0
  79. data/lib/sequel/connection_pool.rb +99 -0
  80. data/lib/sequel/connection_pool/sharded_single.rb +84 -0
  81. data/lib/sequel/connection_pool/sharded_threaded.rb +211 -0
  82. data/lib/sequel/connection_pool/single.rb +29 -0
  83. data/lib/sequel/connection_pool/threaded.rb +150 -0
  84. data/lib/sequel/core.rb +293 -0
  85. data/lib/sequel/core_sql.rb +241 -0
  86. data/lib/sequel/database.rb +1079 -0
  87. data/lib/sequel/database/schema_generator.rb +327 -0
  88. data/lib/sequel/database/schema_methods.rb +203 -0
  89. data/lib/sequel/database/schema_sql.rb +320 -0
  90. data/lib/sequel/dataset.rb +32 -0
  91. data/lib/sequel/dataset/actions.rb +441 -0
  92. data/lib/sequel/dataset/features.rb +86 -0
  93. data/lib/sequel/dataset/graph.rb +254 -0
  94. data/lib/sequel/dataset/misc.rb +119 -0
  95. data/lib/sequel/dataset/mutation.rb +64 -0
  96. data/lib/sequel/dataset/prepared_statements.rb +227 -0
  97. data/lib/sequel/dataset/query.rb +709 -0
  98. data/lib/sequel/dataset/sql.rb +996 -0
  99. data/lib/sequel/exceptions.rb +51 -0
  100. data/lib/sequel/extensions/blank.rb +43 -0
  101. data/lib/sequel/extensions/inflector.rb +242 -0
  102. data/lib/sequel/extensions/looser_typecasting.rb +21 -0
  103. data/lib/sequel/extensions/migration.rb +239 -0
  104. data/lib/sequel/extensions/named_timezones.rb +61 -0
  105. data/lib/sequel/extensions/pagination.rb +100 -0
  106. data/lib/sequel/extensions/pretty_table.rb +82 -0
  107. data/lib/sequel/extensions/query.rb +52 -0
  108. data/lib/sequel/extensions/schema_dumper.rb +271 -0
  109. data/lib/sequel/extensions/sql_expr.rb +122 -0
  110. data/lib/sequel/extensions/string_date_time.rb +46 -0
  111. data/lib/sequel/extensions/thread_local_timezones.rb +48 -0
  112. data/lib/sequel/metaprogramming.rb +9 -0
  113. data/lib/sequel/model.rb +120 -0
  114. data/lib/sequel/model/associations.rb +1514 -0
  115. data/lib/sequel/model/base.rb +1069 -0
  116. data/lib/sequel/model/default_inflections.rb +45 -0
  117. data/lib/sequel/model/errors.rb +39 -0
  118. data/lib/sequel/model/exceptions.rb +21 -0
  119. data/lib/sequel/model/inflections.rb +162 -0
  120. data/lib/sequel/model/plugins.rb +70 -0
  121. data/lib/sequel/plugins/active_model.rb +59 -0
  122. data/lib/sequel/plugins/association_dependencies.rb +103 -0
  123. data/lib/sequel/plugins/association_proxies.rb +41 -0
  124. data/lib/sequel/plugins/boolean_readers.rb +53 -0
  125. data/lib/sequel/plugins/caching.rb +141 -0
  126. data/lib/sequel/plugins/class_table_inheritance.rb +214 -0
  127. data/lib/sequel/plugins/composition.rb +138 -0
  128. data/lib/sequel/plugins/force_encoding.rb +72 -0
  129. data/lib/sequel/plugins/hook_class_methods.rb +126 -0
  130. data/lib/sequel/plugins/identity_map.rb +116 -0
  131. data/lib/sequel/plugins/instance_filters.rb +98 -0
  132. data/lib/sequel/plugins/instance_hooks.rb +57 -0
  133. data/lib/sequel/plugins/lazy_attributes.rb +77 -0
  134. data/lib/sequel/plugins/many_through_many.rb +208 -0
  135. data/lib/sequel/plugins/nested_attributes.rb +206 -0
  136. data/lib/sequel/plugins/optimistic_locking.rb +81 -0
  137. data/lib/sequel/plugins/rcte_tree.rb +281 -0
  138. data/lib/sequel/plugins/schema.rb +66 -0
  139. data/lib/sequel/plugins/serialization.rb +166 -0
  140. data/lib/sequel/plugins/single_table_inheritance.rb +74 -0
  141. data/lib/sequel/plugins/subclasses.rb +45 -0
  142. data/lib/sequel/plugins/tactical_eager_loading.rb +61 -0
  143. data/lib/sequel/plugins/timestamps.rb +87 -0
  144. data/lib/sequel/plugins/touch.rb +118 -0
  145. data/lib/sequel/plugins/typecast_on_load.rb +72 -0
  146. data/lib/sequel/plugins/validation_class_methods.rb +405 -0
  147. data/lib/sequel/plugins/validation_helpers.rb +223 -0
  148. data/lib/sequel/sql.rb +1020 -0
  149. data/lib/sequel/timezones.rb +161 -0
  150. data/lib/sequel/version.rb +12 -0
  151. data/lib/sequel_core.rb +1 -0
  152. data/lib/sequel_model.rb +1 -0
  153. data/spec/adapters/firebird_spec.rb +407 -0
  154. data/spec/adapters/informix_spec.rb +97 -0
  155. data/spec/adapters/mssql_spec.rb +403 -0
  156. data/spec/adapters/mysql_spec.rb +1019 -0
  157. data/spec/adapters/oracle_spec.rb +286 -0
  158. data/spec/adapters/postgres_spec.rb +969 -0
  159. data/spec/adapters/spec_helper.rb +51 -0
  160. data/spec/adapters/sqlite_spec.rb +432 -0
  161. data/spec/core/connection_pool_spec.rb +808 -0
  162. data/spec/core/core_sql_spec.rb +417 -0
  163. data/spec/core/database_spec.rb +1662 -0
  164. data/spec/core/dataset_spec.rb +3827 -0
  165. data/spec/core/expression_filters_spec.rb +595 -0
  166. data/spec/core/object_graph_spec.rb +296 -0
  167. data/spec/core/schema_generator_spec.rb +159 -0
  168. data/spec/core/schema_spec.rb +830 -0
  169. data/spec/core/spec_helper.rb +56 -0
  170. data/spec/core/version_spec.rb +7 -0
  171. data/spec/extensions/active_model_spec.rb +76 -0
  172. data/spec/extensions/association_dependencies_spec.rb +127 -0
  173. data/spec/extensions/association_proxies_spec.rb +50 -0
  174. data/spec/extensions/blank_spec.rb +67 -0
  175. data/spec/extensions/boolean_readers_spec.rb +92 -0
  176. data/spec/extensions/caching_spec.rb +250 -0
  177. data/spec/extensions/class_table_inheritance_spec.rb +252 -0
  178. data/spec/extensions/composition_spec.rb +194 -0
  179. data/spec/extensions/force_encoding_spec.rb +117 -0
  180. data/spec/extensions/hook_class_methods_spec.rb +470 -0
  181. data/spec/extensions/identity_map_spec.rb +202 -0
  182. data/spec/extensions/inflector_spec.rb +181 -0
  183. data/spec/extensions/instance_filters_spec.rb +55 -0
  184. data/spec/extensions/instance_hooks_spec.rb +133 -0
  185. data/spec/extensions/lazy_attributes_spec.rb +153 -0
  186. data/spec/extensions/looser_typecasting_spec.rb +39 -0
  187. data/spec/extensions/many_through_many_spec.rb +884 -0
  188. data/spec/extensions/migration_spec.rb +332 -0
  189. data/spec/extensions/named_timezones_spec.rb +72 -0
  190. data/spec/extensions/nested_attributes_spec.rb +396 -0
  191. data/spec/extensions/optimistic_locking_spec.rb +100 -0
  192. data/spec/extensions/pagination_spec.rb +99 -0
  193. data/spec/extensions/pretty_table_spec.rb +91 -0
  194. data/spec/extensions/query_spec.rb +85 -0
  195. data/spec/extensions/rcte_tree_spec.rb +205 -0
  196. data/spec/extensions/schema_dumper_spec.rb +357 -0
  197. data/spec/extensions/schema_spec.rb +127 -0
  198. data/spec/extensions/serialization_spec.rb +209 -0
  199. data/spec/extensions/single_table_inheritance_spec.rb +96 -0
  200. data/spec/extensions/spec_helper.rb +91 -0
  201. data/spec/extensions/sql_expr_spec.rb +89 -0
  202. data/spec/extensions/string_date_time_spec.rb +93 -0
  203. data/spec/extensions/subclasses_spec.rb +52 -0
  204. data/spec/extensions/tactical_eager_loading_spec.rb +65 -0
  205. data/spec/extensions/thread_local_timezones_spec.rb +45 -0
  206. data/spec/extensions/timestamps_spec.rb +150 -0
  207. data/spec/extensions/touch_spec.rb +155 -0
  208. data/spec/extensions/typecast_on_load_spec.rb +69 -0
  209. data/spec/extensions/validation_class_methods_spec.rb +984 -0
  210. data/spec/extensions/validation_helpers_spec.rb +438 -0
  211. data/spec/integration/associations_test.rb +281 -0
  212. data/spec/integration/database_test.rb +26 -0
  213. data/spec/integration/dataset_test.rb +963 -0
  214. data/spec/integration/eager_loader_test.rb +734 -0
  215. data/spec/integration/model_test.rb +130 -0
  216. data/spec/integration/plugin_test.rb +814 -0
  217. data/spec/integration/prepared_statement_test.rb +213 -0
  218. data/spec/integration/schema_test.rb +361 -0
  219. data/spec/integration/spec_helper.rb +73 -0
  220. data/spec/integration/timezone_test.rb +55 -0
  221. data/spec/integration/transaction_test.rb +122 -0
  222. data/spec/integration/type_test.rb +96 -0
  223. data/spec/model/association_reflection_spec.rb +175 -0
  224. data/spec/model/associations_spec.rb +2633 -0
  225. data/spec/model/base_spec.rb +418 -0
  226. data/spec/model/dataset_methods_spec.rb +78 -0
  227. data/spec/model/eager_loading_spec.rb +1391 -0
  228. data/spec/model/hooks_spec.rb +240 -0
  229. data/spec/model/inflector_spec.rb +26 -0
  230. data/spec/model/model_spec.rb +593 -0
  231. data/spec/model/plugins_spec.rb +236 -0
  232. data/spec/model/record_spec.rb +1500 -0
  233. data/spec/model/spec_helper.rb +97 -0
  234. data/spec/model/validations_spec.rb +153 -0
  235. data/spec/rcov.opts +6 -0
  236. data/spec/spec_config.rb.example +10 -0
  237. metadata +346 -0
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+ unless Object.const_defined?('Sequel')
4
+ $:.unshift(File.join(File.dirname(__FILE__), "../../lib/"))
5
+ require 'sequel'
6
+ end
7
+ begin
8
+ require File.join(File.dirname(File.dirname(__FILE__)), 'spec_config.rb') unless defined?(INTEGRATION_DB)
9
+ rescue LoadError
10
+ end
11
+
12
+ Sequel::Model.use_transactions = false
13
+
14
+ $sqls = []
15
+ def clear_sqls
16
+ $sqls.clear
17
+ end
18
+
19
+ class Spec::Example::ExampleGroup
20
+ def log
21
+ begin
22
+ INTEGRATION_DB.loggers << Logger.new(STDOUT)
23
+ yield
24
+ ensure
25
+ INTEGRATION_DB.loggers.pop
26
+ end
27
+ end
28
+
29
+ def self.log_specify(message, &block)
30
+ specify(message){log{instance_eval(&block)}}
31
+ end
32
+
33
+ def self.cspecify(message, *checked, &block)
34
+ pending = false
35
+ checked.each do |c|
36
+ case c
37
+ when INTEGRATION_DB.database_type
38
+ pending = c
39
+ when Array
40
+ case c.length
41
+ when 1
42
+ pending = c if c.first == INTEGRATION_DB.class.adapter_scheme
43
+ when 2
44
+ if c.first.is_a?(Proc)
45
+ pending = c if c.first.call(INTEGRATION_DB) && c.last == INTEGRATION_DB.database_type
46
+ elsif c.last.is_a?(Proc)
47
+ pending = c if c.first == INTEGRATION_DB.class.adapter_scheme && c.last.call(INTEGRATION_DB)
48
+ else
49
+ pending = c if c.first == INTEGRATION_DB.class.adapter_scheme && c.last == INTEGRATION_DB.database_type
50
+ end
51
+ when 3
52
+ pending = c if c[0] == INTEGRATION_DB.class.adapter_scheme && c[1] == INTEGRATION_DB.database_type && c[2].call(INTEGRATION_DB)
53
+ end
54
+ end
55
+ break if pending
56
+ end
57
+ if pending
58
+ specify(message){pending("Not yet working on #{Array(pending).join(', ')}", &block)}
59
+ else
60
+ specify(message, &block)
61
+ end
62
+ end
63
+ end
64
+
65
+ if defined?(INTEGRATION_DB) || defined?(INTEGRATION_URL) || ENV['SEQUEL_INTEGRATION_URL']
66
+ unless defined?(INTEGRATION_DB)
67
+ url = defined?(INTEGRATION_URL) ? INTEGRATION_URL : ENV['SEQUEL_INTEGRATION_URL']
68
+ INTEGRATION_DB = Sequel.connect(url)
69
+ #INTEGRATION_DB.instance_variable_set(:@server_version, 80100)
70
+ end
71
+ else
72
+ INTEGRATION_DB = Sequel.sqlite('', :quote_identifiers=>false)
73
+ end
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe "Sequel timezone support" do
4
+ def test_timezone
5
+ t = Time.now
6
+ @db[:t].insert(t)
7
+ t2 = @db[:t].single_value
8
+ t2 = Sequel.database_to_application_timestamp(t2.to_s) unless t2.is_a?(Time)
9
+ (t2 - t).should be_close(0, 2)
10
+ t2.utc_offset.should == 0 if Sequel.application_timezone == :utc
11
+ t2.utc_offset.should == t.getlocal.utc_offset if Sequel.application_timezone == :local
12
+ @db[:t].delete
13
+
14
+ dt = DateTime.now
15
+ Sequel.datetime_class = DateTime
16
+ @db[:t].insert(dt)
17
+ dt2 = @db[:t].single_value
18
+ dt2 = Sequel.database_to_application_timestamp(dt2.to_s) unless dt2.is_a?(DateTime)
19
+ (dt2 - dt).should be_close(0, 0.00002)
20
+ dt2.offset.should == 0 if Sequel.application_timezone == :utc
21
+ dt2.offset.should == dt.offset if Sequel.application_timezone == :local
22
+ end
23
+
24
+ before do
25
+ @db = INTEGRATION_DB
26
+ @db.create_table!(:t){DateTime :t}
27
+ end
28
+ after do
29
+ @db.drop_table(:t)
30
+ Sequel.default_timezone = nil
31
+ Sequel.datetime_class = Time
32
+ end
33
+
34
+ cspecify "should support using UTC for database storage and local time for the application", [:do, proc{|db| db.database_type != :sqlite}] do
35
+ Sequel.database_timezone = :utc
36
+ Sequel.application_timezone = :local
37
+ test_timezone
38
+ end
39
+
40
+ cspecify "should support using local time for database storage and UTC for the application", [:do, proc{|db| db.database_type != :sqlite}] do
41
+ Sequel.database_timezone = :local
42
+ Sequel.application_timezone = :utc
43
+ test_timezone
44
+ end
45
+
46
+ cspecify "should support using UTC for both database storage and for application", [:do, proc{|db| db.database_type != :sqlite}] do
47
+ Sequel.default_timezone = :utc
48
+ test_timezone
49
+ end
50
+
51
+ specify "should support using local time for both database storage and for application" do
52
+ Sequel.default_timezone = :local
53
+ test_timezone
54
+ end
55
+ end
@@ -0,0 +1,122 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe "Database transactions" do
4
+ before do
5
+ INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
6
+ INTEGRATION_DB.create_table(:items, :engine=>'InnoDB'){String :name; Integer :value}
7
+ @d = INTEGRATION_DB[:items]
8
+ clear_sqls
9
+ end
10
+ after do
11
+ INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
12
+ end
13
+
14
+ specify "should support transactions" do
15
+ INTEGRATION_DB.transaction do
16
+ @d << {:name => 'abc', :value => 1}
17
+ end
18
+
19
+ @d.count.should == 1
20
+ end
21
+
22
+ specify "should have #transaction yield the connection" do
23
+ INTEGRATION_DB.transaction do |conn|
24
+ conn.should_not == nil
25
+ end
26
+ end
27
+
28
+ specify "should correctly rollback transactions" do
29
+ proc do
30
+ INTEGRATION_DB.transaction do
31
+ @d << {:name => 'abc', :value => 1}
32
+ raise Interrupt, 'asdf'
33
+ end
34
+ end.should raise_error(Interrupt)
35
+
36
+ proc do
37
+ INTEGRATION_DB.transaction do
38
+ @d << {:name => 'abc', :value => 1}
39
+ raise Sequel::Rollback
40
+ end
41
+ end.should_not raise_error
42
+
43
+ @d.count.should == 0
44
+ end
45
+
46
+ specify "should support nested transactions" do
47
+ @db = INTEGRATION_DB
48
+ @db.transaction do
49
+ @db.transaction do
50
+ @d << {:name => 'abc', :value => 1}
51
+ end
52
+ end
53
+ @d.count.should == 1
54
+
55
+ @d.delete
56
+ proc {@db.transaction do
57
+ @d << {:name => 'abc', :value => 1}
58
+ @db.transaction do
59
+ raise Sequel::Rollback
60
+ end
61
+ end}.should_not raise_error
62
+ @d.count.should == 0
63
+
64
+ proc {@db.transaction do
65
+ @d << {:name => 'abc', :value => 1}
66
+ @db.transaction do
67
+ raise Interrupt, 'asdf'
68
+ end
69
+ end}.should raise_error(Interrupt)
70
+ @d.count.should == 0
71
+ end
72
+
73
+ if INTEGRATION_DB.supports_savepoints?
74
+ cspecify "should support nested transactions through savepoints using the savepoint option", [:jdbc, :sqlite] do
75
+ @db = INTEGRATION_DB
76
+ @db.transaction do
77
+ @d << {:name => '1'}
78
+ @db.transaction(:savepoint=>true) do
79
+ @d << {:name => '2'}
80
+ @db.transaction do
81
+ @d << {:name => '3'}
82
+ raise Sequel::Rollback
83
+ end
84
+ end
85
+ @d << {:name => '4'}
86
+ @db.transaction do
87
+ @d << {:name => '6'}
88
+ @db.transaction(:savepoint=>true) do
89
+ @d << {:name => '7'}
90
+ raise Sequel::Rollback
91
+ end
92
+ end
93
+ @d << {:name => '5'}
94
+ end
95
+
96
+ @d.order(:name).map(:name).should == %w{1 4 5 6}
97
+ end
98
+ end
99
+
100
+ specify "should handle returning inside of the block by committing" do
101
+ def INTEGRATION_DB.ret_commit
102
+ transaction do
103
+ self[:items] << {:name => 'abc'}
104
+ return
105
+ self[:items] << {:name => 'd'}
106
+ end
107
+ end
108
+
109
+ @d.count.should == 0
110
+ INTEGRATION_DB.ret_commit
111
+ @d.count.should == 1
112
+ INTEGRATION_DB.ret_commit
113
+ @d.count.should == 2
114
+ proc do
115
+ INTEGRATION_DB.transaction do
116
+ raise Interrupt, 'asdf'
117
+ end
118
+ end.should raise_error(Interrupt)
119
+
120
+ @d.count.should == 2
121
+ end
122
+ end
@@ -0,0 +1,96 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe "Supported types" do
4
+ def create_items_table_with_column(name, type, opts={})
5
+ INTEGRATION_DB.create_table!(:items){column name, type, opts}
6
+ INTEGRATION_DB[:items]
7
+ end
8
+
9
+ cspecify "should support casting correctly", [:sqlite, :sqlite] do
10
+ ds = create_items_table_with_column(:number, Integer)
11
+ ds.insert(:number => 1)
12
+ ds.select(:number.cast_string.as(:n)).map(:n).should == %w'1'
13
+ ds = create_items_table_with_column(:name, String)
14
+ ds.insert(:name=> '1')
15
+ ds.select(:name.cast_numeric.as(:n)).map(:n).should == [1]
16
+ end
17
+
18
+ specify "should support NULL correctly" do
19
+ ds = create_items_table_with_column(:number, Integer)
20
+ ds.insert(:number => nil)
21
+ ds.all.should == [{:number=>nil}]
22
+ end
23
+
24
+ specify "should support generic integer type" do
25
+ ds = create_items_table_with_column(:number, Integer)
26
+ ds.insert(:number => 2)
27
+ ds.all.should == [{:number=>2}]
28
+ end
29
+
30
+ specify "should support generic fixnum type" do
31
+ ds = create_items_table_with_column(:number, Fixnum)
32
+ ds.insert(:number => 2)
33
+ ds.all.should == [{:number=>2}]
34
+ end
35
+
36
+ specify "should support generic bignum type" do
37
+ ds = create_items_table_with_column(:number, Bignum)
38
+ ds.insert(:number => 2**34)
39
+ ds.all.should == [{:number=>2**34}]
40
+ end
41
+
42
+ specify "should support generic float type" do
43
+ ds = create_items_table_with_column(:number, Float)
44
+ ds.insert(:number => 2.1)
45
+ ds.all.should == [{:number=>2.1}]
46
+ end
47
+
48
+ cspecify "should support generic numeric type", [:odbc, :mssql] do
49
+ ds = create_items_table_with_column(:number, Numeric, :size=>[15, 10])
50
+ ds.insert(:number => BigDecimal.new('2.123456789'))
51
+ ds.all.should == [{:number=>BigDecimal.new('2.123456789')}]
52
+ ds = create_items_table_with_column(:number, BigDecimal, :size=>[15, 10])
53
+ ds.insert(:number => BigDecimal.new('2.123456789'))
54
+ ds.all.should == [{:number=>BigDecimal.new('2.123456789')}]
55
+ end
56
+
57
+ specify "should support generic string type" do
58
+ ds = create_items_table_with_column(:name, String)
59
+ ds.insert(:name => 'Test User')
60
+ ds.all.should == [{:name=>'Test User'}]
61
+ end
62
+
63
+ cspecify "should support generic date type", [:do, :sqlite], [:jdbc, :sqlite], :mssql do
64
+ ds = create_items_table_with_column(:dat, Date)
65
+ d = Date.today
66
+ ds.insert(:dat => d)
67
+ ds.first[:dat].should == d
68
+ end
69
+
70
+ cspecify "should support generic datetime type", [:do, :sqlite], [:jdbc, :sqlite] do
71
+ ds = create_items_table_with_column(:tim, DateTime)
72
+ t = DateTime.now
73
+ ds.insert(:tim => t)
74
+ ds.first[:tim].strftime('%Y%m%d%H%M%S').should == t.strftime('%Y%m%d%H%M%S')
75
+ ds = create_items_table_with_column(:tim, Time)
76
+ t = Time.now
77
+ ds.insert(:tim => t)
78
+ ds.first[:tim].strftime('%Y%m%d%H%M%S').should == t.strftime('%Y%m%d%H%M%S')
79
+ end
80
+
81
+ cspecify "should support generic file type", [:do], [:odbc, :mssql] do
82
+ ds = create_items_table_with_column(:name, File)
83
+ ds.insert(:name => ("a\0"*300).to_sequel_blob)
84
+ ds.all.should == [{:name=>("a\0"*300).to_sequel_blob}]
85
+ ds.first[:name].should be_a_kind_of(::Sequel::SQL::Blob)
86
+ end
87
+
88
+ cspecify "should support generic boolean type", [:do, :sqlite], [:jdbc, :sqlite], [:odbc, :mssql] do
89
+ ds = create_items_table_with_column(:number, TrueClass)
90
+ ds.insert(:number => true)
91
+ ds.all.should == [{:number=>true}]
92
+ ds = create_items_table_with_column(:number, FalseClass)
93
+ ds.insert(:number => true)
94
+ ds.all.should == [{:number=>true}]
95
+ end
96
+ end
@@ -0,0 +1,175 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Sequel::Model::Associations::AssociationReflection, "#associated_class" do
4
+ before do
5
+ @c = Class.new(Sequel::Model)
6
+ class ::ParParent < Sequel::Model; end
7
+ end
8
+
9
+ it "should use the :class value if present" do
10
+ @c.many_to_one :c, :class=>ParParent
11
+ @c.association_reflection(:c).keys.should include(:class)
12
+ @c.association_reflection(:c).associated_class.should == ParParent
13
+ end
14
+ it "should figure out the class if the :class value is not present" do
15
+ @c.many_to_one :c, :class=>'ParParent'
16
+ @c.association_reflection(:c).keys.should_not include(:class)
17
+ @c.association_reflection(:c).associated_class.should == ParParent
18
+ end
19
+ end
20
+
21
+ describe Sequel::Model::Associations::AssociationReflection, "#primary_key" do
22
+ before do
23
+ @c = Class.new(Sequel::Model)
24
+ class ::ParParent < Sequel::Model; end
25
+ end
26
+
27
+ it "should use the :primary_key value if present" do
28
+ @c.many_to_one :c, :class=>ParParent, :primary_key=>:blah__blah
29
+ @c.association_reflection(:c).keys.should include(:primary_key)
30
+ @c.association_reflection(:c).primary_key.should == :blah__blah
31
+ end
32
+ it "should use the associated table's primary key if :primary_key is not present" do
33
+ @c.many_to_one :c, :class=>'ParParent'
34
+ @c.association_reflection(:c).keys.should_not include(:primary_key)
35
+ @c.association_reflection(:c).primary_key.should == :id
36
+ end
37
+ end
38
+
39
+ describe Sequel::Model::Associations::AssociationReflection, "#reciprocal" do
40
+ before do
41
+ class ::ParParent < Sequel::Model; end
42
+ class ::ParParentTwo < Sequel::Model; end
43
+ class ::ParParentThree < Sequel::Model; end
44
+ end
45
+ after do
46
+ Object.send(:remove_const, :ParParent)
47
+ Object.send(:remove_const, :ParParentTwo)
48
+ Object.send(:remove_const, :ParParentThree)
49
+ end
50
+
51
+ it "should use the :reciprocal value if present" do
52
+ @c = Class.new(Sequel::Model)
53
+ @d = Class.new(Sequel::Model)
54
+ @c.many_to_one :c, :class=>@d, :reciprocal=>:xx
55
+ @c.association_reflection(:c).keys.should include(:reciprocal)
56
+ @c.association_reflection(:c).reciprocal.should == :xx
57
+ end
58
+
59
+ it "should require the associated class is the current class to be a reciprocal" do
60
+ ParParent.many_to_one :par_parent_two, :key=>:blah
61
+ ParParent.many_to_one :par_parent_three, :key=>:blah
62
+ ParParentTwo.one_to_many :par_parents, :key=>:blah
63
+ ParParentThree.one_to_many :par_parents, :key=>:blah
64
+
65
+ ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_two
66
+ ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_three
67
+
68
+ ParParent.many_to_many :par_parent_twos, :left_key=>:l, :right_key=>:r, :join_table=>:jt
69
+ ParParent.many_to_many :par_parent_threes, :left_key=>:l, :right_key=>:r, :join_table=>:jt
70
+ ParParentTwo.many_to_many :par_parents, :right_key=>:l, :left_key=>:r, :join_table=>:jt
71
+ ParParentThree.many_to_many :par_parents, :right_key=>:l, :left_key=>:r, :join_table=>:jt
72
+
73
+ ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_twos
74
+ ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_threes
75
+ end
76
+
77
+ it "should handle composite keys" do
78
+ ParParent.many_to_one :par_parent_two, :key=>[:a, :b], :primary_key=>[:c, :b]
79
+ ParParent.many_to_one :par_parent_three, :key=>[:d, :e], :primary_key=>[:c, :b]
80
+ ParParentTwo.one_to_many :par_parents, :primary_key=>[:c, :b], :key=>[:a, :b]
81
+ ParParentThree.one_to_many :par_parents, :primary_key=>[:c, :b], :key=>[:d, :e]
82
+
83
+ ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_two
84
+ ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_three
85
+
86
+ ParParent.many_to_many :par_parent_twos, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:pl1, :pl2], :right_primary_key=>[:pr1, :pr2], :join_table=>:jt
87
+ ParParent.many_to_many :par_parent_threes, :right_key=>[:l1, :l2], :left_key=>[:r1, :r2], :left_primary_key=>[:pl1, :pl2], :right_primary_key=>[:pr1, :pr2], :join_table=>:jt
88
+ ParParentTwo.many_to_many :par_parents, :right_key=>[:l1, :l2], :left_key=>[:r1, :r2], :right_primary_key=>[:pl1, :pl2], :left_primary_key=>[:pr1, :pr2], :join_table=>:jt
89
+ ParParentThree.many_to_many :par_parents, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :right_primary_key=>[:pl1, :pl2], :left_primary_key=>[:pr1, :pr2], :join_table=>:jt
90
+
91
+ ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_twos
92
+ ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_threes
93
+ end
94
+
95
+ it "should figure out the reciprocal if the :reciprocal value is not present" do
96
+ ParParent.many_to_one :par_parent_two
97
+ ParParentTwo.one_to_many :par_parents
98
+ ParParent.many_to_many :par_parent_threes
99
+ ParParentThree.many_to_many :par_parents
100
+
101
+ ParParent.association_reflection(:par_parent_two).keys.should_not include(:reciprocal)
102
+ ParParent.association_reflection(:par_parent_two).reciprocal.should == :par_parents
103
+ ParParentTwo.association_reflection(:par_parents).keys.should_not include(:reciprocal)
104
+ ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_two
105
+ ParParent.association_reflection(:par_parent_threes).keys.should_not include(:reciprocal)
106
+ ParParent.association_reflection(:par_parent_threes).reciprocal.should == :par_parents
107
+ ParParentThree.association_reflection(:par_parents).keys.should_not include(:reciprocal)
108
+ ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_threes
109
+ end
110
+ end
111
+
112
+ describe Sequel::Model::Associations::AssociationReflection, "#select" do
113
+ before do
114
+ @c = Class.new(Sequel::Model)
115
+ class ::ParParent < Sequel::Model; end
116
+ end
117
+
118
+ it "should use the :select value if present" do
119
+ @c.many_to_one :c, :class=>ParParent, :select=>[:par_parents__id]
120
+ @c.association_reflection(:c).keys.should include(:select)
121
+ @c.association_reflection(:c).select.should == [:par_parents__id]
122
+ end
123
+ it "should be the associated_table.* if :select is not present for a many_to_many associaiton" do
124
+ @c.many_to_many :cs, :class=>'ParParent'
125
+ @c.association_reflection(:cs).keys.should_not include(:select)
126
+ @c.association_reflection(:cs).select.should == :par_parents.*
127
+ end
128
+ it "should be if :select is not present for a many_to_one and one_to_many associaiton" do
129
+ @c.one_to_many :cs, :class=>'ParParent'
130
+ @c.association_reflection(:cs).keys.should_not include(:select)
131
+ @c.association_reflection(:cs).select.should == nil
132
+ @c.many_to_one :c, :class=>'ParParent'
133
+ @c.association_reflection(:c).keys.should_not include(:select)
134
+ @c.association_reflection(:c).select.should == nil
135
+ end
136
+ end
137
+
138
+ describe Sequel::Model::Associations::AssociationReflection, "#can_have_associated_objects?" do
139
+ it "should be true for any given object (for backward compatibility)" do
140
+ Sequel::Model::Associations::AssociationReflection.new.can_have_associated_objects?(Object.new).should == true
141
+ end
142
+ end
143
+
144
+ describe Sequel::Model::Associations::AssociationReflection, "#associated_object_keys" do
145
+ before do
146
+ @c = Class.new(Sequel::Model)
147
+ class ::ParParent < Sequel::Model; end
148
+ end
149
+
150
+ it "should use the primary keys for a many_to_one association" do
151
+ @c.many_to_one :c, :class=>ParParent
152
+ @c.association_reflection(:c).associated_object_keys.should == [:id]
153
+ @c.many_to_one :c, :class=>ParParent, :primary_key=>:d_id
154
+ @c.association_reflection(:c).associated_object_keys.should == [:d_id]
155
+ @c.many_to_one :c, :class=>ParParent, :key=>[:c_id1, :c_id2], :primary_key=>[:id1, :id2]
156
+ @c.association_reflection(:c).associated_object_keys.should == [:id1, :id2]
157
+ end
158
+ it "should use the keys for a one_to_many association" do
159
+ ParParent.one_to_many :cs, :class=>ParParent
160
+ ParParent.association_reflection(:cs).associated_object_keys.should == [:par_parent_id]
161
+ @c.one_to_many :cs, :class=>ParParent, :key=>:d_id
162
+ @c.association_reflection(:cs).associated_object_keys.should == [:d_id]
163
+ @c.one_to_many :cs, :class=>ParParent, :key=>[:c_id1, :c_id2], :primary_key=>[:id1, :id2]
164
+ @c.association_reflection(:cs).associated_object_keys.should == [:c_id1, :c_id2]
165
+ end
166
+ it "should use the right primary keys for a many_to_many association" do
167
+ @c.many_to_many :cs, :class=>ParParent
168
+ @c.association_reflection(:cs).associated_object_keys.should == [:id]
169
+ @c.many_to_many :cs, :class=>ParParent, :right_primary_key=>:d_id
170
+ @c.association_reflection(:cs).associated_object_keys.should == [:d_id]
171
+ @c.many_to_many :cs, :class=>ParParent, :right_key=>[:c_id1, :c_id2], :right_primary_key=>[:id1, :id2]
172
+ @c.association_reflection(:cs).associated_object_keys.should == [:id1, :id2]
173
+ end
174
+ end
175
+