forked from caseydreier/dirty_associations
/
test_helper.rb
72 lines (59 loc) · 2.28 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
ENV["RAILS_ENV"] = "test"
plugin_test_dir = File.dirname(__FILE__)
require File.join(plugin_test_dir, '../../../../config/environment')
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
require 'test_help'
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
end
# This loads a custom schema into the DB (usually a SQLite one) for testing purposes of
# our plugin. Pulls database.yml and schema.rb from the current test directory of the plugin.
def load_test_schema
config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
db_adapter = ENV['DB']
# no db passed, try one of these DBs before bombing.
db_adapter ||= begin
require 'sqlite'
'sqlite'
rescue MissingSourceFile
begin
require 'sqlite3'
'sqlite3'
rescue MissingSourceFile
end
end
if db_adapter.nil?
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
end
# Load our plugin test schema into our DB #
ActiveRecord::Base.establish_connection(config[db_adapter])
load(File.join(File.dirname(__FILE__), "schema.rb"))
require File.join(File.dirname(__FILE__), '../init.rb')
end
# Populates our test db with some records.
def load_test_data
@u1 = User.create(:username => "Alfonzo")
@preferred_user = User.create(:username => "Mortanzo")
@t1 = Task.create(:name => "New Test", :user => @u1)
@k1 = Keyword.create(:word => "RoR")
@k2 = Keyword.create(:word => "Internet")
@k3 = Keyword.create(:word => "Unassigned")
@t1.keywords << @k1
@t1.keywords << @k2
@comment1 = @t1.comments.create(:body => 'Comment text')
@comment1_response = @comment1.responses.create(:body => "Comment response", :task => @t1)
@t1.todos.create(:description => "New Todo Item", :open => true)
@task_with_preferred_user = Task.create(:name => "Blocked Test", :user => @u1, :preferred_user => @preferred_user)
@task_with_preferred_user.blocking_tasks << @t1
end
# Removes all test data from our test db.
def remove_test_data
Task.delete_all
Keyword.delete_all
Todo.delete_all
Dependency.delete_all
end