Module: RGeo::ActiveRecord::AdapterTestHelper

Defined in:
lib/rgeo/active_record/adapter_test_helper.rb

Overview

A helper module for creating unit tests for adapters.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass_) ⇒ Object

When this module is included in a test case class, it automatically attempts to load the database config file from the path specified by constants defined in the class. It first tries OVERRIDE_DATABASE_CONFIG_PATH, and then falls back on DATABASE_CONFIG_PATH. It then defines the DATABASE_CONFIG and DEFAULT_AR_CLASS constants in the testcase class.

When you define your test methods, you should wrap them in a call to the class method define_test_methods. This will cause them to be defined conditionally based on whether the database config is present.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 24

def self.included(klass_)
  database_config_ = ::YAML.load_file(klass_.const_get(:OVERRIDE_DATABASE_CONFIG_PATH)) rescue nil
  database_config_ ||= ::YAML.load_file(klass_.const_get(:DATABASE_CONFIG_PATH)) rescue nil
  if database_config_
    database_config_.stringify_keys!
    if klass_.respond_to?(:before_open_database)
      klass_.before_open_database(:config => database_config_)
    end
    klass_.const_set(:DATABASE_CONFIG, database_config_)
    ar_class_ = AdapterTestHelper.new_class(database_config_)
    klass_.const_set(:DEFAULT_AR_CLASS, ar_class_)
    if klass_.respond_to?(:initialize_database)
      klass_.initialize_database(:ar_class => ar_class_, :connection => ar_class_.connection)
    end
    def klass_.define_test_methods
      yield
    end
  else
    def klass_.define_test_methods
      def test_warning
        puts "WARNING: Couldn't find database.yml; skipping tests."
      end
    end
  end
end

.new_class(param_) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 50

def self.new_class(param_)  # :nodoc:
  base_ = param_.kind_of?(::Class) ? param_ : ::ActiveRecord::Base
  config_ = param_.kind_of?(::Hash) ? param_ : nil
  klass_ = ::Class.new(base_)
  @class_num += 1
  self.const_set("Klass#{@class_num}".to_sym, klass_)
  klass_.class_eval do
    establish_connection(config_) if config_
    self.table_name = :spatial_test
  end
  klass_
end

Instance Method Details

#cleanup_cachesObject

Utility method that cleans up any schema info that was cached by ActiveRecord during a test. Normally called automatically at setup and teardown. If you override those methods, you’ll need to call this from your method.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 99

def cleanup_caches
  klass_ = self.class.const_get(:DEFAULT_AR_CLASS)

  # Clear any RGeo factory settings.
  klass_.connection_pool.rgeo_factory_settings.clear!

  # Clear out any ActiveRecord caches that are present.
  # Different Rails versions use different types of caches.
  klass_.connection_pool.with_connection do |c_|
    if c_.respond_to?(:schema_cache)
      # 3.2.x and 4.0.x
      c_.schema_cache.clear!
    end
    if c_.respond_to?(:clear_cache!)
      # 3.1 and above
      c_.clear_cache!
    end
    # All 3.x and 4.0
    c_.clear_query_cache
  end
end

#cleanup_tablesObject

Utility method that attempts to clean up any table that was created by a test method. Normally called automatically at setup and teardown. If you override those methods, you’ll need to call this from your method.



86
87
88
89
90
91
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 86

def cleanup_tables
  klass_ = self.class.const_get(:DEFAULT_AR_CLASS)
  if klass_.connection.tables.include?('spatial_test')
    klass_.connection.drop_table(:spatial_test)
  end
end

#create_ar_class(opts_ = {}) ⇒ Object

Utility method that creates and returns a new ActiveRecord class subclassing the DEFAULT_AR_CLASS.



124
125
126
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 124

def create_ar_class(opts_={})
  @ar_class = AdapterTestHelper.new_class(self.class.const_get(:DEFAULT_AR_CLASS))
end

#setupObject

Default setup method that calls cleanup_tables. It also defines a couple of useful factories: @factory (a cartesian factory) and @geographic_factory (a spherical factory)



67
68
69
70
71
72
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 67

def setup
  @factory = ::RGeo::Cartesian.preferred_factory(:srid => 3785)
  @geographic_factory = ::RGeo::Geographic.spherical_factory(:srid => 4326)
  cleanup_tables
  cleanup_caches
end

#teardownObject

Default teardown method that calls cleanup_tables.



76
77
78
79
# File 'lib/rgeo/active_record/adapter_test_helper.rb', line 76

def teardown
  cleanup_tables
  cleanup_caches
end