Class: ActiveSupport::TestCase

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::TestFixtures
Defined in:
lib/adaptation/test/test_help.rb

Instance Method Summary collapse

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



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



190
191
192
193
194
195
196
197
# File 'lib/adaptation/test/test_help.rb', line 190

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



180
181
182
183
184
185
186
187
# File 'lib/adaptation/test/test_help.rb', line 180

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.



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 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.nil? ? Adaptation::Message.new(xml_message) : message_class.new(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|
    published   =  line.chop
    expected    =  message_object.to_xml.to_s
    if compare_xml_elements published, 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_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 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.



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 message_symbol
  data, message_object = load_message_fixture message_symbol
 
  #parsed_data = Adaptation::Message.new(data)
  error = build_message error, 
          "? not parsed ok:\n initial: ?\n parsed:  ?", 
          message_symbol.to_s, 
          data, 
          message_object.to_xml
  assert_block error do
    compare_xml_elements data, 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.



45
46
47
48
49
50
51
52
53
54
# File 'lib/adaptation/test/test_help.rb', line 45

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

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 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 messages from the mom are processed by adaptation, but using a test environment. Messages published with publish will be published to a mocked MOM (and can be checked with assert_message_published)



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