##// END OF EJS Templates
Updated some object_daddy exemplars for the Models....
Eric Davis -
r2823:6456f7c4a481
parent child
Show More
@@ -0,0 +1,9
1 class Query < ActiveRecord::Base
2 generator_for :name, :method => :next_name
3
4 def self.next_name
5 @last_name ||= 'Query 0'
6 @last_name.succ!
7 @last_name
8 end
9 end
@@ -1,17 +1,31
1 class Project < ActiveRecord::Base
1 class Project < ActiveRecord::Base
2 generator_for :name, :method => :next_name
2 generator_for :name, :method => :next_name
3 generator_for :identifier, :method => :next_identifier_from_object_daddy
3 generator_for :identifier, :method => :next_identifier_from_object_daddy
4
4 generator_for :enabled_modules, :method => :all_modules
5 generator_for :trackers, :method => :next_tracker
6
5 def self.next_name
7 def self.next_name
6 @last_name ||= 'Project 0'
8 @last_name ||= 'Project 0'
7 @last_name.succ!
9 @last_name.succ!
8 @last_name
10 @last_name
9 end
11 end
10
12
11 # Project#next_identifier is defined on Redmine
13 # Project#next_identifier is defined on Redmine
12 def self.next_identifier_from_object_daddy
14 def self.next_identifier_from_object_daddy
13 @last_identifier ||= 'project0'
15 @last_identifier ||= 'project0'
14 @last_identifier.succ!
16 @last_identifier.succ!
15 @last_identifier
17 @last_identifier
16 end
18 end
19
20 def self.all_modules
21 returning [] do |modules|
22 Redmine::AccessControl.available_project_modules.each do |name|
23 modules << EnabledModule.new(:name => name.to_s)
24 end
25 end
26 end
27
28 def self.next_tracker
29 [Tracker.generate!]
30 end
17 end
31 end
@@ -1,87 +1,105
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 ENV["RAILS_ENV"] ||= "test"
18 ENV["RAILS_ENV"] ||= "test"
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 require 'test_help'
20 require 'test_help'
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23
23
24 # TODO: The gem or official version of ObjectDaddy doesn't set
24 # TODO: The gem or official version of ObjectDaddy doesn't set
25 # protected attributes so they need to be wrapped.
25 # protected attributes so they need to be wrapped.
26 def User.generate_with_protected!(attributes={})
26 def User.generate_with_protected!(attributes={})
27 user = User.spawn(attributes) do |user|
27 user = User.spawn(attributes) do |user|
28 user.login = User.next_login
28 user.login = User.next_login
29 attributes.each do |attr,v|
29 attributes.each do |attr,v|
30 user.send("#{attr}=", v)
30 user.send("#{attr}=", v)
31 end
31 end
32 end
32 end
33 user.save!
33 user.save!
34 user
34 user
35 end
35 end
36
36
37 # Generate the default Query
38 def Query.generate_default!(attributes={})
39 query = Query.spawn(attributes)
40 query.name ||= '_'
41 query.save!
42 query
43 end
44
45 # Generate an issue for a project, using it's trackers
46 def Issue.generate_for_project!(project, attributes={})
47 issue = Issue.spawn(attributes) do |issue|
48 issue.project = project
49 end
50 issue.tracker = project.trackers.first unless project.trackers.empty?
51 issue.save!
52 issue
53 end
54
37 class ActiveSupport::TestCase
55 class ActiveSupport::TestCase
38 # Transactional fixtures accelerate your tests by wrapping each test method
56 # Transactional fixtures accelerate your tests by wrapping each test method
39 # in a transaction that's rolled back on completion. This ensures that the
57 # in a transaction that's rolled back on completion. This ensures that the
40 # test database remains unchanged so your fixtures don't have to be reloaded
58 # test database remains unchanged so your fixtures don't have to be reloaded
41 # between every test method. Fewer database queries means faster tests.
59 # between every test method. Fewer database queries means faster tests.
42 #
60 #
43 # Read Mike Clark's excellent walkthrough at
61 # Read Mike Clark's excellent walkthrough at
44 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
62 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
45 #
63 #
46 # Every Active Record database supports transactions except MyISAM tables
64 # Every Active Record database supports transactions except MyISAM tables
47 # in MySQL. Turn off transactional fixtures in this case; however, if you
65 # in MySQL. Turn off transactional fixtures in this case; however, if you
48 # don't care one way or the other, switching from MyISAM to InnoDB tables
66 # don't care one way or the other, switching from MyISAM to InnoDB tables
49 # is recommended.
67 # is recommended.
50 self.use_transactional_fixtures = true
68 self.use_transactional_fixtures = true
51
69
52 # Instantiated fixtures are slow, but give you @david where otherwise you
70 # Instantiated fixtures are slow, but give you @david where otherwise you
53 # would need people(:david). If you don't want to migrate your existing
71 # would need people(:david). If you don't want to migrate your existing
54 # test cases which use the @david style and don't mind the speed hit (each
72 # test cases which use the @david style and don't mind the speed hit (each
55 # instantiated fixtures translates to a database query per test method),
73 # instantiated fixtures translates to a database query per test method),
56 # then set this back to true.
74 # then set this back to true.
57 self.use_instantiated_fixtures = false
75 self.use_instantiated_fixtures = false
58
76
59 # Add more helper methods to be used by all tests here...
77 # Add more helper methods to be used by all tests here...
60
78
61 def log_user(login, password)
79 def log_user(login, password)
62 get "/login"
80 get "/login"
63 assert_equal nil, session[:user_id]
81 assert_equal nil, session[:user_id]
64 assert_response :success
82 assert_response :success
65 assert_template "account/login"
83 assert_template "account/login"
66 post "/login", :username => login, :password => password
84 post "/login", :username => login, :password => password
67 assert_equal login, User.find(session[:user_id]).login
85 assert_equal login, User.find(session[:user_id]).login
68 end
86 end
69
87
70 def uploaded_test_file(name, mime)
88 def uploaded_test_file(name, mime)
71 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
89 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
72 end
90 end
73
91
74 # Use a temporary directory for attachment related tests
92 # Use a temporary directory for attachment related tests
75 def set_tmp_attachments_directory
93 def set_tmp_attachments_directory
76 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
94 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
77 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
95 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
78 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
96 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
79 end
97 end
80
98
81 def with_settings(options, &block)
99 def with_settings(options, &block)
82 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
100 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
83 options.each {|k, v| Setting[k] = v}
101 options.each {|k, v| Setting[k] = v}
84 yield
102 yield
85 saved_settings.each {|k, v| Setting[k] = v}
103 saved_settings.each {|k, v| Setting[k] = v}
86 end
104 end
87 end
105 end
General Comments 0
You need to be logged in to leave comments. Login now