sequel 4.22.0 → 4.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +22 -0
- data/README.rdoc +6 -0
- data/Rakefile +59 -81
- data/doc/migration.rdoc +2 -0
- data/doc/release_notes/4.23.0.txt +65 -0
- data/doc/sharding.rdoc +16 -14
- data/doc/testing.rdoc +61 -77
- data/lib/sequel/adapters/jdbc.rb +1 -0
- data/lib/sequel/adapters/mock.rb +0 -1
- data/lib/sequel/adapters/postgres.rb +1 -0
- data/lib/sequel/adapters/postgresql.rb +1 -0
- data/lib/sequel/adapters/shared/postgres.rb +3 -3
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +9 -1
- data/lib/sequel/database/connecting.rb +1 -1
- data/lib/sequel/database/transactions.rb +2 -1
- data/lib/sequel/dataset/prepared_statements.rb +1 -1
- data/lib/sequel/extensions/constraint_validations.rb +12 -12
- data/lib/sequel/extensions/date_arithmetic.rb +0 -4
- data/lib/sequel/extensions/pagination.rb +14 -2
- data/lib/sequel/extensions/pg_enum.rb +2 -2
- data/lib/sequel/extensions/pg_hstore.rb +1 -1
- data/lib/sequel/extensions/pg_json_ops.rb +2 -2
- data/lib/sequel/plugins/csv_serializer.rb +2 -0
- data/lib/sequel/plugins/delay_add_association.rb +50 -0
- data/lib/sequel/plugins/list.rb +2 -2
- data/lib/sequel/plugins/nested_attributes.rb +8 -28
- data/lib/sequel/plugins/update_refresh.rb +50 -0
- data/lib/sequel/plugins/validate_associated.rb +55 -0
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +29 -29
- data/spec/adapters/firebird_spec.rb +97 -103
- data/spec/adapters/informix_spec.rb +25 -25
- data/spec/adapters/mssql_spec.rb +156 -172
- data/spec/adapters/mysql_spec.rb +334 -359
- data/spec/adapters/oracle_spec.rb +67 -69
- data/spec/adapters/postgres_spec.rb +1298 -1249
- data/spec/adapters/spec_helper.rb +2 -35
- data/spec/adapters/sqlanywhere_spec.rb +39 -39
- data/spec/adapters/sqlite_spec.rb +203 -200
- data/spec/bin_spec.rb +57 -59
- data/spec/core/connection_pool_spec.rb +402 -401
- data/spec/core/database_spec.rb +953 -944
- data/spec/core/dataset_spec.rb +2178 -2168
- data/spec/core/deprecated_spec.rb +19 -19
- data/spec/core/expression_filters_spec.rb +415 -415
- data/spec/core/mock_adapter_spec.rb +212 -212
- data/spec/core/object_graph_spec.rb +73 -73
- data/spec/core/placeholder_literalizer_spec.rb +71 -71
- data/spec/core/schema_generator_spec.rb +44 -44
- data/spec/core/schema_spec.rb +470 -472
- data/spec/core/spec_helper.rb +5 -20
- data/spec/core/version_spec.rb +2 -2
- data/spec/core_extensions_spec.rb +320 -320
- data/spec/extensions/accessed_columns_spec.rb +12 -12
- data/spec/extensions/active_model_spec.rb +3 -3
- data/spec/extensions/after_initialize_spec.rb +2 -2
- data/spec/extensions/arbitrary_servers_spec.rb +23 -23
- data/spec/extensions/association_dependencies_spec.rb +34 -34
- data/spec/extensions/association_pks_spec.rb +98 -98
- data/spec/extensions/association_proxies_spec.rb +33 -33
- data/spec/extensions/auto_validations_spec.rb +46 -46
- data/spec/extensions/blacklist_security_spec.rb +19 -18
- data/spec/extensions/blank_spec.rb +36 -36
- data/spec/extensions/boolean_readers_spec.rb +36 -36
- data/spec/extensions/caching_spec.rb +82 -82
- data/spec/extensions/class_table_inheritance_spec.rb +72 -72
- data/spec/extensions/column_conflicts_spec.rb +19 -14
- data/spec/extensions/column_select_spec.rb +19 -19
- data/spec/extensions/columns_introspection_spec.rb +43 -43
- data/spec/extensions/composition_spec.rb +64 -64
- data/spec/extensions/connection_validator_spec.rb +92 -90
- data/spec/extensions/constraint_validations_plugin_spec.rb +92 -92
- data/spec/extensions/constraint_validations_spec.rb +80 -80
- data/spec/extensions/core_refinements_spec.rb +220 -220
- data/spec/extensions/csv_serializer_spec.rb +44 -44
- data/spec/extensions/current_datetime_timestamp_spec.rb +8 -8
- data/spec/extensions/dataset_associations_spec.rb +65 -65
- data/spec/extensions/dataset_source_alias_spec.rb +16 -16
- data/spec/extensions/date_arithmetic_spec.rb +51 -58
- data/spec/extensions/defaults_setter_spec.rb +19 -19
- data/spec/extensions/delay_add_association_spec.rb +52 -0
- data/spec/extensions/dirty_spec.rb +51 -51
- data/spec/extensions/eager_each_spec.rb +8 -8
- data/spec/extensions/empty_array_ignore_nulls_spec.rb +10 -10
- data/spec/extensions/error_splitter_spec.rb +2 -2
- data/spec/extensions/error_sql_spec.rb +4 -4
- data/spec/extensions/eval_inspect_spec.rb +3 -3
- data/spec/extensions/filter_having_spec.rb +8 -8
- data/spec/extensions/force_encoding_spec.rb +30 -30
- data/spec/extensions/from_block_spec.rb +7 -7
- data/spec/extensions/graph_each_spec.rb +19 -19
- data/spec/extensions/hash_aliases_spec.rb +5 -5
- data/spec/extensions/hook_class_methods_spec.rb +100 -100
- data/spec/extensions/inflector_spec.rb +54 -54
- data/spec/extensions/input_transformer_spec.rb +10 -10
- data/spec/extensions/insert_returning_select_spec.rb +8 -8
- data/spec/extensions/instance_filters_spec.rb +26 -26
- data/spec/extensions/instance_hooks_spec.rb +85 -85
- data/spec/extensions/json_serializer_spec.rb +68 -68
- data/spec/extensions/lazy_attributes_spec.rb +49 -49
- data/spec/extensions/list_spec.rb +77 -75
- data/spec/extensions/looser_typecasting_spec.rb +16 -16
- data/spec/extensions/many_through_many_spec.rb +627 -627
- data/spec/extensions/meta_def_spec.rb +7 -7
- data/spec/extensions/migration_spec.rb +217 -217
- data/spec/extensions/modification_detection_spec.rb +20 -20
- data/spec/extensions/mssql_optimistic_locking_spec.rb +21 -21
- data/spec/extensions/named_timezones_spec.rb +18 -18
- data/spec/extensions/nested_attributes_spec.rb +107 -107
- data/spec/extensions/null_dataset_spec.rb +24 -24
- data/spec/extensions/optimistic_locking_spec.rb +21 -21
- data/spec/extensions/pagination_spec.rb +52 -52
- data/spec/extensions/pg_array_associations_spec.rb +273 -273
- data/spec/extensions/pg_array_ops_spec.rb +52 -52
- data/spec/extensions/pg_array_spec.rb +152 -152
- data/spec/extensions/pg_enum_spec.rb +13 -13
- data/spec/extensions/pg_hstore_ops_spec.rb +63 -63
- data/spec/extensions/pg_hstore_spec.rb +84 -84
- data/spec/extensions/pg_inet_spec.rb +15 -15
- data/spec/extensions/pg_interval_spec.rb +29 -29
- data/spec/extensions/pg_json_ops_spec.rb +86 -84
- data/spec/extensions/pg_json_spec.rb +104 -104
- data/spec/extensions/pg_loose_count_spec.rb +6 -6
- data/spec/extensions/pg_range_ops_spec.rb +24 -24
- data/spec/extensions/pg_range_spec.rb +143 -143
- data/spec/extensions/pg_row_ops_spec.rb +14 -14
- data/spec/extensions/pg_row_plugin_spec.rb +12 -12
- data/spec/extensions/pg_row_spec.rb +118 -118
- data/spec/extensions/pg_static_cache_updater_spec.rb +28 -28
- data/spec/extensions/pg_typecast_on_load_spec.rb +21 -21
- data/spec/extensions/prepared_statements_associations_spec.rb +42 -42
- data/spec/extensions/prepared_statements_safe_spec.rb +18 -18
- data/spec/extensions/prepared_statements_spec.rb +28 -28
- data/spec/extensions/prepared_statements_with_pk_spec.rb +11 -11
- data/spec/extensions/pretty_table_spec.rb +16 -16
- data/spec/extensions/query_literals_spec.rb +37 -37
- data/spec/extensions/query_spec.rb +32 -32
- data/spec/extensions/rcte_tree_spec.rb +141 -141
- data/spec/extensions/round_timestamps_spec.rb +21 -21
- data/spec/extensions/schema_caching_spec.rb +8 -8
- data/spec/extensions/schema_dumper_spec.rb +78 -78
- data/spec/extensions/schema_spec.rb +31 -27
- data/spec/extensions/scissors_spec.rb +3 -3
- data/spec/extensions/select_remove_spec.rb +14 -14
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +28 -28
- data/spec/extensions/serialization_modification_detection_spec.rb +33 -33
- data/spec/extensions/serialization_spec.rb +79 -78
- data/spec/extensions/server_block_spec.rb +17 -17
- data/spec/extensions/set_overrides_spec.rb +30 -30
- data/spec/extensions/sharding_spec.rb +65 -65
- data/spec/extensions/shared_caching_spec.rb +29 -29
- data/spec/extensions/single_table_inheritance_spec.rb +79 -79
- data/spec/extensions/skip_create_refresh_spec.rb +3 -3
- data/spec/extensions/spec_helper.rb +4 -29
- data/spec/extensions/split_array_nil_spec.rb +9 -9
- data/spec/extensions/split_values_spec.rb +7 -7
- data/spec/extensions/sql_expr_spec.rb +32 -32
- data/spec/extensions/static_cache_spec.rb +123 -123
- data/spec/extensions/string_date_time_spec.rb +34 -34
- data/spec/extensions/string_stripper_spec.rb +15 -15
- data/spec/extensions/subclasses_spec.rb +31 -31
- data/spec/extensions/table_select_spec.rb +15 -15
- data/spec/extensions/tactical_eager_loading_spec.rb +23 -23
- data/spec/extensions/thread_local_timezones_spec.rb +13 -13
- data/spec/extensions/timestamps_spec.rb +40 -40
- data/spec/extensions/to_dot_spec.rb +34 -34
- data/spec/extensions/touch_spec.rb +52 -52
- data/spec/extensions/tree_spec.rb +72 -72
- data/spec/extensions/typecast_on_load_spec.rb +25 -25
- data/spec/extensions/unlimited_update_spec.rb +2 -2
- data/spec/extensions/update_or_create_spec.rb +36 -36
- data/spec/extensions/update_primary_key_spec.rb +35 -35
- data/spec/extensions/update_refresh_spec.rb +41 -0
- data/spec/extensions/validate_associated_spec.rb +52 -0
- data/spec/extensions/validation_class_methods_spec.rb +314 -317
- data/spec/extensions/validation_helpers_spec.rb +195 -195
- data/spec/extensions/xml_serializer_spec.rb +48 -48
- data/spec/guards_helper.rb +55 -0
- data/spec/integration/associations_test.rb +1089 -1088
- data/spec/integration/database_test.rb +29 -29
- data/spec/integration/dataset_test.rb +661 -661
- data/spec/integration/eager_loader_test.rb +147 -147
- data/spec/integration/migrator_test.rb +122 -122
- data/spec/integration/model_test.rb +70 -70
- data/spec/integration/plugin_test.rb +682 -640
- data/spec/integration/prepared_statement_test.rb +172 -172
- data/spec/integration/schema_test.rb +245 -245
- data/spec/integration/spec_helper.rb +1 -64
- data/spec/integration/timezone_test.rb +17 -17
- data/spec/integration/transaction_test.rb +87 -87
- data/spec/integration/type_test.rb +33 -33
- data/spec/model/association_reflection_spec.rb +130 -121
- data/spec/model/associations_spec.rb +1112 -1113
- data/spec/model/base_spec.rb +197 -196
- data/spec/model/class_dataset_methods_spec.rb +118 -118
- data/spec/model/dataset_methods_spec.rb +49 -49
- data/spec/model/eager_loading_spec.rb +705 -702
- data/spec/model/hooks_spec.rb +169 -168
- data/spec/model/inflector_spec.rb +5 -5
- data/spec/model/model_spec.rb +287 -297
- data/spec/model/plugins_spec.rb +47 -47
- data/spec/model/record_spec.rb +534 -535
- data/spec/model/spec_helper.rb +3 -21
- data/spec/model/validations_spec.rb +72 -70
- data/spec/spec_config.rb +8 -0
- metadata +41 -9
- data/lib/sequel/adapters/fdbsql.rb +0 -286
- data/lib/sequel/adapters/jdbc/fdbsql.rb +0 -66
- data/lib/sequel/adapters/openbase.rb +0 -54
- data/lib/sequel/adapters/shared/fdbsql.rb +0 -550
- data/spec/adapters/fdbsql_spec.rb +0 -429
- data/spec/rspec_helper.rb +0 -22
@@ -20,66 +20,66 @@ describe Sequel::Model, "tree plugin" do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should define the correct associations" do
|
23
|
-
@c.associations.sort_by{|x| x.to_s}.
|
23
|
+
@c.associations.sort_by{|x| x.to_s}.must_equal [:children, :parent]
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should define the correct reciprocals" do
|
27
|
-
@c.associations.sort_by{|x| x.to_s}.map{|x| @c.association_reflection(x).reciprocal}.
|
27
|
+
@c.associations.sort_by{|x| x.to_s}.map{|x| @c.association_reflection(x).reciprocal}.must_equal [:parent, :children]
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should define the correct associations when giving options" do
|
31
|
-
klass(:children=>{:name=>:cs}, :parent=>{:name=>:p}).associations.sort_by{|x| x.to_s}.
|
31
|
+
klass(:children=>{:name=>:cs}, :parent=>{:name=>:p}).associations.sort_by{|x| x.to_s}.must_equal [:cs, :p]
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should use the correct SQL for lazy associations" do
|
35
|
-
@o.parent_dataset.sql.
|
36
|
-
@o.children_dataset.sql.
|
35
|
+
@o.parent_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.id = 1) LIMIT 1'
|
36
|
+
@o.children_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.parent_id = 2)'
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should use the correct SQL for lazy associations when giving options" do
|
40
40
|
o = klass(:primary_key=>:i, :key=>:pi, :order=>:name, :children=>{:name=>:cs}, :parent=>{:name=>:p}).load(:id=>2, :parent_id=>1, :name=>'AA', :i=>3, :pi=>4)
|
41
|
-
o.p_dataset.sql.
|
42
|
-
o.cs_dataset.sql.
|
41
|
+
o.p_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.i = 4) ORDER BY name LIMIT 1'
|
42
|
+
o.cs_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.pi = 3) ORDER BY name'
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should have parent_column give the symbol of the parent column" do
|
46
|
-
@c.parent_column.
|
47
|
-
klass(:key=>:p_id).parent_column.
|
46
|
+
@c.parent_column.must_equal :parent_id
|
47
|
+
klass(:key=>:p_id).parent_column.must_equal :p_id
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should have tree_order give the order of the association" do
|
51
|
-
@c.tree_order.
|
52
|
-
klass(:order=>:name).tree_order.
|
53
|
-
klass(:order=>[:parent_id, :name]).tree_order.
|
51
|
+
@c.tree_order.must_equal nil
|
52
|
+
klass(:order=>:name).tree_order.must_equal :name
|
53
|
+
klass(:order=>[:parent_id, :name]).tree_order.must_equal [:parent_id, :name]
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should work correctly in subclasses" do
|
57
57
|
o = Class.new(klass(:primary_key=>:i, :key=>:pi, :order=>:name, :children=>{:name=>:cs}, :parent=>{:name=>:p})).load(:id=>2, :parent_id=>1, :name=>'AA', :i=>3, :pi=>4)
|
58
|
-
o.p_dataset.sql.
|
59
|
-
o.cs_dataset.sql.
|
58
|
+
o.p_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.i = 4) ORDER BY name LIMIT 1'
|
59
|
+
o.cs_dataset.sql.must_equal 'SELECT * FROM nodes WHERE (nodes.pi = 3) ORDER BY name'
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should have roots return an array of the tree's roots" do
|
63
63
|
@ds._fetch = [{:id=>1, :parent_id=>nil, :name=>'r'}]
|
64
|
-
@c.roots.
|
65
|
-
@db.sqls.
|
64
|
+
@c.roots.must_equal [@c.load(:id=>1, :parent_id=>nil, :name=>'r')]
|
65
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id IS NULL)"]
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should have roots_dataset be a dataset representing the tree's roots" do
|
69
|
-
@c.roots_dataset.sql.
|
69
|
+
@c.roots_dataset.sql.must_equal "SELECT * FROM nodes WHERE (parent_id IS NULL)"
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should have ancestors return the ancestors of the current node" do
|
73
73
|
@ds._fetch = [[{:id=>1, :parent_id=>5, :name=>'r'}], [{:id=>5, :parent_id=>nil, :name=>'r2'}]]
|
74
|
-
@o.ancestors.
|
75
|
-
@db.sqls.
|
74
|
+
@o.ancestors.must_equal [@c.load(:id=>1, :parent_id=>5, :name=>'r'), @c.load(:id=>5, :parent_id=>nil, :name=>'r2')]
|
75
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE id = 1",
|
76
76
|
"SELECT * FROM nodes WHERE id = 5"]
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should have descendants return the descendants of the current node" do
|
80
80
|
@ds._fetch = [[{:id=>3, :parent_id=>2, :name=>'r'}, {:id=>4, :parent_id=>2, :name=>'r2'}], [{:id=>5, :parent_id=>4, :name=>'r3'}], []]
|
81
|
-
@o.descendants.
|
82
|
-
@db.sqls.
|
81
|
+
@o.descendants.must_equal [@c.load(:id=>3, :parent_id=>2, :name=>'r'), @c.load(:id=>4, :parent_id=>2, :name=>'r2'), @c.load(:id=>5, :parent_id=>4, :name=>'r3')]
|
82
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.parent_id = 2)",
|
83
83
|
"SELECT * FROM nodes WHERE (nodes.parent_id = 3)",
|
84
84
|
"SELECT * FROM nodes WHERE (nodes.parent_id = 5)",
|
85
85
|
"SELECT * FROM nodes WHERE (nodes.parent_id = 4)"]
|
@@ -87,31 +87,31 @@ describe Sequel::Model, "tree plugin" do
|
|
87
87
|
|
88
88
|
it "should have root return the root of the current node" do
|
89
89
|
@ds._fetch = [[{:id=>1, :parent_id=>5, :name=>'r'}], [{:id=>5, :parent_id=>nil, :name=>'r2'}]]
|
90
|
-
@o.root.
|
91
|
-
@db.sqls.
|
90
|
+
@o.root.must_equal @c.load(:id=>5, :parent_id=>nil, :name=>'r2')
|
91
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE id = 1",
|
92
92
|
"SELECT * FROM nodes WHERE id = 5"]
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should have root? return true for a root node and false for a child node" do
|
96
|
-
@c.load(:parent_id => nil).root?.
|
97
|
-
@c.load(:parent_id => 1).root?.
|
96
|
+
@c.load(:parent_id => nil).root?.must_equal true
|
97
|
+
@c.load(:parent_id => 1).root?.must_equal false
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should have root? return false for an new node" do
|
101
|
-
@c.new.root?.
|
101
|
+
@c.new.root?.must_equal false
|
102
102
|
end
|
103
103
|
|
104
104
|
it "should have self_and_siblings return the children of the current node's parent" do
|
105
105
|
@ds._fetch = [[{:id=>1, :parent_id=>3, :name=>'r'}], [{:id=>7, :parent_id=>1, :name=>'r2'}, @o.values.dup]]
|
106
|
-
@o.self_and_siblings.
|
107
|
-
@db.sqls.
|
106
|
+
@o.self_and_siblings.must_equal [@c.load(:id=>7, :parent_id=>1, :name=>'r2'), @o]
|
107
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE id = 1",
|
108
108
|
"SELECT * FROM nodes WHERE (nodes.parent_id = 1)"]
|
109
109
|
end
|
110
110
|
|
111
111
|
it "should have siblings return the children of the current node's parent, except for the current node" do
|
112
112
|
@ds._fetch = [[{:id=>1, :parent_id=>3, :name=>'r'}], [{:id=>7, :parent_id=>1, :name=>'r2'}, @o.values.dup]]
|
113
|
-
@o.siblings.
|
114
|
-
@db.sqls.
|
113
|
+
@o.siblings.must_equal [@c.load(:id=>7, :parent_id=>1, :name=>'r2')]
|
114
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE id = 1",
|
115
115
|
"SELECT * FROM nodes WHERE (nodes.parent_id = 1)"]
|
116
116
|
end
|
117
117
|
|
@@ -122,23 +122,23 @@ describe Sequel::Model, "tree plugin" do
|
|
122
122
|
|
123
123
|
it "should have root class method return the root" do
|
124
124
|
@c.dataset._fetch = [{:id=>1, :parent_id=>nil, :name=>'r'}]
|
125
|
-
@c.root.
|
125
|
+
@c.root.must_equal @c.load(:id=>1, :parent_id=>nil, :name=>'r')
|
126
126
|
end
|
127
127
|
|
128
128
|
it "prevents creating a second root" do
|
129
129
|
@c.dataset._fetch = [{:id=>1, :parent_id=>nil, :name=>'r'}]
|
130
|
-
lambda { @c.create }.
|
130
|
+
lambda { @c.create }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
131
131
|
end
|
132
132
|
|
133
133
|
it "errors when promoting an existing record to a second root" do
|
134
134
|
@c.dataset._fetch = [{:id=>1, :parent_id=>nil, :name=>'r'}]
|
135
135
|
n = @c.load(:id => 2, :parent_id => 1)
|
136
|
-
lambda { n.update(:parent_id => nil) }.
|
136
|
+
lambda { n.update(:parent_id => nil) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
137
137
|
end
|
138
138
|
|
139
139
|
it "allows updating existing root" do
|
140
140
|
@c.dataset._fetch = [{:id=>1, :parent_id=>nil, :name=>'r'}]
|
141
|
-
|
141
|
+
@c.root.update(:name => 'fdsa')
|
142
142
|
end
|
143
143
|
end
|
144
144
|
end
|
@@ -165,37 +165,37 @@ describe Sequel::Model, "tree plugin with composite keys" do
|
|
165
165
|
|
166
166
|
|
167
167
|
it "should use the correct SQL for lazy associations" do
|
168
|
-
@o.parent_dataset.sql.
|
169
|
-
@o.children_dataset.sql.
|
168
|
+
@o.parent_dataset.sql.must_equal 'SELECT * FROM nodes WHERE ((nodes.id = 1) AND (nodes.id2 = 6)) LIMIT 1'
|
169
|
+
@o.children_dataset.sql.must_equal 'SELECT * FROM nodes WHERE ((nodes.parent_id = 2) AND (nodes.parent_id2 = 5))'
|
170
170
|
end
|
171
171
|
|
172
172
|
it "should have parent_column give an array of symbols of the parent column" do
|
173
|
-
@c.parent_column.
|
173
|
+
@c.parent_column.must_equal [:parent_id, :parent_id2]
|
174
174
|
end
|
175
175
|
|
176
176
|
it "should have roots return an array of the tree's roots" do
|
177
177
|
@ds._fetch = [{:id=>1, :parent_id=>nil, :parent_id2=>nil, :name=>'r'}]
|
178
|
-
@c.roots.
|
179
|
-
@db.sqls.
|
178
|
+
@c.roots.must_equal [@c.load(:id=>1, :parent_id=>nil, :parent_id2=>nil, :name=>'r')]
|
179
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE ((parent_id IS NULL) OR (parent_id2 IS NULL))"]
|
180
180
|
end
|
181
181
|
|
182
182
|
it "should have roots_dataset be a dataset representing the tree's roots" do
|
183
|
-
@c.roots_dataset.sql.
|
183
|
+
@c.roots_dataset.sql.must_equal "SELECT * FROM nodes WHERE ((parent_id IS NULL) OR (parent_id2 IS NULL))"
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should have ancestors return the ancestors of the current node" do
|
187
187
|
@ds._fetch = [[{:id=>1, :id2=>6, :parent_id=>5, :parent_id2=>7, :name=>'r'}], [{:id=>5, :id2=>7, :parent_id=>nil, :parent_id2=>nil, :name=>'r2'}]]
|
188
|
-
@o.ancestors.
|
188
|
+
@o.ancestors.must_equal [@c.load(:id=>1, :id2=>6, :parent_id=>5, :parent_id2=>7, :name=>'r'), @c.load(:id=>5, :id2=>7, :parent_id=>nil, :parent_id2=>nil, :name=>'r2')]
|
189
189
|
sqls = @db.sqls
|
190
|
-
sqls.length.
|
191
|
-
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].
|
192
|
-
["SELECT * FROM nodes WHERE ((id = 5) AND (id2 = 7)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 7) AND (id = 5)) LIMIT 1"].
|
190
|
+
sqls.length.must_equal 2
|
191
|
+
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].must_include(sqls[0])
|
192
|
+
["SELECT * FROM nodes WHERE ((id = 5) AND (id2 = 7)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 7) AND (id = 5)) LIMIT 1"].must_include(sqls[1])
|
193
193
|
end
|
194
194
|
|
195
195
|
it "should have descendants return the descendants of the current node" do
|
196
196
|
@ds._fetch = [[{:id=>3, :id2=>7, :parent_id=>2, :parent_id2=>5, :name=>'r'}, {:id=>4, :id2=>8, :parent_id=>2, :parent_id2=>5, :name=>'r2'}], [{:id=>5, :id2=>9, :parent_id=>4, :parent_id2=>8, :name=>'r3'}], []]
|
197
|
-
@o.descendants.
|
198
|
-
@db.sqls.
|
197
|
+
@o.descendants.must_equal [@c.load(:id=>3, :id2=>7, :parent_id=>2, :parent_id2=>5, :name=>'r'), @c.load(:id=>4, :id2=>8, :parent_id=>2, :parent_id2=>5, :name=>'r2'), @c.load(:id=>5, :id2=>9, :parent_id=>4, :parent_id2=>8, :name=>'r3')]
|
198
|
+
@db.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.parent_id = 2) AND (nodes.parent_id2 = 5))",
|
199
199
|
"SELECT * FROM nodes WHERE ((nodes.parent_id = 3) AND (nodes.parent_id2 = 7))",
|
200
200
|
"SELECT * FROM nodes WHERE ((nodes.parent_id = 5) AND (nodes.parent_id2 = 9))",
|
201
201
|
"SELECT * FROM nodes WHERE ((nodes.parent_id = 4) AND (nodes.parent_id2 = 8))"]
|
@@ -203,40 +203,40 @@ describe Sequel::Model, "tree plugin with composite keys" do
|
|
203
203
|
|
204
204
|
it "should have root return the root of the current node" do
|
205
205
|
@ds._fetch = [[{:id=>1, :id2=>6, :parent_id=>5, :parent_id2=>7, :name=>'r'}], [{:id=>5, :id2=>7, :parent_id=>nil, :parent_id2=>nil, :name=>'r2'}]]
|
206
|
-
@o.root.
|
206
|
+
@o.root.must_equal @c.load(:id=>5, :id2=>7, :parent_id=>nil, :parent_id2=>nil, :name=>'r2')
|
207
207
|
sqls = @db.sqls
|
208
|
-
sqls.length.
|
209
|
-
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].
|
210
|
-
["SELECT * FROM nodes WHERE ((id = 5) AND (id2 = 7)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 7) AND (id = 5)) LIMIT 1"].
|
208
|
+
sqls.length.must_equal 2
|
209
|
+
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].must_include(sqls[0])
|
210
|
+
["SELECT * FROM nodes WHERE ((id = 5) AND (id2 = 7)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 7) AND (id = 5)) LIMIT 1"].must_include(sqls[1])
|
211
211
|
end
|
212
212
|
|
213
213
|
it "should have root? return true for a root node and false for a child node" do
|
214
|
-
@c.load(:parent_id => nil, :parent_id2=>nil).root?.
|
215
|
-
@c.load(:parent_id => 1, :parent_id2=>nil).root?.
|
216
|
-
@c.load(:parent_id => nil, :parent_id2=>2).root?.
|
217
|
-
@c.load(:parent_id => 1, :parent_id2=>2).root?.
|
214
|
+
@c.load(:parent_id => nil, :parent_id2=>nil).root?.must_equal true
|
215
|
+
@c.load(:parent_id => 1, :parent_id2=>nil).root?.must_equal true
|
216
|
+
@c.load(:parent_id => nil, :parent_id2=>2).root?.must_equal true
|
217
|
+
@c.load(:parent_id => 1, :parent_id2=>2).root?.must_equal false
|
218
218
|
end
|
219
219
|
|
220
220
|
it "should have root? return false for an new node" do
|
221
|
-
@c.new.root?.
|
221
|
+
@c.new.root?.must_equal false
|
222
222
|
end
|
223
223
|
|
224
224
|
it "should have self_and_siblings return the children of the current node's parent" do
|
225
225
|
@ds._fetch = [[{:id=>1, :id2=>6, :parent_id=>3, :parent_id2=>7, :name=>'r'}], [{:id=>7, :id2=>9, :parent_id=>1, :parent_id2=>6, :name=>'r2'}, @o.values.dup]]
|
226
|
-
@o.self_and_siblings.
|
226
|
+
@o.self_and_siblings.must_equal [@c.load(:id=>7, :id2=>9, :parent_id=>1, :parent_id2=>6, :name=>'r2'), @o]
|
227
227
|
sqls = @db.sqls
|
228
|
-
sqls.length.
|
229
|
-
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].
|
230
|
-
sqls[1].
|
228
|
+
sqls.length.must_equal 2
|
229
|
+
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].must_include(sqls[0])
|
230
|
+
sqls[1].must_equal "SELECT * FROM nodes WHERE ((nodes.parent_id = 1) AND (nodes.parent_id2 = 6))"
|
231
231
|
end
|
232
232
|
|
233
233
|
it "should have siblings return the children of the current node's parent, except for the current node" do
|
234
234
|
@ds._fetch = [[{:id=>1, :id2=>6, :parent_id=>3, :parent_id2=>7, :name=>'r'}], [{:id=>7, :id2=>9, :parent_id=>1, :parent_id2=>6, :name=>'r2'}, @o.values.dup]]
|
235
|
-
@o.siblings.
|
235
|
+
@o.siblings.must_equal [@c.load(:id=>7, :id2=>9, :parent_id=>1, :parent_id2=>6, :name=>'r2')]
|
236
236
|
sqls = @db.sqls
|
237
|
-
sqls.length.
|
238
|
-
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].
|
239
|
-
sqls[1].
|
237
|
+
sqls.length.must_equal 2
|
238
|
+
["SELECT * FROM nodes WHERE ((id = 1) AND (id2 = 6)) LIMIT 1", "SELECT * FROM nodes WHERE ((id2 = 6) AND (id = 1)) LIMIT 1"].must_include(sqls[0])
|
239
|
+
sqls[1].must_equal "SELECT * FROM nodes WHERE ((nodes.parent_id = 1) AND (nodes.parent_id2 = 6))"
|
240
240
|
end
|
241
241
|
|
242
242
|
describe ":single_root option" do
|
@@ -246,29 +246,29 @@ describe Sequel::Model, "tree plugin with composite keys" do
|
|
246
246
|
|
247
247
|
it "prevents creating a second root" do
|
248
248
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>nil, :name=>'r'}]
|
249
|
-
lambda { @c.create }.
|
249
|
+
lambda { @c.create }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
250
250
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>1, :parent_id2=>nil, :name=>'r'}]
|
251
|
-
lambda { @c.create(:parent_id2=>1) }.
|
251
|
+
lambda { @c.create(:parent_id2=>1) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
252
252
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>2, :name=>'r'}]
|
253
|
-
lambda { @c.create(:parent_id=>2) }.
|
253
|
+
lambda { @c.create(:parent_id=>2) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
254
254
|
end
|
255
255
|
|
256
256
|
it "errors when promoting an existing record to a second root" do
|
257
257
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>nil, :name=>'r'}]
|
258
|
-
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id => nil, :parent_id2=>nil) }.
|
258
|
+
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id => nil, :parent_id2=>nil) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
259
259
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>1, :parent_id2=>nil, :name=>'r'}]
|
260
|
-
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id => nil) }.
|
260
|
+
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id => nil) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
261
261
|
@c.dataset._fetch = [{:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>2, :name=>'r'}]
|
262
|
-
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id2 => nil) }.
|
262
|
+
lambda { @c.load(:id => 2, :id2=>7, :parent_id => 1, :parent_id2=>2).update(:parent_id2 => nil) }.must_raise(Sequel::Plugins::Tree::TreeMultipleRootError)
|
263
263
|
end
|
264
264
|
|
265
265
|
it "allows updating existing root" do
|
266
266
|
@c.dataset._fetch = {:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>nil, :name=>'r'}
|
267
|
-
|
267
|
+
@c.root.update(:name => 'fdsa')
|
268
268
|
@c.dataset._fetch = {:id=>1, :id2=>6, :parent_id=>1, :parent_id2=>nil, :name=>'r'}
|
269
|
-
|
269
|
+
@c.root.update(:name => 'fdsa')
|
270
270
|
@c.dataset._fetch = {:id=>1, :id2=>6, :parent_id=>nil, :parent_id2=>2, :name=>'r'}
|
271
|
-
|
271
|
+
@c.root.update(:name => 'fdsa')
|
272
272
|
end
|
273
273
|
end
|
274
274
|
end
|
@@ -16,65 +16,65 @@ describe Sequel::Model, "TypecastOnLoad plugin" do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
it "should call setter method with value when loading the object, for all given columns" do
|
20
20
|
@c.plugin :typecast_on_load, :b
|
21
21
|
o = @c.load(:id=>1, :b=>"1", :y=>"0")
|
22
|
-
o.values.
|
23
|
-
o.bset.
|
22
|
+
o.values.must_equal(:id=>1, :b=>1, :y=>"0")
|
23
|
+
o.bset.must_equal true
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
it "should call setter method with value when reloading the object, for all given columns" do
|
27
27
|
@c.plugin :typecast_on_load, :b
|
28
28
|
o = @c.load(:id=>1, :b=>"1", :y=>"0")
|
29
29
|
o.refresh
|
30
|
-
o.values.
|
31
|
-
o.bset.
|
30
|
+
o.values.must_equal(:id=>1, :b=>1, :y=>"0")
|
31
|
+
o.bset.must_equal true
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
it "should call setter method with value when automatically reloading the object on creation" do
|
35
35
|
@c.plugin :typecast_on_load, :b
|
36
36
|
o = @c.new(:b=>"1", :y=>"0")
|
37
|
-
o.save.values.
|
38
|
-
o.bset.
|
37
|
+
o.save.values.must_equal(:id=>1, :b=>1, :y=>"0")
|
38
|
+
o.bset.must_equal true
|
39
39
|
end
|
40
40
|
|
41
|
-
|
41
|
+
it "should call setter method with value when automatically reloading the object on creation via insert_select" do
|
42
42
|
@c.plugin :typecast_on_load, :b
|
43
43
|
@c.dataset.meta_def(:insert_select){|h| insert(h); first}
|
44
44
|
o = @c.new(:b=>"1", :y=>"0")
|
45
|
-
o.save.values.
|
46
|
-
o.bset.
|
45
|
+
o.save.values.must_equal(:id=>1, :b=>1, :y=>"0")
|
46
|
+
o.bset.must_equal true
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
it "should allowing setting columns separately via add_typecast_on_load_columns" do
|
50
50
|
@c.plugin :typecast_on_load
|
51
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
51
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>"1", :y=>"0")
|
52
52
|
@c.add_typecast_on_load_columns :b
|
53
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
53
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>1, :y=>"0")
|
54
54
|
@c.add_typecast_on_load_columns :y
|
55
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
55
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>1, :y=>false)
|
56
56
|
end
|
57
57
|
|
58
|
-
|
58
|
+
it "should work with subclasses" do
|
59
59
|
@c.plugin :typecast_on_load
|
60
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
60
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>"1", :y=>"0")
|
61
61
|
|
62
62
|
c1 = Class.new(@c)
|
63
63
|
@c.add_typecast_on_load_columns :b
|
64
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
65
|
-
c1.load(:id=>1, :b=>"1", :y=>"0").values.
|
64
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>1, :y=>"0")
|
65
|
+
c1.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>"1", :y=>"0")
|
66
66
|
|
67
67
|
c2 = Class.new(@c)
|
68
68
|
@c.add_typecast_on_load_columns :y
|
69
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").values.
|
70
|
-
c2.load(:id=>1, :b=>"1", :y=>"0").values.
|
69
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>1, :y=>false)
|
70
|
+
c2.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>1, :y=>"0")
|
71
71
|
|
72
72
|
c1.add_typecast_on_load_columns :y
|
73
|
-
c1.load(:id=>1, :b=>"1", :y=>"0").values.
|
73
|
+
c1.load(:id=>1, :b=>"1", :y=>"0").values.must_equal(:id=>1, :b=>"1", :y=>false)
|
74
74
|
end
|
75
75
|
|
76
|
-
|
76
|
+
it "should not mark the object as modified" do
|
77
77
|
@c.plugin :typecast_on_load, :b
|
78
|
-
@c.load(:id=>1, :b=>"1", :y=>"0").modified?.
|
78
|
+
@c.load(:id=>1, :b=>"1", :y=>"0").modified?.must_equal false
|
79
79
|
end
|
80
80
|
end
|
@@ -11,10 +11,10 @@ describe "Sequel::Plugins::UnlimitedUpdate" do
|
|
11
11
|
|
12
12
|
it "should remove limit from update dataset" do
|
13
13
|
@o.save
|
14
|
-
@db.sqls.
|
14
|
+
@db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) LIMIT 1"]
|
15
15
|
|
16
16
|
@c.plugin :unlimited_update
|
17
17
|
@o.save
|
18
|
-
@db.sqls.
|
18
|
+
@db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1)"]
|
19
19
|
end
|
20
20
|
end
|
@@ -11,77 +11,77 @@ describe "Sequel::Plugins::UpdateOrCreate" do
|
|
11
11
|
|
12
12
|
it ".update_or_create should update an existing record if one exists" do
|
13
13
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
14
|
-
@c.update_or_create(:a=>2){|t| t.b = 4}.
|
15
|
-
@db.sqls.
|
14
|
+
@c.update_or_create(:a=>2){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>2, :b=>4)
|
15
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1", "UPDATE test SET b = 4 WHERE (id = 1)"]
|
16
16
|
|
17
17
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
18
|
-
@c.update_or_create({:a=>2}, :b=>4).
|
19
|
-
@db.sqls.
|
18
|
+
@c.update_or_create({:a=>2}, :b=>4).must_equal @c.load(:id=>1, :a=>2, :b=>4)
|
19
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1", "UPDATE test SET b = 4 WHERE (id = 1)"]
|
20
20
|
|
21
21
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
22
|
-
@c.update_or_create({:a=>2}, :a=>3){|t| t.b = 4}.
|
22
|
+
@c.update_or_create({:a=>2}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
|
23
23
|
sqls = @db.sqls
|
24
|
-
sqls.shift.
|
25
|
-
sqls.shift.
|
24
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (a = 2) LIMIT 1"
|
25
|
+
sqls.shift.must_match(/UPDATE test SET [ab] = [34], [ab] = [34] WHERE \(id = 1\)/)
|
26
26
|
end
|
27
27
|
|
28
28
|
it ".update_or_create should create a record if an existing record does not exist" do
|
29
29
|
@db.fetch = [[], [{:id=>1, :a=>1, :b=>4}]]
|
30
|
-
@c.update_or_create(:a=>1){|t| t.b = 4}.
|
30
|
+
@c.update_or_create(:a=>1){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>1, :b=>4)
|
31
31
|
sqls = @db.sqls
|
32
|
-
sqls.shift.
|
33
|
-
sqls.shift.
|
34
|
-
sqls.shift.
|
32
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (a = 1) LIMIT 1"
|
33
|
+
sqls.shift.must_match(/INSERT INTO test \([ab], [ab]\) VALUES \([14], [14]\)/)
|
34
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (id = 1) LIMIT 1"
|
35
35
|
|
36
36
|
@db.fetch = [[], [{:id=>1, :a=>1, :b=>4}]]
|
37
|
-
@c.update_or_create({:a=>1}, :b=>4).
|
37
|
+
@c.update_or_create({:a=>1}, :b=>4).must_equal @c.load(:id=>1, :a=>1, :b=>4)
|
38
38
|
sqls = @db.sqls
|
39
|
-
sqls.shift.
|
40
|
-
sqls.shift.
|
41
|
-
sqls.shift.
|
39
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (a = 1) LIMIT 1"
|
40
|
+
sqls.shift.must_match(/INSERT INTO test \([ab], [ab]\) VALUES \([14], [14]\)/)
|
41
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (id = 1) LIMIT 1"
|
42
42
|
|
43
43
|
@db.fetch = [[], [{:id=>1, :a=>3, :b=>4}]]
|
44
|
-
@c.update_or_create({:a=>1}, :a=>3){|t| t.b = 4}.
|
44
|
+
@c.update_or_create({:a=>1}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
|
45
45
|
sqls = @db.sqls
|
46
|
-
sqls.shift.
|
47
|
-
sqls.shift.
|
48
|
-
sqls.shift.
|
46
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (a = 1) LIMIT 1"
|
47
|
+
sqls.shift.must_match(/INSERT INTO test \([ab], [ab]\) VALUES \([34], [34]\)/)
|
48
|
+
sqls.shift.must_equal "SELECT * FROM test WHERE (id = 1) LIMIT 1"
|
49
49
|
end
|
50
50
|
|
51
51
|
it ".update_or_create should return an existing record even if no changes necessary" do
|
52
52
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
53
|
-
@c.update_or_create(:a=>2){|t| t.b = 3}.
|
54
|
-
@db.sqls.
|
53
|
+
@c.update_or_create(:a=>2){|t| t.b = 3}.must_equal @c.load(:id=>1, :a=>2, :b=>3)
|
54
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
|
55
55
|
end
|
56
56
|
|
57
57
|
it ".find_or_new should return an existing record" do
|
58
58
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
59
|
-
@c.find_or_new(:a=>2){|t| t.b = 4}.
|
60
|
-
@db.sqls.
|
59
|
+
@c.find_or_new(:a=>2){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>2, :b=>4)
|
60
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
|
61
61
|
|
62
62
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
63
|
-
@c.find_or_new({:a=>2}, :b=>4).
|
64
|
-
@db.sqls.
|
63
|
+
@c.find_or_new({:a=>2}, :b=>4).must_equal @c.load(:id=>1, :a=>2, :b=>4)
|
64
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
|
65
65
|
|
66
66
|
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
|
67
|
-
@c.find_or_new({:a=>2}, :a=>3){|t| t.b = 4}.
|
68
|
-
@db.sqls.
|
67
|
+
@c.find_or_new({:a=>2}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
|
68
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
|
69
69
|
end
|
70
70
|
|
71
71
|
it ".find_or_new should return a new record if no record exists" do
|
72
72
|
o = @c.find_or_new(:a=>1){|t| t.b = 4}
|
73
|
-
o.
|
74
|
-
o.new?.
|
75
|
-
@db.sqls.
|
73
|
+
o.must_equal @c.load(:a=>1, :b=>4)
|
74
|
+
o.new?.must_equal true
|
75
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
|
76
76
|
|
77
77
|
o = @c.find_or_new({:a=>1}, :b=>4)
|
78
|
-
o.
|
79
|
-
o.new?.
|
80
|
-
@db.sqls.
|
78
|
+
o.must_equal @c.load(:a=>1, :b=>4)
|
79
|
+
o.new?.must_equal true
|
80
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
|
81
81
|
|
82
82
|
o = @c.find_or_new({:a=>1}, :a=>3){|t| t.b = 4}
|
83
|
-
o.
|
84
|
-
o.new?.
|
85
|
-
@db.sqls.
|
83
|
+
o.must_equal @c.load(:a=>3, :b=>4)
|
84
|
+
o.new?.must_equal true
|
85
|
+
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
|
86
86
|
end
|
87
87
|
end
|