sequel 3.21.0 → 3.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +855 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -7
- data/Rakefile +42 -24
- data/bin/sequel +26 -8
- data/doc/active_record.rdoc +1 -0
- data/doc/advanced_associations.rdoc +171 -11
- data/doc/association_basics.rdoc +293 -43
- data/doc/dataset_basics.rdoc +5 -5
- data/doc/dataset_filtering.rdoc +8 -0
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +85 -552
- data/doc/model_hooks.rdoc +78 -27
- data/doc/object_model.rdoc +541 -0
- data/doc/opening_databases.rdoc +97 -50
- data/doc/prepared_statements.rdoc +40 -14
- data/doc/querying.rdoc +28 -4
- data/doc/reflection.rdoc +30 -4
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/schema_modification.rdoc +572 -0
- data/doc/sharding.rdoc +95 -3
- data/doc/testing.rdoc +129 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/virtual_rows.rdoc +51 -2
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +17 -9
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +1 -57
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +18 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +453 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/db2.rb +47 -0
- data/lib/sequel/adapters/jdbc/derby.rb +287 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +73 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +58 -48
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +265 -94
- data/lib/sequel/adapters/mock.rb +361 -0
- data/lib/sequel/adapters/mysql.rb +118 -222
- data/lib/sequel/adapters/mysql2.rb +58 -57
- data/lib/sequel/adapters/odbc/mssql.rb +15 -7
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +3 -5
- data/lib/sequel/adapters/oracle.rb +349 -53
- data/lib/sequel/adapters/postgres.rb +356 -107
- data/lib/sequel/adapters/shared/access.rb +32 -9
- data/lib/sequel/adapters/shared/db2.rb +342 -0
- data/lib/sequel/adapters/shared/firebird.rb +221 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +335 -118
- data/lib/sequel/adapters/shared/mysql.rb +340 -76
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
- data/lib/sequel/adapters/shared/oracle.rb +243 -45
- data/lib/sequel/adapters/shared/postgres.rb +524 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +265 -89
- data/lib/sequel/adapters/sqlite.rb +101 -39
- data/lib/sequel/adapters/swift/mysql.rb +9 -12
- data/lib/sequel/adapters/swift/postgres.rb +9 -74
- data/lib/sequel/adapters/swift/sqlite.rb +8 -6
- data/lib/sequel/adapters/swift.rb +8 -7
- data/lib/sequel/adapters/tinytds.rb +161 -21
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +11 -1
- data/lib/sequel/connection_pool/sharded_threaded.rb +28 -2
- data/lib/sequel/connection_pool/single.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +38 -14
- data/lib/sequel/connection_pool.rb +10 -2
- data/lib/sequel/core.rb +118 -11
- data/lib/sequel/database/connecting.rb +38 -28
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +150 -46
- data/lib/sequel/database/query.rb +215 -65
- data/lib/sequel/database/schema_generator.rb +64 -20
- data/lib/sequel/database/schema_methods.rb +212 -54
- data/lib/sequel/dataset/actions.rb +322 -85
- data/lib/sequel/dataset/features.rb +96 -8
- data/lib/sequel/dataset/graph.rb +38 -11
- data/lib/sequel/dataset/misc.rb +50 -23
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +237 -44
- data/lib/sequel/dataset/sql.rb +698 -278
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +79 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +108 -0
- data/lib/sequel/extensions/columns_introspection.rb +61 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +16 -34
- data/lib/sequel/extensions/migration.rb +97 -11
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/null_dataset.rb +90 -0
- data/lib/sequel/extensions/pg_array.rb +460 -0
- data/lib/sequel/extensions/pg_array_ops.rb +220 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +169 -0
- data/lib/sequel/extensions/pg_hstore.rb +296 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +259 -0
- data/lib/sequel/extensions/pg_inet.rb +89 -0
- data/lib/sequel/extensions/pg_json.rb +178 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +316 -0
- data/lib/sequel/extensions/pretty_table.rb +5 -71
- data/lib/sequel/extensions/query.rb +1 -1
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +251 -37
- data/lib/sequel/extensions/select_remove.rb +35 -0
- data/lib/sequel/extensions/server_block.rb +139 -0
- data/lib/sequel/extensions/sql_expr.rb +4 -110
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/extensions/to_dot.rb +95 -83
- data/lib/sequel/model/associations.rb +1187 -393
- data/lib/sequel/model/base.rb +578 -170
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +5 -1
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +58 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +123 -8
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +58 -6
- data/lib/sequel/plugins/list.rb +12 -2
- data/lib/sequel/plugins/many_through_many.rb +79 -28
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +139 -63
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/prepared_statements.rb +167 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/schema.rb +7 -2
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +10 -2
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +1 -1
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +526 -59
- data/lib/sequel/timezones.rb +57 -38
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +1 -1
- data/spec/adapters/mssql_spec.rb +142 -40
- data/spec/adapters/mysql_spec.rb +380 -234
- data/spec/adapters/oracle_spec.rb +67 -77
- data/spec/adapters/postgres_spec.rb +1050 -212
- data/spec/adapters/spec_helper.rb +8 -5
- data/spec/adapters/sqlite_spec.rb +139 -27
- data/spec/core/connection_pool_spec.rb +196 -93
- data/spec/core/database_spec.rb +879 -510
- data/spec/core/dataset_spec.rb +1415 -1086
- data/spec/core/expression_filters_spec.rb +554 -109
- data/spec/core/mock_adapter_spec.rb +449 -0
- data/spec/core/object_graph_spec.rb +86 -114
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +482 -38
- data/spec/core/spec_helper.rb +6 -48
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +66 -32
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/{core/core_sql_spec.rb → extensions/core_extensions_spec.rb} +119 -139
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +64 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +69 -92
- data/spec/extensions/identity_map_spec.rb +131 -22
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -6
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +48 -39
- data/spec/extensions/many_through_many_spec.rb +283 -250
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +251 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +105 -0
- data/spec/extensions/pg_array_spec.rb +196 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +136 -0
- data/spec/extensions/pg_hstore_spec.rb +195 -0
- data/spec/extensions/pg_inet_spec.rb +44 -0
- data/spec/extensions/pg_json_spec.rb +101 -0
- data/spec/extensions/pg_statement_cache_spec.rb +209 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +168 -0
- data/spec/extensions/query_spec.rb +6 -0
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +429 -22
- data/spec/extensions/schema_spec.rb +25 -21
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +85 -43
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +23 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +22 -61
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +4 -7
- data/spec/extensions/touch_spec.rb +13 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +30 -51
- data/spec/extensions/validation_helpers_spec.rb +16 -35
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +9 -3
- data/spec/integration/dataset_test.rb +605 -91
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +454 -143
- data/spec/integration/prepared_statement_test.rb +263 -128
- data/spec/integration/schema_test.rb +309 -43
- data/spec/integration/spec_helper.rb +40 -30
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +18 -4
- data/spec/model/association_reflection_spec.rb +201 -7
- data/spec/model/associations_spec.rb +1067 -661
- data/spec/model/base_spec.rb +329 -95
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +677 -676
- data/spec/model/hooks_spec.rb +349 -67
- data/spec/model/model_spec.rb +240 -168
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +490 -232
- data/spec/model/spec_helper.rb +14 -71
- data/spec/model/validations_spec.rb +11 -0
- data/spec/rcov.opts +2 -0
- metadata +178 -80
|
@@ -5,13 +5,14 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
5
5
|
class ::IdentityMapModel < Sequel::Model
|
|
6
6
|
plugin :identity_map
|
|
7
7
|
attr_accessor :foo
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
dataset._fetch = proc do |sql|
|
|
9
|
+
sql =~ /WHERE \(?(\w+\.)?(\w+) = (\d)\)?/
|
|
10
|
+
{$2.to_sym=>$3.to_i}
|
|
11
|
+
end
|
|
12
|
+
def self.waw_identity_map(&block) # with and without
|
|
13
|
+
with_identity_map(&block)
|
|
14
|
+
db.reset
|
|
15
|
+
yield
|
|
15
16
|
end
|
|
16
17
|
end
|
|
17
18
|
class ::IdentityMapAlbum < ::IdentityMapModel
|
|
@@ -105,9 +106,9 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
105
106
|
MODEL_DB.sqls.length.should == 1
|
|
106
107
|
o.foo = 1
|
|
107
108
|
@c[1].foo.should == o.foo
|
|
108
|
-
MODEL_DB.sqls.length.should ==
|
|
109
|
+
MODEL_DB.sqls.length.should == 0
|
|
109
110
|
@c[2].foo.should_not == o.foo
|
|
110
|
-
MODEL_DB.sqls.length.should ==
|
|
111
|
+
MODEL_DB.sqls.length.should == 1
|
|
111
112
|
end
|
|
112
113
|
end
|
|
113
114
|
|
|
@@ -118,22 +119,22 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
118
119
|
MODEL_DB.sqls.length.should == 1
|
|
119
120
|
o.foo = 1
|
|
120
121
|
@c[1].should == o
|
|
121
|
-
MODEL_DB.sqls.length.should ==
|
|
122
|
+
MODEL_DB.sqls.length.should == 0
|
|
122
123
|
o.destroy
|
|
123
|
-
MODEL_DB.sqls.length.should ==
|
|
124
|
+
MODEL_DB.sqls.length.should == 1
|
|
124
125
|
@c[1].foo.should_not == o.foo
|
|
125
|
-
MODEL_DB.sqls.length.should ==
|
|
126
|
+
MODEL_DB.sqls.length.should == 1
|
|
126
127
|
|
|
127
128
|
MODEL_DB.reset
|
|
128
129
|
o = @c[2]
|
|
129
130
|
MODEL_DB.sqls.length.should == 1
|
|
130
131
|
o.foo = 1
|
|
131
132
|
@c[2].should == o
|
|
132
|
-
MODEL_DB.sqls.length.should ==
|
|
133
|
+
MODEL_DB.sqls.length.should == 0
|
|
133
134
|
o.delete
|
|
134
|
-
MODEL_DB.sqls.length.should ==
|
|
135
|
+
MODEL_DB.sqls.length.should == 1
|
|
135
136
|
@c[2].foo.should_not == o.foo
|
|
136
|
-
MODEL_DB.sqls.length.should ==
|
|
137
|
+
MODEL_DB.sqls.length.should == 1
|
|
137
138
|
end
|
|
138
139
|
end
|
|
139
140
|
|
|
@@ -148,10 +149,10 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
148
149
|
MODEL_DB.sqls.length.should == 1
|
|
149
150
|
o = @c1.load(:id=>1, :another_id=>2, :artist_id=>2)
|
|
150
151
|
o.artist.should == a
|
|
151
|
-
MODEL_DB.sqls.length.should ==
|
|
152
|
+
MODEL_DB.sqls.length.should == 0
|
|
152
153
|
o = @c1.load(:id=>3, :another_id=>3, :artist_id=>3)
|
|
153
154
|
o.artist.should_not == a
|
|
154
|
-
MODEL_DB.sqls.length.should ==
|
|
155
|
+
MODEL_DB.sqls.length.should == 1
|
|
155
156
|
end
|
|
156
157
|
end
|
|
157
158
|
|
|
@@ -165,10 +166,10 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
165
166
|
MODEL_DB.sqls.length.should == 1
|
|
166
167
|
o = @c1.load(:id=>2, :artist_id=>2)
|
|
167
168
|
o.artist.should == a
|
|
168
|
-
MODEL_DB.sqls.length.should ==
|
|
169
|
+
MODEL_DB.sqls.length.should == 0
|
|
169
170
|
o = @c1.load(:id=>3, :artist_id=>3)
|
|
170
171
|
o.artist.should_not == a
|
|
171
|
-
MODEL_DB.sqls.length.should ==
|
|
172
|
+
MODEL_DB.sqls.length.should == 1
|
|
172
173
|
end
|
|
173
174
|
end
|
|
174
175
|
|
|
@@ -182,9 +183,9 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
182
183
|
a.should be_a_kind_of(@c1)
|
|
183
184
|
MODEL_DB.sqls.length.should == 1
|
|
184
185
|
o.reload
|
|
185
|
-
MODEL_DB.sqls.length.should ==
|
|
186
|
+
MODEL_DB.sqls.length.should == 1
|
|
186
187
|
o.artist.should == a
|
|
187
|
-
MODEL_DB.sqls.length.should ==
|
|
188
|
+
MODEL_DB.sqls.length.should == 1
|
|
188
189
|
end
|
|
189
190
|
end
|
|
190
191
|
|
|
@@ -199,7 +200,7 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
199
200
|
MODEL_DB.sqls.length.should == 1
|
|
200
201
|
o = @c1.load(:id=>2, :artist_id=>2)
|
|
201
202
|
o.artist.should == a
|
|
202
|
-
MODEL_DB.sqls.length.should ==
|
|
203
|
+
MODEL_DB.sqls.length.should == 1
|
|
203
204
|
end
|
|
204
205
|
end
|
|
205
206
|
|
|
@@ -213,7 +214,115 @@ describe "Sequel::Plugins::IdentityMap" do
|
|
|
213
214
|
MODEL_DB.sqls.length.should == 1
|
|
214
215
|
o = @c1.load(:id=>2, :artist_id=>2)
|
|
215
216
|
o.artist.should == a
|
|
217
|
+
MODEL_DB.sqls.length.should == 1
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
it "should not use the identity map as a lookup cache if a dynamic callback is used" do
|
|
222
|
+
@c1.many_to_one :artist, :class=>@c2
|
|
223
|
+
@c.with_identity_map do
|
|
224
|
+
MODEL_DB.sqls.length.should == 0
|
|
225
|
+
o = @c1.load(:id=>1, :artist_id=>2)
|
|
226
|
+
a = o.artist
|
|
227
|
+
a.should be_a_kind_of(@c2)
|
|
228
|
+
MODEL_DB.sqls.length.should == 1
|
|
229
|
+
o = @c1.load(:id=>2, :artist_id=>2)
|
|
230
|
+
o.artist.should == a
|
|
231
|
+
MODEL_DB.sqls.length.should == 0
|
|
232
|
+
o = @c1.load(:id=>3, :artist_id=>3)
|
|
233
|
+
o.artist.should_not == a
|
|
234
|
+
MODEL_DB.sqls.length.should == 1
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
it "should not override custom :eager_loaders for many_to_many associations" do
|
|
239
|
+
@c1.columns :id
|
|
240
|
+
@c2.columns :id
|
|
241
|
+
c = @c2
|
|
242
|
+
@c1.many_to_many :artists, :class=>@c2, :left_key=>:album_id, :right_key=>:artist_id, :join_table=>:aa, :eager_loader=>(proc do |eo|
|
|
243
|
+
eo[:rows].each{|object| object.associations[:artists] = [c.load(:id=>object.id)]}
|
|
244
|
+
end)
|
|
245
|
+
@c1.dataset._fetch = [{:id=>1}, {:id=>2}, {:id=>3}]
|
|
246
|
+
|
|
247
|
+
@c.waw_identity_map do
|
|
248
|
+
MODEL_DB.sqls.length.should == 0
|
|
249
|
+
a = @c1.eager(:artists).all
|
|
250
|
+
MODEL_DB.sqls.length.should == 1
|
|
251
|
+
a.should == [@c1.load(:id=>1), @c1.load(:id=>2), @c1.load(:id=>3)]
|
|
252
|
+
a.map{|x| x.artists}.should == [[@c2.load(:id=>1)], [@c2.load(:id=>2)], [@c2.load(:id=>3)]]
|
|
253
|
+
MODEL_DB.sqls.length.should == 0
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
it "should work correctly when eagerly loading many_to_many associations" do
|
|
258
|
+
@c1.columns :id
|
|
259
|
+
@c2.columns :id
|
|
260
|
+
@c1.many_to_many :artists, :class=>@c2, :left_key=>:album_id, :right_key=>:artist_id, :join_table=>:aa
|
|
261
|
+
@c1.dataset._fetch = [{:id=>1}, {:id=>2}, {:id=>3}]
|
|
262
|
+
@c2.dataset._fetch = [{:id=>1, :x_foreign_key_x=>1}, {:id=>1, :x_foreign_key_x=>2}, {:id=>2, :x_foreign_key_x=>1}, {:id=>2, :x_foreign_key_x=>2}, {:id=>3, :x_foreign_key_x=>1}, {:id=>3, :x_foreign_key_x=>1}]
|
|
263
|
+
|
|
264
|
+
@c.waw_identity_map do
|
|
265
|
+
MODEL_DB.sqls.length.should == 0
|
|
266
|
+
a = @c1.eager(:artists).all
|
|
267
|
+
MODEL_DB.sqls.length.should == 2
|
|
268
|
+
a.should == [@c1.load(:id=>1), @c1.load(:id=>2), @c1.load(:id=>3)]
|
|
269
|
+
a.map{|x| x.artists}.should == [[@c2.load(:id=>1), @c2.load(:id=>2), @c2.load(:id=>3), @c2.load(:id=>3)], [@c2.load(:id=>1), @c2.load(:id=>2)], []]
|
|
270
|
+
MODEL_DB.sqls.length.should == 0
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
it "should work correctly when eagerly loading many_to_many associations with composite keys" do
|
|
275
|
+
@c1.columns :id, :id2
|
|
276
|
+
@c2.columns :id
|
|
277
|
+
@c1.set_primary_key :id, :id2
|
|
278
|
+
@c1.many_to_many :artists, :class=>@c2, :left_key=>[:album_id1, :album_id2], :right_key=>:artist_id, :join_table=>:aa
|
|
279
|
+
@c1.dataset._fetch = [{:id=>1, :id2=>4}, {:id=>2, :id2=>5}, {:id=>3, :id2=>6}]
|
|
280
|
+
@c2.dataset._fetch = [ {:id=>1, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>1, :x_foreign_key_0_x=>2, :x_foreign_key_1_x=>5}, {:id=>2, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>2, :x_foreign_key_0_x=>2, :x_foreign_key_1_x=>5}, {:id=>3, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>3, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}]
|
|
281
|
+
|
|
282
|
+
@c.waw_identity_map do
|
|
283
|
+
MODEL_DB.sqls.length.should == 0
|
|
284
|
+
a = @c1.eager(:artists).all
|
|
285
|
+
MODEL_DB.sqls.length.should == 2
|
|
286
|
+
a.should == [@c1.load(:id=>1, :id2=>4), @c1.load(:id=>2, :id2=>5), @c1.load(:id=>3, :id2=>6)]
|
|
287
|
+
a.map{|x| x.artists}.should == [[@c2.load(:id=>1), @c2.load(:id=>2), @c2.load(:id=>3), @c2.load(:id=>3)], [@c2.load(:id=>1), @c2.load(:id=>2)], []]
|
|
288
|
+
MODEL_DB.sqls.length.should == 0
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
it "should work correctly when eagerly loading many_through_many associations" do
|
|
293
|
+
@c1.columns :id
|
|
294
|
+
@c2.columns :id
|
|
295
|
+
@c1.plugin :many_through_many
|
|
296
|
+
@c1.many_through_many :artists, [[:aa, :album_id, :artist_id]], :class=>@c2
|
|
297
|
+
@c1.dataset._fetch = [{:id=>1}, {:id=>2}, {:id=>3}]
|
|
298
|
+
@c2.dataset._fetch = [{:id=>1, :x_foreign_key_x=>1}, {:id=>1, :x_foreign_key_x=>2}, {:id=>2, :x_foreign_key_x=>1}, {:id=>2, :x_foreign_key_x=>2}, {:id=>3, :x_foreign_key_x=>1}, {:id=>3, :x_foreign_key_x=>1}]
|
|
299
|
+
|
|
300
|
+
@c.waw_identity_map do
|
|
301
|
+
MODEL_DB.sqls.length.should == 0
|
|
302
|
+
a = @c1.eager(:artists).all
|
|
303
|
+
MODEL_DB.sqls.length.should == 2
|
|
304
|
+
a.should == [@c1.load(:id=>1), @c1.load(:id=>2), @c1.load(:id=>3)]
|
|
305
|
+
a.map{|x| x.artists}.should == [[@c2.load(:id=>1), @c2.load(:id=>2), @c2.load(:id=>3), @c2.load(:id=>3)], [@c2.load(:id=>1), @c2.load(:id=>2)], []]
|
|
306
|
+
MODEL_DB.sqls.length.should == 0
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
it "should work correctly when eagerly loading many_to_many associations with composite keys" do
|
|
311
|
+
@c1.columns :id, :id2
|
|
312
|
+
@c2.columns :id
|
|
313
|
+
@c1.set_primary_key :id, :id2
|
|
314
|
+
@c1.plugin :many_through_many
|
|
315
|
+
@c1.many_through_many :artists, [[:aa, [:album_id1, :album_id2], :artist_id]], :class=>@c2
|
|
316
|
+
@c1.dataset._fetch = [{:id=>1, :id2=>4}, {:id=>2, :id2=>5}, {:id=>3, :id2=>6}]
|
|
317
|
+
@c2.dataset._fetch = [ {:id=>1, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>1, :x_foreign_key_0_x=>2, :x_foreign_key_1_x=>5}, {:id=>2, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>2, :x_foreign_key_0_x=>2, :x_foreign_key_1_x=>5}, {:id=>3, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}, {:id=>3, :x_foreign_key_0_x=>1, :x_foreign_key_1_x=>4}]
|
|
318
|
+
|
|
319
|
+
@c.waw_identity_map do
|
|
320
|
+
MODEL_DB.sqls.length.should == 0
|
|
321
|
+
a = @c1.eager(:artists).all
|
|
216
322
|
MODEL_DB.sqls.length.should == 2
|
|
323
|
+
a.should == [@c1.load(:id=>1, :id2=>4), @c1.load(:id=>2, :id2=>5), @c1.load(:id=>3, :id2=>6)]
|
|
324
|
+
a.map{|x| x.artists}.should == [[@c2.load(:id=>1), @c2.load(:id=>2), @c2.load(:id=>3), @c2.load(:id=>3)], [@c2.load(:id=>1), @c2.load(:id=>2)], []]
|
|
325
|
+
MODEL_DB.sqls.length.should == 0
|
|
217
326
|
end
|
|
218
327
|
end
|
|
219
328
|
end
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
|
|
2
2
|
|
|
3
|
-
if defined?(ActiveSupport::Inflector)
|
|
4
|
-
skip_warn "inflector extension: active_support inflector loaded"
|
|
5
|
-
else
|
|
6
3
|
describe String do
|
|
7
4
|
it "#camelize and #camelcase should transform the word to CamelCase" do
|
|
8
5
|
"egg_and_hams".camelize.should == "EggAndHams"
|
|
@@ -182,4 +179,3 @@ describe 'Default inflections' do
|
|
|
182
179
|
end
|
|
183
180
|
end
|
|
184
181
|
end
|
|
185
|
-
end
|
|
@@ -2,54 +2,44 @@ require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
|
|
2
2
|
|
|
3
3
|
describe "instance_filters plugin" do
|
|
4
4
|
before do
|
|
5
|
-
@c = Class.new(Sequel::Model(:people))
|
|
6
|
-
end
|
|
7
|
-
@sql = sql = ''
|
|
8
|
-
@v = v = [1]
|
|
5
|
+
@c = Class.new(Sequel::Model(:people))
|
|
9
6
|
@c.dataset.quote_identifiers = false
|
|
10
|
-
@c.dataset.meta_def(:update) do |opts|
|
|
11
|
-
sql.replace(update_sql(opts))
|
|
12
|
-
return v.first
|
|
13
|
-
end
|
|
14
|
-
@c.dataset.meta_def(:delete) do
|
|
15
|
-
sql.replace(delete_sql)
|
|
16
|
-
return v.first
|
|
17
|
-
end
|
|
18
7
|
@c.columns :id, :name, :num
|
|
19
8
|
@c.plugin :instance_filters
|
|
20
9
|
@p = @c.load(:id=>1, :name=>'John', :num=>1)
|
|
10
|
+
MODEL_DB.sqls
|
|
21
11
|
end
|
|
22
12
|
|
|
23
13
|
specify "should raise an error when updating a stale record" do
|
|
24
14
|
@p.update(:name=>'Bob')
|
|
25
|
-
|
|
15
|
+
MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
|
|
26
16
|
@p.instance_filter(:name=>'Jim')
|
|
27
|
-
@
|
|
17
|
+
@p.this.numrows = 0
|
|
28
18
|
proc{@p.update(:name=>'Joe')}.should raise_error(Sequel::Plugins::InstanceFilters::Error)
|
|
29
|
-
|
|
19
|
+
MODEL_DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Jim'))"]
|
|
30
20
|
end
|
|
31
21
|
|
|
32
22
|
specify "should raise an error when destroying a stale record" do
|
|
33
23
|
@p.destroy
|
|
34
|
-
|
|
24
|
+
MODEL_DB.sqls.should == ["DELETE FROM people WHERE id = 1"]
|
|
35
25
|
@p.instance_filter(:name=>'Jim')
|
|
36
|
-
@
|
|
26
|
+
@p.this.numrows = 0
|
|
37
27
|
proc{@p.destroy}.should raise_error(Sequel::Plugins::InstanceFilters::Error)
|
|
38
|
-
|
|
28
|
+
MODEL_DB.sqls.should == ["DELETE FROM people WHERE ((id = 1) AND (name = 'Jim'))"]
|
|
39
29
|
end
|
|
40
30
|
|
|
41
31
|
specify "should apply all instance filters" do
|
|
42
32
|
@p.instance_filter(:name=>'Jim')
|
|
43
33
|
@p.instance_filter{num > 2}
|
|
44
34
|
@p.update(:name=>'Bob')
|
|
45
|
-
|
|
35
|
+
MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE ((id = 1) AND (name = 'Jim') AND (num > 2))"]
|
|
46
36
|
end
|
|
47
37
|
|
|
48
38
|
specify "should drop instance filters after updating" do
|
|
49
39
|
@p.instance_filter(:name=>'Joe')
|
|
50
40
|
@p.update(:name=>'Joe')
|
|
51
|
-
|
|
41
|
+
MODEL_DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Joe'))"]
|
|
52
42
|
@p.update(:name=>'Bob')
|
|
53
|
-
|
|
43
|
+
MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
|
|
54
44
|
end
|
|
55
45
|
end
|
|
@@ -177,3 +177,75 @@ describe "InstanceHooks plugin" do
|
|
|
177
177
|
@r.should == [2, 1, 4, 3]
|
|
178
178
|
end
|
|
179
179
|
end
|
|
180
|
+
|
|
181
|
+
describe "InstanceHooks plugin with transactions" do
|
|
182
|
+
before do
|
|
183
|
+
@db = Sequel.mock(:numrows=>1)
|
|
184
|
+
pr = proc{|x| r(x)}
|
|
185
|
+
@c = Class.new(Sequel::Model(@db[:items])) do
|
|
186
|
+
attr_accessor :rb
|
|
187
|
+
def after_save
|
|
188
|
+
db.execute('as')
|
|
189
|
+
raise Sequel::Rollback if rb
|
|
190
|
+
end
|
|
191
|
+
def after_destroy
|
|
192
|
+
db.execute('ad')
|
|
193
|
+
raise Sequel::Rollback if rb
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
@c.use_transactions = true
|
|
197
|
+
@c.plugin :instance_hooks
|
|
198
|
+
@o = @c.load({:id=>1})
|
|
199
|
+
@or = @c.load({:id=>1})
|
|
200
|
+
@or.rb = true
|
|
201
|
+
@r = []
|
|
202
|
+
@db.sqls
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it "should support after_commit_hook" do
|
|
206
|
+
@o.after_commit_hook{@db.execute('ac1')}
|
|
207
|
+
@o.after_commit_hook{@db.execute('ac2')}
|
|
208
|
+
@o.save.should_not be_nil
|
|
209
|
+
@db.sqls.should == ['BEGIN', 'as', 'COMMIT', 'ac1', 'ac2']
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
it "should support after_rollback_hook" do
|
|
213
|
+
@or.after_rollback_hook{@db.execute('ar1')}
|
|
214
|
+
@or.after_rollback_hook{@db.execute('ar2')}
|
|
215
|
+
@or.save.should be_nil
|
|
216
|
+
@db.sqls.should == ['BEGIN', 'as', 'ROLLBACK', 'ar1', 'ar2']
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it "should support after_commit_hook" do
|
|
220
|
+
@o.after_destroy_commit_hook{@db.execute('adc1')}
|
|
221
|
+
@o.after_destroy_commit_hook{@db.execute('adc2')}
|
|
222
|
+
@o.destroy.should_not be_nil
|
|
223
|
+
@db.sqls.should == ['BEGIN', "DELETE FROM items WHERE (id = 1)", 'ad', 'COMMIT', 'adc1', 'adc2']
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it "should support after_rollback_hook" do
|
|
227
|
+
@or.after_destroy_rollback_hook{@db.execute('adr1')}
|
|
228
|
+
@or.after_destroy_rollback_hook{@db.execute('adr2')}
|
|
229
|
+
@or.destroy.should be_nil
|
|
230
|
+
@db.sqls.should == ['BEGIN', "DELETE FROM items WHERE (id = 1)", 'ad', 'ROLLBACK', 'adr1', 'adr2']
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
it "should have *_hook methods return self "do
|
|
234
|
+
@o.before_destroy_hook{r 1}.should equal(@o)
|
|
235
|
+
@o.before_validation_hook{r 1}.should equal(@o)
|
|
236
|
+
@o.before_save_hook{r 1}.should equal(@o)
|
|
237
|
+
@o.before_update_hook{r 1}.should equal(@o)
|
|
238
|
+
@o.before_create_hook{r 1}.should equal(@o)
|
|
239
|
+
|
|
240
|
+
@o.after_destroy_hook{r 1}.should equal(@o)
|
|
241
|
+
@o.after_validation_hook{r 1}.should equal(@o)
|
|
242
|
+
@o.after_save_hook{r 1}.should equal(@o)
|
|
243
|
+
@o.after_update_hook{r 1}.should equal(@o)
|
|
244
|
+
@o.after_create_hook{r 1}.should equal(@o)
|
|
245
|
+
@o.after_commit_hook{r 1}.should equal(@o)
|
|
246
|
+
@o.after_rollback_hook{r 1}.should equal(@o)
|
|
247
|
+
@o.after_destroy_commit_hook{r 1}.should equal(@o)
|
|
248
|
+
@o.after_destroy_rollback_hook{r 1}.should equal(@o)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
end
|
|
@@ -96,14 +96,30 @@ describe "Sequel::Plugins::JsonSerializer" do
|
|
|
96
96
|
@artist.id.should == 2
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
+
it "should support #from_json to support specific :fields" do
|
|
100
|
+
@album.from_json('{"name": "AS", "artist_id": 3}', :fields=>['name'])
|
|
101
|
+
@album.name.should == 'AS'
|
|
102
|
+
@album.artist_id.should == 2
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "should support #from_json to support :missing=>:skip option" do
|
|
106
|
+
@album.from_json('{"artist_id": 3}', :fields=>['name'], :missing=>:skip)
|
|
107
|
+
@album.name.should == 'RF'
|
|
108
|
+
@album.artist_id.should == 2
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it "should support #from_json to support :missing=>:raise option" do
|
|
112
|
+
proc{@album.from_json('{"artist_id": 3}', :fields=>['name'], :missing=>:raise)}.should raise_error(Sequel::Error)
|
|
113
|
+
end
|
|
114
|
+
|
|
99
115
|
it "should raise an exception for json keys that aren't associations, columns, or setter methods" do
|
|
100
116
|
Album.send(:undef_method, :blah=)
|
|
101
117
|
proc{JSON.parse(@album.to_json(:include=>:blah))}.should raise_error(Sequel::Error)
|
|
102
118
|
end
|
|
103
119
|
|
|
104
120
|
it "should support a to_json class and dataset method" do
|
|
105
|
-
|
|
106
|
-
|
|
121
|
+
Album.dataset._fetch = {:id=>1, :name=>'RF', :artist_id=>2}
|
|
122
|
+
Artist.dataset._fetch = {:id=>2, :name=>'YJM'}
|
|
107
123
|
JSON.parse(Album.to_json).should == [@album]
|
|
108
124
|
JSON.parse(Album.to_json(:include=>:artist)).map{|x| x.artist}.should == [@artist]
|
|
109
125
|
JSON.parse(Album.dataset.to_json(:only=>:name)).should == [Album.load(:name=>@album.name)]
|
|
@@ -112,10 +128,23 @@ describe "Sequel::Plugins::JsonSerializer" do
|
|
|
112
128
|
it "should have dataset to_json method work with naked datasets" do
|
|
113
129
|
album = @album
|
|
114
130
|
ds = Album.dataset.naked
|
|
115
|
-
ds.
|
|
131
|
+
ds._fetch = {:id=>1, :name=>'RF', :artist_id=>2}
|
|
116
132
|
JSON.parse(ds.to_json).should == [@album.values.inject({}){|h, (k, v)| h[k.to_s] = v; h}]
|
|
117
133
|
end
|
|
118
134
|
|
|
135
|
+
it "should have dataset to_json method respect :array option for the array to use" do
|
|
136
|
+
a = Album.load(:id=>1, :name=>'RF', :artist_id=>3)
|
|
137
|
+
JSON.parse(Album.to_json(:array=>[a])).should == [a]
|
|
138
|
+
|
|
139
|
+
a.associations[:artist] = artist = Artist.load(:id=>3, :name=>'YJM')
|
|
140
|
+
JSON.parse(Album.to_json(:array=>[a], :include=>:artist)).first.artist.should == artist
|
|
141
|
+
|
|
142
|
+
artist.associations[:albums] = [a]
|
|
143
|
+
x = JSON.parse(Artist.to_json(:array=>[artist], :include=>:albums))
|
|
144
|
+
x.should == [artist]
|
|
145
|
+
x.first.albums.should == [a]
|
|
146
|
+
end
|
|
147
|
+
|
|
119
148
|
it "should propagate class default options to instance to_json output" do
|
|
120
149
|
class ::Album2 < Sequel::Model
|
|
121
150
|
attr_accessor :blah
|
|
@@ -136,9 +165,23 @@ describe "Sequel::Plugins::JsonSerializer" do
|
|
|
136
165
|
@album.to_json(:root=>true, :only => :name).to_s.should == '{"album":{"name":"RF"}}'
|
|
137
166
|
end
|
|
138
167
|
|
|
139
|
-
it "should handle the :root option to qualify a dataset of records" do
|
|
140
|
-
|
|
141
|
-
Album.dataset.
|
|
168
|
+
it "should handle the :root=>:both option to qualify a dataset of records" do
|
|
169
|
+
Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
|
|
170
|
+
Album.dataset.to_json(:root=>true, :only => :id).to_s.should == '{"albums":[{"album":{"id":1}},{"album":{"id":1}}]}'
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it "should handle the :root=>:collection option to qualify just the collection" do
|
|
174
|
+
Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
|
|
175
|
+
Album.dataset.to_json(:root=>:collection, :only => :id).to_s.should == '{"albums":[{"id":1},{"id":1}]}'
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it "should handle the :root=>:instance option to qualify just the instances" do
|
|
179
|
+
Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
|
|
180
|
+
Album.dataset.to_json(:root=>:instance, :only => :id).to_s.should == '[{"album":{"id":1}},{"album":{"id":1}}]'
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
it "should handle the :root=>true option be the same as :root=>:both for backwards compatibility" do
|
|
184
|
+
Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
|
|
142
185
|
Album.dataset.to_json(:root=>true, :only => :id).to_s.should == '{"albums":[{"album":{"id":1}},{"album":{"id":1}}]}'
|
|
143
186
|
end
|
|
144
187
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
|
2
|
+
require 'yaml'
|
|
2
3
|
|
|
3
4
|
describe "Sequel::Plugins::LazyAttributes" do
|
|
4
5
|
before do
|
|
5
|
-
@db =
|
|
6
|
+
@db = Sequel.mock
|
|
6
7
|
@db.meta_def(:schema){|*a| [[:id, {:type=>:integer}], [:name,{:type=>:string}]]}
|
|
7
8
|
class ::LazyAttributesModel < Sequel::Model(@db[:la])
|
|
8
9
|
plugin :lazy_attributes
|
|
@@ -10,27 +11,23 @@ describe "Sequel::Plugins::LazyAttributes" do
|
|
|
10
11
|
meta_def(:columns){[:id, :name]}
|
|
11
12
|
lazy_attributes :name
|
|
12
13
|
meta_def(:columns){[:id]}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
where = @opts[:where]
|
|
18
|
-
block = @mod_block || proc{|s| s}
|
|
19
|
-
if !where
|
|
20
|
-
if select.include?(:name)
|
|
21
|
-
yield(block[:id=>1, :name=>'1'])
|
|
22
|
-
yield(block[:id=>2, :name=>'2'])
|
|
14
|
+
instance_dataset._fetch = dataset._fetch = proc do |sql|
|
|
15
|
+
if sql !~ /WHERE/
|
|
16
|
+
if sql =~ /name/
|
|
17
|
+
[{:id=>1, :name=>'1'}, {:id=>2, :name=>'2'}]
|
|
23
18
|
else
|
|
24
|
-
|
|
25
|
-
yield(:id=>2)
|
|
19
|
+
[{:id=>1}, {:id=>2}]
|
|
26
20
|
end
|
|
27
21
|
else
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
if sql =~ /id IN \(([\d, ]+)\)/
|
|
23
|
+
$1.split(', ')
|
|
24
|
+
elsif sql =~ /id = (\d)/
|
|
25
|
+
[$1]
|
|
26
|
+
end.map do |x|
|
|
30
27
|
if sql =~ /SELECT name FROM/
|
|
31
|
-
|
|
28
|
+
{:name=>x.to_s}
|
|
32
29
|
else
|
|
33
|
-
|
|
30
|
+
{:id=>x.to_i, :name=>x.to_s}
|
|
34
31
|
end
|
|
35
32
|
end
|
|
36
33
|
end
|
|
@@ -38,7 +35,7 @@ describe "Sequel::Plugins::LazyAttributes" do
|
|
|
38
35
|
end
|
|
39
36
|
@c = ::LazyAttributesModel
|
|
40
37
|
@ds = LazyAttributesModel.dataset
|
|
41
|
-
@db.
|
|
38
|
+
@db.sqls
|
|
42
39
|
end
|
|
43
40
|
after do
|
|
44
41
|
Object.send(:remove_const, :LazyAttributesModel)
|
|
@@ -136,7 +133,7 @@ describe "Sequel::Plugins::LazyAttributes" do
|
|
|
136
133
|
|
|
137
134
|
it "should work with the serialization plugin" do
|
|
138
135
|
@c.plugin :serialization, :yaml, :name
|
|
139
|
-
@ds.
|
|
136
|
+
@c.instance_dataset._fetch = @ds._fetch = [[{:id=>1}, {:id=>2}], [{:id=>1, :name=>"--- 3\n"}, {:id=>2, :name=>"--- 6\n"}], [{:id=>1}], [{:name=>"--- 3\n"}]]
|
|
140
137
|
@c.with_identity_map do
|
|
141
138
|
ms = @ds.all
|
|
142
139
|
ms.map{|m| m.values}.should == [{:id=>1}, {:id=>2}]
|
|
@@ -146,7 +143,6 @@ describe "Sequel::Plugins::LazyAttributes" do
|
|
|
146
143
|
ms.map{|m| m.name}.should == [3,6]
|
|
147
144
|
@db.sqls.should == ['SELECT id FROM la', 'SELECT id, name FROM la WHERE (id IN (1, 2))']
|
|
148
145
|
end
|
|
149
|
-
@db.reset
|
|
150
146
|
@c.with_identity_map do
|
|
151
147
|
m = @ds.first
|
|
152
148
|
m.values.should == {:id=>1}
|