##// END OF EJS Templates
Refactor: extract test method...
Eric Davis -
r3659:cf334cee3189
parent child
Show More
@@ -1,21 +1,17
1 class Attachment < ActiveRecord::Base
1 class Attachment < ActiveRecord::Base
2 generator_for :container, :method => :generate_project
2 generator_for :container, :method => :generate_project
3 generator_for :file, :method => :generate_file
3 generator_for :file, :method => :generate_file
4 generator_for :author, :method => :generate_author
4 generator_for :author, :method => :generate_author
5
5
6 def self.generate_project
6 def self.generate_project
7 Project.generate!
7 Project.generate!
8 end
8 end
9
9
10 def self.generate_author
10 def self.generate_author
11 User.generate_with_protected!
11 User.generate_with_protected!
12 end
12 end
13
13
14 def self.generate_file
14 def self.generate_file
15 @file = 'a_file.png'
15 @file = mock_file
16 @file.stubs(:original_filename).returns('a_file.png')
17 @file.stubs(:content_type).returns('image/png')
18 @file.stubs(:read).returns(false)
19 @file
20 end
16 end
21 end
17 end
@@ -1,155 +1,165
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 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 include ObjectDaddyHelpers
25 include ObjectDaddyHelpers
26
26
27 class ActiveSupport::TestCase
27 class ActiveSupport::TestCase
28 # Transactional fixtures accelerate your tests by wrapping each test method
28 # Transactional fixtures accelerate your tests by wrapping each test method
29 # in a transaction that's rolled back on completion. This ensures that the
29 # in a transaction that's rolled back on completion. This ensures that the
30 # test database remains unchanged so your fixtures don't have to be reloaded
30 # test database remains unchanged so your fixtures don't have to be reloaded
31 # between every test method. Fewer database queries means faster tests.
31 # between every test method. Fewer database queries means faster tests.
32 #
32 #
33 # Read Mike Clark's excellent walkthrough at
33 # Read Mike Clark's excellent walkthrough at
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 #
35 #
36 # Every Active Record database supports transactions except MyISAM tables
36 # Every Active Record database supports transactions except MyISAM tables
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 # is recommended.
39 # is recommended.
40 self.use_transactional_fixtures = true
40 self.use_transactional_fixtures = true
41
41
42 # Instantiated fixtures are slow, but give you @david where otherwise you
42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 # would need people(:david). If you don't want to migrate your existing
43 # would need people(:david). If you don't want to migrate your existing
44 # test cases which use the @david style and don't mind the speed hit (each
44 # test cases which use the @david style and don't mind the speed hit (each
45 # instantiated fixtures translates to a database query per test method),
45 # instantiated fixtures translates to a database query per test method),
46 # then set this back to true.
46 # then set this back to true.
47 self.use_instantiated_fixtures = false
47 self.use_instantiated_fixtures = false
48
48
49 # Add more helper methods to be used by all tests here...
49 # Add more helper methods to be used by all tests here...
50
50
51 def log_user(login, password)
51 def log_user(login, password)
52 User.anonymous
52 User.anonymous
53 get "/login"
53 get "/login"
54 assert_equal nil, session[:user_id]
54 assert_equal nil, session[:user_id]
55 assert_response :success
55 assert_response :success
56 assert_template "account/login"
56 assert_template "account/login"
57 post "/login", :username => login, :password => password
57 post "/login", :username => login, :password => password
58 assert_equal login, User.find(session[:user_id]).login
58 assert_equal login, User.find(session[:user_id]).login
59 end
59 end
60
60
61 def uploaded_test_file(name, mime)
61 def uploaded_test_file(name, mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
63 end
63 end
64
65 # Mock out a file
66 def mock_file
67 file = 'a_file.png'
68 file.stubs(:size).returns(32)
69 file.stubs(:original_filename).returns('a_file.png')
70 file.stubs(:content_type).returns('image/png')
71 file.stubs(:read).returns(false)
72 file
73 end
64
74
65 # Use a temporary directory for attachment related tests
75 # Use a temporary directory for attachment related tests
66 def set_tmp_attachments_directory
76 def set_tmp_attachments_directory
67 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
77 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
68 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
78 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
69 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
79 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
70 end
80 end
71
81
72 def with_settings(options, &block)
82 def with_settings(options, &block)
73 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
83 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
74 options.each {|k, v| Setting[k] = v}
84 options.each {|k, v| Setting[k] = v}
75 yield
85 yield
76 saved_settings.each {|k, v| Setting[k] = v}
86 saved_settings.each {|k, v| Setting[k] = v}
77 end
87 end
78
88
79 def self.ldap_configured?
89 def self.ldap_configured?
80 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
90 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
81 return @test_ldap.bind
91 return @test_ldap.bind
82 rescue Exception => e
92 rescue Exception => e
83 # LDAP is not listening
93 # LDAP is not listening
84 return nil
94 return nil
85 end
95 end
86
96
87 # Returns the path to the test +vendor+ repository
97 # Returns the path to the test +vendor+ repository
88 def self.repository_path(vendor)
98 def self.repository_path(vendor)
89 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
99 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
90 end
100 end
91
101
92 # Returns true if the +vendor+ test repository is configured
102 # Returns true if the +vendor+ test repository is configured
93 def self.repository_configured?(vendor)
103 def self.repository_configured?(vendor)
94 File.directory?(repository_path(vendor))
104 File.directory?(repository_path(vendor))
95 end
105 end
96
106
97 # Shoulda macros
107 # Shoulda macros
98 def self.should_render_404
108 def self.should_render_404
99 should_respond_with :not_found
109 should_respond_with :not_found
100 should_render_template 'common/404'
110 should_render_template 'common/404'
101 end
111 end
102
112
103 def self.should_have_before_filter(expected_method, options = {})
113 def self.should_have_before_filter(expected_method, options = {})
104 should_have_filter('before', expected_method, options)
114 should_have_filter('before', expected_method, options)
105 end
115 end
106
116
107 def self.should_have_after_filter(expected_method, options = {})
117 def self.should_have_after_filter(expected_method, options = {})
108 should_have_filter('after', expected_method, options)
118 should_have_filter('after', expected_method, options)
109 end
119 end
110
120
111 def self.should_have_filter(filter_type, expected_method, options)
121 def self.should_have_filter(filter_type, expected_method, options)
112 description = "have #{filter_type}_filter :#{expected_method}"
122 description = "have #{filter_type}_filter :#{expected_method}"
113 description << " with #{options.inspect}" unless options.empty?
123 description << " with #{options.inspect}" unless options.empty?
114
124
115 should description do
125 should description do
116 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
126 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
117 expected = klass.new(:filter, expected_method.to_sym, options)
127 expected = klass.new(:filter, expected_method.to_sym, options)
118 assert_equal 1, @controller.class.filter_chain.select { |filter|
128 assert_equal 1, @controller.class.filter_chain.select { |filter|
119 filter.method == expected.method && filter.kind == expected.kind &&
129 filter.method == expected.method && filter.kind == expected.kind &&
120 filter.options == expected.options && filter.class == expected.class
130 filter.options == expected.options && filter.class == expected.class
121 }.size
131 }.size
122 end
132 end
123 end
133 end
124
134
125 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
135 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
126 context "" do
136 context "" do
127 setup do
137 setup do
128 if block_given?
138 if block_given?
129 instance_eval &block
139 instance_eval &block
130 else
140 else
131 @old_value = model.generate!
141 @old_value = model.generate!
132 @new_value = model.generate!
142 @new_value = model.generate!
133 end
143 end
134 end
144 end
135
145
136 should "use the new value's name" do
146 should "use the new value's name" do
137 @detail = JournalDetail.generate!(:property => 'attr',
147 @detail = JournalDetail.generate!(:property => 'attr',
138 :old_value => @old_value.id,
148 :old_value => @old_value.id,
139 :value => @new_value.id,
149 :value => @new_value.id,
140 :prop_key => prop_key)
150 :prop_key => prop_key)
141
151
142 assert_match @new_value.name, show_detail(@detail, true)
152 assert_match @new_value.name, show_detail(@detail, true)
143 end
153 end
144
154
145 should "use the old value's name" do
155 should "use the old value's name" do
146 @detail = JournalDetail.generate!(:property => 'attr',
156 @detail = JournalDetail.generate!(:property => 'attr',
147 :old_value => @old_value.id,
157 :old_value => @old_value.id,
148 :value => @new_value.id,
158 :value => @new_value.id,
149 :prop_key => prop_key)
159 :prop_key => prop_key)
150
160
151 assert_match @old_value.name, show_detail(@detail, true)
161 assert_match @old_value.name, show_detail(@detail, true)
152 end
162 end
153 end
163 end
154 end
164 end
155 end
165 end
@@ -1,92 +1,85
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # redMine - project management software
3 # redMine - project management software
4 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # Copyright (C) 2006-2007 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.dirname(__FILE__) + '/../test_helper'
20 require File.dirname(__FILE__) + '/../test_helper'
21
21
22 class AttachmentTest < ActiveSupport::TestCase
22 class AttachmentTest < ActiveSupport::TestCase
23 fixtures :issues, :users
23 fixtures :issues, :users
24
24
25 def setup
25 def setup
26 end
26 end
27
27
28 def test_create
28 def test_create
29 a = Attachment.new(:container => Issue.find(1),
29 a = Attachment.new(:container => Issue.find(1),
30 :file => uploaded_test_file("testfile.txt", "text/plain"),
30 :file => uploaded_test_file("testfile.txt", "text/plain"),
31 :author => User.find(1))
31 :author => User.find(1))
32 assert a.save
32 assert a.save
33 assert_equal 'testfile.txt', a.filename
33 assert_equal 'testfile.txt', a.filename
34 assert_equal 59, a.filesize
34 assert_equal 59, a.filesize
35 assert_equal 'text/plain', a.content_type
35 assert_equal 'text/plain', a.content_type
36 assert_equal 0, a.downloads
36 assert_equal 0, a.downloads
37 assert_equal Digest::MD5.hexdigest(uploaded_test_file("testfile.txt", "text/plain").read), a.digest
37 assert_equal Digest::MD5.hexdigest(uploaded_test_file("testfile.txt", "text/plain").read), a.digest
38 assert File.exist?(a.diskfile)
38 assert File.exist?(a.diskfile)
39 end
39 end
40
40
41 def test_create_should_auto_assign_content_type
41 def test_create_should_auto_assign_content_type
42 a = Attachment.new(:container => Issue.find(1),
42 a = Attachment.new(:container => Issue.find(1),
43 :file => uploaded_test_file("testfile.txt", ""),
43 :file => uploaded_test_file("testfile.txt", ""),
44 :author => User.find(1))
44 :author => User.find(1))
45 assert a.save
45 assert a.save
46 assert_equal 'text/plain', a.content_type
46 assert_equal 'text/plain', a.content_type
47 end
47 end
48
48
49 def test_identical_attachments_at_the_same_time_should_not_overwrite
49 def test_identical_attachments_at_the_same_time_should_not_overwrite
50 a1 = Attachment.create!(:container => Issue.find(1),
50 a1 = Attachment.create!(:container => Issue.find(1),
51 :file => uploaded_test_file("testfile.txt", ""),
51 :file => uploaded_test_file("testfile.txt", ""),
52 :author => User.find(1))
52 :author => User.find(1))
53 a2 = Attachment.create!(:container => Issue.find(1),
53 a2 = Attachment.create!(:container => Issue.find(1),
54 :file => uploaded_test_file("testfile.txt", ""),
54 :file => uploaded_test_file("testfile.txt", ""),
55 :author => User.find(1))
55 :author => User.find(1))
56 assert a1.disk_filename != a2.disk_filename
56 assert a1.disk_filename != a2.disk_filename
57 end
57 end
58
58
59 def test_diskfilename
59 def test_diskfilename
60 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
60 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
61 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
61 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
62 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
62 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
63 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
63 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
64 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
64 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
65 end
65 end
66
66
67 context "Attachmnet#attach_files" do
67 context "Attachmnet#attach_files" do
68 should "add unsaved files to the object as unsaved attachments" do
68 should "add unsaved files to the object as unsaved attachments" do
69 # Max size of 0 to force Attachment creation failures
69 # Max size of 0 to force Attachment creation failures
70 with_settings(:attachment_max_size => 0) do
70 with_settings(:attachment_max_size => 0) do
71 # Mock out a file
72 @file = 'a_file.png'
73 @file.stubs(:size).returns(32)
74 @file.stubs(:original_filename).returns('a_file.png')
75 @file.stubs(:content_type).returns('image/png')
76 @file.stubs(:read).returns(false)
77
78 @project = Project.generate!
71 @project = Project.generate!
79 response = Attachment.attach_files(@project, {
72 response = Attachment.attach_files(@project, {
80 '1' => {'file' => @file, 'description' => 'test'},
73 '1' => {'file' => mock_file, 'description' => 'test'},
81 '2' => {'file' => @file, 'description' => 'test'}
74 '2' => {'file' => mock_file, 'description' => 'test'}
82 })
75 })
83
76
84 assert response[:unsaved].present?
77 assert response[:unsaved].present?
85 assert_equal 2, response[:unsaved].length
78 assert_equal 2, response[:unsaved].length
86 assert response[:unsaved].first.new_record?
79 assert response[:unsaved].first.new_record?
87 assert response[:unsaved].second.new_record?
80 assert response[:unsaved].second.new_record?
88 assert_equal response[:unsaved], @project.unsaved_attachments
81 assert_equal response[:unsaved], @project.unsaved_attachments
89 end
82 end
90 end
83 end
91 end
84 end
92 end
85 end
General Comments 0
You need to be logged in to leave comments. Login now