Class: ActiveSupport::TestCase
- Includes:
- ActiveRecord::TestFixtures
- Defined in:
- lib/adaptation/test/test_help.rb
Instance Method Summary collapse
-
#assert_database_not_present(db_settings_hash) ⇒ Object
Asserts a database doesn’t exist.
-
#assert_database_present(db_settings_hash) ⇒ Object
Asserts a database exists.
-
#assert_file_not_present(file) ⇒ Object
Asserts a file doesn’t exist.
-
#assert_file_present(file) ⇒ Object
Asserts a file exists.
-
#assert_message_published(xml_message) ⇒ Object
Asserts that a message has been published in test environment.
-
#assert_not_validates(message_symbol) ⇒ Object
Asserts that an Adaptation::Message message, build from a xml fixture file doesn’t pass all the validations specified in the class definition.
-
#assert_parsed(message_symbol) ⇒ Object
Asserts that a message in a xml fixture file is converted into an Adaptation::Message that if serialized again to xml is equivalent to the xml data in the initial fixture file.
-
#assert_validates(message_symbol) ⇒ Object
Asserts that an Adaptation::Message message build from a xml fixture file passes all the validations specified in the class definition.
-
#get_message_from_fixture(message_symbol) ⇒ Object
Returns an Adaptation::Message object from a fixture, without processing it (or an instance of the corresponding subclass, if it’s defined).
-
#message(message_symbol) ⇒ Object
Builds a message from a xml fixture file and processes it the same way messages from the mom are processed by adaptation, but using a test environment.
Instance Method Details
#assert_database_not_present(db_settings_hash) ⇒ Object
Asserts a database doesn’t exist. To do so this method tries to establish a connection with the specified database. Connection information must be provided with a hash. This method assert if connection fails, but that could also mean that provided connection hash is wrong. The connection options are the same as in assert_database_present:
:database
-
=> database name
:host
-
=> database host
:username
-
=> database user
:password
-
=> database password
:adapter
-
=> database type (default is "mysql")
These options correspond to those in Activerecord::Base.establish_conection
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/adaptation/test/test_help.rb', line 156 def assert_database_not_present db_settings_hash update_activerecord_test_configuration db_settings_hash ActiveRecord::Base.remove_connection ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ADAPTOR_ENV]) database_exists = true begin connection = ActiveRecord::Base.connection rescue Exception => e database_exists = false end error = error, "? database shouldn't exist", ActiveRecord::Base.configurations[ADAPTOR_ENV][:database] assert_block error do !database_exists end end |
#assert_database_present(db_settings_hash) ⇒ Object
Asserts a database exists. To do so this method tries to establish a connection with the specified database. Conection information must be provided with a hash:
:database
-
=> database name
:host
-
=> database host
:username
-
=> database user
:password
-
=> database password
:adapter
-
=> database type (default is "mysql")
These options correspond to those in Activerecord::Base.establish_conection
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/adaptation/test/test_help.rb', line 121 def assert_database_present db_settings_hash update_activerecord_test_configuration db_settings_hash ActiveRecord::Base.remove_connection ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ADAPTOR_ENV]) database_exists = true begin connection = ActiveRecord::Base.connection rescue Exception => e database_exists = false end error = error, "? database not found", ActiveRecord::Base.configurations[ADAPTOR_ENV][:database] assert_block error do database_exists end end |
#assert_file_not_present(file) ⇒ Object
Asserts a file doesn’t exist
190 191 192 193 194 195 196 197 |
# File 'lib/adaptation/test/test_help.rb', line 190 def assert_file_not_present file error = error, "? shouldn't exist", file assert_block error do !File.exists?(file) end end |
#assert_file_present(file) ⇒ Object
Asserts a file exists
180 181 182 183 184 185 186 187 |
# File 'lib/adaptation/test/test_help.rb', line 180 def assert_file_present file error = error, "? not found", file assert_block error do File.exists?(file) end end |
#assert_message_published(xml_message) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/adaptation/test/test_help.rb', line 75 def = if .is_a?(String) # build Message object with xml_data = [1..(.index(/(>| )/) - 1)] = Adaptation::Message.get_class_object(.capitalize) = .nil? ? Adaptation::Message.new() : .new() end # check for all messages "published" in the mom (that's file /tmp/mom.txt), # if any line corresponds to the message passed as a parameter. = false expected = published = "" File.open(ADAPTOR_ROOT + '/test/mocks/test/mom.txt', 'r').each{ |line| published = line.chop expected = .to_xml.to_s if compare_xml_elements published, .to_xml = true break end } error = (error, "? message not published:\n \ Expected : ?\n \ Published: ?\n", .class, expected, published) assert_block error do end end |
#assert_not_validates(message_symbol) ⇒ Object
Asserts that an Adaptation::Message message, build from a xml fixture file doesn’t pass all the validations specified in the class definition.
59 60 61 62 63 64 65 66 67 |
# File 'lib/adaptation/test/test_help.rb', line 59 def assert_not_validates data, = error = error, "? message shouldn't validate", .to_s assert_block error do !.valid? end end |
#assert_parsed(message_symbol) ⇒ Object
Asserts that a message in a xml fixture file is converted into an Adaptation::Message that if serialized again to xml is equivalent to the xml data in the initial fixture file.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/adaptation/test/test_help.rb', line 28 def assert_parsed data, = #parsed_data = Adaptation::Message.new(data) error = error, "? not parsed ok:\n initial: ?\n parsed: ?", .to_s, data, .to_xml assert_block error do compare_xml_elements data, .to_xml end end |
#assert_validates(message_symbol) ⇒ Object
Asserts that an Adaptation::Message message build from a xml fixture file passes all the validations specified in the class definition.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/adaptation/test/test_help.rb', line 45 def assert_validates data, = error = error, "invalid message ?", .to_s .clear_errors assert_block error do .valid? end end |
#get_message_from_fixture(message_symbol) ⇒ Object
Returns an Adaptation::Message object from a fixture, without processing it (or an instance of the corresponding subclass, if it’s defined).
225 226 227 |
# File 'lib/adaptation/test/test_help.rb', line 225 def ()[1] end |
#message(message_symbol) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/adaptation/test/test_help.rb', line 204 def # build a message object from fixture , = # load mock objects Dir["test/mocks/test/*.rb"].each do |f| require f end # clean mom (delete mom.txt file) mom_mock_file = ADAPTOR_ROOT + '/test/mocks/test/mom.txt' if File.exists? mom_mock_file File.delete mom_mock_file end Adaptation::Base.new.process end |