sequel 4.22.0 → 4.23.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.
- 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
|
@@ -21,66 +21,66 @@ describe "List plugin" do
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it "should default to using :position as the position field" do
|
|
24
|
-
@c.position_field.
|
|
25
|
-
@c.new.list_dataset.sql.
|
|
24
|
+
@c.position_field.must_equal :position
|
|
25
|
+
@c.new.list_dataset.sql.must_equal 'SELECT * FROM items ORDER BY position'
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it "should accept a :field option to modify the position field" do
|
|
29
|
-
klass(:field=>:pos).position_field.
|
|
29
|
+
klass(:field=>:pos).position_field.must_equal :pos
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it "should accept a :scope option with a symbol for a single scope column" do
|
|
33
|
-
@sc.new(:scope_id=>4).list_dataset.sql.
|
|
33
|
+
@sc.new(:scope_id=>4).list_dataset.sql.must_equal 'SELECT * FROM items WHERE (scope_id = 4) ORDER BY scope_id, position'
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
it "should accept a :scope option with an array of symbols for multiple scope columns" do
|
|
37
37
|
['SELECT * FROM items WHERE ((scope_id = 4) AND (pos = 3)) ORDER BY scope_id, pos, position',
|
|
38
38
|
'SELECT * FROM items WHERE ((pos = 3) AND (scope_id = 4)) ORDER BY scope_id, pos, position'].
|
|
39
|
-
|
|
39
|
+
must_include(klass(:scope=>[:scope_id, :pos]).new(:scope_id=>4, :pos=>3).list_dataset.sql)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it "should accept a :scope option with a proc for a custom list scope" do
|
|
43
|
-
klass(:scope=>proc{|o| o.model.dataset.filter(:active).filter(:scope_id=>o.scope_id)}).new(:scope_id=>4).list_dataset.sql.
|
|
43
|
+
klass(:scope=>proc{|o| o.model.dataset.filter(:active).filter(:scope_id=>o.scope_id)}).new(:scope_id=>4).list_dataset.sql.must_equal 'SELECT * FROM items WHERE (active AND (scope_id = 4)) ORDER BY position'
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it "should modify the order when using the plugin" do
|
|
47
47
|
c = Class.new(Sequel::Model(:items))
|
|
48
|
-
c.dataset.sql.
|
|
48
|
+
c.dataset.sql.must_equal 'SELECT * FROM items'
|
|
49
49
|
c.plugin :list
|
|
50
|
-
c.dataset.sql.
|
|
50
|
+
c.dataset.sql.must_equal 'SELECT * FROM items ORDER BY position'
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it "should be able to access the position field as a class attribute" do
|
|
54
|
-
@c.position_field.
|
|
55
|
-
klass(:field=>:pos).position_field.
|
|
54
|
+
@c.position_field.must_equal :position
|
|
55
|
+
klass(:field=>:pos).position_field.must_equal :pos
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
it "should be able to access the scope proc as a class attribute" do
|
|
59
|
-
@c.scope_proc.
|
|
60
|
-
@sc.scope_proc[@sc.new(:scope_id=>4)].sql.
|
|
59
|
+
@c.scope_proc.must_equal nil
|
|
60
|
+
@sc.scope_proc[@sc.new(:scope_id=>4)].sql.must_equal 'SELECT * FROM items WHERE (scope_id = 4) ORDER BY scope_id, position'
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
it "should work correctly in subclasses" do
|
|
64
64
|
c = Class.new(klass(:scope=>:scope_id))
|
|
65
|
-
c.position_field.
|
|
66
|
-
c.scope_proc[c.new(:scope_id=>4)].sql.
|
|
65
|
+
c.position_field.must_equal :position
|
|
66
|
+
c.scope_proc[c.new(:scope_id=>4)].sql.must_equal 'SELECT * FROM items WHERE (scope_id = 4) ORDER BY scope_id, position'
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "should have at_position return the model object at the given position" do
|
|
70
70
|
@c.dataset._fetch = {:id=>1, :position=>1}
|
|
71
|
-
@o.at_position(10).
|
|
71
|
+
@o.at_position(10).must_equal @c.load(:id=>1, :position=>1)
|
|
72
72
|
@sc.dataset._fetch = {:id=>2, :position=>2, :scope_id=>5}
|
|
73
|
-
@so.at_position(20).
|
|
74
|
-
@db.sqls.
|
|
73
|
+
@so.at_position(20).must_equal @sc.load(:id=>2, :position=>2, :scope_id=>5)
|
|
74
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 10) ORDER BY position LIMIT 1",
|
|
75
75
|
"SELECT * FROM items WHERE ((scope_id = 5) AND (position = 20)) ORDER BY scope_id, position LIMIT 1"]
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
it "should have position field set to max+1 when creating if not already set" do
|
|
79
79
|
@c.instance_dataset._fetch = @c.dataset._fetch = [[{:pos=>nil}], [{:id=>1, :position=>1}], [{:pos=>1}], [{:id=>2, :position=>2}]]
|
|
80
80
|
@c.instance_dataset.autoid = @c.dataset.autoid = 1
|
|
81
|
-
@c.create.values.
|
|
82
|
-
@c.create.values.
|
|
83
|
-
@db.sqls.
|
|
81
|
+
@c.create.values.must_equal(:id=>1, :position=>1)
|
|
82
|
+
@c.create.values.must_equal(:id=>2, :position=>2)
|
|
83
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
84
84
|
"INSERT INTO items (position) VALUES (1)",
|
|
85
85
|
"SELECT * FROM items WHERE (id = 1) ORDER BY position LIMIT 1",
|
|
86
86
|
"SELECT max(position) AS max FROM items LIMIT 1",
|
|
@@ -91,14 +91,14 @@ describe "List plugin" do
|
|
|
91
91
|
it "should have position field set to max+1 in scope when creating if not already set" do
|
|
92
92
|
@sc.instance_dataset._fetch = @sc.dataset._fetch = [[{:pos=>nil}], [{:id=>1, :scope_id=>1, :position=>1}], [{:pos=>1}], [{:id=>2, :scope_id=>1, :position=>2}], [{:pos=>nil}], [{:id=>3, :scope_id=>2, :position=>1}]]
|
|
93
93
|
@sc.instance_dataset.autoid = @sc.dataset.autoid = 1
|
|
94
|
-
@sc.create(:scope_id=>1).values.
|
|
95
|
-
@sc.create(:scope_id=>1).values.
|
|
96
|
-
@sc.create(:scope_id=>2).values.
|
|
94
|
+
@sc.create(:scope_id=>1).values.must_equal(:id=>1, :scope_id=>1, :position=>1)
|
|
95
|
+
@sc.create(:scope_id=>1).values.must_equal(:id=>2, :scope_id=>1, :position=>2)
|
|
96
|
+
@sc.create(:scope_id=>2).values.must_equal(:id=>3, :scope_id=>2, :position=>1)
|
|
97
97
|
sqls = @db.sqls
|
|
98
|
-
sqls.slice!(7).
|
|
99
|
-
sqls.slice!(4).
|
|
100
|
-
sqls.slice!(1).
|
|
101
|
-
sqls.
|
|
98
|
+
sqls.slice!(7).must_match(/INSERT INTO items \((scope_id|position), (scope_id|position)\) VALUES \([12], [12]\)/)
|
|
99
|
+
sqls.slice!(4).must_match(/INSERT INTO items \((scope_id|position), (scope_id|position)\) VALUES \([12], [12]\)/)
|
|
100
|
+
sqls.slice!(1).must_match(/INSERT INTO items \((scope_id|position), (scope_id|position)\) VALUES \(1, 1\)/)
|
|
101
|
+
sqls.must_equal ["SELECT max(position) AS max FROM items WHERE (scope_id = 1) LIMIT 1",
|
|
102
102
|
"SELECT * FROM items WHERE (id = 1) ORDER BY scope_id, position LIMIT 1",
|
|
103
103
|
"SELECT max(position) AS max FROM items WHERE (scope_id = 1) LIMIT 1",
|
|
104
104
|
"SELECT * FROM items WHERE (id = 2) ORDER BY scope_id, position LIMIT 1",
|
|
@@ -108,61 +108,63 @@ describe "List plugin" do
|
|
|
108
108
|
|
|
109
109
|
it "should update positions automatically on deletion" do
|
|
110
110
|
@o.destroy
|
|
111
|
-
@db.sqls.
|
|
111
|
+
@db.sqls.must_equal ["DELETE FROM items WHERE (id = 7)", "UPDATE items SET position = (position - 1) WHERE (position > 3)"]
|
|
112
112
|
end
|
|
113
113
|
|
|
114
114
|
it "should have last_position return the last position in the list" do
|
|
115
115
|
@c.dataset._fetch = {:max=>10}
|
|
116
|
-
@o.last_position.
|
|
116
|
+
@o.last_position.must_equal 10
|
|
117
117
|
@sc.dataset._fetch = {:max=>20}
|
|
118
|
-
@so.last_position.
|
|
119
|
-
@db.sqls.
|
|
118
|
+
@so.last_position.must_equal 20
|
|
119
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
120
120
|
"SELECT max(position) AS max FROM items WHERE (scope_id = 5) LIMIT 1"]
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
it "should have list_dataset return the model's dataset for non scoped lists" do
|
|
124
|
-
@o.list_dataset.sql.
|
|
124
|
+
@o.list_dataset.sql.must_equal 'SELECT * FROM items ORDER BY position'
|
|
125
125
|
end
|
|
126
126
|
|
|
127
127
|
it "should have list dataset return a scoped dataset for scoped lists" do
|
|
128
|
-
@so.list_dataset.sql.
|
|
128
|
+
@so.list_dataset.sql.must_equal 'SELECT * FROM items WHERE (scope_id = 5) ORDER BY scope_id, position'
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
it "should have move_down without an argument move down a single position" do
|
|
132
132
|
@c.dataset._fetch = {:max=>10}
|
|
133
|
-
@o.move_down.
|
|
134
|
-
@o.position.
|
|
135
|
-
@db.sqls.
|
|
133
|
+
@o.move_down.must_equal @o
|
|
134
|
+
@o.position.must_equal 4
|
|
135
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
136
136
|
"UPDATE items SET position = (position - 1) WHERE ((position >= 4) AND (position <= 4))",
|
|
137
137
|
"UPDATE items SET position = 4 WHERE (id = 7)"]
|
|
138
138
|
end
|
|
139
139
|
|
|
140
140
|
it "should have move_down with an argument move down the given number of positions" do
|
|
141
141
|
@c.dataset._fetch = {:max=>10}
|
|
142
|
-
@o.move_down(3).
|
|
143
|
-
@o.position.
|
|
144
|
-
@db.sqls.
|
|
142
|
+
@o.move_down(3).must_equal @o
|
|
143
|
+
@o.position.must_equal 6
|
|
144
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
145
145
|
"UPDATE items SET position = (position - 1) WHERE ((position >= 4) AND (position <= 6))",
|
|
146
146
|
"UPDATE items SET position = 6 WHERE (id = 7)"]
|
|
147
147
|
end
|
|
148
148
|
|
|
149
149
|
it "should have move_down with a negative argument move up the given number of positions" do
|
|
150
|
-
@o.move_down(-1).
|
|
151
|
-
@o.position.
|
|
152
|
-
@db.sqls.
|
|
150
|
+
@o.move_down(-1).must_equal @o
|
|
151
|
+
@o.position.must_equal 2
|
|
152
|
+
@db.sqls.must_equal ["UPDATE items SET position = (position + 1) WHERE ((position >= 2) AND (position < 3))",
|
|
153
153
|
"UPDATE items SET position = 2 WHERE (id = 7)"]
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
-
it "should have move_to
|
|
157
|
-
|
|
156
|
+
it "should have move_to handle out of range targets" do
|
|
157
|
+
@o.move_to(0)
|
|
158
|
+
@o.position.must_equal 1
|
|
158
159
|
@c.dataset._fetch = {:max=>10}
|
|
159
|
-
|
|
160
|
+
@o.move_to(11)
|
|
161
|
+
@o.position.must_equal 10
|
|
160
162
|
end
|
|
161
163
|
|
|
162
164
|
it "should have move_to use a transaction if the instance is configured to use transactions" do
|
|
163
165
|
@o.use_transactions = true
|
|
164
166
|
@o.move_to(2)
|
|
165
|
-
@db.sqls.
|
|
167
|
+
@db.sqls.must_equal ["BEGIN",
|
|
166
168
|
"UPDATE items SET position = (position + 1) WHERE ((position >= 2) AND (position < 3))",
|
|
167
169
|
"UPDATE items SET position = 2 WHERE (id = 7)",
|
|
168
170
|
"COMMIT"]
|
|
@@ -170,96 +172,96 @@ describe "List plugin" do
|
|
|
170
172
|
|
|
171
173
|
it "should have move_to do nothing if the target position is the same as the current position" do
|
|
172
174
|
@o.use_transactions = true
|
|
173
|
-
@o.move_to(@o.position).
|
|
174
|
-
@o.position.
|
|
175
|
-
@db.sqls.
|
|
175
|
+
@o.move_to(@o.position).must_equal @o
|
|
176
|
+
@o.position.must_equal 3
|
|
177
|
+
@db.sqls.must_equal []
|
|
176
178
|
end
|
|
177
179
|
|
|
178
180
|
it "should have move to shift entries correctly between current and target if moving up" do
|
|
179
181
|
@o.move_to(2)
|
|
180
|
-
@db.sqls.first.
|
|
182
|
+
@db.sqls.first.must_equal "UPDATE items SET position = (position + 1) WHERE ((position >= 2) AND (position < 3))"
|
|
181
183
|
end
|
|
182
184
|
|
|
183
185
|
it "should have move to shift entries correctly between current and target if moving down" do
|
|
184
186
|
@c.dataset._fetch = {:max=>10}
|
|
185
187
|
@o.move_to(4)
|
|
186
|
-
@db.sqls[1].
|
|
188
|
+
@db.sqls[1].must_equal "UPDATE items SET position = (position - 1) WHERE ((position >= 4) AND (position <= 4))"
|
|
187
189
|
end
|
|
188
190
|
|
|
189
191
|
it "should have move_to_bottom move the item to the last position" do
|
|
190
192
|
@c.dataset._fetch = {:max=>10}
|
|
191
193
|
@o.move_to_bottom
|
|
192
|
-
@db.sqls.
|
|
194
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
193
195
|
"UPDATE items SET position = (position - 1) WHERE ((position >= 4) AND (position <= 10))",
|
|
194
196
|
"UPDATE items SET position = 10 WHERE (id = 7)"]
|
|
195
197
|
end
|
|
196
198
|
|
|
197
199
|
it "should have move_to_top move the item to the first position" do
|
|
198
200
|
@o.move_to_top
|
|
199
|
-
@db.sqls.
|
|
201
|
+
@db.sqls.must_equal ["UPDATE items SET position = (position + 1) WHERE ((position >= 1) AND (position < 3))",
|
|
200
202
|
"UPDATE items SET position = 1 WHERE (id = 7)"]
|
|
201
203
|
end
|
|
202
204
|
|
|
203
205
|
it "should have move_up without an argument move up a single position" do
|
|
204
|
-
@o.move_up.
|
|
205
|
-
@o.position.
|
|
206
|
-
@db.sqls.
|
|
206
|
+
@o.move_up.must_equal @o
|
|
207
|
+
@o.position.must_equal 2
|
|
208
|
+
@db.sqls.must_equal ["UPDATE items SET position = (position + 1) WHERE ((position >= 2) AND (position < 3))",
|
|
207
209
|
"UPDATE items SET position = 2 WHERE (id = 7)"]
|
|
208
210
|
end
|
|
209
211
|
|
|
210
212
|
it "should have move_up with an argument move up the given number of positions" do
|
|
211
|
-
@o.move_up(2).
|
|
212
|
-
@o.position.
|
|
213
|
-
@db.sqls.
|
|
213
|
+
@o.move_up(2).must_equal @o
|
|
214
|
+
@o.position.must_equal 1
|
|
215
|
+
@db.sqls.must_equal ["UPDATE items SET position = (position + 1) WHERE ((position >= 1) AND (position < 3))",
|
|
214
216
|
"UPDATE items SET position = 1 WHERE (id = 7)"]
|
|
215
217
|
end
|
|
216
218
|
|
|
217
219
|
it "should have move_up with a negative argument move down the given number of positions" do
|
|
218
220
|
@c.dataset._fetch = {:max=>10}
|
|
219
|
-
@o.move_up(-1).
|
|
220
|
-
@o.position.
|
|
221
|
-
@db.sqls.
|
|
221
|
+
@o.move_up(-1).must_equal @o
|
|
222
|
+
@o.position.must_equal 4
|
|
223
|
+
@db.sqls.must_equal ["SELECT max(position) AS max FROM items LIMIT 1",
|
|
222
224
|
"UPDATE items SET position = (position - 1) WHERE ((position >= 4) AND (position <= 4))",
|
|
223
225
|
"UPDATE items SET position = 4 WHERE (id = 7)"]
|
|
224
226
|
end
|
|
225
227
|
|
|
226
228
|
it "should have next return the next entry in the list if not given an argument" do
|
|
227
229
|
@c.dataset._fetch = {:id=>9, :position=>4}
|
|
228
|
-
@o.next.
|
|
229
|
-
@db.sqls.
|
|
230
|
+
@o.next.must_equal @c.load(:id=>9, :position=>4)
|
|
231
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 4) ORDER BY position LIMIT 1"]
|
|
230
232
|
end
|
|
231
233
|
|
|
232
234
|
it "should have next return the entry the given number of positions below the instance if given an argument" do
|
|
233
235
|
@c.dataset._fetch = {:id=>9, :position=>5}
|
|
234
|
-
@o.next(2).
|
|
235
|
-
@db.sqls.
|
|
236
|
+
@o.next(2).must_equal @c.load(:id=>9, :position=>5)
|
|
237
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 5) ORDER BY position LIMIT 1"]
|
|
236
238
|
end
|
|
237
239
|
|
|
238
240
|
it "should have next return a previous entry if given a negative argument" do
|
|
239
241
|
@c.dataset._fetch = {:id=>9, :position=>2}
|
|
240
|
-
@o.next(-1).
|
|
241
|
-
@db.sqls.
|
|
242
|
+
@o.next(-1).must_equal @c.load(:id=>9, :position=>2)
|
|
243
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 2) ORDER BY position LIMIT 1"]
|
|
242
244
|
end
|
|
243
245
|
|
|
244
246
|
it "should have position_value return the value of the position field" do
|
|
245
|
-
@o.position_value.
|
|
247
|
+
@o.position_value.must_equal 3
|
|
246
248
|
end
|
|
247
249
|
|
|
248
250
|
it "should have prev return the previous entry in the list if not given an argument" do
|
|
249
251
|
@c.dataset._fetch = {:id=>9, :position=>2}
|
|
250
|
-
@o.prev.
|
|
251
|
-
@db.sqls.
|
|
252
|
+
@o.prev.must_equal @c.load(:id=>9, :position=>2)
|
|
253
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 2) ORDER BY position LIMIT 1"]
|
|
252
254
|
end
|
|
253
255
|
|
|
254
256
|
it "should have prev return the entry the given number of positions above the instance if given an argument" do
|
|
255
257
|
@c.dataset._fetch = {:id=>9, :position=>1}
|
|
256
|
-
@o.prev(2).
|
|
257
|
-
@db.sqls.
|
|
258
|
+
@o.prev(2).must_equal @c.load(:id=>9, :position=>1)
|
|
259
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 1) ORDER BY position LIMIT 1"]
|
|
258
260
|
end
|
|
259
261
|
|
|
260
262
|
it "should have prev return a following entry if given a negative argument" do
|
|
261
263
|
@c.dataset._fetch = {:id=>9, :position=>4}
|
|
262
|
-
@o.prev(-1).
|
|
263
|
-
@db.sqls.
|
|
264
|
+
@o.prev(-1).must_equal @c.load(:id=>9, :position=>4)
|
|
265
|
+
@db.sqls.must_equal ["SELECT * FROM items WHERE (position = 4) ORDER BY position LIMIT 1"]
|
|
264
266
|
end
|
|
265
267
|
end
|
|
@@ -15,29 +15,29 @@ describe "LooserTypecasting Extension" do
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
@c.new(:b=>'a').b.
|
|
20
|
-
@c.new(:b=>'a').b.
|
|
18
|
+
it "should not raise errors for invalid strings in integer columns" do
|
|
19
|
+
@c.new(:b=>'a').b.must_equal 0
|
|
20
|
+
@c.new(:b=>'a').b.must_be_kind_of(Integer)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
@c.new(:z=>'a').z.
|
|
25
|
-
@c.new(:z=>'a').z.
|
|
23
|
+
it "should not raise errors for invalid strings in float columns" do
|
|
24
|
+
@c.new(:z=>'a').z.must_equal 0.0
|
|
25
|
+
@c.new(:z=>'a').z.must_be_kind_of(Float)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
@c.new(:s=>'a').s.
|
|
30
|
-
@c.new(:s=>[]).s.
|
|
31
|
-
@c.new(:s=>{}).s.
|
|
28
|
+
it "should not raise errors for hash or array input to string columns" do
|
|
29
|
+
@c.new(:s=>'a').s.must_equal 'a'
|
|
30
|
+
@c.new(:s=>[]).s.must_be_kind_of(String)
|
|
31
|
+
@c.new(:s=>{}).s.must_be_kind_of(String)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
@c.new(:d=>'a').d.
|
|
36
|
-
@c.new(:d=>'a').d.
|
|
34
|
+
it "should not raise errors for invalid strings in decimal columns" do
|
|
35
|
+
@c.new(:d=>'a').d.must_equal 0.0
|
|
36
|
+
@c.new(:d=>'a').d.must_be_kind_of(BigDecimal)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
@c.new(:d=>1).d.
|
|
41
|
-
@c.new(:d=>1).d.
|
|
39
|
+
it "should not affect conversions of other types in decimal columns" do
|
|
40
|
+
@c.new(:d=>1).d.must_equal 1
|
|
41
|
+
@c.new(:d=>1).d.must_be_kind_of(BigDecimal)
|
|
42
42
|
end
|
|
43
43
|
end
|
|
@@ -23,16 +23,16 @@ describe Sequel::Model, "many_through_many" do
|
|
|
23
23
|
|
|
24
24
|
it "should raise an error if current class does not have a primary key, and :left_primary_key is not specified" do
|
|
25
25
|
@c1.no_primary_key
|
|
26
|
-
proc{@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]}.
|
|
27
|
-
DB.sqls.
|
|
26
|
+
proc{@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]}.must_raise(Sequel::Error)
|
|
27
|
+
DB.sqls.must_equal []
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it "should raise an error if associated class does not have a primary key, and :right_primary_key is not specified" do
|
|
31
31
|
@c2.no_primary_key
|
|
32
32
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
33
33
|
n = @c1.load(:id => 1234)
|
|
34
|
-
proc{n.tags}.
|
|
35
|
-
DB.sqls.
|
|
34
|
+
proc{n.tags}.must_raise(Sequel::Error)
|
|
35
|
+
DB.sqls.must_equal []
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "should populate :key_hash and :id_map option correctly for custom eager loaders" do
|
|
@@ -40,12 +40,12 @@ describe Sequel::Model, "many_through_many" do
|
|
|
40
40
|
pr = proc{|h| khs << [h[:key_hash], h[:id_map]]}
|
|
41
41
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_loader=>pr
|
|
42
42
|
@c1.eager(:tags).all
|
|
43
|
-
khs.
|
|
43
|
+
khs.must_equal [[{:id=>{1=>[Artist.load(:x=>1, :id=>1)]}}, {1=>[Artist.load(:x=>1, :id=>1)]}]]
|
|
44
44
|
|
|
45
45
|
khs.clear
|
|
46
46
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:id, :left_primary_key_column=>:i, :eager_loader=>pr
|
|
47
47
|
@c1.eager(:tags).all
|
|
48
|
-
khs.
|
|
48
|
+
khs.must_equal [[{:id=>{1=>[Artist.load(:x=>1, :id=>1)]}}, {1=>[Artist.load(:x=>1, :id=>1)]}]]
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
it "should support using a custom :left_primary_key option when eager loading many_to_many associations" do
|
|
@@ -54,10 +54,10 @@ describe Sequel::Model, "many_through_many" do
|
|
|
54
54
|
@c2.dataset._fetch = {:id=>4, :x_foreign_key_x=>3}
|
|
55
55
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:id3
|
|
56
56
|
a = @c1.eager(:tags).all
|
|
57
|
-
a.
|
|
58
|
-
DB.sqls.
|
|
59
|
-
a.first.tags.
|
|
60
|
-
DB.sqls.
|
|
57
|
+
a.must_equal [@c1.load(:id => 1)]
|
|
58
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (3))"]
|
|
59
|
+
a.first.tags.must_equal [@c2.load(:id=>4)]
|
|
60
|
+
DB.sqls.must_equal []
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
it "should handle a :eager_loading_predicate_key option to change the SQL used in the lookup" do
|
|
@@ -65,22 +65,22 @@ describe Sequel::Model, "many_through_many" do
|
|
|
65
65
|
@c2.dataset._fetch = {:id=>4, :x_foreign_key_x=>1}
|
|
66
66
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_loading_predicate_key=>Sequel./(:albums_artists__artist_id, 3)
|
|
67
67
|
a = @c1.eager(:tags).all
|
|
68
|
-
a.
|
|
69
|
-
DB.sqls.
|
|
70
|
-
a.first.tags.
|
|
68
|
+
a.must_equal [@c1.load(:id => 1)]
|
|
69
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT tags.*, (albums_artists.artist_id / 3) AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id / 3) IN (1))"]
|
|
70
|
+
a.first.tags.must_equal [@c2.load(:id=>4)]
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
it "should handle schema qualified tables" do
|
|
74
74
|
@c1.many_through_many :tags, :through=>[[:myschema__albums_artists, :artist_id, :album_id], [:myschema__albums, :id, :id], [:myschema__albums_tags, :album_id, :tag_id]]
|
|
75
|
-
@c1.load(:id=>1).tags_dataset.sql.
|
|
75
|
+
@c1.load(:id=>1).tags_dataset.sql.must_equal "SELECT tags.* FROM tags INNER JOIN myschema.albums_tags ON (myschema.albums_tags.tag_id = tags.id) INNER JOIN myschema.albums ON (myschema.albums.id = myschema.albums_tags.album_id) INNER JOIN myschema.albums_artists ON (myschema.albums_artists.album_id = myschema.albums.id) WHERE (myschema.albums_artists.artist_id = 1)"
|
|
76
76
|
|
|
77
77
|
@c1.dataset._fetch = {:id=>1}
|
|
78
78
|
@c2.dataset._fetch = {:id=>4, :x_foreign_key_x=>1}
|
|
79
79
|
a = @c1.eager(:tags).all
|
|
80
|
-
a.
|
|
81
|
-
DB.sqls.
|
|
80
|
+
a.must_equal [@c1.load(:id => 1)]
|
|
81
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT tags.*, myschema.albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN myschema.albums_tags ON (myschema.albums_tags.tag_id = tags.id) INNER JOIN myschema.albums ON (myschema.albums.id = myschema.albums_tags.album_id) INNER JOIN myschema.albums_artists ON (myschema.albums_artists.album_id = myschema.albums.id) WHERE (myschema.albums_artists.artist_id IN (1))"]
|
|
82
82
|
|
|
83
|
-
@c1.eager_graph(:tags).sql.
|
|
83
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id, tags.h1, tags.h2 FROM artists LEFT OUTER JOIN myschema.albums_artists AS albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN myschema.albums AS albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN myschema.albums_tags AS albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
84
84
|
end
|
|
85
85
|
|
|
86
86
|
it "should default to associating to other models in the same scope" do
|
|
@@ -94,38 +94,38 @@ describe Sequel::Model, "many_through_many" do
|
|
|
94
94
|
end
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
-
::AssociationModuleTest::Artist.association_reflection(:tags).associated_class.
|
|
97
|
+
::AssociationModuleTest::Artist.association_reflection(:tags).associated_class.must_equal ::AssociationModuleTest::Tag
|
|
98
98
|
ensure
|
|
99
99
|
Object.send(:remove_const, :AssociationModuleTest)
|
|
100
100
|
end
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
it "should raise an error if in invalid form of through is used" do
|
|
104
|
-
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id]]}.
|
|
105
|
-
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], {:table=>:album_tags, :left=>:album_id}]}.
|
|
106
|
-
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], :album_tags]}.
|
|
104
|
+
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id]]}.must_raise(Sequel::Error)
|
|
105
|
+
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], {:table=>:album_tags, :left=>:album_id}]}.must_raise(Sequel::Error)
|
|
106
|
+
proc{@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], :album_tags]}.must_raise(Sequel::Error)
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
it "should allow only two arguments with the :through option" do
|
|
110
110
|
@c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
111
111
|
n = @c1.load(:id => 1234)
|
|
112
|
-
n.tags_dataset.sql.
|
|
113
|
-
n.tags.
|
|
112
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
113
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
it "should be clonable" do
|
|
117
117
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
118
118
|
@c1.many_through_many :other_tags, :clone=>:tags
|
|
119
119
|
n = @c1.load(:id => 1234)
|
|
120
|
-
n.other_tags_dataset.sql.
|
|
121
|
-
n.tags.
|
|
120
|
+
n.other_tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
121
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
122
122
|
end
|
|
123
123
|
|
|
124
124
|
it "should use join tables given" do
|
|
125
125
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
126
126
|
n = @c1.load(:id => 1234)
|
|
127
|
-
n.tags_dataset.sql.
|
|
128
|
-
n.tags.
|
|
127
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
128
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
it "should handle multiple aliasing of tables" do
|
|
@@ -134,8 +134,8 @@ describe Sequel::Model, "many_through_many" do
|
|
|
134
134
|
end
|
|
135
135
|
@c1.many_through_many :albums, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id], [:artists, :id, :id], [:albums_artists, :artist_id, :album_id]]
|
|
136
136
|
n = @c1.load(:id => 1234)
|
|
137
|
-
n.albums_dataset.sql.
|
|
138
|
-
n.albums.
|
|
137
|
+
n.albums_dataset.sql.must_equal 'SELECT albums.* FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) INNER JOIN artists ON (artists.id = albums_artists.artist_id) INNER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) INNER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) INNER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) WHERE (albums_artists_1.artist_id = 1234)'
|
|
138
|
+
n.albums.must_equal [Album.load(:id=>1, :x=>1)]
|
|
139
139
|
ensure
|
|
140
140
|
Object.send(:remove_const, :Album)
|
|
141
141
|
end
|
|
@@ -144,316 +144,316 @@ describe Sequel::Model, "many_through_many" do
|
|
|
144
144
|
it "should use explicit class if given" do
|
|
145
145
|
@c1.many_through_many :albums_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag
|
|
146
146
|
n = @c1.load(:id => 1234)
|
|
147
|
-
n.albums_tags_dataset.sql.
|
|
148
|
-
n.albums_tags.
|
|
147
|
+
n.albums_tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
148
|
+
n.albums_tags.must_equal [@c2.load(:id=>1)]
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
it "should accept :left_primary_key and :right_primary_key option for primary keys to use in current and associated table" do
|
|
152
152
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :right_primary_key=>:tag_id, :left_primary_key=>:yyy
|
|
153
153
|
n = @c1.load(:id => 1234)
|
|
154
154
|
n.yyy = 85
|
|
155
|
-
n.tags_dataset.sql.
|
|
156
|
-
n.tags.
|
|
155
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.tag_id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 85)'
|
|
156
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
157
157
|
end
|
|
158
158
|
|
|
159
159
|
it "should handle composite keys" do
|
|
160
160
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
161
161
|
n = @c1.load(:id => 1234)
|
|
162
162
|
n.yyy = 85
|
|
163
|
-
n.tags_dataset.sql.
|
|
164
|
-
n.tags.
|
|
163
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1 = 1234) AND (albums_artists.b2 = 85))'
|
|
164
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
165
165
|
end
|
|
166
166
|
|
|
167
167
|
it "should allowing filtering by many_through_many associations" do
|
|
168
168
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
169
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
169
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
170
170
|
end
|
|
171
171
|
|
|
172
172
|
it "should allowing filtering by many_through_many associations with a single through table" do
|
|
173
173
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id]]
|
|
174
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
174
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists WHERE ((albums_artists.album_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
175
175
|
end
|
|
176
176
|
|
|
177
177
|
it "should allowing filtering by many_through_many associations with aliased tables" do
|
|
178
178
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums_artists, :id, :id], [:albums_artists, :album_id, :tag_id]]
|
|
179
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
179
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.id = albums_artists.album_id) INNER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_artists_0.id) WHERE ((albums_artists_1.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
180
180
|
end
|
|
181
181
|
|
|
182
182
|
it "should allowing filtering by many_through_many associations with composite keys" do
|
|
183
183
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
184
|
-
@c1.filter(:tags=>@c2.load(:h1=>1234, :h2=>85)).sql.
|
|
184
|
+
@c1.filter(:tags=>@c2.load(:h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE ((albums_tags.g1 = 1234) AND (albums_tags.g2 = 85) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
185
185
|
end
|
|
186
186
|
|
|
187
187
|
it "should allowing filtering by many_through_many associations with :conditions" do
|
|
188
188
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
189
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
189
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234))))"
|
|
190
190
|
end
|
|
191
191
|
|
|
192
192
|
it "should allowing filtering by many_through_many associations with :conditions with a single through table" do
|
|
193
193
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id]], :conditions=>{:name=>'A'}
|
|
194
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
194
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_artists ON (albums_artists.album_id = tags.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234))))"
|
|
195
195
|
end
|
|
196
196
|
|
|
197
197
|
it "should allowing filtering by many_through_many associations with :conditions and composite keys" do
|
|
198
198
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
199
|
-
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
199
|
+
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id = 1))))"
|
|
200
200
|
end
|
|
201
201
|
|
|
202
202
|
it "should allowing filtering by many_through_many associations with :limit" do
|
|
203
203
|
def (@c2.dataset).supports_window_functions?; true end
|
|
204
204
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :limit=>10
|
|
205
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
205
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id IS NOT NULL) AND ((albums_artists.artist_id, tags.id) IN (SELECT b, c FROM (SELECT albums_artists.artist_id AS b, tags.id AS c, row_number() OVER (PARTITION BY albums_artists.artist_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id)) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 1234))))'
|
|
206
206
|
end
|
|
207
207
|
|
|
208
208
|
it "should allowing filtering by many_through_many associations with :limit and composite keys" do
|
|
209
209
|
def (@c2.dataset).supports_window_functions?; true end
|
|
210
210
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :limit=>10
|
|
211
|
-
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
211
|
+
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND ((albums_artists.b1, albums_artists.b2, tags.id) IN (SELECT b, c, d FROM (SELECT albums_artists.b1 AS b, albums_artists.b2 AS c, tags.id AS d, row_number() OVER (PARTITION BY albums_artists.b1, albums_artists.b2) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2))) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 1))))'
|
|
212
212
|
end
|
|
213
213
|
|
|
214
214
|
it "should allowing filtering by many_through_many associations with :limit and :conditions" do
|
|
215
215
|
def (@c2.dataset).supports_window_functions?; true end
|
|
216
216
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}, :limit=>10
|
|
217
|
-
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.
|
|
217
|
+
@c1.filter(:tags=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND ((albums_artists.artist_id, tags.id) IN (SELECT b, c FROM (SELECT albums_artists.artist_id AS b, tags.id AS c, row_number() OVER (PARTITION BY albums_artists.artist_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 1234))))"
|
|
218
218
|
end
|
|
219
219
|
|
|
220
220
|
it "should allowing filtering by many_through_many associations with :limit and :conditions and composite keys" do
|
|
221
221
|
def (@c2.dataset).supports_window_functions?; true end
|
|
222
222
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}, :limit=>10
|
|
223
|
-
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
223
|
+
@c1.filter(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND ((albums_artists.b1, albums_artists.b2, tags.id) IN (SELECT b, c, d FROM (SELECT albums_artists.b1 AS b, albums_artists.b2 AS c, tags.id AS d, row_number() OVER (PARTITION BY albums_artists.b1, albums_artists.b2) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 1))))"
|
|
224
224
|
end
|
|
225
225
|
|
|
226
226
|
it "should allowing excluding by many_through_many associations" do
|
|
227
227
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
228
|
-
@c1.exclude(:tags=>@c2.load(:id=>1234)).sql.
|
|
228
|
+
@c1.exclude(:tags=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
229
229
|
end
|
|
230
230
|
|
|
231
231
|
it "should allowing excluding by many_through_many associations with composite keys" do
|
|
232
232
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
233
|
-
@c1.exclude(:tags=>@c2.load(:h1=>1234, :h2=>85)).sql.
|
|
233
|
+
@c1.exclude(:tags=>@c2.load(:h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE ((albums_tags.g1 = 1234) AND (albums_tags.g2 = 85) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
234
234
|
end
|
|
235
235
|
|
|
236
236
|
it "should allowing excluding by many_through_many associations with :conditions" do
|
|
237
237
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
238
|
-
@c1.exclude(:tags=>@c2.load(:id=>1234)).sql.
|
|
238
|
+
@c1.exclude(:tags=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234)))) OR (artists.id IS NULL))"
|
|
239
239
|
end
|
|
240
240
|
|
|
241
241
|
it "should allowing excluding by many_through_many associations with :conditions and composite keys" do
|
|
242
242
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
243
|
-
@c1.exclude(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
243
|
+
@c1.exclude(:tags=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id = 1)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
244
244
|
end
|
|
245
245
|
|
|
246
246
|
it "should allowing filtering by multiple many_through_many associations" do
|
|
247
247
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
248
|
-
@c1.filter(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
248
|
+
@c1.filter(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (1234, 2345)) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
249
249
|
end
|
|
250
250
|
|
|
251
251
|
it "should allowing filtering by multiple many_through_many associations with composite keys" do
|
|
252
252
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
253
|
-
@c1.filter(:tags=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.
|
|
253
|
+
@c1.filter(:tags=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN ((1234, 85), (2345, 95))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
254
254
|
end
|
|
255
255
|
|
|
256
256
|
it "should allowing filtering by multiple many_through_many associations with :conditions" do
|
|
257
257
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
258
|
-
@c1.filter(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
258
|
+
@c1.filter(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (1234, 2345)))))"
|
|
259
259
|
end
|
|
260
260
|
|
|
261
261
|
it "should allowing filtering by multiple many_through_many associations with :conditions and composite keys" do
|
|
262
262
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
263
|
-
@c1.filter(:tags=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.
|
|
263
|
+
@c1.filter(:tags=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (1, 2)))))"
|
|
264
264
|
end
|
|
265
265
|
|
|
266
266
|
it "should allowing excluding by multiple many_through_many associations" do
|
|
267
267
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
268
|
-
@c1.exclude(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
268
|
+
@c1.exclude(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (1234, 2345)) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
269
269
|
end
|
|
270
270
|
|
|
271
271
|
it "should allowing excluding by multiple many_through_many associations with composite keys" do
|
|
272
272
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
273
|
-
@c1.exclude(:tags=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.
|
|
273
|
+
@c1.exclude(:tags=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN ((1234, 85), (2345, 95))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
274
274
|
end
|
|
275
275
|
|
|
276
276
|
it "should allowing excluding by multiple many_through_many associations with :conditions" do
|
|
277
277
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
278
|
-
@c1.exclude(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
278
|
+
@c1.exclude(:tags=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (1234, 2345))))) OR (artists.id IS NULL))"
|
|
279
279
|
end
|
|
280
280
|
|
|
281
281
|
it "should allowing excluding by multiple many_through_many associations with :conditions and composite keys" do
|
|
282
282
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
283
|
-
@c1.exclude(:tags=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.
|
|
283
|
+
@c1.exclude(:tags=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (1, 2))))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
284
284
|
end
|
|
285
285
|
|
|
286
286
|
it "should allowing filtering/excluding many_through_many associations with NULL values" do
|
|
287
287
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
288
|
-
@c1.filter(:tags=>@c2.new).sql.
|
|
289
|
-
@c1.exclude(:tags=>@c2.new).sql.
|
|
288
|
+
@c1.filter(:tags=>@c2.new).sql.must_equal 'SELECT * FROM artists WHERE \'f\''
|
|
289
|
+
@c1.exclude(:tags=>@c2.new).sql.must_equal 'SELECT * FROM artists WHERE \'t\''
|
|
290
290
|
end
|
|
291
291
|
|
|
292
292
|
it "should allowing filtering by many_through_many association datasets" do
|
|
293
293
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
294
|
-
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.
|
|
294
|
+
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
295
295
|
end
|
|
296
296
|
|
|
297
297
|
it "should allowing filtering by many_through_many association datasets with composite keys" do
|
|
298
298
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
299
|
-
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.
|
|
299
|
+
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN (SELECT tags.h1, tags.h2 FROM tags WHERE ((x = 1) AND (tags.h1 IS NOT NULL) AND (tags.h2 IS NOT NULL)))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
300
300
|
end
|
|
301
301
|
|
|
302
302
|
it "should allowing filtering by many_through_many association datasets with :conditions" do
|
|
303
303
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
304
|
-
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.
|
|
304
|
+
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
|
|
305
305
|
end
|
|
306
306
|
|
|
307
307
|
it "should allowing filtering by many_through_many association datasets with :conditions and composite keys" do
|
|
308
308
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
309
|
-
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.
|
|
309
|
+
@c1.filter(:tags=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
|
|
310
310
|
end
|
|
311
311
|
|
|
312
312
|
it "should allowing excluding by many_through_many association datasets" do
|
|
313
313
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
314
|
-
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.
|
|
314
|
+
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
315
315
|
end
|
|
316
316
|
|
|
317
317
|
it "should allowing excluding by many_through_many association datasets with composite keys" do
|
|
318
318
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
319
|
-
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.
|
|
319
|
+
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN (SELECT tags.h1, tags.h2 FROM tags WHERE ((x = 1) AND (tags.h1 IS NOT NULL) AND (tags.h2 IS NOT NULL)))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
320
320
|
end
|
|
321
321
|
|
|
322
322
|
it "should allowing excluding by many_through_many association datasets with :conditions" do
|
|
323
323
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
324
|
-
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.
|
|
324
|
+
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (artists.id IS NULL))"
|
|
325
325
|
end
|
|
326
326
|
|
|
327
327
|
it "should allowing excluding by many_through_many association datasets with :conditions and composite keys" do
|
|
328
328
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
329
|
-
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.
|
|
329
|
+
@c1.exclude(:tags=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
330
330
|
end
|
|
331
331
|
|
|
332
332
|
it "should support a :conditions option" do
|
|
333
333
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
334
334
|
n = @c1.load(:id => 1234)
|
|
335
|
-
n.tags_dataset.sql.
|
|
336
|
-
n.tags.
|
|
335
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 32) AND (albums_artists.artist_id = 1234))'
|
|
336
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
337
337
|
|
|
338
338
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>['a = ?', 42]
|
|
339
339
|
n = @c1.load(:id => 1234)
|
|
340
|
-
n.tags_dataset.sql.
|
|
341
|
-
n.tags.
|
|
340
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 42) AND (albums_artists.artist_id = 1234))'
|
|
341
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
342
342
|
end
|
|
343
343
|
|
|
344
344
|
it "should support an :order option" do
|
|
345
345
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah
|
|
346
346
|
n = @c1.load(:id => 1234)
|
|
347
|
-
n.tags_dataset.sql.
|
|
348
|
-
n.tags.
|
|
347
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) ORDER BY blah'
|
|
348
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
349
349
|
end
|
|
350
350
|
|
|
351
351
|
it "should support an array for the :order option" do
|
|
352
352
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
353
353
|
n = @c1.load(:id => 1234)
|
|
354
|
-
n.tags_dataset.sql.
|
|
355
|
-
n.tags.
|
|
354
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) ORDER BY blah1, blah2'
|
|
355
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
356
356
|
end
|
|
357
357
|
|
|
358
358
|
it "should support a select option" do
|
|
359
359
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>:blah
|
|
360
360
|
n = @c1.load(:id => 1234)
|
|
361
|
-
n.tags_dataset.sql.
|
|
362
|
-
n.tags.
|
|
361
|
+
n.tags_dataset.sql.must_equal 'SELECT blah FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
362
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
363
363
|
end
|
|
364
364
|
|
|
365
365
|
it "should support an array for the select option" do
|
|
366
366
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>[Sequel::SQL::ColumnAll.new(:tags), :albums__name]
|
|
367
367
|
n = @c1.load(:id => 1234)
|
|
368
|
-
n.tags_dataset.sql.
|
|
369
|
-
n.tags.
|
|
368
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.*, albums.name FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)'
|
|
369
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
370
370
|
end
|
|
371
371
|
|
|
372
372
|
it "should accept a block" do
|
|
373
373
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]] do |ds| ds.filter(:yyy=>@yyy) end
|
|
374
374
|
n = @c1.load(:id => 1234)
|
|
375
375
|
n.yyy = 85
|
|
376
|
-
n.tags_dataset.sql.
|
|
377
|
-
n.tags.
|
|
376
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id = 1234) AND (yyy = 85))'
|
|
377
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
378
378
|
end
|
|
379
379
|
|
|
380
380
|
it "should allow the :order option while accepting a block" do
|
|
381
381
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah do |ds| ds.filter(:yyy=>@yyy) end
|
|
382
382
|
n = @c1.load(:id => 1234)
|
|
383
383
|
n.yyy = 85
|
|
384
|
-
n.tags_dataset.sql.
|
|
385
|
-
n.tags.
|
|
384
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id = 1234) AND (yyy = 85)) ORDER BY blah'
|
|
385
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
386
386
|
end
|
|
387
387
|
|
|
388
388
|
it "should support a :dataset option that is used instead of the default" do
|
|
389
389
|
@c1.many_through_many :tags, [[:a, :b, :c]], :dataset=>proc{Tag.join(:albums_tags, [:tag_id]).join(:albums, [:album_id]).join(:albums_artists, [:album_id]).filter(:albums_artists__artist_id=>id)}
|
|
390
390
|
n = @c1.load(:id => 1234)
|
|
391
|
-
n.tags_dataset.sql.
|
|
392
|
-
n.tags.
|
|
391
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags USING (tag_id) INNER JOIN albums USING (album_id) INNER JOIN albums_artists USING (album_id) WHERE (albums_artists.artist_id = 1234)'
|
|
392
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
393
393
|
end
|
|
394
394
|
|
|
395
395
|
it "should support a :limit option" do
|
|
396
396
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :limit=>10
|
|
397
397
|
n = @c1.load(:id => 1234)
|
|
398
|
-
n.tags_dataset.sql.
|
|
399
|
-
n.tags.
|
|
398
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 10'
|
|
399
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
400
400
|
|
|
401
401
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :limit=>[10, 10]
|
|
402
402
|
n = @c1.load(:id => 1234)
|
|
403
|
-
n.tags_dataset.sql.
|
|
404
|
-
n.tags.
|
|
403
|
+
n.tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 10 OFFSET 10'
|
|
404
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
405
405
|
end
|
|
406
406
|
|
|
407
407
|
it "should have the :eager option affect the _dataset method" do
|
|
408
408
|
@c2.many_to_many :fans
|
|
409
409
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:fans
|
|
410
|
-
@c1.load(:id => 1234).tags_dataset.opts[:eager].
|
|
410
|
+
@c1.load(:id => 1234).tags_dataset.opts[:eager].must_equal(:fans=>nil)
|
|
411
411
|
end
|
|
412
412
|
|
|
413
413
|
it "should provide an array with all members of the association" do
|
|
414
414
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
415
|
-
@c1.load(:id => 1234).tags.
|
|
416
|
-
DB.sqls.
|
|
415
|
+
@c1.load(:id => 1234).tags.must_equal [@c2.load(:id=>1)]
|
|
416
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)']
|
|
417
417
|
end
|
|
418
418
|
|
|
419
419
|
it "should populate cache when accessed" do
|
|
420
420
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
421
421
|
n = @c1.load(:id => 1234)
|
|
422
|
-
n.associations[:tags].
|
|
423
|
-
DB.sqls.
|
|
424
|
-
n.tags.
|
|
425
|
-
DB.sqls.
|
|
426
|
-
n.associations[:tags].
|
|
427
|
-
DB.sqls.length.
|
|
422
|
+
n.associations[:tags].must_equal nil
|
|
423
|
+
DB.sqls.must_equal []
|
|
424
|
+
n.tags.must_equal [@c2.load(:id=>1)]
|
|
425
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)']
|
|
426
|
+
n.associations[:tags].must_equal n.tags
|
|
427
|
+
DB.sqls.length.must_equal 0
|
|
428
428
|
end
|
|
429
429
|
|
|
430
430
|
it "should use cache if available" do
|
|
431
431
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
432
432
|
n = @c1.load(:id => 1234)
|
|
433
433
|
n.associations[:tags] = []
|
|
434
|
-
n.tags.
|
|
435
|
-
DB.sqls.
|
|
434
|
+
n.tags.must_equal []
|
|
435
|
+
DB.sqls.must_equal []
|
|
436
436
|
end
|
|
437
437
|
|
|
438
438
|
it "should not use cache if asked to reload" do
|
|
439
439
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
440
440
|
n = @c1.load(:id => 1234)
|
|
441
441
|
n.associations[:tags] = []
|
|
442
|
-
DB.sqls.
|
|
443
|
-
n.tags(true).
|
|
444
|
-
DB.sqls.
|
|
445
|
-
n.associations[:tags].
|
|
446
|
-
DB.sqls.length.
|
|
442
|
+
DB.sqls.must_equal []
|
|
443
|
+
n.tags(true).must_equal [@c2.load(:id=>1)]
|
|
444
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234)']
|
|
445
|
+
n.associations[:tags].must_equal n.tags
|
|
446
|
+
DB.sqls.length.must_equal 0
|
|
447
447
|
end
|
|
448
448
|
|
|
449
449
|
it "should not add associations methods directly to class" do
|
|
450
450
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
451
451
|
im = @c1.instance_methods.collect{|x| x.to_s}
|
|
452
|
-
im.
|
|
453
|
-
im.
|
|
452
|
+
im.must_include('tags')
|
|
453
|
+
im.must_include('tags_dataset')
|
|
454
454
|
im2 = @c1.instance_methods(false).collect{|x| x.to_s}
|
|
455
|
-
im2.
|
|
456
|
-
im2.
|
|
455
|
+
im2.wont_include('tags')
|
|
456
|
+
im2.wont_include('tags_dataset')
|
|
457
457
|
end
|
|
458
458
|
|
|
459
459
|
it "should support after_load association callback" do
|
|
@@ -468,14 +468,14 @@ describe Sequel::Model, "many_through_many" do
|
|
|
468
468
|
@c2.dataset._fetch = [{:id=>20}, {:id=>30}]
|
|
469
469
|
p = @c1.load(:id=>10, :parent_id=>20)
|
|
470
470
|
p.tags
|
|
471
|
-
h.
|
|
472
|
-
p.tags.collect{|a| a.pk}.
|
|
471
|
+
h.must_equal [400, 600]
|
|
472
|
+
p.tags.collect{|a| a.pk}.must_equal [20, 30]
|
|
473
473
|
end
|
|
474
474
|
|
|
475
475
|
it "should support a :uniq option that removes duplicates from the association" do
|
|
476
476
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :uniq=>true
|
|
477
477
|
@c2.dataset._fetch = [{:id=>20}, {:id=>30}, {:id=>20}, {:id=>30}]
|
|
478
|
-
@c1.load(:id=>10).tags.
|
|
478
|
+
@c1.load(:id=>10).tags.must_equal [@c2.load(:id=>20), @c2.load(:id=>30)]
|
|
479
479
|
end
|
|
480
480
|
end
|
|
481
481
|
|
|
@@ -496,25 +496,25 @@ describe 'Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection
|
|
|
496
496
|
end
|
|
497
497
|
|
|
498
498
|
it "#edges should be an array of joins to make when eager graphing" do
|
|
499
|
-
@ar.edges.
|
|
499
|
+
@ar.edges.must_equal [{:conditions=>[], :left=>:id, :right=>:artist_id, :table=>:albums_artists, :join_type=>:left_outer, :block=>nil}, {:conditions=>[], :left=>:album_id, :right=>:id, :table=>:albums, :join_type=>:left_outer, :block=>nil}, {:conditions=>[], :left=>:id, :right=>:album_id, :table=>:albums_tags, :join_type=>:left_outer, :block=>nil}]
|
|
500
500
|
end
|
|
501
501
|
|
|
502
502
|
it "#edges should handle composite keys" do
|
|
503
503
|
Artist.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
504
|
-
Artist.association_reflection(:tags).edges.
|
|
504
|
+
Artist.association_reflection(:tags).edges.must_equal [{:conditions=>[], :left=>[:id, :yyy], :right=>[:b1, :b2], :table=>:albums_artists, :join_type=>:left_outer, :block=>nil}, {:conditions=>[], :left=>[:c1, :c2], :right=>[:d1, :d2], :table=>:albums, :join_type=>:left_outer, :block=>nil}, {:conditions=>[], :left=>[:e1, :e2], :right=>[:f1, :f2], :table=>:albums_tags, :join_type=>:left_outer, :block=>nil}]
|
|
505
505
|
end
|
|
506
506
|
|
|
507
507
|
it "#reverse_edges should be an array of joins to make when lazy loading or eager loading" do
|
|
508
|
-
@ar.reverse_edges.
|
|
508
|
+
@ar.reverse_edges.must_equal [{:alias=>:albums_tags, :left=>:tag_id, :right=>:id, :table=>:albums_tags}, {:alias=>:albums, :left=>:id, :right=>:album_id, :table=>:albums}]
|
|
509
509
|
end
|
|
510
510
|
|
|
511
511
|
it "#reverse_edges should handle composite keys" do
|
|
512
512
|
Artist.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
513
|
-
Artist.association_reflection(:tags).reverse_edges.
|
|
513
|
+
Artist.association_reflection(:tags).reverse_edges.must_equal [{:alias=>:albums_tags, :left=>[:g1, :g2], :right=>[:h1, :h2], :table=>:albums_tags}, {:alias=>:albums, :left=>[:e1, :e2], :right=>[:f1, :f2], :table=>:albums}]
|
|
514
514
|
end
|
|
515
515
|
|
|
516
516
|
it "#reciprocal should be nil" do
|
|
517
|
-
@ar.reciprocal.
|
|
517
|
+
@ar.reciprocal.must_equal nil
|
|
518
518
|
end
|
|
519
519
|
end
|
|
520
520
|
|
|
@@ -579,78 +579,78 @@ describe "many_through_many eager loading methods" do
|
|
|
579
579
|
|
|
580
580
|
it "should eagerly load a single many_through_many association" do
|
|
581
581
|
a = @c1.eager(:tags).all
|
|
582
|
-
a.
|
|
583
|
-
DB.sqls.
|
|
584
|
-
a.first.tags.
|
|
585
|
-
DB.sqls.length.
|
|
582
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
583
|
+
DB.sqls.must_equal ['SELECT * FROM artists', 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
584
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
585
|
+
DB.sqls.length.must_equal 0
|
|
586
586
|
end
|
|
587
587
|
|
|
588
588
|
it "should eagerly load multiple associations in a single call" do
|
|
589
589
|
a = @c1.eager(:tags, :albums).all
|
|
590
|
-
a.
|
|
590
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
591
591
|
sqls = DB.sqls
|
|
592
|
-
sqls.length.
|
|
593
|
-
sqls[0].
|
|
594
|
-
sqls[1..-1].
|
|
595
|
-
sqls[1..-1].
|
|
592
|
+
sqls.length.must_equal 3
|
|
593
|
+
sqls[0].must_equal 'SELECT * FROM artists'
|
|
594
|
+
sqls[1..-1].must_include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
595
|
+
sqls[1..-1].must_include('SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
596
596
|
a = a.first
|
|
597
|
-
a.tags.
|
|
598
|
-
a.albums.
|
|
599
|
-
DB.sqls.length.
|
|
597
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
598
|
+
a.albums.must_equal [Album.load(:id=>3)]
|
|
599
|
+
DB.sqls.length.must_equal 0
|
|
600
600
|
end
|
|
601
601
|
|
|
602
602
|
it "should eagerly load multiple associations in separate" do
|
|
603
603
|
a = @c1.eager(:tags).eager(:albums).all
|
|
604
|
-
a.
|
|
604
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
605
605
|
sqls = DB.sqls
|
|
606
|
-
sqls.length.
|
|
607
|
-
sqls[0].
|
|
608
|
-
sqls[1..-1].
|
|
609
|
-
sqls[1..-1].
|
|
606
|
+
sqls.length.must_equal 3
|
|
607
|
+
sqls[0].must_equal 'SELECT * FROM artists'
|
|
608
|
+
sqls[1..-1].must_include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
609
|
+
sqls[1..-1].must_include('SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
610
610
|
a = a.first
|
|
611
|
-
a.tags.
|
|
612
|
-
a.albums.
|
|
613
|
-
DB.sqls.length.
|
|
611
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
612
|
+
a.albums.must_equal [Album.load(:id=>3)]
|
|
613
|
+
DB.sqls.length.must_equal 0
|
|
614
614
|
end
|
|
615
615
|
|
|
616
616
|
it "should allow cascading of eager loading for associations of associated models" do
|
|
617
617
|
a = @c1.eager(:tags=>:tracks).all
|
|
618
|
-
a.
|
|
619
|
-
DB.sqls.
|
|
618
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
619
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
620
620
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))',
|
|
621
621
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
622
622
|
a = a.first
|
|
623
|
-
a.tags.
|
|
624
|
-
a.tags.first.tracks.
|
|
625
|
-
DB.sqls.length.
|
|
623
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
624
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
625
|
+
DB.sqls.length.must_equal 0
|
|
626
626
|
end
|
|
627
627
|
|
|
628
628
|
it "should cascade eagerly loading when the :eager association option is used" do
|
|
629
629
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:tracks
|
|
630
630
|
a = @c1.eager(:tags).all
|
|
631
|
-
a.
|
|
632
|
-
DB.sqls.
|
|
631
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
632
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
633
633
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))',
|
|
634
634
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
635
635
|
a = a.first
|
|
636
|
-
a.tags.
|
|
637
|
-
a.tags.first.tracks.
|
|
638
|
-
DB.sqls.length.
|
|
636
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
637
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
638
|
+
DB.sqls.length.must_equal 0
|
|
639
639
|
end
|
|
640
640
|
|
|
641
641
|
it "should respect :eager when lazily loading an association" do
|
|
642
642
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:tracks
|
|
643
643
|
a = @c1.load(:id=>1)
|
|
644
|
-
a.tags.
|
|
645
|
-
DB.sqls.
|
|
644
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
645
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1)',
|
|
646
646
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
647
|
-
a.tags.first.tracks.
|
|
648
|
-
DB.sqls.length.
|
|
647
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
648
|
+
DB.sqls.length.must_equal 0
|
|
649
649
|
end
|
|
650
650
|
|
|
651
651
|
it "should raise error if attempting to eagerly load an association using :eager_graph option" do
|
|
652
652
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:tracks
|
|
653
|
-
proc{@c1.eager(:tags).all}.
|
|
653
|
+
proc{@c1.eager(:tags).all}.must_raise(Sequel::Error)
|
|
654
654
|
end
|
|
655
655
|
|
|
656
656
|
it "should respect :eager_graph when lazily loading an association" do
|
|
@@ -663,106 +663,106 @@ describe "many_through_many eager loading methods" do
|
|
|
663
663
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:tracks
|
|
664
664
|
a = @c1.load(:id=>1)
|
|
665
665
|
a.tags
|
|
666
|
-
DB.sqls.
|
|
667
|
-
a.tags.
|
|
668
|
-
a.tags.first.tracks.
|
|
669
|
-
DB.sqls.length.
|
|
666
|
+
DB.sqls.must_equal [ 'SELECT tags.id, tracks.id AS tracks_id FROM (SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1)) AS tags LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums ON (albums.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)']
|
|
667
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
668
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
669
|
+
DB.sqls.length.must_equal 0
|
|
670
670
|
end
|
|
671
671
|
|
|
672
672
|
it "should respect :conditions when eagerly loading" do
|
|
673
673
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
674
674
|
a = @c1.eager(:tags).all
|
|
675
|
-
a.
|
|
676
|
-
DB.sqls.
|
|
675
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
676
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
677
677
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 32) AND (albums_artists.artist_id IN (1)))']
|
|
678
|
-
a.first.tags.
|
|
679
|
-
DB.sqls.length.
|
|
678
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
679
|
+
DB.sqls.length.must_equal 0
|
|
680
680
|
end
|
|
681
681
|
|
|
682
682
|
it "should respect :order when eagerly loading" do
|
|
683
683
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah
|
|
684
684
|
a = @c1.eager(:tags).all
|
|
685
|
-
a.
|
|
686
|
-
DB.sqls.
|
|
685
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
686
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
687
687
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1)) ORDER BY blah']
|
|
688
|
-
a.first.tags.
|
|
689
|
-
DB.sqls.length.
|
|
688
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
689
|
+
DB.sqls.length.must_equal 0
|
|
690
690
|
end
|
|
691
691
|
|
|
692
692
|
it "should use the association's block when eager loading by default" do
|
|
693
693
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]] do |ds| ds.filter(:a) end
|
|
694
694
|
a = @c1.eager(:tags).all
|
|
695
|
-
a.
|
|
696
|
-
DB.sqls.
|
|
695
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
696
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
697
697
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (a AND (albums_artists.artist_id IN (1)))']
|
|
698
|
-
a.first.tags.
|
|
699
|
-
DB.sqls.length.
|
|
698
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
699
|
+
DB.sqls.length.must_equal 0
|
|
700
700
|
end
|
|
701
701
|
|
|
702
702
|
it "should use the :eager_block option when eager loading if given" do
|
|
703
703
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_block=>proc{|ds| ds.filter(:b)} do |ds| ds.filter(:a) end
|
|
704
704
|
a = @c1.eager(:tags).all
|
|
705
|
-
a.
|
|
706
|
-
DB.sqls.
|
|
705
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
706
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
707
707
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (b AND (albums_artists.artist_id IN (1)))']
|
|
708
|
-
a.first.tags.
|
|
709
|
-
DB.sqls.length.
|
|
708
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
709
|
+
DB.sqls.length.must_equal 0
|
|
710
710
|
end
|
|
711
711
|
|
|
712
712
|
it "should respect the :limit option on a many_through_many association" do
|
|
713
713
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2
|
|
714
714
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}]
|
|
715
715
|
a = @c1.eager(:first_two_tags).all
|
|
716
|
-
a.
|
|
717
|
-
DB.sqls.
|
|
716
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
717
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
718
718
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (1 = albums_artists.artist_id) LIMIT 2) AS t1']
|
|
719
|
-
a.first.first_two_tags.
|
|
720
|
-
DB.sqls.length.
|
|
719
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
720
|
+
DB.sqls.length.must_equal 0
|
|
721
721
|
|
|
722
722
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[1,1]
|
|
723
723
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>6}]
|
|
724
724
|
a = @c1.eager(:first_two_tags).all
|
|
725
|
-
a.
|
|
726
|
-
DB.sqls.
|
|
725
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
726
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
727
727
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (1 = albums_artists.artist_id) LIMIT 1 OFFSET 1) AS t1']
|
|
728
|
-
a.first.first_two_tags.
|
|
729
|
-
DB.sqls.length.
|
|
728
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>6)]
|
|
729
|
+
DB.sqls.length.must_equal 0
|
|
730
730
|
|
|
731
731
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1]
|
|
732
732
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>6}, {:x_foreign_key_x=>1, :id=>7}]
|
|
733
733
|
a = @c1.eager(:first_two_tags).all
|
|
734
|
-
a.
|
|
735
|
-
DB.sqls.
|
|
734
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
735
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
736
736
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (1 = albums_artists.artist_id) OFFSET 1) AS t1']
|
|
737
|
-
a.first.first_two_tags.
|
|
738
|
-
DB.sqls.length.
|
|
737
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>6), Tag.load(:id=>7)]
|
|
738
|
+
DB.sqls.length.must_equal 0
|
|
739
739
|
end
|
|
740
740
|
|
|
741
741
|
it "should respect the :limit option on a many_through_many association using a :ruby strategy" do
|
|
742
742
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :eager_limit_strategy=>:ruby
|
|
743
743
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}, {:x_foreign_key_x=>1, :id=>7}]
|
|
744
744
|
a = @c1.eager(:first_two_tags).all
|
|
745
|
-
a.
|
|
746
|
-
DB.sqls.
|
|
745
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
746
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
747
747
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
748
|
-
a.first.first_two_tags.
|
|
749
|
-
DB.sqls.length.
|
|
748
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
749
|
+
DB.sqls.length.must_equal 0
|
|
750
750
|
|
|
751
751
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[1,1], :eager_limit_strategy=>:ruby
|
|
752
752
|
a = @c1.eager(:first_two_tags).all
|
|
753
|
-
a.
|
|
754
|
-
DB.sqls.
|
|
753
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
754
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
755
755
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
756
|
-
a.first.first_two_tags.
|
|
757
|
-
DB.sqls.length.
|
|
756
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>6)]
|
|
757
|
+
DB.sqls.length.must_equal 0
|
|
758
758
|
|
|
759
759
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1], :eager_limit_strategy=>:ruby
|
|
760
760
|
a = @c1.eager(:first_two_tags).all
|
|
761
|
-
a.
|
|
762
|
-
DB.sqls.
|
|
761
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
762
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
763
763
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
764
|
-
a.first.first_two_tags.
|
|
765
|
-
DB.sqls.length.
|
|
764
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>6), Tag.load(:id=>7)]
|
|
765
|
+
DB.sqls.length.must_equal 0
|
|
766
766
|
end
|
|
767
767
|
|
|
768
768
|
it "should respect the :limit option on a many_through_many association using a :window_function strategy" do
|
|
@@ -770,27 +770,27 @@ describe "many_through_many eager loading methods" do
|
|
|
770
770
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :order=>:name, :eager_limit_strategy=>:window_function
|
|
771
771
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}]
|
|
772
772
|
a = @c1.eager(:first_two_tags).all
|
|
773
|
-
a.
|
|
774
|
-
DB.sqls.
|
|
773
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
774
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
775
775
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))) AS t1 WHERE (x_sequel_row_number_x <= 2)']
|
|
776
|
-
a.first.first_two_tags.
|
|
777
|
-
DB.sqls.length.
|
|
776
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
777
|
+
DB.sqls.length.must_equal 0
|
|
778
778
|
|
|
779
779
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :order=>:name, :eager_limit_strategy=>:window_function
|
|
780
780
|
a = @c1.eager(:first_two_tags).all
|
|
781
|
-
a.
|
|
782
|
-
DB.sqls.
|
|
781
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
782
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
783
783
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))) AS t1 WHERE ((x_sequel_row_number_x >= 2) AND (x_sequel_row_number_x < 4))']
|
|
784
|
-
a.first.first_two_tags.
|
|
785
|
-
DB.sqls.length.
|
|
784
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
785
|
+
DB.sqls.length.must_equal 0
|
|
786
786
|
|
|
787
787
|
@c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1], :order=>:name, :eager_limit_strategy=>:window_function
|
|
788
788
|
a = @c1.eager(:first_two_tags).all
|
|
789
|
-
a.
|
|
790
|
-
DB.sqls.
|
|
789
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
790
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
791
791
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))) AS t1 WHERE (x_sequel_row_number_x >= 2)']
|
|
792
|
-
a.first.first_two_tags.
|
|
793
|
-
DB.sqls.length.
|
|
792
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
793
|
+
DB.sqls.length.must_equal 0
|
|
794
794
|
end
|
|
795
795
|
|
|
796
796
|
it "should respect the :limit option on a many_through_many association with composite primary keys on the main table" do
|
|
@@ -801,19 +801,19 @@ describe "many_through_many eager loading methods" do
|
|
|
801
801
|
@c1.dataset._fetch = [{:id1=>1, :id2=>2}]
|
|
802
802
|
Tag.dataset._fetch = [{:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>5}, {:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>6}]
|
|
803
803
|
a = @c1.eager(:first_two_tags).all
|
|
804
|
-
a.
|
|
805
|
-
DB.sqls.
|
|
804
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
805
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
806
806
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((1 = albums_artists.artist_id1) AND (2 = albums_artists.artist_id2)) ORDER BY name LIMIT 2) AS t1']
|
|
807
|
-
a.first.first_two_tags.
|
|
808
|
-
DB.sqls.length.
|
|
807
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
808
|
+
DB.sqls.length.must_equal 0
|
|
809
809
|
|
|
810
810
|
@c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1]
|
|
811
811
|
a = @c1.eager(:first_two_tags).all
|
|
812
|
-
a.
|
|
813
|
-
DB.sqls.
|
|
812
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
813
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
814
814
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((1 = albums_artists.artist_id1) AND (2 = albums_artists.artist_id2)) LIMIT 2 OFFSET 1) AS t1']
|
|
815
|
-
a.first.first_two_tags.
|
|
816
|
-
DB.sqls.length.
|
|
815
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
816
|
+
DB.sqls.length.must_equal 0
|
|
817
817
|
end
|
|
818
818
|
|
|
819
819
|
it "should respect the :limit option on a many_through_many association with composite primary keys on the main table using a :window_function strategy" do
|
|
@@ -824,35 +824,35 @@ describe "many_through_many eager loading methods" do
|
|
|
824
824
|
@c1.dataset._fetch = [{:id1=>1, :id2=>2}]
|
|
825
825
|
Tag.dataset._fetch = [{:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>5}, {:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>6}]
|
|
826
826
|
a = @c1.eager(:first_two_tags).all
|
|
827
|
-
a.
|
|
828
|
-
DB.sqls.
|
|
827
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
828
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
829
829
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x, row_number() OVER (PARTITION BY albums_artists.artist_id1, albums_artists.artist_id2 ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id1, albums_artists.artist_id2) IN ((1, 2)))) AS t1 WHERE (x_sequel_row_number_x <= 2)']
|
|
830
|
-
a.first.first_two_tags.
|
|
831
|
-
DB.sqls.length.
|
|
830
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
831
|
+
DB.sqls.length.must_equal 0
|
|
832
832
|
|
|
833
833
|
@c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :order=>:name, :eager_limit_strategy=>:window_function
|
|
834
834
|
a = @c1.eager(:first_two_tags).all
|
|
835
|
-
a.
|
|
836
|
-
DB.sqls.
|
|
835
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
836
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
837
837
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x, row_number() OVER (PARTITION BY albums_artists.artist_id1, albums_artists.artist_id2 ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id1, albums_artists.artist_id2) IN ((1, 2)))) AS t1 WHERE ((x_sequel_row_number_x >= 2) AND (x_sequel_row_number_x < 4))']
|
|
838
|
-
a.first.first_two_tags.
|
|
839
|
-
DB.sqls.length.
|
|
838
|
+
a.first.first_two_tags.must_equal [Tag.load(:id=>5), Tag.load(:id=>6)]
|
|
839
|
+
DB.sqls.length.must_equal 0
|
|
840
840
|
end
|
|
841
841
|
|
|
842
842
|
it "should raise an error when attempting to eagerly load an association with the :allow_eager option set to false" do
|
|
843
|
-
|
|
843
|
+
@c1.eager(:tags).all
|
|
844
844
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :allow_eager=>false
|
|
845
|
-
proc{@c1.eager(:tags).all}.
|
|
845
|
+
proc{@c1.eager(:tags).all}.must_raise(Sequel::Error)
|
|
846
846
|
end
|
|
847
847
|
|
|
848
848
|
it "should respect the association's :select option" do
|
|
849
849
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>:tags__name
|
|
850
850
|
a = @c1.eager(:tags).all
|
|
851
|
-
a.
|
|
852
|
-
DB.sqls.
|
|
851
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
852
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
853
853
|
'SELECT tags.name, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
854
|
-
a.first.tags.
|
|
855
|
-
DB.sqls.length.
|
|
854
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
855
|
+
DB.sqls.length.must_equal 0
|
|
856
856
|
end
|
|
857
857
|
|
|
858
858
|
it "should respect many_through_many association's :left_primary_key and :right_primary_key options" do
|
|
@@ -861,11 +861,11 @@ describe "many_through_many eager loading methods" do
|
|
|
861
861
|
@c1.dataset.meta_def(:columns){[:id, :yyy]}
|
|
862
862
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:yyy, :right_primary_key=>:tag_id
|
|
863
863
|
a = @c1.eager(:tags).all
|
|
864
|
-
a.
|
|
865
|
-
DB.sqls.
|
|
864
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
865
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
866
866
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.tag_id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (8))']
|
|
867
|
-
a.first.tags.
|
|
868
|
-
DB.sqls.length.
|
|
867
|
+
a.first.tags.must_equal [Tag.load(:tag_id=>2)]
|
|
868
|
+
DB.sqls.length.must_equal 0
|
|
869
869
|
end
|
|
870
870
|
|
|
871
871
|
it "should handle composite keys" do
|
|
@@ -874,41 +874,41 @@ describe "many_through_many eager loading methods" do
|
|
|
874
874
|
@c1.dataset.meta_def(:columns){[:id, :yyy]}
|
|
875
875
|
@c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
876
876
|
a = @c1.eager(:tags).all
|
|
877
|
-
a.
|
|
878
|
-
DB.sqls.
|
|
877
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
878
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
879
879
|
'SELECT tags.*, albums_artists.b1 AS x_foreign_key_0_x, albums_artists.b2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1, albums_artists.b2) IN ((1, 8)))']
|
|
880
|
-
a.first.tags.
|
|
881
|
-
DB.sqls.length.
|
|
880
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
881
|
+
DB.sqls.length.must_equal 0
|
|
882
882
|
end
|
|
883
883
|
|
|
884
884
|
it "should respect :after_load callbacks on associations when eager loading" do
|
|
885
885
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :after_load=>lambda{|o, as| o[:id] *= 2; as.each{|a| a[:id] *= 3}}
|
|
886
886
|
a = @c1.eager(:tags).all
|
|
887
|
-
a.
|
|
888
|
-
DB.sqls.
|
|
887
|
+
a.must_equal [@c1.load(:id=>2)]
|
|
888
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
889
889
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
890
|
-
a.first.tags.
|
|
891
|
-
DB.sqls.length.
|
|
890
|
+
a.first.tags.must_equal [Tag.load(:id=>6)]
|
|
891
|
+
DB.sqls.length.must_equal 0
|
|
892
892
|
end
|
|
893
893
|
|
|
894
894
|
it "should raise an error if called without a symbol or hash" do
|
|
895
|
-
proc{@c1.eager_graph(Object.new)}.
|
|
895
|
+
proc{@c1.eager_graph(Object.new)}.must_raise(Sequel::Error)
|
|
896
896
|
end
|
|
897
897
|
|
|
898
898
|
it "should support association_join" do
|
|
899
|
-
@c1.association_join(:tags).sql.
|
|
899
|
+
@c1.association_join(:tags).sql.must_equal "SELECT * FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags ON (tags.id = albums_tags.tag_id)"
|
|
900
900
|
end
|
|
901
901
|
|
|
902
902
|
it "should support custom selects when using association_join" do
|
|
903
|
-
@c1.select{a(b)}.association_join(:tags).sql.
|
|
903
|
+
@c1.select{a(b)}.association_join(:tags).sql.must_equal "SELECT a(b) FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags ON (tags.id = albums_tags.tag_id)"
|
|
904
904
|
end
|
|
905
905
|
|
|
906
906
|
it "should eagerly graph a single many_through_many association" do
|
|
907
907
|
a = @c1.eager_graph(:tags).all
|
|
908
|
-
a.
|
|
909
|
-
DB.sqls.
|
|
910
|
-
a.first.tags.
|
|
911
|
-
DB.sqls.length.
|
|
908
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
909
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
|
|
910
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
911
|
+
DB.sqls.length.must_equal 0
|
|
912
912
|
end
|
|
913
913
|
|
|
914
914
|
it "should eagerly graph a single many_through_many association using the :window_function strategy" do
|
|
@@ -918,40 +918,40 @@ describe "many_through_many eager loading methods" do
|
|
|
918
918
|
ds = @c1.eager_graph_with_options(:tags, :limit_strategy=>true)
|
|
919
919
|
ds._fetch = {:id=>1, :tags_id=>2}
|
|
920
920
|
a = ds.all
|
|
921
|
-
a.
|
|
922
|
-
DB.sqls.
|
|
923
|
-
a.first.tags.
|
|
924
|
-
DB.sqls.length.
|
|
921
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
922
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN (SELECT id, x_foreign_key_x FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id)) AS t1 WHERE (x_sequel_row_number_x <= 2)) AS tags ON (tags.x_foreign_key_x = artists.id)']
|
|
923
|
+
a.first.tags.must_equal [Tag.load(:id=>2)]
|
|
924
|
+
DB.sqls.length.must_equal 0
|
|
925
925
|
end
|
|
926
926
|
|
|
927
927
|
it "should eagerly graph multiple associations in a single call" do
|
|
928
928
|
a = @c1.eager_graph(:tags, :albums).all
|
|
929
|
-
a.
|
|
930
|
-
DB.sqls.
|
|
929
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
930
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
|
|
931
931
|
a = a.first
|
|
932
|
-
a.tags.
|
|
933
|
-
a.albums.
|
|
934
|
-
DB.sqls.length.
|
|
932
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
933
|
+
a.albums.must_equal [Album.load(:id=>3)]
|
|
934
|
+
DB.sqls.length.must_equal 0
|
|
935
935
|
end
|
|
936
936
|
|
|
937
937
|
it "should eagerly graph multiple associations in separate calls" do
|
|
938
938
|
a = @c1.eager_graph(:tags).eager_graph(:albums).all
|
|
939
|
-
a.
|
|
940
|
-
DB.sqls.
|
|
939
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
940
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
|
|
941
941
|
a = a.first
|
|
942
|
-
a.tags.
|
|
943
|
-
a.albums.
|
|
944
|
-
DB.sqls.length.
|
|
942
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
943
|
+
a.albums.must_equal [Album.load(:id=>3)]
|
|
944
|
+
DB.sqls.length.must_equal 0
|
|
945
945
|
end
|
|
946
946
|
|
|
947
947
|
it "should allow cascading of eager graphing for associations of associated models" do
|
|
948
948
|
a = @c1.eager_graph(:tags=>:tracks).all
|
|
949
|
-
a.
|
|
950
|
-
DB.sqls.
|
|
949
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
950
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
|
|
951
951
|
a = a.first
|
|
952
|
-
a.tags.
|
|
953
|
-
a.tags.first.tracks.
|
|
954
|
-
DB.sqls.length.
|
|
952
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
953
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
954
|
+
DB.sqls.length.must_equal 0
|
|
955
955
|
end
|
|
956
956
|
|
|
957
957
|
it "eager graphing should eliminate duplicates caused by cartesian products" do
|
|
@@ -959,82 +959,82 @@ describe "many_through_many eager loading methods" do
|
|
|
959
959
|
# Assume artist has 2 albums each with 2 tags
|
|
960
960
|
ds._fetch = [{:id=>1, :tags_id=>2}, {:id=>1, :tags_id=>3}, {:id=>1, :tags_id=>2}, {:id=>1, :tags_id=>3}]
|
|
961
961
|
a = ds.all
|
|
962
|
-
a.
|
|
963
|
-
DB.sqls.
|
|
964
|
-
a.first.tags.
|
|
965
|
-
DB.sqls.length.
|
|
962
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
963
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
|
|
964
|
+
a.first.tags.must_equal [Tag.load(:id=>2), Tag.load(:id=>3)]
|
|
965
|
+
DB.sqls.length.must_equal 0
|
|
966
966
|
end
|
|
967
967
|
|
|
968
968
|
it "should eager graph multiple associations from the same table" do
|
|
969
969
|
a = @c1.eager_graph(:tags, :other_tags).all
|
|
970
|
-
a.
|
|
971
|
-
DB.sqls.
|
|
970
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
971
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, other_tags.id AS other_tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.album_id = albums_0.id) LEFT OUTER JOIN tags AS other_tags ON (other_tags.id = albums_tags_0.tag_id)']
|
|
972
972
|
a = a.first
|
|
973
|
-
a.tags.
|
|
974
|
-
a.other_tags.
|
|
975
|
-
DB.sqls.length.
|
|
973
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
974
|
+
a.other_tags.must_equal [Tag.load(:id=>9)]
|
|
975
|
+
DB.sqls.length.must_equal 0
|
|
976
976
|
end
|
|
977
977
|
|
|
978
978
|
it "should eager graph a self_referential association" do
|
|
979
979
|
a = @c1.eager_graph(:tags, :artists).all
|
|
980
|
-
a.
|
|
981
|
-
DB.sqls.
|
|
980
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
981
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, artists_0.id AS artists_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.id = albums_artists_1.artist_id)']
|
|
982
982
|
a = a.first
|
|
983
|
-
a.tags.
|
|
984
|
-
a.artists.
|
|
985
|
-
DB.sqls.length.
|
|
983
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
984
|
+
a.artists.must_equal [@c1.load(:id=>10)]
|
|
985
|
+
DB.sqls.length.must_equal 0
|
|
986
986
|
end
|
|
987
987
|
|
|
988
988
|
it "eager graphing should give you a plain hash when called without .all" do
|
|
989
|
-
@c1.eager_graph(:tags, :artists).first.
|
|
989
|
+
@c1.eager_graph(:tags, :artists).first.must_equal(:albums_0_id=>3, :artists_0_id=>10, :id=>1, :tags_id=>2)
|
|
990
990
|
end
|
|
991
991
|
|
|
992
992
|
it "should be able to use eager and eager_graph together" do
|
|
993
993
|
a = @c1.eager_graph(:tags).eager(:albums).all
|
|
994
|
-
a.
|
|
995
|
-
DB.sqls.
|
|
994
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
995
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)',
|
|
996
996
|
'SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
997
997
|
a = a.first
|
|
998
|
-
a.tags.
|
|
999
|
-
a.albums.
|
|
1000
|
-
DB.sqls.length.
|
|
998
|
+
a.tags.must_equal [Tag.load(:id=>2)]
|
|
999
|
+
a.albums.must_equal [Album.load(:id=>3)]
|
|
1000
|
+
DB.sqls.length.must_equal 0
|
|
1001
1001
|
end
|
|
1002
1002
|
|
|
1003
1003
|
it "should handle no associated records when eagerly graphing a single many_through_many association" do
|
|
1004
1004
|
ds = @c1.eager_graph(:tags)
|
|
1005
1005
|
ds._fetch = {:id=>1, :tags_id=>nil}
|
|
1006
1006
|
a = ds.all
|
|
1007
|
-
a.
|
|
1008
|
-
DB.sqls.
|
|
1009
|
-
a.first.tags.
|
|
1010
|
-
DB.sqls.length.
|
|
1007
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1008
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
|
|
1009
|
+
a.first.tags.must_equal []
|
|
1010
|
+
DB.sqls.length.must_equal 0
|
|
1011
1011
|
end
|
|
1012
1012
|
|
|
1013
1013
|
it "should handle no associated records when eagerly graphing multiple many_through_many associations" do
|
|
1014
1014
|
ds = @c1.eager_graph(:tags, :albums)
|
|
1015
1015
|
ds._fetch = [{:id=>1, :tags_id=>nil, :albums_0_id=>3}, {:id=>1, :tags_id=>2, :albums_0_id=>nil}, {:id=>1, :tags_id=>5, :albums_0_id=>6}, {:id=>7, :tags_id=>nil, :albums_0_id=>nil}]
|
|
1016
1016
|
a = ds.all
|
|
1017
|
-
a.
|
|
1018
|
-
DB.sqls.
|
|
1019
|
-
a.first.tags.
|
|
1020
|
-
a.first.albums.
|
|
1021
|
-
a.last.tags.
|
|
1022
|
-
a.last.albums.
|
|
1023
|
-
DB.sqls.length.
|
|
1017
|
+
a.must_equal [@c1.load(:id=>1), @c1.load(:id=>7)]
|
|
1018
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
|
|
1019
|
+
a.first.tags.must_equal [Tag.load(:id=>2), Tag.load(:id=>5)]
|
|
1020
|
+
a.first.albums.must_equal [Album.load(:id=>3), Album.load(:id=>6)]
|
|
1021
|
+
a.last.tags.must_equal []
|
|
1022
|
+
a.last.albums.must_equal []
|
|
1023
|
+
DB.sqls.length.must_equal 0
|
|
1024
1024
|
end
|
|
1025
1025
|
|
|
1026
1026
|
it "should handle missing associated records when cascading eager graphing for associations of associated models" do
|
|
1027
1027
|
ds = @c1.eager_graph(:tags=>:tracks)
|
|
1028
1028
|
ds._fetch = [{:id=>1, :tags_id=>2, :tracks_id=>4}, {:id=>1, :tags_id=>3, :tracks_id=>nil}, {:id=>2, :tags_id=>nil, :tracks_id=>nil}]
|
|
1029
1029
|
a = ds.all
|
|
1030
|
-
a.
|
|
1031
|
-
DB.sqls.
|
|
1032
|
-
a.last.tags.
|
|
1030
|
+
a.must_equal [@c1.load(:id=>1), @c1.load(:id=>2)]
|
|
1031
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
|
|
1032
|
+
a.last.tags.must_equal []
|
|
1033
1033
|
a = a.first
|
|
1034
|
-
a.tags.
|
|
1035
|
-
a.tags.first.tracks.
|
|
1036
|
-
a.tags.last.tracks.
|
|
1037
|
-
DB.sqls.length.
|
|
1034
|
+
a.tags.must_equal [Tag.load(:id=>2), Tag.load(:id=>3)]
|
|
1035
|
+
a.tags.first.tracks.must_equal [Track.load(:id=>4)]
|
|
1036
|
+
a.tags.last.tracks.must_equal []
|
|
1037
|
+
DB.sqls.length.must_equal 0
|
|
1038
1038
|
end
|
|
1039
1039
|
|
|
1040
1040
|
it "eager graphing should respect :left_primary_key and :right_primary_key options" do
|
|
@@ -1044,10 +1044,10 @@ describe "many_through_many eager loading methods" do
|
|
|
1044
1044
|
ds = @c1.eager_graph(:tags)
|
|
1045
1045
|
ds._fetch = {:id=>1, :yyy=>8, :tags_id=>2, :tag_id=>4}
|
|
1046
1046
|
a = ds.all
|
|
1047
|
-
a.
|
|
1048
|
-
DB.sqls.
|
|
1049
|
-
a.first.tags.
|
|
1050
|
-
DB.sqls.length.
|
|
1047
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
1048
|
+
DB.sqls.must_equal ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.yyy) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.tag_id = albums_tags.tag_id)']
|
|
1049
|
+
a.first.tags.must_equal [Tag.load(:id=>2, :tag_id=>4)]
|
|
1050
|
+
DB.sqls.length.must_equal 0
|
|
1051
1051
|
end
|
|
1052
1052
|
|
|
1053
1053
|
it "eager graphing should respect composite keys" do
|
|
@@ -1057,10 +1057,10 @@ describe "many_through_many eager loading methods" do
|
|
|
1057
1057
|
ds = @c1.eager_graph(:tags)
|
|
1058
1058
|
ds._fetch = {:id=>1, :yyy=>8, :tags_id=>2, :tag_id=>4}
|
|
1059
1059
|
a = ds.all
|
|
1060
|
-
a.
|
|
1061
|
-
DB.sqls.
|
|
1062
|
-
a.first.tags.
|
|
1063
|
-
DB.sqls.length.
|
|
1060
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
1061
|
+
DB.sqls.must_equal ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON ((albums_artists.b1 = artists.id) AND (albums_artists.b2 = artists.yyy)) LEFT OUTER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) LEFT OUTER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.g1) AND (tags.tag_id = albums_tags.g2))']
|
|
1062
|
+
a.first.tags.must_equal [Tag.load(:id=>2, :tag_id=>4)]
|
|
1063
|
+
DB.sqls.length.must_equal 0
|
|
1064
1064
|
end
|
|
1065
1065
|
|
|
1066
1066
|
it "should respect the association's :graph_select option" do
|
|
@@ -1068,94 +1068,94 @@ describe "many_through_many eager loading methods" do
|
|
|
1068
1068
|
ds = @c1.eager_graph(:tags)
|
|
1069
1069
|
ds._fetch = {:id=>1, :b=>2}
|
|
1070
1070
|
a = ds.all
|
|
1071
|
-
a.
|
|
1072
|
-
DB.sqls.
|
|
1073
|
-
a.first.tags.
|
|
1074
|
-
DB.sqls.length.
|
|
1071
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1072
|
+
DB.sqls.must_equal ['SELECT artists.id, tags.b FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
|
|
1073
|
+
a.first.tags.must_equal [Tag.load(:b=>2)]
|
|
1074
|
+
DB.sqls.length.must_equal 0
|
|
1075
1075
|
end
|
|
1076
1076
|
|
|
1077
1077
|
it "should respect the association's :graph_join_type option" do
|
|
1078
1078
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :graph_join_type=>:inner
|
|
1079
|
-
@c1.eager_graph(:tags).sql.
|
|
1079
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
1080
1080
|
end
|
|
1081
1081
|
|
|
1082
1082
|
it "should respect the association's :join_type option on through" do
|
|
1083
1083
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :join_type=>:natural}, [:albums_tags, :album_id, :tag_id]], :graph_join_type=>:inner
|
|
1084
|
-
@c1.eager_graph(:tags).sql.
|
|
1084
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) NATURAL JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
1085
1085
|
end
|
|
1086
1086
|
|
|
1087
1087
|
it "should respect the association's :conditions option" do
|
|
1088
1088
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
1089
|
-
@c1.eager_graph(:tags).sql.
|
|
1089
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.tag_id) AND (tags.a = 32))'
|
|
1090
1090
|
end
|
|
1091
1091
|
|
|
1092
1092
|
it "should respect the association's :graph_conditions option" do
|
|
1093
1093
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_conditions=>{:a=>42}
|
|
1094
|
-
@c1.eager_graph(:tags).sql.
|
|
1094
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.tag_id) AND (tags.a = 42))'
|
|
1095
1095
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_conditions=>{:a=>42}, :conditions=>{:a=>32}
|
|
1096
|
-
@c1.eager_graph(:tags).sql.
|
|
1096
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.tag_id) AND (tags.a = 42))'
|
|
1097
1097
|
end
|
|
1098
1098
|
|
|
1099
1099
|
it "should respect the association's :conditions option on through" do
|
|
1100
1100
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :conditions=>{:a=>42}}, [:albums_tags, :album_id, :tag_id]]
|
|
1101
|
-
@c1.eager_graph(:tags).sql.
|
|
1101
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON ((albums.id = albums_artists.album_id) AND (albums.a = 42)) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
1102
1102
|
end
|
|
1103
1103
|
|
|
1104
1104
|
it "should respect the association's :graph_block option" do
|
|
1105
1105
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_block=>proc{|ja,lja,js| {Sequel.qualify(ja, :active)=>true}}
|
|
1106
|
-
@c1.eager_graph(:tags).sql.
|
|
1106
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.tag_id) AND (tags.active IS TRUE))'
|
|
1107
1107
|
end
|
|
1108
1108
|
|
|
1109
1109
|
it "should respect the association's :block option on through" do
|
|
1110
1110
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :block=>proc{|ja,lja,js| {Sequel.qualify(ja, :active)=>true}}}, [:albums_tags, :album_id, :tag_id]]
|
|
1111
|
-
@c1.eager_graph(:tags).sql.
|
|
1111
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON ((albums.id = albums_artists.album_id) AND (albums.active IS TRUE)) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
1112
1112
|
end
|
|
1113
1113
|
|
|
1114
1114
|
it "should respect the association's :graph_only_conditions option" do
|
|
1115
1115
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_only_conditions=>{:a=>32}
|
|
1116
|
-
@c1.eager_graph(:tags).sql.
|
|
1116
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.a = 32)'
|
|
1117
1117
|
end
|
|
1118
1118
|
|
|
1119
1119
|
it "should respect the association's :only_conditions option on through" do
|
|
1120
1120
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :only_conditions=>{:a=>42}}, [:albums_tags, :album_id, :tag_id]]
|
|
1121
|
-
@c1.eager_graph(:tags).sql.
|
|
1121
|
+
@c1.eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.a = 42) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)'
|
|
1122
1122
|
end
|
|
1123
1123
|
|
|
1124
1124
|
it "should create unique table aliases for all associations" do
|
|
1125
|
-
@c1.eager_graph(:artists=>{:artists=>:artists}).sql.
|
|
1125
|
+
@c1.eager_graph(:artists=>{:artists=>:artists}).sql.must_equal "SELECT artists.id, artists_0.id AS artists_0_id, artists_1.id AS artists_1_id, artists_2.id AS artists_2_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.album_id = albums.id) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.id = albums_artists_0.artist_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.artist_id = artists_0.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_1.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_2 ON (albums_artists_2.album_id = albums_0.id) LEFT OUTER JOIN artists AS artists_1 ON (artists_1.id = albums_artists_2.artist_id) LEFT OUTER JOIN albums_artists AS albums_artists_3 ON (albums_artists_3.artist_id = artists_1.id) LEFT OUTER JOIN albums AS albums_1 ON (albums_1.id = albums_artists_3.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_4 ON (albums_artists_4.album_id = albums_1.id) LEFT OUTER JOIN artists AS artists_2 ON (artists_2.id = albums_artists_4.artist_id)"
|
|
1126
1126
|
end
|
|
1127
1127
|
|
|
1128
1128
|
it "should respect the association's :order" do
|
|
1129
1129
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
1130
|
-
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.
|
|
1130
|
+
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) ORDER BY artists.blah2, artists.blah3, tags.blah1, tags.blah2'
|
|
1131
1131
|
end
|
|
1132
1132
|
|
|
1133
1133
|
it "should only qualify unqualified symbols, identifiers, or ordered versions in association's :order" do
|
|
1134
1134
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[Sequel.identifier(:blah__id), Sequel.identifier(:blah__id).desc, Sequel.desc(:blah__id), :blah__id, :album_id, Sequel.desc(:album_id), 1, Sequel.lit('RANDOM()'), Sequel.qualify(:b, :a)]
|
|
1135
|
-
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.
|
|
1135
|
+
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) ORDER BY artists.blah2, artists.blah3, tags.blah__id, tags.blah__id DESC, blah.id DESC, blah.id, tags.album_id, tags.album_id DESC, 1, RANDOM(), b.a'
|
|
1136
1136
|
end
|
|
1137
1137
|
|
|
1138
1138
|
it "should not respect the association's :order if :order_eager_graph is false" do
|
|
1139
1139
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2], :order_eager_graph=>false
|
|
1140
|
-
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.
|
|
1140
|
+
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tags).sql.must_equal 'SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) ORDER BY artists.blah2, artists.blah3'
|
|
1141
1141
|
end
|
|
1142
1142
|
|
|
1143
1143
|
it "should add the associations :order for multiple associations" do
|
|
1144
1144
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
1145
1145
|
@c1.many_through_many :albums, [[:albums_artists, :artist_id, :album_id]], :order=>[:blah3, :blah4]
|
|
1146
|
-
@c1.eager_graph(:tags, :albums).sql.
|
|
1146
|
+
@c1.eager_graph(:tags, :albums).sql.must_equal 'SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) ORDER BY tags.blah1, tags.blah2, albums_0.blah3, albums_0.blah4'
|
|
1147
1147
|
end
|
|
1148
1148
|
|
|
1149
1149
|
it "should add the association's :order for cascading associations" do
|
|
1150
1150
|
@c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
1151
1151
|
Tag.many_through_many :tracks, [[:albums_tags, :tag_id, :album_id], [:albums, :id, :id]], :right_primary_key=>:album_id, :order=>[:blah3, :blah4]
|
|
1152
|
-
@c1.eager_graph(:tags=>:tracks).sql.
|
|
1152
|
+
@c1.eager_graph(:tags=>:tracks).sql.must_equal 'SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id) ORDER BY tags.blah1, tags.blah2, tracks.blah3, tracks.blah4'
|
|
1153
1153
|
end
|
|
1154
1154
|
|
|
1155
1155
|
it "should use the correct qualifier when graphing multiple tables with extra conditions" do
|
|
1156
1156
|
@c1.many_through_many :tags, [{:table=>:albums_artists, :left=>:artist_id, :right=>:album_id, :conditions=>{:a=>:b}}, {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]]
|
|
1157
1157
|
@c1.many_through_many :albums, [{:table=>:albums_artists, :left=>:artist_id, :right=>:album_id, :conditions=>{:c=>:d}}]
|
|
1158
|
-
@c1.eager_graph(:tags, :albums).sql.
|
|
1158
|
+
@c1.eager_graph(:tags, :albums).sql.must_equal 'SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON ((albums_artists.artist_id = artists.id) AND (albums_artists.a = artists.b)) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON ((albums_artists_0.artist_id = artists.id) AND (albums_artists_0.c = artists.d)) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)'
|
|
1159
1159
|
end
|
|
1160
1160
|
end
|
|
1161
1161
|
|
|
@@ -1177,30 +1177,30 @@ describe "many_through_many associations with non-column expression keys" do
|
|
|
1177
1177
|
end
|
|
1178
1178
|
|
|
1179
1179
|
it "should have working regular association methods" do
|
|
1180
|
-
@Foo.first.foos.
|
|
1181
|
-
@db.sqls.
|
|
1180
|
+
@Foo.first.foos.must_equal [@foo]
|
|
1181
|
+
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT foos.* FROM foos INNER JOIN f ON (f.r[1] = foos.object_ids[0]) INNER JOIN f AS f_0 ON (f_0.r[0] = f.l[1]) WHERE (f_0.l[0] = 2)"]
|
|
1182
1182
|
end
|
|
1183
1183
|
|
|
1184
1184
|
it "should have working eager loading methods" do
|
|
1185
1185
|
@db.fetch = [[{:id=>1, :object_ids=>[2]}], [{:id=>1, :object_ids=>[2], :x_foreign_key_x=>2}]]
|
|
1186
|
-
@Foo.eager(:foos).all.map{|o| [o, o.foos]}.
|
|
1187
|
-
@db.sqls.
|
|
1186
|
+
@Foo.eager(:foos).all.map{|o| [o, o.foos]}.must_equal [[@foo, [@foo]]]
|
|
1187
|
+
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT foos.*, f_0.l[0] AS x_foreign_key_x FROM foos INNER JOIN f ON (f.r[1] = foos.object_ids[0]) INNER JOIN f AS f_0 ON (f_0.r[0] = f.l[1]) WHERE (f_0.l[0] IN (2))"]
|
|
1188
1188
|
end
|
|
1189
1189
|
|
|
1190
1190
|
it "should have working eager graphing methods" do
|
|
1191
1191
|
@db.fetch = {:id=>1, :object_ids=>[2], :foos_0_id=>1, :foos_0_object_ids=>[2]}
|
|
1192
|
-
@Foo.eager_graph(:foos).all.map{|o| [o, o.foos]}.
|
|
1193
|
-
@db.sqls.
|
|
1192
|
+
@Foo.eager_graph(:foos).all.map{|o| [o, o.foos]}.must_equal [[@foo, [@foo]]]
|
|
1193
|
+
@db.sqls.must_equal ["SELECT foos.id, foos.object_ids, foos_0.id AS foos_0_id, foos_0.object_ids AS foos_0_object_ids FROM foos LEFT OUTER JOIN f ON (f.l[0] = foos.object_ids[0]) LEFT OUTER JOIN f AS f_0 ON (f_0.l[1] = f.r[0]) LEFT OUTER JOIN foos AS foos_0 ON (foos_0.object_ids[0] = f_0.r[1])"]
|
|
1194
1194
|
end
|
|
1195
1195
|
|
|
1196
1196
|
it "should have working filter by associations with model instances" do
|
|
1197
|
-
@Foo.first(:foos=>@foo).
|
|
1198
|
-
@db.sqls.
|
|
1197
|
+
@Foo.first(:foos=>@foo).must_equal @foo
|
|
1198
|
+
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT f.l[0] FROM f INNER JOIN f AS f_0 ON (f_0.l[1] = f.r[0]) WHERE ((f_0.r[1] = 2) AND (f.l[0] IS NOT NULL)))) LIMIT 1"]
|
|
1199
1199
|
end
|
|
1200
1200
|
|
|
1201
1201
|
it "should have working filter by associations with model datasets" do
|
|
1202
|
-
@Foo.first(:foos=>@Foo.where(:id=>@foo.id)).
|
|
1203
|
-
@db.sqls.
|
|
1202
|
+
@Foo.first(:foos=>@Foo.where(:id=>@foo.id)).must_equal @foo
|
|
1203
|
+
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT f.l[0] FROM f INNER JOIN f AS f_0 ON (f_0.l[1] = f.r[0]) WHERE ((f_0.r[1] IN (SELECT foos.object_ids[0] FROM foos WHERE ((id = 1) AND (foos.object_ids[0] IS NOT NULL)))) AND (f.l[0] IS NOT NULL)))) LIMIT 1"]
|
|
1204
1204
|
end
|
|
1205
1205
|
end
|
|
1206
1206
|
|
|
@@ -1231,10 +1231,10 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1231
1231
|
@c2.dataset._fetch = {:id=>4, :x_foreign_key_x=>3}
|
|
1232
1232
|
@c1.one_through_many :tag, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:id3
|
|
1233
1233
|
a = @c1.eager(:tag).all
|
|
1234
|
-
a.
|
|
1235
|
-
DB.sqls.
|
|
1236
|
-
a.first.tag.
|
|
1237
|
-
DB.sqls.
|
|
1234
|
+
a.must_equal [@c1.load(:id => 1)]
|
|
1235
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (3))"]
|
|
1236
|
+
a.first.tag.must_equal @c2.load(:id=>4)
|
|
1237
|
+
DB.sqls.must_equal []
|
|
1238
1238
|
end
|
|
1239
1239
|
|
|
1240
1240
|
it "should handle a :eager_loading_predicate_key option to change the SQL used in the lookup" do
|
|
@@ -1242,22 +1242,22 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1242
1242
|
@c2.dataset._fetch = {:id=>4, :x_foreign_key_x=>1}
|
|
1243
1243
|
@c1.one_through_many :tag, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_loading_predicate_key=>Sequel./(:albums_artists__artist_id, 3)
|
|
1244
1244
|
a = @c1.eager(:tag).all
|
|
1245
|
-
a.
|
|
1246
|
-
DB.sqls.
|
|
1247
|
-
a.first.tag.
|
|
1245
|
+
a.must_equal [@c1.load(:id => 1)]
|
|
1246
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT tags.*, (albums_artists.artist_id / 3) AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id / 3) IN (1))"]
|
|
1247
|
+
a.first.tag.must_equal @c2.load(:id=>4)
|
|
1248
1248
|
end
|
|
1249
1249
|
|
|
1250
1250
|
it "should raise an error if in invalid form of through is used" do
|
|
1251
|
-
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id]]}.
|
|
1252
|
-
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], {:table=>:album_tags, :left=>:album_id}]}.
|
|
1253
|
-
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], :album_tags]}.
|
|
1251
|
+
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id]]}.must_raise(Sequel::Error)
|
|
1252
|
+
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], {:table=>:album_tags, :left=>:album_id}]}.must_raise(Sequel::Error)
|
|
1253
|
+
proc{@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], :album_tags]}.must_raise(Sequel::Error)
|
|
1254
1254
|
end
|
|
1255
1255
|
|
|
1256
1256
|
it "should allow only two arguments with the :through option" do
|
|
1257
1257
|
@c1.one_through_many :tag, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1258
1258
|
n = @c1.load(:id => 1234)
|
|
1259
|
-
n.tag_dataset.sql.
|
|
1260
|
-
n.tag.
|
|
1259
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1260
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1261
1261
|
end
|
|
1262
1262
|
|
|
1263
1263
|
it "should be clonable" do
|
|
@@ -1265,15 +1265,15 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1265
1265
|
@c1.many_through_many :tags, :clone=>:tag
|
|
1266
1266
|
@c1.one_through_many :tag, :clone=>:tags
|
|
1267
1267
|
n = @c1.load(:id => 1234)
|
|
1268
|
-
n.tag_dataset.sql.
|
|
1269
|
-
n.tag.
|
|
1268
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1269
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1270
1270
|
end
|
|
1271
1271
|
|
|
1272
1272
|
it "should use join tables given" do
|
|
1273
1273
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1274
1274
|
n = @c1.load(:id => 1234)
|
|
1275
|
-
n.tag_dataset.sql.
|
|
1276
|
-
n.tag.
|
|
1275
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1276
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1277
1277
|
end
|
|
1278
1278
|
|
|
1279
1279
|
it "should handle multiple aliasing of tables" do
|
|
@@ -1282,8 +1282,8 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1282
1282
|
end
|
|
1283
1283
|
@c1.one_through_many :album, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id], [:artists, :id, :id], [:albums_artists, :artist_id, :album_id]]
|
|
1284
1284
|
n = @c1.load(:id => 1234)
|
|
1285
|
-
n.album_dataset.sql.
|
|
1286
|
-
n.album.
|
|
1285
|
+
n.album_dataset.sql.must_equal 'SELECT albums.* FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) INNER JOIN artists ON (artists.id = albums_artists.artist_id) INNER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) INNER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) INNER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) WHERE (albums_artists_1.artist_id = 1234) LIMIT 1'
|
|
1286
|
+
n.album.must_equal Album.load(:id=>1, :x=>1)
|
|
1287
1287
|
ensure
|
|
1288
1288
|
Object.send(:remove_const, :Album)
|
|
1289
1289
|
end
|
|
@@ -1292,311 +1292,311 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1292
1292
|
it "should use explicit class if given" do
|
|
1293
1293
|
@c1.one_through_many :album_tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag
|
|
1294
1294
|
n = @c1.load(:id => 1234)
|
|
1295
|
-
n.album_tag_dataset.sql.
|
|
1296
|
-
n.album_tag.
|
|
1295
|
+
n.album_tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1296
|
+
n.album_tag.must_equal @c2.load(:id=>1)
|
|
1297
1297
|
end
|
|
1298
1298
|
|
|
1299
1299
|
it "should accept :left_primary_key and :right_primary_key option for primary keys to use in current and associated table" do
|
|
1300
1300
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :right_primary_key=>:tag_id, :left_primary_key=>:yyy
|
|
1301
1301
|
n = @c1.load(:id => 1234)
|
|
1302
1302
|
n.yyy = 85
|
|
1303
|
-
n.tag_dataset.sql.
|
|
1304
|
-
n.tag.
|
|
1303
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.tag_id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 85) LIMIT 1'
|
|
1304
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1305
1305
|
end
|
|
1306
1306
|
|
|
1307
1307
|
it "should handle composite keys" do
|
|
1308
1308
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1309
1309
|
n = @c1.load(:id => 1234)
|
|
1310
1310
|
n.yyy = 85
|
|
1311
|
-
n.tag_dataset.sql.
|
|
1312
|
-
n.tag.
|
|
1311
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1 = 1234) AND (albums_artists.b2 = 85)) LIMIT 1'
|
|
1312
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1313
1313
|
end
|
|
1314
1314
|
|
|
1315
1315
|
it "should allowing filtering by one_through_many associations" do
|
|
1316
1316
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1317
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1317
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
1318
1318
|
end
|
|
1319
1319
|
|
|
1320
1320
|
it "should allowing filtering by one_through_many associations with a single through table" do
|
|
1321
1321
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id]]
|
|
1322
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1322
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists WHERE ((albums_artists.album_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
1323
1323
|
end
|
|
1324
1324
|
|
|
1325
1325
|
it "should allowing filtering by one_through_many associations with aliased tables" do
|
|
1326
1326
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums_artists, :id, :id], [:albums_artists, :album_id, :tag_id]]
|
|
1327
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1327
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.id = albums_artists.album_id) INNER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_artists_0.id) WHERE ((albums_artists_1.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
1328
1328
|
end
|
|
1329
1329
|
|
|
1330
1330
|
it "should allowing filtering by one_through_many associations with composite keys" do
|
|
1331
1331
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1332
|
-
@c1.filter(:tag=>@c2.load(:h1=>1234, :h2=>85)).sql.
|
|
1332
|
+
@c1.filter(:tag=>@c2.load(:h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE ((albums_tags.g1 = 1234) AND (albums_tags.g2 = 85) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
1333
1333
|
end
|
|
1334
1334
|
|
|
1335
1335
|
it "should allowing filtering by one_through_many associations with :conditions" do
|
|
1336
1336
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1337
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1337
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234))))"
|
|
1338
1338
|
end
|
|
1339
1339
|
|
|
1340
1340
|
it "should allowing filtering by one_through_many associations with :conditions with a single through table" do
|
|
1341
1341
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id]], :conditions=>{:name=>'A'}
|
|
1342
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1342
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_artists ON (albums_artists.album_id = tags.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234))))"
|
|
1343
1343
|
end
|
|
1344
1344
|
|
|
1345
1345
|
it "should allowing filtering by one_through_many associations with :conditions and composite keys" do
|
|
1346
1346
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1347
|
-
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
1347
|
+
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id = 1))))"
|
|
1348
1348
|
end
|
|
1349
1349
|
|
|
1350
1350
|
it "should allowing filtering by one_through_many associations with :order" do
|
|
1351
1351
|
def (@c2.dataset).supports_distinct_on?; true end
|
|
1352
1352
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:name
|
|
1353
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1353
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id IS NOT NULL) AND ((albums_artists.artist_id, tags.id) IN (SELECT DISTINCT ON (albums_artists.artist_id) albums_artists.artist_id, tags.id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) ORDER BY albums_artists.artist_id, name)) AND (tags.id = 1234))))'
|
|
1354
1354
|
end
|
|
1355
1355
|
|
|
1356
1356
|
it "should allowing filtering by one_through_many associations with :order and composite keys" do
|
|
1357
1357
|
def (@c2.dataset).supports_distinct_on?; true end
|
|
1358
1358
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :order=>:name
|
|
1359
|
-
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
1359
|
+
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND ((albums_artists.b1, albums_artists.b2, tags.id) IN (SELECT DISTINCT ON (albums_artists.b1, albums_artists.b2) albums_artists.b1, albums_artists.b2, tags.id FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) ORDER BY albums_artists.b1, albums_artists.b2, name)) AND (tags.id = 1))))'
|
|
1360
1360
|
end
|
|
1361
1361
|
|
|
1362
1362
|
it "should allowing filtering by one_through_many associations with :order and :conditions" do
|
|
1363
1363
|
def (@c2.dataset).supports_distinct_on?; true end
|
|
1364
1364
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}, :order=>:name
|
|
1365
|
-
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.
|
|
1365
|
+
@c1.filter(:tag=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND ((albums_artists.artist_id, tags.id) IN (SELECT DISTINCT ON (albums_artists.artist_id) albums_artists.artist_id, tags.id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (name = 'A') ORDER BY albums_artists.artist_id, name)) AND (tags.id = 1234))))"
|
|
1366
1366
|
end
|
|
1367
1367
|
|
|
1368
1368
|
it "should allowing filtering by one_through_many associations with :order and :conditions and composite keys" do
|
|
1369
1369
|
def (@c2.dataset).supports_distinct_on?; true end
|
|
1370
1370
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}, :order=>:name
|
|
1371
|
-
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
1371
|
+
@c1.filter(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND ((albums_artists.b1, albums_artists.b2, tags.id) IN (SELECT DISTINCT ON (albums_artists.b1, albums_artists.b2) albums_artists.b1, albums_artists.b2, tags.id FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE (name = 'A') ORDER BY albums_artists.b1, albums_artists.b2, name)) AND (tags.id = 1))))"
|
|
1372
1372
|
end
|
|
1373
1373
|
|
|
1374
1374
|
it "should allowing excluding by one_through_many associations" do
|
|
1375
1375
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1376
|
-
@c1.exclude(:tag=>@c2.load(:id=>1234)).sql.
|
|
1376
|
+
@c1.exclude(:tag=>@c2.load(:id=>1234)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id = 1234) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
1377
1377
|
end
|
|
1378
1378
|
|
|
1379
1379
|
it "should allowing excluding by one_through_many associations with composite keys" do
|
|
1380
1380
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1381
|
-
@c1.exclude(:tag=>@c2.load(:h1=>1234, :h2=>85)).sql.
|
|
1381
|
+
@c1.exclude(:tag=>@c2.load(:h1=>1234, :h2=>85)).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE ((albums_tags.g1 = 1234) AND (albums_tags.g2 = 85) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
1382
1382
|
end
|
|
1383
1383
|
|
|
1384
1384
|
it "should allowing excluding by one_through_many associations with :conditions" do
|
|
1385
1385
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1386
|
-
@c1.exclude(:tag=>@c2.load(:id=>1234)).sql.
|
|
1386
|
+
@c1.exclude(:tag=>@c2.load(:id=>1234)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id = 1234)))) OR (artists.id IS NULL))"
|
|
1387
1387
|
end
|
|
1388
1388
|
|
|
1389
1389
|
it "should allowing excluding by one_through_many associations with :conditions and composite keys" do
|
|
1390
1390
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1391
|
-
@c1.exclude(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.
|
|
1391
|
+
@c1.exclude(:tag=>@c2.load(:id=>1, :h1=>1234, :h2=>85)).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id = 1)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
1392
1392
|
end
|
|
1393
1393
|
|
|
1394
1394
|
it "should allowing filtering by multiple one_through_many associations" do
|
|
1395
1395
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1396
|
-
@c1.filter(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
1396
|
+
@c1.filter(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (1234, 2345)) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
1397
1397
|
end
|
|
1398
1398
|
|
|
1399
1399
|
it "should allowing filtering by multiple one_through_many associations with composite keys" do
|
|
1400
1400
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1401
|
-
@c1.filter(:tag=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.
|
|
1401
|
+
@c1.filter(:tag=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN ((1234, 85), (2345, 95))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
1402
1402
|
end
|
|
1403
1403
|
|
|
1404
1404
|
it "should allowing filtering by multiple one_through_many associations with :conditions" do
|
|
1405
1405
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1406
|
-
@c1.filter(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
1406
|
+
@c1.filter(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (1234, 2345)))))"
|
|
1407
1407
|
end
|
|
1408
1408
|
|
|
1409
1409
|
it "should allowing filtering by multiple one_through_many associations with :conditions and composite keys" do
|
|
1410
1410
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1411
|
-
@c1.filter(:tag=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.
|
|
1411
|
+
@c1.filter(:tag=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (1, 2)))))"
|
|
1412
1412
|
end
|
|
1413
1413
|
|
|
1414
1414
|
it "should allowing excluding by multiple one_through_many associations" do
|
|
1415
1415
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1416
|
-
@c1.exclude(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
1416
|
+
@c1.exclude(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (1234, 2345)) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
1417
1417
|
end
|
|
1418
1418
|
|
|
1419
1419
|
it "should allowing excluding by multiple one_through_many associations with composite keys" do
|
|
1420
1420
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1421
|
-
@c1.exclude(:tag=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.
|
|
1421
|
+
@c1.exclude(:tag=>[@c2.load(:h1=>1234, :h2=>85), @c2.load(:h1=>2345, :h2=>95)]).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN ((1234, 85), (2345, 95))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
1422
1422
|
end
|
|
1423
1423
|
|
|
1424
1424
|
it "should allowing excluding by multiple one_through_many associations with :conditions" do
|
|
1425
1425
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1426
|
-
@c1.exclude(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.
|
|
1426
|
+
@c1.exclude(:tag=>[@c2.load(:id=>1234), @c2.load(:id=>2345)]).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (1234, 2345))))) OR (artists.id IS NULL))"
|
|
1427
1427
|
end
|
|
1428
1428
|
|
|
1429
1429
|
it "should allowing excluding by multiple one_through_many associations with :conditions and composite keys" do
|
|
1430
1430
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1431
|
-
@c1.exclude(:tag=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.
|
|
1431
|
+
@c1.exclude(:tag=>[@c2.load(:id=>1, :h1=>1234, :h2=>85), @c2.load(:id=>2, :h1=>2345, :h2=>95)]).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (1, 2))))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
1432
1432
|
end
|
|
1433
1433
|
|
|
1434
1434
|
it "should allowing filtering/excluding one_through_many associations with NULL values" do
|
|
1435
1435
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1436
|
-
@c1.filter(:tag=>@c2.new).sql.
|
|
1437
|
-
@c1.exclude(:tag=>@c2.new).sql.
|
|
1436
|
+
@c1.filter(:tag=>@c2.new).sql.must_equal 'SELECT * FROM artists WHERE \'f\''
|
|
1437
|
+
@c1.exclude(:tag=>@c2.new).sql.must_equal 'SELECT * FROM artists WHERE \'t\''
|
|
1438
1438
|
end
|
|
1439
1439
|
|
|
1440
1440
|
it "should allowing filtering by one_through_many association datasets" do
|
|
1441
1441
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1442
|
-
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.
|
|
1442
|
+
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_artists.artist_id IS NOT NULL))))'
|
|
1443
1443
|
end
|
|
1444
1444
|
|
|
1445
1445
|
it "should allowing filtering by one_through_many association datasets with composite keys" do
|
|
1446
1446
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1447
|
-
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.
|
|
1447
|
+
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN (SELECT tags.h1, tags.h2 FROM tags WHERE ((x = 1) AND (tags.h1 IS NOT NULL) AND (tags.h2 IS NOT NULL)))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL))))'
|
|
1448
1448
|
end
|
|
1449
1449
|
|
|
1450
1450
|
it "should allowing filtering by one_through_many association datasets with :conditions" do
|
|
1451
1451
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1452
|
-
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.
|
|
1452
|
+
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE (artists.id IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
|
|
1453
1453
|
end
|
|
1454
1454
|
|
|
1455
1455
|
it "should allowing filtering by one_through_many association datasets with :conditions and composite keys" do
|
|
1456
1456
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1457
|
-
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.
|
|
1457
|
+
@c1.filter(:tag=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id, artists.yyy) IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
|
|
1458
1458
|
end
|
|
1459
1459
|
|
|
1460
1460
|
it "should allowing excluding by one_through_many association datasets" do
|
|
1461
1461
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1462
|
-
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.
|
|
1462
|
+
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM albums_artists INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_artists.artist_id IS NOT NULL)))) OR (artists.id IS NULL))'
|
|
1463
1463
|
end
|
|
1464
1464
|
|
|
1465
1465
|
it "should allowing excluding by one_through_many association datasets with composite keys" do
|
|
1466
1466
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1467
|
-
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.
|
|
1467
|
+
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.must_equal 'SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM albums_artists INNER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) INNER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) WHERE (((albums_tags.g1, albums_tags.g2) IN (SELECT tags.h1, tags.h2 FROM tags WHERE ((x = 1) AND (tags.h1 IS NOT NULL) AND (tags.h2 IS NOT NULL)))) AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL)))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))'
|
|
1468
1468
|
end
|
|
1469
1469
|
|
|
1470
1470
|
it "should allowing excluding by one_through_many association datasets with :conditions" do
|
|
1471
1471
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:name=>'A'}
|
|
1472
|
-
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.
|
|
1472
|
+
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE ((artists.id NOT IN (SELECT albums_artists.artist_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((name = 'A') AND (albums_artists.artist_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (artists.id IS NULL))"
|
|
1473
1473
|
end
|
|
1474
1474
|
|
|
1475
1475
|
it "should allowing excluding by one_through_many association datasets with :conditions and composite keys" do
|
|
1476
1476
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy], :conditions=>{:name=>'A'}
|
|
1477
|
-
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.
|
|
1477
|
+
@c1.exclude(:tag=>@c2.filter(:x=>1)).sql.must_equal "SELECT * FROM artists WHERE (((artists.id, artists.yyy) NOT IN (SELECT albums_artists.b1, albums_artists.b2 FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((name = 'A') AND (albums_artists.b1 IS NOT NULL) AND (albums_artists.b2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (artists.id IS NULL) OR (artists.yyy IS NULL))"
|
|
1478
1478
|
end
|
|
1479
1479
|
|
|
1480
1480
|
it "should support a :conditions option" do
|
|
1481
1481
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
1482
1482
|
n = @c1.load(:id => 1234)
|
|
1483
|
-
n.tag_dataset.sql.
|
|
1484
|
-
n.tag.
|
|
1483
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 32) AND (albums_artists.artist_id = 1234)) LIMIT 1'
|
|
1484
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1485
1485
|
|
|
1486
1486
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>['a = ?', 42]
|
|
1487
1487
|
n = @c1.load(:id => 1234)
|
|
1488
|
-
n.tag_dataset.sql.
|
|
1489
|
-
n.tag.
|
|
1488
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 42) AND (albums_artists.artist_id = 1234)) LIMIT 1'
|
|
1489
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1490
1490
|
end
|
|
1491
1491
|
|
|
1492
1492
|
it "should support an :order option" do
|
|
1493
1493
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah
|
|
1494
1494
|
n = @c1.load(:id => 1234)
|
|
1495
|
-
n.tag_dataset.sql.
|
|
1496
|
-
n.tag.
|
|
1495
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) ORDER BY blah LIMIT 1'
|
|
1496
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1497
1497
|
end
|
|
1498
1498
|
|
|
1499
1499
|
it "should support an array for the :order option" do
|
|
1500
1500
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
1501
1501
|
n = @c1.load(:id => 1234)
|
|
1502
|
-
n.tag_dataset.sql.
|
|
1503
|
-
n.tag.
|
|
1502
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) ORDER BY blah1, blah2 LIMIT 1'
|
|
1503
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1504
1504
|
end
|
|
1505
1505
|
|
|
1506
1506
|
it "should support a select option" do
|
|
1507
1507
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>:blah
|
|
1508
1508
|
n = @c1.load(:id => 1234)
|
|
1509
|
-
n.tag_dataset.sql.
|
|
1510
|
-
n.tag.
|
|
1509
|
+
n.tag_dataset.sql.must_equal 'SELECT blah FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1510
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1511
1511
|
end
|
|
1512
1512
|
|
|
1513
1513
|
it "should support an array for the select option" do
|
|
1514
1514
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>[Sequel::SQL::ColumnAll.new(:tags), :albums__name]
|
|
1515
1515
|
n = @c1.load(:id => 1234)
|
|
1516
|
-
n.tag_dataset.sql.
|
|
1517
|
-
n.tag.
|
|
1516
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.*, albums.name FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1517
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1518
1518
|
end
|
|
1519
1519
|
|
|
1520
1520
|
it "should accept a block" do
|
|
1521
1521
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]] do |ds| ds.filter(:yyy=>@yyy) end
|
|
1522
1522
|
n = @c1.load(:id => 1234)
|
|
1523
1523
|
n.yyy = 85
|
|
1524
|
-
n.tag_dataset.sql.
|
|
1525
|
-
n.tag.
|
|
1524
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id = 1234) AND (yyy = 85)) LIMIT 1'
|
|
1525
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1526
1526
|
end
|
|
1527
1527
|
|
|
1528
1528
|
it "should allow the :order option while accepting a block" do
|
|
1529
1529
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah do |ds| ds.filter(:yyy=>@yyy) end
|
|
1530
1530
|
n = @c1.load(:id => 1234)
|
|
1531
1531
|
n.yyy = 85
|
|
1532
|
-
n.tag_dataset.sql.
|
|
1533
|
-
n.tag.
|
|
1532
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id = 1234) AND (yyy = 85)) ORDER BY blah LIMIT 1'
|
|
1533
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1534
1534
|
end
|
|
1535
1535
|
|
|
1536
1536
|
it "should support a :dataset option that is used instead of the default" do
|
|
1537
1537
|
@c1.one_through_many :tag, [[:a, :b, :c]], :dataset=>proc{Tag.join(:albums_tags, [:tag_id]).join(:albums, [:album_id]).join(:albums_artists, [:album_id]).filter(:albums_artists__artist_id=>id)}
|
|
1538
1538
|
n = @c1.load(:id => 1234)
|
|
1539
|
-
n.tag_dataset.sql.
|
|
1540
|
-
n.tag.
|
|
1539
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags USING (tag_id) INNER JOIN albums USING (album_id) INNER JOIN albums_artists USING (album_id) WHERE (albums_artists.artist_id = 1234) LIMIT 1'
|
|
1540
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1541
1541
|
end
|
|
1542
1542
|
|
|
1543
1543
|
it "should support a :limit option to specify an offset" do
|
|
1544
1544
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :limit=>[nil, 10]
|
|
1545
1545
|
n = @c1.load(:id => 1234)
|
|
1546
|
-
n.tag_dataset.sql.
|
|
1547
|
-
n.tag.
|
|
1546
|
+
n.tag_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1 OFFSET 10'
|
|
1547
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1548
1548
|
end
|
|
1549
1549
|
|
|
1550
1550
|
it "should have the :eager option affect the _dataset method" do
|
|
1551
1551
|
@c2.many_to_many :fans
|
|
1552
1552
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:fans
|
|
1553
|
-
@c1.load(:id => 1234).tag_dataset.opts[:eager].
|
|
1553
|
+
@c1.load(:id => 1234).tag_dataset.opts[:eager].must_equal(:fans=>nil)
|
|
1554
1554
|
end
|
|
1555
1555
|
|
|
1556
1556
|
it "should return the associated object" do
|
|
1557
1557
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1558
|
-
@c1.load(:id => 1234).tag.
|
|
1559
|
-
DB.sqls.
|
|
1558
|
+
@c1.load(:id => 1234).tag.must_equal @c2.load(:id=>1)
|
|
1559
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1']
|
|
1560
1560
|
end
|
|
1561
1561
|
|
|
1562
1562
|
it "should populate cache when accessed" do
|
|
1563
1563
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1564
1564
|
n = @c1.load(:id => 1234)
|
|
1565
|
-
n.associations[:tag].
|
|
1566
|
-
DB.sqls.
|
|
1567
|
-
n.tag.
|
|
1568
|
-
DB.sqls.
|
|
1569
|
-
n.associations[:tag].
|
|
1570
|
-
DB.sqls.length.
|
|
1565
|
+
n.associations[:tag].must_equal nil
|
|
1566
|
+
DB.sqls.must_equal []
|
|
1567
|
+
n.tag.must_equal @c2.load(:id=>1)
|
|
1568
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1']
|
|
1569
|
+
n.associations[:tag].must_equal n.tag
|
|
1570
|
+
DB.sqls.length.must_equal 0
|
|
1571
1571
|
end
|
|
1572
1572
|
|
|
1573
1573
|
it "should use cache if available" do
|
|
1574
1574
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1575
1575
|
n = @c1.load(:id => 1234)
|
|
1576
1576
|
n.associations[:tag] = nil
|
|
1577
|
-
n.tag.
|
|
1578
|
-
DB.sqls.
|
|
1577
|
+
n.tag.must_equal nil
|
|
1578
|
+
DB.sqls.must_equal []
|
|
1579
1579
|
end
|
|
1580
1580
|
|
|
1581
1581
|
it "should not use cache if asked to reload" do
|
|
1582
1582
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1583
1583
|
n = @c1.load(:id => 1234)
|
|
1584
1584
|
n.associations[:tag] = nil
|
|
1585
|
-
DB.sqls.
|
|
1586
|
-
n.tag(true).
|
|
1587
|
-
DB.sqls.
|
|
1588
|
-
n.associations[:tag].
|
|
1589
|
-
DB.sqls.length.
|
|
1585
|
+
DB.sqls.must_equal []
|
|
1586
|
+
n.tag(true).must_equal @c2.load(:id=>1)
|
|
1587
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1234) LIMIT 1']
|
|
1588
|
+
n.associations[:tag].must_equal n.tag
|
|
1589
|
+
DB.sqls.length.must_equal 0
|
|
1590
1590
|
end
|
|
1591
1591
|
|
|
1592
1592
|
it "should not add associations methods directly to class" do
|
|
1593
1593
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
|
|
1594
1594
|
im = @c1.instance_methods.collect{|x| x.to_s}
|
|
1595
|
-
im.
|
|
1596
|
-
im.
|
|
1595
|
+
im.must_include('tag')
|
|
1596
|
+
im.must_include('tag_dataset')
|
|
1597
1597
|
im2 = @c1.instance_methods(false).collect{|x| x.to_s}
|
|
1598
|
-
im2.
|
|
1599
|
-
im2.
|
|
1598
|
+
im2.wont_include('tag')
|
|
1599
|
+
im2.wont_include('tag_dataset')
|
|
1600
1600
|
end
|
|
1601
1601
|
|
|
1602
1602
|
it "should support after_load association callback" do
|
|
@@ -1611,8 +1611,8 @@ describe Sequel::Model, "one_through_many" do
|
|
|
1611
1611
|
@c2.dataset._fetch = [{:id=>20}]
|
|
1612
1612
|
p = @c1.load(:id=>10, :parent_id=>20)
|
|
1613
1613
|
p.tag
|
|
1614
|
-
h.
|
|
1615
|
-
p.tag.pk.
|
|
1614
|
+
h.must_equal [400]
|
|
1615
|
+
p.tag.pk.must_equal 20
|
|
1616
1616
|
end
|
|
1617
1617
|
end
|
|
1618
1618
|
|
|
@@ -1677,78 +1677,78 @@ describe "one_through_many eager loading methods" do
|
|
|
1677
1677
|
|
|
1678
1678
|
it "should eagerly load a single one_through_many association" do
|
|
1679
1679
|
a = @c1.eager(:tag).all
|
|
1680
|
-
a.
|
|
1681
|
-
DB.sqls.
|
|
1682
|
-
a.first.tag.
|
|
1683
|
-
DB.sqls.length.
|
|
1680
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1681
|
+
DB.sqls.must_equal ['SELECT * FROM artists', 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
1682
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1683
|
+
DB.sqls.length.must_equal 0
|
|
1684
1684
|
end
|
|
1685
1685
|
|
|
1686
1686
|
it "should eagerly load multiple associations in a single call" do
|
|
1687
1687
|
a = @c1.eager(:tag, :album).all
|
|
1688
|
-
a.
|
|
1688
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1689
1689
|
sqls = DB.sqls
|
|
1690
|
-
sqls.length.
|
|
1691
|
-
sqls[0].
|
|
1692
|
-
sqls[1..-1].
|
|
1693
|
-
sqls[1..-1].
|
|
1690
|
+
sqls.length.must_equal 3
|
|
1691
|
+
sqls[0].must_equal 'SELECT * FROM artists'
|
|
1692
|
+
sqls[1..-1].must_include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
1693
|
+
sqls[1..-1].must_include('SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
1694
1694
|
a = a.first
|
|
1695
|
-
a.tag.
|
|
1696
|
-
a.album.
|
|
1697
|
-
DB.sqls.length.
|
|
1695
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1696
|
+
a.album.must_equal Album.load(:id=>3)
|
|
1697
|
+
DB.sqls.length.must_equal 0
|
|
1698
1698
|
end
|
|
1699
1699
|
|
|
1700
1700
|
it "should eagerly load multiple associations in separate" do
|
|
1701
1701
|
a = @c1.eager(:tag).eager(:album).all
|
|
1702
|
-
a.
|
|
1702
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1703
1703
|
sqls = DB.sqls
|
|
1704
|
-
sqls.length.
|
|
1705
|
-
sqls[0].
|
|
1706
|
-
sqls[1..-1].
|
|
1707
|
-
sqls[1..-1].
|
|
1704
|
+
sqls.length.must_equal 3
|
|
1705
|
+
sqls[0].must_equal 'SELECT * FROM artists'
|
|
1706
|
+
sqls[1..-1].must_include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
1707
|
+
sqls[1..-1].must_include('SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))')
|
|
1708
1708
|
a = a.first
|
|
1709
|
-
a.tag.
|
|
1710
|
-
a.album.
|
|
1711
|
-
DB.sqls.length.
|
|
1709
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1710
|
+
a.album.must_equal Album.load(:id=>3)
|
|
1711
|
+
DB.sqls.length.must_equal 0
|
|
1712
1712
|
end
|
|
1713
1713
|
|
|
1714
1714
|
it "should allow cascading of eager loading for associations of associated models" do
|
|
1715
1715
|
a = @c1.eager(:tag=>:track).all
|
|
1716
|
-
a.
|
|
1717
|
-
DB.sqls.
|
|
1716
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1717
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1718
1718
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))',
|
|
1719
1719
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
1720
1720
|
a = a.first
|
|
1721
|
-
a.tag.
|
|
1722
|
-
a.tag.track.
|
|
1723
|
-
DB.sqls.length.
|
|
1721
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1722
|
+
a.tag.track.must_equal Track.load(:id=>4)
|
|
1723
|
+
DB.sqls.length.must_equal 0
|
|
1724
1724
|
end
|
|
1725
1725
|
|
|
1726
1726
|
it "should cascade eagerly loading when the :eager association option is used" do
|
|
1727
1727
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:track
|
|
1728
1728
|
a = @c1.eager(:tag).all
|
|
1729
|
-
a.
|
|
1730
|
-
DB.sqls.
|
|
1729
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1730
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1731
1731
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))',
|
|
1732
1732
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
1733
1733
|
a = a.first
|
|
1734
|
-
a.tag.
|
|
1735
|
-
a.tag.track.
|
|
1736
|
-
DB.sqls.length.
|
|
1734
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1735
|
+
a.tag.track.must_equal Track.load(:id=>4)
|
|
1736
|
+
DB.sqls.length.must_equal 0
|
|
1737
1737
|
end
|
|
1738
1738
|
|
|
1739
1739
|
it "should respect :eager when lazily loading an association" do
|
|
1740
1740
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:track
|
|
1741
1741
|
a = @c1.load(:id=>1)
|
|
1742
|
-
a.tag.
|
|
1743
|
-
DB.sqls.
|
|
1742
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1743
|
+
DB.sqls.must_equal ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1) LIMIT 1',
|
|
1744
1744
|
'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) WHERE (albums_tags.tag_id IN (2))']
|
|
1745
|
-
a.tag.track.
|
|
1746
|
-
DB.sqls.length.
|
|
1745
|
+
a.tag.track.must_equal Track.load(:id=>4)
|
|
1746
|
+
DB.sqls.length.must_equal 0
|
|
1747
1747
|
end
|
|
1748
1748
|
|
|
1749
1749
|
it "should raise error if attempting to eagerly load an association using :eager_graph option" do
|
|
1750
1750
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:track
|
|
1751
|
-
proc{@c1.eager(:tag).all}.
|
|
1751
|
+
proc{@c1.eager(:tag).all}.must_raise(Sequel::Error)
|
|
1752
1752
|
end
|
|
1753
1753
|
|
|
1754
1754
|
it "should respect :eager_graph when lazily loading an association" do
|
|
@@ -1761,72 +1761,72 @@ describe "one_through_many eager loading methods" do
|
|
|
1761
1761
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:track
|
|
1762
1762
|
a = @c1.load(:id=>1)
|
|
1763
1763
|
a.tag
|
|
1764
|
-
DB.sqls.
|
|
1765
|
-
a.tag.
|
|
1766
|
-
a.tag.track.
|
|
1767
|
-
DB.sqls.length.
|
|
1764
|
+
DB.sqls.must_equal [ 'SELECT tags.id, track.id AS track_id FROM (SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id = 1) LIMIT 1) AS tags LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums ON (albums.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks AS track ON (track.album_id = albums.id)']
|
|
1765
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1766
|
+
a.tag.track.must_equal Track.load(:id=>4)
|
|
1767
|
+
DB.sqls.length.must_equal 0
|
|
1768
1768
|
end
|
|
1769
1769
|
|
|
1770
1770
|
it "should respect :conditions when eagerly loading" do
|
|
1771
1771
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
1772
1772
|
a = @c1.eager(:tag).all
|
|
1773
|
-
a.
|
|
1774
|
-
DB.sqls.
|
|
1773
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1774
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1775
1775
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((a = 32) AND (albums_artists.artist_id IN (1)))']
|
|
1776
|
-
a.first.tag.
|
|
1777
|
-
DB.sqls.length.
|
|
1776
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1777
|
+
DB.sqls.length.must_equal 0
|
|
1778
1778
|
end
|
|
1779
1779
|
|
|
1780
1780
|
it "should respect :order when eagerly loading" do
|
|
1781
1781
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah, :eager_limit_strategy=>:ruby
|
|
1782
1782
|
a = @c1.eager(:tag).all
|
|
1783
|
-
a.
|
|
1784
|
-
DB.sqls.
|
|
1783
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1784
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1785
1785
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1)) ORDER BY blah']
|
|
1786
|
-
a.first.tag.
|
|
1787
|
-
DB.sqls.length.
|
|
1786
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1787
|
+
DB.sqls.length.must_equal 0
|
|
1788
1788
|
end
|
|
1789
1789
|
|
|
1790
1790
|
it "should use the association's block when eager loading by default" do
|
|
1791
1791
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]] do |ds| ds.filter(:a) end
|
|
1792
1792
|
a = @c1.eager(:tag).all
|
|
1793
|
-
a.
|
|
1794
|
-
DB.sqls.
|
|
1793
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1794
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1795
1795
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (a AND (albums_artists.artist_id IN (1)))']
|
|
1796
|
-
a.first.tag.
|
|
1797
|
-
DB.sqls.length.
|
|
1796
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1797
|
+
DB.sqls.length.must_equal 0
|
|
1798
1798
|
end
|
|
1799
1799
|
|
|
1800
1800
|
it "should use the :eager_block option when eager loading if given" do
|
|
1801
1801
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_block=>proc{|ds| ds.filter(:b)} do |ds| ds.filter(:a) end
|
|
1802
1802
|
a = @c1.eager(:tag).all
|
|
1803
|
-
a.
|
|
1804
|
-
DB.sqls.
|
|
1803
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1804
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1805
1805
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (b AND (albums_artists.artist_id IN (1)))']
|
|
1806
|
-
a.first.tag.
|
|
1807
|
-
DB.sqls.length.
|
|
1806
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1807
|
+
DB.sqls.length.must_equal 0
|
|
1808
1808
|
end
|
|
1809
1809
|
|
|
1810
1810
|
it "should respect the :limit option on a one_through_many association" do
|
|
1811
1811
|
@c1.one_through_many :second_tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1]
|
|
1812
1812
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>6}]
|
|
1813
1813
|
a = @c1.eager(:second_tag).all
|
|
1814
|
-
a.
|
|
1815
|
-
DB.sqls.
|
|
1814
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1815
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1816
1816
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (1 = albums_artists.artist_id) LIMIT 1 OFFSET 1) AS t1']
|
|
1817
|
-
a.first.second_tag.
|
|
1818
|
-
DB.sqls.length.
|
|
1817
|
+
a.first.second_tag.must_equal Tag.load(:id=>6)
|
|
1818
|
+
DB.sqls.length.must_equal 0
|
|
1819
1819
|
end
|
|
1820
1820
|
|
|
1821
1821
|
it "should respect the :limit option on a one_through_many association using the :ruby strategy" do
|
|
1822
1822
|
@c1.one_through_many :second_tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1], :eager_limit_strategy=>:ruby
|
|
1823
1823
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5}, {:x_foreign_key_x=>1, :id=>6}]
|
|
1824
1824
|
a = @c1.eager(:second_tag).all
|
|
1825
|
-
a.
|
|
1826
|
-
DB.sqls.
|
|
1825
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1826
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1827
1827
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
1828
|
-
a.first.second_tag.
|
|
1829
|
-
DB.sqls.length.
|
|
1828
|
+
a.first.second_tag.must_equal Tag.load(:id=>6)
|
|
1829
|
+
DB.sqls.length.must_equal 0
|
|
1830
1830
|
end
|
|
1831
1831
|
|
|
1832
1832
|
it "should eagerly load a single one_through_many association using the :distinct_on strategy" do
|
|
@@ -1834,10 +1834,10 @@ describe "one_through_many eager loading methods" do
|
|
|
1834
1834
|
@c1.one_through_many :second_tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :order=>:name, :eager_limit_strategy=>:distinct_on
|
|
1835
1835
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5}]
|
|
1836
1836
|
a = @c1.eager(:second_tag).all
|
|
1837
|
-
a.
|
|
1838
|
-
DB.sqls.
|
|
1839
|
-
a.first.second_tag.
|
|
1840
|
-
DB.sqls.length.
|
|
1837
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1838
|
+
DB.sqls.must_equal ['SELECT * FROM artists', "SELECT DISTINCT ON (albums_artists.artist_id) tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1)) ORDER BY albums_artists.artist_id, name"]
|
|
1839
|
+
a.first.second_tag.must_equal Tag.load(:id=>5)
|
|
1840
|
+
DB.sqls.length.must_equal 0
|
|
1841
1841
|
end
|
|
1842
1842
|
|
|
1843
1843
|
it "should eagerly load a single one_through_many association using the :window_function strategy" do
|
|
@@ -1845,11 +1845,11 @@ describe "one_through_many eager loading methods" do
|
|
|
1845
1845
|
@c1.one_through_many :second_tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1], :order=>:name, :eager_limit_strategy=>:window_function
|
|
1846
1846
|
Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5}]
|
|
1847
1847
|
a = @c1.eager(:second_tag).all
|
|
1848
|
-
a.
|
|
1849
|
-
DB.sqls.
|
|
1848
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1849
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1850
1850
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))) AS t1 WHERE (x_sequel_row_number_x = 2)']
|
|
1851
|
-
a.first.second_tag.
|
|
1852
|
-
DB.sqls.length.
|
|
1851
|
+
a.first.second_tag.must_equal Tag.load(:id=>5)
|
|
1852
|
+
DB.sqls.length.must_equal 0
|
|
1853
1853
|
end
|
|
1854
1854
|
|
|
1855
1855
|
it "should respect the :limit option on a one_through_many association with composite primary keys on the main table" do
|
|
@@ -1862,11 +1862,11 @@ describe "one_through_many eager loading methods" do
|
|
|
1862
1862
|
ds._fetch = {:id1=>1, :id2=>2}
|
|
1863
1863
|
Tag.dataset._fetch = [{:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>5}]
|
|
1864
1864
|
a = ds.all
|
|
1865
|
-
a.
|
|
1866
|
-
DB.sqls.
|
|
1865
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
1866
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1867
1867
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((1 = albums_artists.artist_id1) AND (2 = albums_artists.artist_id2)) ORDER BY name LIMIT 1 OFFSET 1) AS t1']
|
|
1868
|
-
a.first.second_tag.
|
|
1869
|
-
DB.sqls.length.
|
|
1868
|
+
a.first.second_tag.must_equal Tag.load(:id=>5)
|
|
1869
|
+
DB.sqls.length.must_equal 0
|
|
1870
1870
|
end
|
|
1871
1871
|
|
|
1872
1872
|
it "should respect the :limit option on a one_through_many association with composite primary keys on the main table using a :window_function strategy" do
|
|
@@ -1879,27 +1879,27 @@ describe "one_through_many eager loading methods" do
|
|
|
1879
1879
|
ds._fetch = {:id1=>1, :id2=>2}
|
|
1880
1880
|
Tag.dataset._fetch = [{:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>5}]
|
|
1881
1881
|
a = ds.all
|
|
1882
|
-
a.
|
|
1883
|
-
DB.sqls.
|
|
1882
|
+
a.must_equal [@c1.load(:id1=>1, :id2=>2)]
|
|
1883
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1884
1884
|
'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x, row_number() OVER (PARTITION BY albums_artists.artist_id1, albums_artists.artist_id2 ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE ((albums_artists.artist_id1, albums_artists.artist_id2) IN ((1, 2)))) AS t1 WHERE (x_sequel_row_number_x = 2)']
|
|
1885
|
-
a.first.second_tag.
|
|
1886
|
-
DB.sqls.length.
|
|
1885
|
+
a.first.second_tag.must_equal Tag.load(:id=>5)
|
|
1886
|
+
DB.sqls.length.must_equal 0
|
|
1887
1887
|
end
|
|
1888
1888
|
|
|
1889
1889
|
it "should raise an error when attempting to eagerly load an association with the :allow_eager option set to false" do
|
|
1890
|
-
|
|
1890
|
+
@c1.eager(:tag).all
|
|
1891
1891
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :allow_eager=>false
|
|
1892
|
-
proc{@c1.eager(:tag).all}.
|
|
1892
|
+
proc{@c1.eager(:tag).all}.must_raise(Sequel::Error)
|
|
1893
1893
|
end
|
|
1894
1894
|
|
|
1895
1895
|
it "should respect the association's :select option" do
|
|
1896
1896
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>:tags__name
|
|
1897
1897
|
a = @c1.eager(:tag).all
|
|
1898
|
-
a.
|
|
1899
|
-
DB.sqls.
|
|
1898
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1899
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1900
1900
|
'SELECT tags.name, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
1901
|
-
a.first.tag.
|
|
1902
|
-
DB.sqls.length.
|
|
1901
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1902
|
+
DB.sqls.length.must_equal 0
|
|
1903
1903
|
end
|
|
1904
1904
|
|
|
1905
1905
|
it "should respect one_through_many association's :left_primary_key and :right_primary_key options" do
|
|
@@ -1908,11 +1908,11 @@ describe "one_through_many eager loading methods" do
|
|
|
1908
1908
|
@c1.dataset.meta_def(:columns){[:id, :yyy]}
|
|
1909
1909
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:yyy, :right_primary_key=>:tag_id
|
|
1910
1910
|
a = @c1.eager(:tag).all
|
|
1911
|
-
a.
|
|
1912
|
-
DB.sqls.
|
|
1911
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
1912
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1913
1913
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.tag_id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (8))']
|
|
1914
|
-
a.first.tag.
|
|
1915
|
-
DB.sqls.length.
|
|
1914
|
+
a.first.tag.must_equal Tag.load(:tag_id=>2)
|
|
1915
|
+
DB.sqls.length.must_equal 0
|
|
1916
1916
|
end
|
|
1917
1917
|
|
|
1918
1918
|
it "should handle composite keys" do
|
|
@@ -1921,33 +1921,33 @@ describe "one_through_many eager loading methods" do
|
|
|
1921
1921
|
@c1.dataset.meta_def(:columns){[:id, :yyy]}
|
|
1922
1922
|
@c1.one_through_many :tag, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
|
|
1923
1923
|
a = @c1.eager(:tag).all
|
|
1924
|
-
a.
|
|
1925
|
-
DB.sqls.
|
|
1924
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
1925
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1926
1926
|
'SELECT tags.*, albums_artists.b1 AS x_foreign_key_0_x, albums_artists.b2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2)) WHERE ((albums_artists.b1, albums_artists.b2) IN ((1, 8)))']
|
|
1927
|
-
a.first.tag.
|
|
1928
|
-
DB.sqls.length.
|
|
1927
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1928
|
+
DB.sqls.length.must_equal 0
|
|
1929
1929
|
end
|
|
1930
1930
|
|
|
1931
1931
|
it "should respect :after_load callbacks on associations when eager loading" do
|
|
1932
1932
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :after_load=>lambda{|o, a| o[:id] *= 2; a[:id] *= 3}
|
|
1933
1933
|
a = @c1.eager(:tag).all
|
|
1934
|
-
a.
|
|
1935
|
-
DB.sqls.
|
|
1934
|
+
a.must_equal [@c1.load(:id=>2)]
|
|
1935
|
+
DB.sqls.must_equal ['SELECT * FROM artists',
|
|
1936
1936
|
'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
1937
|
-
a.first.tag.
|
|
1938
|
-
DB.sqls.length.
|
|
1937
|
+
a.first.tag.must_equal Tag.load(:id=>6)
|
|
1938
|
+
DB.sqls.length.must_equal 0
|
|
1939
1939
|
end
|
|
1940
1940
|
|
|
1941
1941
|
it "should support association_join" do
|
|
1942
|
-
@c1.association_join(:tag).sql.
|
|
1942
|
+
@c1.association_join(:tag).sql.must_equal "SELECT * FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)"
|
|
1943
1943
|
end
|
|
1944
1944
|
|
|
1945
1945
|
it "should eagerly graph a single one_through_many association" do
|
|
1946
1946
|
a = @c1.eager_graph(:tag).all
|
|
1947
|
-
a.
|
|
1948
|
-
DB.sqls.
|
|
1949
|
-
a.first.tag.
|
|
1950
|
-
DB.sqls.length.
|
|
1947
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1948
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)']
|
|
1949
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1950
|
+
DB.sqls.length.must_equal 0
|
|
1951
1951
|
end
|
|
1952
1952
|
|
|
1953
1953
|
it "should eagerly graph a single one_through_many association using the :distinct_on strategy" do
|
|
@@ -1955,10 +1955,10 @@ describe "one_through_many eager loading methods" do
|
|
|
1955
1955
|
ds = @c1.eager_graph_with_options(:tag, :limit_strategy=>true)
|
|
1956
1956
|
ds._fetch = {:id=>1, :tag_id=>2}
|
|
1957
1957
|
a = ds.all
|
|
1958
|
-
a.
|
|
1959
|
-
DB.sqls.
|
|
1960
|
-
a.first.tag.
|
|
1961
|
-
DB.sqls.length.
|
|
1958
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1959
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN (SELECT DISTINCT ON (albums_artists.artist_id) tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) ORDER BY albums_artists.artist_id) AS tag ON (tag.x_foreign_key_x = artists.id)']
|
|
1960
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1961
|
+
DB.sqls.length.must_equal 0
|
|
1962
1962
|
end
|
|
1963
1963
|
|
|
1964
1964
|
it "should eagerly graph a single one_through_many association using the :window_function strategy" do
|
|
@@ -1967,109 +1967,109 @@ describe "one_through_many eager loading methods" do
|
|
|
1967
1967
|
ds = @c1.eager_graph_with_options(:tag, :limit_strategy=>true)
|
|
1968
1968
|
ds._fetch = {:id=>1, :tag_id=>2}
|
|
1969
1969
|
a = ds.all
|
|
1970
|
-
a.
|
|
1971
|
-
DB.sqls.
|
|
1972
|
-
a.first.tag.
|
|
1973
|
-
DB.sqls.length.
|
|
1970
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1971
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN (SELECT id, x_foreign_key_x FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON (albums_artists.album_id = albums.id)) AS t1 WHERE (x_sequel_row_number_x = 1)) AS tag ON (tag.x_foreign_key_x = artists.id)']
|
|
1972
|
+
a.first.tag.must_equal Tag.load(:id=>2)
|
|
1973
|
+
DB.sqls.length.must_equal 0
|
|
1974
1974
|
end
|
|
1975
1975
|
|
|
1976
1976
|
it "should eagerly graph multiple associations in a single call" do
|
|
1977
1977
|
a = @c1.eager_graph(:tag, :album).all
|
|
1978
|
-
a.
|
|
1979
|
-
DB.sqls.
|
|
1978
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1979
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, album.id AS album_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS album ON (album.id = albums_artists_0.album_id)']
|
|
1980
1980
|
a = a.first
|
|
1981
|
-
a.tag.
|
|
1982
|
-
a.album.
|
|
1983
|
-
DB.sqls.length.
|
|
1981
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1982
|
+
a.album.must_equal Album.load(:id=>3)
|
|
1983
|
+
DB.sqls.length.must_equal 0
|
|
1984
1984
|
end
|
|
1985
1985
|
|
|
1986
1986
|
it "should eagerly graph multiple associations in separate calls" do
|
|
1987
1987
|
a = @c1.eager_graph(:tag).eager_graph(:album).all
|
|
1988
|
-
a.
|
|
1989
|
-
DB.sqls.
|
|
1988
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1989
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, album.id AS album_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS album ON (album.id = albums_artists_0.album_id)']
|
|
1990
1990
|
a = a.first
|
|
1991
|
-
a.tag.
|
|
1992
|
-
a.album.
|
|
1993
|
-
DB.sqls.length.
|
|
1991
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
1992
|
+
a.album.must_equal Album.load(:id=>3)
|
|
1993
|
+
DB.sqls.length.must_equal 0
|
|
1994
1994
|
end
|
|
1995
1995
|
|
|
1996
1996
|
it "should allow cascading of eager graphing for associations of associated models" do
|
|
1997
1997
|
a = @c1.eager_graph(:tag=>:track).all
|
|
1998
|
-
a.
|
|
1999
|
-
DB.sqls.
|
|
1998
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
1999
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, track.id AS track_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tag.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks AS track ON (track.album_id = albums_0.id)']
|
|
2000
2000
|
a = a.first
|
|
2001
|
-
a.tag.
|
|
2002
|
-
a.tag.track.
|
|
2003
|
-
DB.sqls.length.
|
|
2001
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
2002
|
+
a.tag.track.must_equal Track.load(:id=>4)
|
|
2003
|
+
DB.sqls.length.must_equal 0
|
|
2004
2004
|
end
|
|
2005
2005
|
|
|
2006
2006
|
it "should eager graph multiple associations from the same table" do
|
|
2007
2007
|
a = @c1.eager_graph(:tag, :other_tag).all
|
|
2008
|
-
a.
|
|
2009
|
-
DB.sqls.
|
|
2008
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
2009
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, other_tag.id AS other_tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.album_id = albums_0.id) LEFT OUTER JOIN tags AS other_tag ON (other_tag.id = albums_tags_0.tag_id)']
|
|
2010
2010
|
a = a.first
|
|
2011
|
-
a.tag.
|
|
2012
|
-
a.other_tag.
|
|
2013
|
-
DB.sqls.length.
|
|
2011
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
2012
|
+
a.other_tag.must_equal Tag.load(:id=>9)
|
|
2013
|
+
DB.sqls.length.must_equal 0
|
|
2014
2014
|
end
|
|
2015
2015
|
|
|
2016
2016
|
it "should eager graph a self_referential association" do
|
|
2017
2017
|
ds = @c1.eager_graph(:tag, :artist)
|
|
2018
2018
|
ds._fetch = {:id=>1, :tag_id=>2, :artist_id=>10}
|
|
2019
2019
|
a = ds.all
|
|
2020
|
-
a.
|
|
2021
|
-
DB.sqls.
|
|
2020
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
2021
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, artist.id AS artist_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) LEFT OUTER JOIN artists AS artist ON (artist.id = albums_artists_1.artist_id)']
|
|
2022
2022
|
a = a.first
|
|
2023
|
-
a.tag.
|
|
2024
|
-
a.artist.
|
|
2025
|
-
DB.sqls.length.
|
|
2023
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
2024
|
+
a.artist.must_equal @c1.load(:id=>10)
|
|
2025
|
+
DB.sqls.length.must_equal 0
|
|
2026
2026
|
end
|
|
2027
2027
|
|
|
2028
2028
|
it "should be able to use eager and eager_graph together" do
|
|
2029
2029
|
a = @c1.eager_graph(:tag).eager(:album).all
|
|
2030
|
-
a.
|
|
2031
|
-
DB.sqls.
|
|
2030
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
2031
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)',
|
|
2032
2032
|
'SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON (albums_artists.album_id = albums.id) WHERE (albums_artists.artist_id IN (1))']
|
|
2033
2033
|
a = a.first
|
|
2034
|
-
a.tag.
|
|
2035
|
-
a.album.
|
|
2036
|
-
DB.sqls.length.
|
|
2034
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
2035
|
+
a.album.must_equal Album.load(:id=>3)
|
|
2036
|
+
DB.sqls.length.must_equal 0
|
|
2037
2037
|
end
|
|
2038
2038
|
|
|
2039
2039
|
it "should handle no associated records when eagerly graphing a single one_through_many association" do
|
|
2040
2040
|
ds = @c1.eager_graph(:tag)
|
|
2041
2041
|
ds._fetch = {:id=>1, :tag_id=>nil}
|
|
2042
2042
|
a = ds.all
|
|
2043
|
-
a.
|
|
2044
|
-
DB.sqls.
|
|
2045
|
-
a.first.tag.
|
|
2046
|
-
DB.sqls.length.
|
|
2043
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
2044
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)']
|
|
2045
|
+
a.first.tag.must_equal nil
|
|
2046
|
+
DB.sqls.length.must_equal 0
|
|
2047
2047
|
end
|
|
2048
2048
|
|
|
2049
2049
|
it "should handle no associated records when eagerly graphing multiple one_through_many associations" do
|
|
2050
2050
|
ds = @c1.eager_graph(:tag, :album)
|
|
2051
2051
|
ds._fetch = [{:id=>1, :tag_id=>5, :album_id=>6}, {:id=>7, :tag_id=>nil, :albums_0_id=>nil}]
|
|
2052
2052
|
a = ds.all
|
|
2053
|
-
a.
|
|
2054
|
-
DB.sqls.
|
|
2055
|
-
a.first.tag.
|
|
2056
|
-
a.first.album.
|
|
2057
|
-
a.last.tag.
|
|
2058
|
-
a.last.album.
|
|
2059
|
-
DB.sqls.length.
|
|
2053
|
+
a.must_equal [@c1.load(:id=>1), @c1.load(:id=>7)]
|
|
2054
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, album.id AS album_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS album ON (album.id = albums_artists_0.album_id)']
|
|
2055
|
+
a.first.tag.must_equal Tag.load(:id=>5)
|
|
2056
|
+
a.first.album.must_equal Album.load(:id=>6)
|
|
2057
|
+
a.last.tag.must_equal nil
|
|
2058
|
+
a.last.album.must_equal nil
|
|
2059
|
+
DB.sqls.length.must_equal 0
|
|
2060
2060
|
end
|
|
2061
2061
|
|
|
2062
2062
|
it "should handle missing associated records when cascading eager graphing for associations of associated models" do
|
|
2063
2063
|
ds = @c1.eager_graph(:tag=>:track)
|
|
2064
2064
|
ds._fetch = [{:id=>1, :tag_id=>2, :track_id=>nil}, {:id=>2, :tag_id=>nil, :tracks_id=>nil}]
|
|
2065
2065
|
a = ds.all
|
|
2066
|
-
a.
|
|
2067
|
-
DB.sqls.
|
|
2068
|
-
a.last.tag.
|
|
2066
|
+
a.must_equal [@c1.load(:id=>1), @c1.load(:id=>2)]
|
|
2067
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.id AS tag_id, track.id AS track_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tag.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks AS track ON (track.album_id = albums_0.id)']
|
|
2068
|
+
a.last.tag.must_equal nil
|
|
2069
2069
|
a = a.first
|
|
2070
|
-
a.tag.
|
|
2071
|
-
a.tag.track.
|
|
2072
|
-
DB.sqls.length.
|
|
2070
|
+
a.tag.must_equal Tag.load(:id=>2)
|
|
2071
|
+
a.tag.track.must_equal nil
|
|
2072
|
+
DB.sqls.length.must_equal 0
|
|
2073
2073
|
end
|
|
2074
2074
|
|
|
2075
2075
|
it "eager graphing should respect :left_primary_key and :right_primary_key options" do
|
|
@@ -2079,10 +2079,10 @@ describe "one_through_many eager loading methods" do
|
|
|
2079
2079
|
ds = @c1.eager_graph(:tag)
|
|
2080
2080
|
ds._fetch = {:id=>1, :yyy=>8, :tag_id=>2, :tag_tag_id=>4}
|
|
2081
2081
|
a = ds.all
|
|
2082
|
-
a.
|
|
2083
|
-
DB.sqls.
|
|
2084
|
-
a.first.tag.
|
|
2085
|
-
DB.sqls.length.
|
|
2082
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
2083
|
+
DB.sqls.must_equal ['SELECT artists.id, artists.yyy, tag.id AS tag_id, tag.tag_id AS tag_tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.yyy) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.tag_id = albums_tags.tag_id)']
|
|
2084
|
+
a.first.tag.must_equal Tag.load(:id=>2, :tag_id=>4)
|
|
2085
|
+
DB.sqls.length.must_equal 0
|
|
2086
2086
|
end
|
|
2087
2087
|
|
|
2088
2088
|
it "eager graphing should respect composite keys" do
|
|
@@ -2092,10 +2092,10 @@ describe "one_through_many eager loading methods" do
|
|
|
2092
2092
|
ds = @c1.eager_graph(:tag)
|
|
2093
2093
|
ds._fetch = {:id=>1, :yyy=>8, :tag_id=>2, :tag_tag_id=>4}
|
|
2094
2094
|
a = ds.all
|
|
2095
|
-
a.
|
|
2096
|
-
DB.sqls.
|
|
2097
|
-
a.first.tag.
|
|
2098
|
-
DB.sqls.length.
|
|
2095
|
+
a.must_equal [@c1.load(:id=>1, :yyy=>8)]
|
|
2096
|
+
DB.sqls.must_equal ['SELECT artists.id, artists.yyy, tag.id AS tag_id, tag.tag_id AS tag_tag_id FROM artists LEFT OUTER JOIN albums_artists ON ((albums_artists.b1 = artists.id) AND (albums_artists.b2 = artists.yyy)) LEFT OUTER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) LEFT OUTER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) LEFT OUTER JOIN tags AS tag ON ((tag.id = albums_tags.g1) AND (tag.tag_id = albums_tags.g2))']
|
|
2097
|
+
a.first.tag.must_equal Tag.load(:id=>2, :tag_id=>4)
|
|
2098
|
+
DB.sqls.length.must_equal 0
|
|
2099
2099
|
end
|
|
2100
2100
|
|
|
2101
2101
|
it "should respect the association's :graph_select option" do
|
|
@@ -2103,70 +2103,70 @@ describe "one_through_many eager loading methods" do
|
|
|
2103
2103
|
ds = @c1.eager_graph(:tag)
|
|
2104
2104
|
ds._fetch = {:id=>1, :b=>2}
|
|
2105
2105
|
a = ds.all
|
|
2106
|
-
a.
|
|
2107
|
-
DB.sqls.
|
|
2108
|
-
a.first.tag.
|
|
2109
|
-
DB.sqls.length.
|
|
2106
|
+
a.must_equal [@c1.load(:id=>1)]
|
|
2107
|
+
DB.sqls.must_equal ['SELECT artists.id, tag.b FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)']
|
|
2108
|
+
a.first.tag.must_equal Tag.load(:b=>2)
|
|
2109
|
+
DB.sqls.length.must_equal 0
|
|
2110
2110
|
end
|
|
2111
2111
|
|
|
2112
2112
|
it "should respect the association's :graph_join_type option" do
|
|
2113
2113
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :graph_join_type=>:inner
|
|
2114
|
-
@c1.eager_graph(:tag).sql.
|
|
2114
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) INNER JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)'
|
|
2115
2115
|
end
|
|
2116
2116
|
|
|
2117
2117
|
it "should respect the association's :join_type option on through" do
|
|
2118
2118
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :join_type=>:natural}, [:albums_tags, :album_id, :tag_id]], :graph_join_type=>:inner
|
|
2119
|
-
@c1.eager_graph(:tag).sql.
|
|
2119
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists INNER JOIN albums_artists ON (albums_artists.artist_id = artists.id) NATURAL JOIN albums ON (albums.id = albums_artists.album_id) INNER JOIN albums_tags ON (albums_tags.album_id = albums.id) INNER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)'
|
|
2120
2120
|
end
|
|
2121
2121
|
|
|
2122
2122
|
it "should respect the association's :conditions option" do
|
|
2123
2123
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
|
|
2124
|
-
@c1.eager_graph(:tag).sql.
|
|
2124
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON ((tag.id = albums_tags.tag_id) AND (tag.a = 32))'
|
|
2125
2125
|
end
|
|
2126
2126
|
|
|
2127
2127
|
it "should respect the association's :graph_conditions option" do
|
|
2128
2128
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_conditions=>{:a=>42}
|
|
2129
|
-
@c1.eager_graph(:tag).sql.
|
|
2129
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON ((tag.id = albums_tags.tag_id) AND (tag.a = 42))'
|
|
2130
2130
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_conditions=>{:a=>42}, :conditions=>{:a=>32}
|
|
2131
|
-
@c1.eager_graph(:tag).sql.
|
|
2131
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON ((tag.id = albums_tags.tag_id) AND (tag.a = 42))'
|
|
2132
2132
|
end
|
|
2133
2133
|
|
|
2134
2134
|
it "should respect the association's :conditions option on through" do
|
|
2135
2135
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :conditions=>{:a=>42}}, [:albums_tags, :album_id, :tag_id]]
|
|
2136
|
-
@c1.eager_graph(:tag).sql.
|
|
2136
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON ((albums.id = albums_artists.album_id) AND (albums.a = 42)) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)'
|
|
2137
2137
|
end
|
|
2138
2138
|
|
|
2139
2139
|
it "should respect the association's :graph_block option" do
|
|
2140
2140
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_block=>proc{|ja,lja,js| {Sequel.qualify(ja, :active)=>true}}
|
|
2141
|
-
@c1.eager_graph(:tag).sql.
|
|
2141
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON ((tag.id = albums_tags.tag_id) AND (tag.active IS TRUE))'
|
|
2142
2142
|
end
|
|
2143
2143
|
|
|
2144
2144
|
it "should respect the association's :block option on through" do
|
|
2145
2145
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :block=>proc{|ja,lja,js| {Sequel.qualify(ja, :active)=>true}}}, [:albums_tags, :album_id, :tag_id]]
|
|
2146
|
-
@c1.eager_graph(:tag).sql.
|
|
2146
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON ((albums.id = albums_artists.album_id) AND (albums.active IS TRUE)) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)'
|
|
2147
2147
|
end
|
|
2148
2148
|
|
|
2149
2149
|
it "should respect the association's :graph_only_conditions option" do
|
|
2150
2150
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :graph_only_conditions=>{:a=>32}
|
|
2151
|
-
@c1.eager_graph(:tag).sql.
|
|
2151
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.a = 32)'
|
|
2152
2152
|
end
|
|
2153
2153
|
|
|
2154
2154
|
it "should respect the association's :only_conditions option on through" do
|
|
2155
2155
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id, :only_conditions=>{:a=>42}}, [:albums_tags, :album_id, :tag_id]]
|
|
2156
|
-
@c1.eager_graph(:tag).sql.
|
|
2156
|
+
@c1.eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.a = 42) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id)'
|
|
2157
2157
|
end
|
|
2158
2158
|
|
|
2159
2159
|
it "should create unique table aliases for all associations" do
|
|
2160
|
-
@c1.eager_graph(:artist=>{:artist=>:artist}).sql.
|
|
2160
|
+
@c1.eager_graph(:artist=>{:artist=>:artist}).sql.must_equal "SELECT artists.id, artist.id AS artist_id, artist_0.id AS artist_0_id, artist_1.id AS artist_1_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.album_id = albums.id) LEFT OUTER JOIN artists AS artist ON (artist.id = albums_artists_0.artist_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.artist_id = artist.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_1.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_2 ON (albums_artists_2.album_id = albums_0.id) LEFT OUTER JOIN artists AS artist_0 ON (artist_0.id = albums_artists_2.artist_id) LEFT OUTER JOIN albums_artists AS albums_artists_3 ON (albums_artists_3.artist_id = artist_0.id) LEFT OUTER JOIN albums AS albums_1 ON (albums_1.id = albums_artists_3.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_4 ON (albums_artists_4.album_id = albums_1.id) LEFT OUTER JOIN artists AS artist_1 ON (artist_1.id = albums_artists_4.artist_id)"
|
|
2161
2161
|
end
|
|
2162
2162
|
|
|
2163
2163
|
it "should respect the association's :order" do
|
|
2164
2164
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[:blah1, :blah2]
|
|
2165
|
-
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tag).sql.
|
|
2165
|
+
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) ORDER BY artists.blah2, artists.blah3, tag.blah1, tag.blah2'
|
|
2166
2166
|
end
|
|
2167
2167
|
|
|
2168
2168
|
it "should only qualify unqualified symbols, identifiers, or ordered versions in association's :order" do
|
|
2169
2169
|
@c1.one_through_many :tag, [[:albums_artists, :artist_id, :album_id], {:table=>:albums, :left=>:id, :right=>:id}, [:albums_tags, :album_id, :tag_id]], :order=>[Sequel.identifier(:blah__id), Sequel.identifier(:blah__id).desc, Sequel.desc(:blah__id), :blah__id, :album_id, Sequel.desc(:album_id), 1, Sequel.lit('RANDOM()'), Sequel.qualify(:b, :a)]
|
|
2170
|
-
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tag).sql.
|
|
2170
|
+
@c1.order(:artists__blah2, :artists__blah3).eager_graph(:tag).sql.must_equal 'SELECT artists.id, tag.id AS tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags AS tag ON (tag.id = albums_tags.tag_id) ORDER BY artists.blah2, artists.blah3, tag.blah__id, tag.blah__id DESC, blah.id DESC, blah.id, tag.album_id, tag.album_id DESC, 1, RANDOM(), b.a'
|
|
2171
2171
|
end
|
|
2172
2172
|
end
|