composite_primary_keys 14.0.9 → 14.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +10 -0
  3. data/README.rdoc +182 -182
  4. data/Rakefile +37 -37
  5. data/lib/composite_primary_keys/associations/collection_association.rb +38 -38
  6. data/lib/composite_primary_keys/associations/preloader/association.rb +52 -52
  7. data/lib/composite_primary_keys/autosave_association.rb +60 -60
  8. data/lib/composite_primary_keys/composite_arrays.rb +88 -88
  9. data/lib/composite_primary_keys/composite_predicates.rb +121 -121
  10. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +36 -36
  11. data/lib/composite_primary_keys/core.rb +71 -48
  12. data/lib/composite_primary_keys/persistence.rb +96 -96
  13. data/lib/composite_primary_keys/reflection.rb +93 -91
  14. data/lib/composite_primary_keys/relation/calculations.rb +110 -110
  15. data/lib/composite_primary_keys/relation/query_methods.rb +40 -40
  16. data/lib/composite_primary_keys/relation.rb +199 -199
  17. data/lib/composite_primary_keys/validations/uniqueness.rb +40 -40
  18. data/lib/composite_primary_keys/version.rb +1 -1
  19. data/lib/composite_primary_keys.rb +117 -117
  20. data/scripts/console.rb +48 -48
  21. data/tasks/databases/trilogy.rake +23 -23
  22. data/test/abstract_unit.rb +124 -124
  23. data/test/connections/databases.ci.yml +32 -32
  24. data/test/fixtures/admin.rb +4 -4
  25. data/test/fixtures/db_definitions/db2-create-tables.sql +146 -146
  26. data/test/fixtures/db_definitions/db2-drop-tables.sql +23 -23
  27. data/test/fixtures/db_definitions/mysql.sql +203 -203
  28. data/test/fixtures/db_definitions/oracle.drop.sql +45 -45
  29. data/test/fixtures/db_definitions/oracle.sql +220 -220
  30. data/test/fixtures/db_definitions/postgresql.sql +205 -205
  31. data/test/fixtures/db_definitions/sqlite.sql +190 -190
  32. data/test/fixtures/db_definitions/sqlserver.sql +199 -199
  33. data/test/fixtures/department.rb +20 -20
  34. data/test/fixtures/moderator.rb +4 -4
  35. data/test/fixtures/room.rb +14 -14
  36. data/test/fixtures/room_assignment.rb +18 -18
  37. data/test/fixtures/staff_room.rb +6 -6
  38. data/test/fixtures/staff_room_key.rb +6 -6
  39. data/test/fixtures/user.rb +14 -14
  40. data/test/test_associations.rb +403 -403
  41. data/test/test_composite_arrays.rb +44 -44
  42. data/test/test_equal.rb +55 -26
  43. data/test/test_has_one_through.rb +30 -30
  44. data/test/test_hash.rb +73 -0
  45. data/test/test_nested_attributes.rb +90 -90
  46. metadata +7 -8
@@ -1,117 +1,117 @@
1
- #--
2
- # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- unless defined?(ActiveRecord)
25
- require 'rubygems'
26
- gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
- require 'active_record'
28
- end
29
-
30
- # ActiveModel files we override
31
- # _write_attribute
32
- require 'active_model/attribute_assignment'
33
-
34
- # ActiveRecord files we override
35
- require 'active_record/attribute_methods'
36
- require 'active_record/autosave_association'
37
- require 'active_record/counter_cache'
38
- require 'active_record/fixtures'
39
- require 'active_record/model_schema'
40
- require 'active_record/persistence'
41
- require 'active_record/reflection'
42
- require 'active_record/relation'
43
- require 'active_record/sanitization'
44
- require 'active_record/transactions'
45
-
46
- require 'active_record/associations/association'
47
- require 'active_record/associations/association_scope'
48
- require 'active_record/associations/foreign_association'
49
- require 'active_record/associations/has_many_association'
50
- require 'active_record/associations/has_many_through_association'
51
- require 'active_record/associations/join_dependency'
52
- require 'active_record/associations/preloader/association'
53
- require 'active_record/associations/singular_association'
54
- require 'active_record/associations/collection_association'
55
-
56
- require 'active_record/attribute_methods/primary_key'
57
- require 'active_record/attribute_methods/read'
58
- require 'active_record/attribute_methods/write'
59
- require 'active_record/nested_attributes'
60
-
61
- require 'active_record/connection_adapters/abstract/database_statements'
62
- require 'active_record/connection_adapters/abstract_adapter'
63
- require 'active_record/connection_adapters/postgresql/database_statements'
64
-
65
- require 'active_record/relation/where_clause'
66
- require 'active_record/table_metadata'
67
-
68
- # CPK overrides
69
- require_relative 'composite_primary_keys/active_model/attribute_assignment'
70
- require_relative 'composite_primary_keys/attribute_methods'
71
- require_relative 'composite_primary_keys/autosave_association'
72
- require_relative 'composite_primary_keys/persistence'
73
- require_relative 'composite_primary_keys/base'
74
- require_relative 'composite_primary_keys/core'
75
- require_relative 'composite_primary_keys/composite_arrays'
76
- require_relative 'composite_primary_keys/composite_predicates'
77
- require_relative 'composite_primary_keys/counter_cache'
78
- require_relative 'composite_primary_keys/fixtures'
79
- require_relative 'composite_primary_keys/reflection'
80
- require_relative 'composite_primary_keys/relation'
81
- require_relative 'composite_primary_keys/sanitization'
82
- require_relative 'composite_primary_keys/transactions'
83
- require_relative 'composite_primary_keys/version'
84
-
85
- require_relative 'composite_primary_keys/associations/association'
86
- require_relative 'composite_primary_keys/associations/association_scope'
87
- require_relative 'composite_primary_keys/associations/foreign_association'
88
- require_relative 'composite_primary_keys/associations/has_many_association'
89
- require_relative 'composite_primary_keys/associations/has_many_through_association'
90
- require_relative 'composite_primary_keys/associations/join_association'
91
- require_relative 'composite_primary_keys/associations/preloader/association'
92
- require_relative 'composite_primary_keys/associations/collection_association'
93
-
94
- require_relative 'composite_primary_keys/attribute_methods/primary_key'
95
- require_relative 'composite_primary_keys/attribute_methods/read'
96
- require_relative 'composite_primary_keys/attribute_methods/write'
97
- require_relative 'composite_primary_keys/nested_attributes'
98
-
99
- require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
100
- require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
101
- require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
102
- require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
103
-
104
- require_relative 'composite_primary_keys/relation/batches'
105
- require_relative 'composite_primary_keys/relation/where_clause'
106
- require_relative 'composite_primary_keys/relation/calculations'
107
- require_relative 'composite_primary_keys/relation/finder_methods'
108
- require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
109
- require_relative 'composite_primary_keys/relation/query_methods'
110
-
111
- require_relative 'composite_primary_keys/validations/uniqueness'
112
-
113
- require_relative 'composite_primary_keys/composite_relation'
114
-
115
- require_relative 'composite_primary_keys/arel/to_sql'
116
- require_relative 'composite_primary_keys/arel/sqlserver'
117
- require_relative 'composite_primary_keys/table_metadata'
1
+ #--
2
+ # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ unless defined?(ActiveRecord)
25
+ require 'rubygems'
26
+ gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
+ require 'active_record'
28
+ end
29
+
30
+ # ActiveModel files we override
31
+ # _write_attribute
32
+ require 'active_model/attribute_assignment'
33
+
34
+ # ActiveRecord files we override
35
+ require 'active_record/attribute_methods'
36
+ require 'active_record/autosave_association'
37
+ require 'active_record/counter_cache'
38
+ require 'active_record/fixtures'
39
+ require 'active_record/model_schema'
40
+ require 'active_record/persistence'
41
+ require 'active_record/reflection'
42
+ require 'active_record/relation'
43
+ require 'active_record/sanitization'
44
+ require 'active_record/transactions'
45
+
46
+ require 'active_record/associations/association'
47
+ require 'active_record/associations/association_scope'
48
+ require 'active_record/associations/foreign_association'
49
+ require 'active_record/associations/has_many_association'
50
+ require 'active_record/associations/has_many_through_association'
51
+ require 'active_record/associations/join_dependency'
52
+ require 'active_record/associations/preloader/association'
53
+ require 'active_record/associations/singular_association'
54
+ require 'active_record/associations/collection_association'
55
+
56
+ require 'active_record/attribute_methods/primary_key'
57
+ require 'active_record/attribute_methods/read'
58
+ require 'active_record/attribute_methods/write'
59
+ require 'active_record/nested_attributes'
60
+
61
+ require 'active_record/connection_adapters/abstract/database_statements'
62
+ require 'active_record/connection_adapters/abstract_adapter'
63
+ require 'active_record/connection_adapters/postgresql/database_statements'
64
+
65
+ require 'active_record/relation/where_clause'
66
+ require 'active_record/table_metadata'
67
+
68
+ # CPK overrides
69
+ require_relative 'composite_primary_keys/active_model/attribute_assignment'
70
+ require_relative 'composite_primary_keys/attribute_methods'
71
+ require_relative 'composite_primary_keys/autosave_association'
72
+ require_relative 'composite_primary_keys/persistence'
73
+ require_relative 'composite_primary_keys/base'
74
+ require_relative 'composite_primary_keys/core'
75
+ require_relative 'composite_primary_keys/composite_arrays'
76
+ require_relative 'composite_primary_keys/composite_predicates'
77
+ require_relative 'composite_primary_keys/counter_cache'
78
+ require_relative 'composite_primary_keys/fixtures'
79
+ require_relative 'composite_primary_keys/reflection'
80
+ require_relative 'composite_primary_keys/relation'
81
+ require_relative 'composite_primary_keys/sanitization'
82
+ require_relative 'composite_primary_keys/transactions'
83
+ require_relative 'composite_primary_keys/version'
84
+
85
+ require_relative 'composite_primary_keys/associations/association'
86
+ require_relative 'composite_primary_keys/associations/association_scope'
87
+ require_relative 'composite_primary_keys/associations/foreign_association'
88
+ require_relative 'composite_primary_keys/associations/has_many_association'
89
+ require_relative 'composite_primary_keys/associations/has_many_through_association'
90
+ require_relative 'composite_primary_keys/associations/join_association'
91
+ require_relative 'composite_primary_keys/associations/preloader/association'
92
+ require_relative 'composite_primary_keys/associations/collection_association'
93
+
94
+ require_relative 'composite_primary_keys/attribute_methods/primary_key'
95
+ require_relative 'composite_primary_keys/attribute_methods/read'
96
+ require_relative 'composite_primary_keys/attribute_methods/write'
97
+ require_relative 'composite_primary_keys/nested_attributes'
98
+
99
+ require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
100
+ require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
101
+ require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
102
+ require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
103
+
104
+ require_relative 'composite_primary_keys/relation/batches'
105
+ require_relative 'composite_primary_keys/relation/where_clause'
106
+ require_relative 'composite_primary_keys/relation/calculations'
107
+ require_relative 'composite_primary_keys/relation/finder_methods'
108
+ require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
109
+ require_relative 'composite_primary_keys/relation/query_methods'
110
+
111
+ require_relative 'composite_primary_keys/validations/uniqueness'
112
+
113
+ require_relative 'composite_primary_keys/composite_relation'
114
+
115
+ require_relative 'composite_primary_keys/arel/to_sql'
116
+ require_relative 'composite_primary_keys/arel/sqlserver'
117
+ require_relative 'composite_primary_keys/table_metadata'
data/scripts/console.rb CHANGED
@@ -1,48 +1,48 @@
1
- #!/usr/bin/env ruby
2
-
3
- #
4
- # if run as script, load the file as library while starting irb
5
- #
6
- if __FILE__ == $0
7
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
8
- ENV['ADAPTER'] = ARGV[0]
9
- exec "#{irb} -f -r #{$0} --simple-prompt"
10
- end
11
-
12
- #
13
- # check if the given adapter is supported (default: mysql)
14
- #
15
- adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db trilogy]
16
- adapter = ENV['ADAPTER'] || 'mysql'
17
- unless adapters.include? adapter
18
- puts "Usage: #{__FILE__} <adapter>"
19
- puts ''
20
- puts 'Adapters: '
21
- puts adapters.map{ |adapter| " #{adapter}" }.join("\n")
22
- exit 1
23
- end
24
-
25
- #
26
- # load all necessary libraries
27
- #
28
- require 'rubygems'
29
- require 'local/database_connections'
30
-
31
- $LOAD_PATH.unshift 'lib'
32
-
33
- begin
34
- require 'local/paths'
35
- $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/active_record/lib" if ENV['EDGE_RAILS_DIR']
36
- $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
37
- rescue
38
- end
39
-
40
- require 'active_support'
41
- require 'active_record'
42
-
43
- require "test/connections/native_#{adapter}/connection"
44
- require 'composite_primary_keys'
45
-
46
- PROJECT_ROOT = File.join(File.dirname(__FILE__), '..')
47
- Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
48
-
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # if run as script, load the file as library while starting irb
5
+ #
6
+ if __FILE__ == $0
7
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
8
+ ENV['ADAPTER'] = ARGV[0]
9
+ exec "#{irb} -f -r #{$0} --simple-prompt"
10
+ end
11
+
12
+ #
13
+ # check if the given adapter is supported (default: mysql)
14
+ #
15
+ adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db trilogy]
16
+ adapter = ENV['ADAPTER'] || 'mysql'
17
+ unless adapters.include? adapter
18
+ puts "Usage: #{__FILE__} <adapter>"
19
+ puts ''
20
+ puts 'Adapters: '
21
+ puts adapters.map{ |adapter| " #{adapter}" }.join("\n")
22
+ exit 1
23
+ end
24
+
25
+ #
26
+ # load all necessary libraries
27
+ #
28
+ require 'rubygems'
29
+ require 'local/database_connections'
30
+
31
+ $LOAD_PATH.unshift 'lib'
32
+
33
+ begin
34
+ require 'local/paths'
35
+ $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/active_record/lib" if ENV['EDGE_RAILS_DIR']
36
+ $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
37
+ rescue
38
+ end
39
+
40
+ require 'active_support'
41
+ require 'active_record'
42
+
43
+ require "test/connections/native_#{adapter}/connection"
44
+ require 'composite_primary_keys'
45
+
46
+ PROJECT_ROOT = File.join(File.dirname(__FILE__), '..')
47
+ Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
48
+
@@ -1,23 +1,23 @@
1
- namespace :trilogy do
2
- task :setup do
3
- require 'bundler'
4
- Bundler.require(:default, :trilogy)
5
- end
6
-
7
- task :create_database => :setup do
8
- Rake::Task["mysql:create_database"].invoke
9
- end
10
-
11
- desc 'Build the MySQL test database'
12
- task :build_database => [:create_database] do
13
- Rake::Task["mysql:build_database"].invoke
14
- end
15
-
16
- desc 'Drop the MySQL test database'
17
- task :drop_database => :setup do
18
- Rake::Task["mysql:drop_database"].invoke
19
- end
20
-
21
- desc 'Rebuild the MySQL test database'
22
- task :rebuild_database => [:drop_database, :build_database]
23
- end
1
+ namespace :trilogy do
2
+ task :setup do
3
+ require 'bundler'
4
+ Bundler.require(:default, :trilogy)
5
+ end
6
+
7
+ task :create_database => :setup do
8
+ Rake::Task["mysql:create_database"].invoke
9
+ end
10
+
11
+ desc 'Build the MySQL test database'
12
+ task :build_database => [:create_database] do
13
+ Rake::Task["mysql:build_database"].invoke
14
+ end
15
+
16
+ desc 'Drop the MySQL test database'
17
+ task :drop_database => :setup do
18
+ Rake::Task["mysql:drop_database"].invoke
19
+ end
20
+
21
+ desc 'Rebuild the MySQL test database'
22
+ task :rebuild_database => [:drop_database, :build_database]
23
+ end
@@ -1,124 +1,124 @@
1
- spec_name = ENV['ADAPTER'] || 'postgresql'
2
- require 'bundler'
3
- require 'minitest/autorun'
4
-
5
- Bundler.setup(:default, spec_name.to_sym)
6
- Bundler.require(:default, spec_name.to_sym)
7
- require 'composite_primary_keys'
8
-
9
- # Require the connection spec
10
- PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
11
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
12
-
13
- spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
- puts "Loaded #{spec_name}"
15
-
16
- # And now connect to the database
17
- if spec_name == "trilogy"
18
- require "activerecord-trilogy-adapter"
19
- require "trilogy_adapter/connection"
20
- ActiveRecord::Base.extend TrilogyAdapter::Connection
21
- end
22
-
23
- ActiveRecord::Base.establish_connection(spec)
24
-
25
- # Tell active record about the configuration
26
- ActiveRecord::Base.configurations = {test: spec}
27
-
28
- # Tell ActiveRecord where to find models
29
- ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
30
- Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
31
- require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
32
- require_relative require_file
33
- end
34
-
35
- I18n.config.enforce_available_locales = true
36
-
37
- class ActiveSupport::TestCase
38
- include ActiveRecord::TestFixtures
39
-
40
- self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
41
- self.use_instantiated_fixtures = false
42
- self.use_transactional_tests = true
43
- self.test_order = :random
44
-
45
- def assert_date_from_db(expected, actual, message = nil)
46
- # SQL Server doesn't have a separate column type just for dates,
47
- # so the time is in the string and incorrectly formatted
48
- if current_adapter?(:SQLServerAdapter)
49
- assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
50
- elsif current_adapter?(:SybaseAdapter)
51
- assert_equal expected.to_s, actual.to_date.to_s, message
52
- else
53
- assert_equal expected.to_s, actual.to_s, message
54
- end
55
- end
56
-
57
- def assert_queries(num = 1)
58
- ActiveRecord::Base.connection.class.class_eval do
59
- self.query_count = 0
60
- alias_method :execute, :execute_with_query_counting
61
- end
62
- yield
63
- ensure
64
- ActiveRecord::Base.connection.class.class_eval do
65
- alias_method :execute, :execute_without_query_counting
66
- end
67
- assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
68
- end
69
-
70
- def assert_no_queries(&block)
71
- assert_queries(0, &block)
72
- end
73
-
74
- cattr_accessor :classes
75
-
76
- protected
77
-
78
- def testing_with(&block)
79
- classes.keys.each do |key_test|
80
- @key_test = key_test
81
- @klass_info = classes[@key_test]
82
- @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
83
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
84
- @first = @klass.order(order).first
85
- yield
86
- end
87
- end
88
-
89
- def first_id
90
- ids = (1..@primary_keys.length).map {|num| 1}
91
- composite? ? ids.to_composite_ids : ids.first
92
- end
93
-
94
- def composite?
95
- @key_test != :single
96
- end
97
-
98
- # Oracle metadata is in all caps.
99
- def with_quoted_identifiers(s)
100
- s.gsub(/'(\w+)'/) { |m|
101
- if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
102
- m.upcase
103
- else
104
- m
105
- end
106
- }
107
- end
108
- end
109
-
110
- def current_adapter?(type)
111
- ActiveRecord::ConnectionAdapters.const_defined?(type) &&
112
- ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
113
- end
114
-
115
- ActiveRecord::Base.connection.class.class_eval do
116
- cattr_accessor :query_count
117
- alias_method :execute_without_query_counting, :execute
118
- def execute_with_query_counting(sql, name = nil)
119
- self.query_count += 1
120
- execute_without_query_counting(sql, name)
121
- end
122
- end
123
-
124
- ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
1
+ spec_name = ENV['ADAPTER'] || 'postgresql'
2
+ require 'bundler'
3
+ require 'minitest/autorun'
4
+
5
+ Bundler.setup(:default, spec_name.to_sym)
6
+ Bundler.require(:default, spec_name.to_sym)
7
+ require 'composite_primary_keys'
8
+
9
+ # Require the connection spec
10
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
11
+ require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
12
+
13
+ spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
+ puts "Loaded #{spec_name}"
15
+
16
+ # And now connect to the database
17
+ if spec_name == "trilogy"
18
+ require "activerecord-trilogy-adapter"
19
+ require "trilogy_adapter/connection"
20
+ ActiveRecord::Base.extend TrilogyAdapter::Connection
21
+ end
22
+
23
+ ActiveRecord::Base.establish_connection(spec)
24
+
25
+ # Tell active record about the configuration
26
+ ActiveRecord::Base.configurations = {test: spec}
27
+
28
+ # Tell ActiveRecord where to find models
29
+ ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
30
+ Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
31
+ require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
32
+ require_relative require_file
33
+ end
34
+
35
+ I18n.config.enforce_available_locales = true
36
+
37
+ class ActiveSupport::TestCase
38
+ include ActiveRecord::TestFixtures
39
+
40
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
41
+ self.use_instantiated_fixtures = false
42
+ self.use_transactional_tests = true
43
+ self.test_order = :random
44
+
45
+ def assert_date_from_db(expected, actual, message = nil)
46
+ # SQL Server doesn't have a separate column type just for dates,
47
+ # so the time is in the string and incorrectly formatted
48
+ if current_adapter?(:SQLServerAdapter)
49
+ assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
50
+ elsif current_adapter?(:SybaseAdapter)
51
+ assert_equal expected.to_s, actual.to_date.to_s, message
52
+ else
53
+ assert_equal expected.to_s, actual.to_s, message
54
+ end
55
+ end
56
+
57
+ def assert_queries(num = 1)
58
+ ActiveRecord::Base.connection.class.class_eval do
59
+ self.query_count = 0
60
+ alias_method :execute, :execute_with_query_counting
61
+ end
62
+ yield
63
+ ensure
64
+ ActiveRecord::Base.connection.class.class_eval do
65
+ alias_method :execute, :execute_without_query_counting
66
+ end
67
+ assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
68
+ end
69
+
70
+ def assert_no_queries(&block)
71
+ assert_queries(0, &block)
72
+ end
73
+
74
+ cattr_accessor :classes
75
+
76
+ protected
77
+
78
+ def testing_with(&block)
79
+ classes.keys.each do |key_test|
80
+ @key_test = key_test
81
+ @klass_info = classes[@key_test]
82
+ @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
83
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
84
+ @first = @klass.order(order).first
85
+ yield
86
+ end
87
+ end
88
+
89
+ def first_id
90
+ ids = (1..@primary_keys.length).map {|num| 1}
91
+ composite? ? ids.to_composite_ids : ids.first
92
+ end
93
+
94
+ def composite?
95
+ @key_test != :single
96
+ end
97
+
98
+ # Oracle metadata is in all caps.
99
+ def with_quoted_identifiers(s)
100
+ s.gsub(/'(\w+)'/) { |m|
101
+ if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
102
+ m.upcase
103
+ else
104
+ m
105
+ end
106
+ }
107
+ end
108
+ end
109
+
110
+ def current_adapter?(type)
111
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
112
+ ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
113
+ end
114
+
115
+ ActiveRecord::Base.connection.class.class_eval do
116
+ cattr_accessor :query_count
117
+ alias_method :execute_without_query_counting, :execute
118
+ def execute_with_query_counting(sql, name = nil)
119
+ self.query_count += 1
120
+ execute_without_query_counting(sql, name)
121
+ end
122
+ end
123
+
124
+ ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)