Class: Test::Unit::TestCase

Inherits:
Object show all
Defined in:
lib/adaptation/test/fake_fixtures.rb,
lib/adaptation/test/test_help.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fixtures(symbol) ⇒ Object



2
3
# File 'lib/adaptation/test/fake_fixtures.rb', line 2

def self.fixtures symbol
end

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/adaptation/test/test_help.rb', line 176

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 = build_message 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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/adaptation/test/test_help.rb', line 141

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 = build_message 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



210
211
212
213
214
215
216
217
# File 'lib/adaptation/test/test_help.rb', line 210

def assert_file_not_present file
  error = build_message error,
          "? shouldn't exist",
          file
  assert_block error do
    !File.exists?(file)
  end
end

#assert_file_present(file) ⇒ Object

Asserts a file exists



200
201
202
203
204
205
206
207
# File 'lib/adaptation/test/test_help.rb', line 200

def assert_file_present file
  error = build_message error,
          "? not found",
          file
  assert_block error do
    File.exists?(file)
  end
end

#assert_message_published(xml_message) ⇒ Object

Asserts that a message has been published in test environment. This means that the message will be searched in the file where the mock object test/mocks/test/publish.rb left it. The file that fakes the mom is deleted every time <em>message<em> method is called.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/adaptation/test/test_help.rb', line 94

def assert_message_published xml_message
  message_object = xml_message

  if message_object.is_a?(String)
    # build Message object with xml_data
    message_type = xml_message[1..(xml_message.index(/(>| )/) - 1)]
    message_class = Adaptation::Message.get_class_object(message_type.capitalize)
    message_object = message_class.to_object(xml_message)
  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.
  message_found = false
  expected = published = ""
  File.open(ADAPTOR_ROOT + '/test/mocks/test/mom.txt', 'r').each{ |line|
    mom_message = REXML::Document.new line
    published   =  line.chop
    expected    =  message_object.to_xml.to_s
    if compare_xml_elements mom_message.root, message_object.to_xml
      message_found = true
      break 
    end
  }
 
  error = build_message(error,
    "? message not published:\n \
Expected : ?\n \
Published: ?\n",
    message_object.class,
    expected,
    published)
  assert_block error do
    message_found
  end
  
end

#assert_not_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 not equivalent to the xml data in the initial fixture file. An Adaptation::Message object cretaed from a xml fixture, will only have the xml tags specified in its class definition (using has_one, has_many, has_text…) so this assertion can be useful to check that undesired xml tags are filtered.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/adaptation/test/test_help.rb', line 47

def assert_not_parsed message_symbol
  data, message_object = load_message_fixture message_symbol

  parsed_data = REXML::Document.new data
  error = build_message error, 
          "? shouldn't be parsed ok:\n data: ?\n real: ?", 
          message_symbol.to_s, 
          parsed_data.to_s, 
          message_object.to_xml.to_s
  assert_block error do
    !compare_xml_elements parsed_data.root, message_object.to_xml
  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.



78
79
80
81
82
83
84
85
86
# File 'lib/adaptation/test/test_help.rb', line 78

def assert_not_validates message_symbol
  data, message_object = load_message_fixture message_symbol
  error = build_message error,
          "? message shouldn't validate",
          message_symbol.to_s           
  assert_block error do
    !message_object.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. An Adaptation::Message object cretaed from a xml fixture, will only have the xml tags specified in its class definition (using has_one, has_many, has_text…) so this assertion can be useful to check that the class is defined correctly.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adaptation/test/test_help.rb', line 26

def assert_parsed message_symbol
  data, message_object = load_message_fixture message_symbol
  
  parsed_data = REXML::Document.new data
  error = build_message error, 
          "? not parsed ok:\n initial: ?\n parsed:  ?", 
          message_symbol.to_s, 
          parsed_data.to_s, 
          message_object.to_xml.to_s
  assert_block error do
    compare_xml_elements parsed_data.root, message_object.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.



64
65
66
67
68
69
70
71
72
73
# File 'lib/adaptation/test/test_help.rb', line 64

def assert_validates message_symbol
  data, message_object = load_message_fixture message_symbol
  error = build_message error,
          "invalid message ?",
          message_symbol.to_s
  message_object.clear_errors
  assert_block error do
    message_object.valid?
  end
end

#get_message_from_fixture(message_symbol) ⇒ Object

Retuns a message object from a fixture, without processing it



244
245
246
# File 'lib/adaptation/test/test_help.rb', line 244

def get_message_from_fixture message_symbol
  load_message_fixture(message_symbol)[1]
end

#message(message_symbol) ⇒ Object

Builds a message from a xml fixture file and processes it the same way mesages from the mom are processed by adaptation, but using a test environment. Published messages will be published to a mock MOM.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/adaptation/test/test_help.rb', line 223

def message message_symbol
  # build a message object from fixture
  message_xml, message_object = load_message_fixture message_symbol

  # 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 message_xml

end