Commit f3847005bdcef02728c88bfe3bac6f76dd68b5d9

Authored by Andrew Kane
1 parent 58e2b2cd

Made it easy to test Mongoid

Showing 1 changed file with 45 additions and 34 deletions   Show diff stats
test/test_helper.rb
... ... @@ -2,47 +2,62 @@ require "bundler/setup"
2 2 Bundler.require(:default)
3 3 require "minitest/autorun"
4 4 require "minitest/pride"
5   -require "active_record"
6 5  
7 6 ENV["RACK_ENV"] = "test"
8 7  
9   -# for debugging
10   -# ActiveRecord::Base.logger = Logger.new(STDOUT)
11   -
12   -# rails does this in activerecord/lib/active_record/railtie.rb
13   -ActiveRecord::Base.default_timezone = :utc
14   -ActiveRecord::Base.time_zone_aware_attributes = true
15   -
16   -# migrations
17   -ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "searchkick_test"
18   -
19   -ActiveRecord::Migration.create_table :products, :force => true do |t|
20   - t.string :name
21   - t.integer :store_id
22   - t.boolean :in_stock
23   - t.boolean :backordered
24   - t.integer :orders_count
25   - t.string :color
26   - t.timestamps
27   -end
28   -
29   -ActiveRecord::Migration.create_table :store, :force => true do |t|
30   -end
31   -
32 8 File.delete("elasticsearch.log") if File.exists?("elasticsearch.log")
33 9 Tire.configure do
34 10 logger "elasticsearch.log", :level => "debug"
35 11 pretty true
36 12 end
37 13  
38   -# Mongoid.configure do |config|
39   -# config.connect_to "searchkick_test"
40   -# end
  14 +if ENV["MONGOID"]
  15 + Mongoid.configure do |config|
  16 + config.connect_to "searchkick_test"
  17 + end
41 18  
42   -class Product < ActiveRecord::Base
43   - # include Mongoid::Document
44   - # include Mongoid::Attributes::Dynamic
  19 + class Product
  20 + include Mongoid::Document
  21 + # include Mongoid::Attributes::Dynamic
  22 + end
45 23  
  24 + class Store
  25 + include Mongoid::Document
  26 + end
  27 +else
  28 + require "active_record"
  29 +
  30 + # for debugging
  31 + # ActiveRecord::Base.logger = Logger.new(STDOUT)
  32 +
  33 + # rails does this in activerecord/lib/active_record/railtie.rb
  34 + ActiveRecord::Base.default_timezone = :utc
  35 + ActiveRecord::Base.time_zone_aware_attributes = true
  36 +
  37 + # migrations
  38 + ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "searchkick_test"
  39 +
  40 + ActiveRecord::Migration.create_table :products, :force => true do |t|
  41 + t.string :name
  42 + t.integer :store_id
  43 + t.boolean :in_stock
  44 + t.boolean :backordered
  45 + t.integer :orders_count
  46 + t.string :color
  47 + t.timestamps
  48 + end
  49 +
  50 + ActiveRecord::Migration.create_table :store, :force => true do |t|
  51 + end
  52 +
  53 + class Product < ActiveRecord::Base
  54 + end
  55 +
  56 + class Store < ActiveRecord::Base
  57 + end
  58 +end
  59 +
  60 +class Product
46 61 belongs_to :store
47 62  
48 63 searchkick \
... ... @@ -70,10 +85,6 @@ class Product &lt; ActiveRecord::Base
70 85 end
71 86 end
72 87  
73   -class Store < ActiveRecord::Base
74   - # include Mongoid::Document
75   -end
76   -
77 88 Product.tire.index.delete if Product.tire.index.exists?
78 89 Product.reindex
79 90 Product.reindex # run twice for both index paths
... ...