marty 2.3.14 → 2.3.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- module DelayedJobHelpers
1
+ module Marty::RSpec::DelayedJobHelpers
2
2
  def start_delayed_job
3
3
  # start delayed job workers and wait a few seconds
4
4
  `RAILS_ENV=test spec/dummy/script/delayed_job -n 4 stop | cat`
@@ -0,0 +1,52 @@
1
+ module Marty::RSpec::DownloadHelper
2
+ TIMEOUT = 10
3
+ PATH = Rails.root.join('spec/tmp/downloads')
4
+
5
+ ACCEPTED_EXTS = ['.xlsx', '.csv']
6
+
7
+ extend self
8
+
9
+ def downloads
10
+ Dir[PATH.join("*")]
11
+ end
12
+
13
+ def download
14
+ downloads.first
15
+ end
16
+
17
+ def download_content
18
+ wait_for_download
19
+ # doesn't work for excel files...
20
+ File.read(download)
21
+ end
22
+
23
+ def download_content_acceptable?
24
+ wait_for_download
25
+ downloads.each do |f|
26
+ return false unless ACCEPTED_EXTS.include? File.extname(f)
27
+ end
28
+ true
29
+ end
30
+
31
+ def wait_for_download
32
+ Timeout.timeout(TIMEOUT) do
33
+ sleep 0.1 until downloaded?
34
+ end
35
+ end
36
+
37
+ def downloaded?
38
+ downloads.any? && !downloading?
39
+ end
40
+
41
+ def downloading?
42
+ downloads.grep(/\.part$/).any? ||
43
+ downloads.select { |f| File.size(f).zero? }.any?
44
+ end
45
+
46
+ def clear_downloads
47
+ FileUtils.rm_f(downloads)
48
+ Timeout.timeout(TIMEOUT) do
49
+ sleep 0.1 until !downloads.any?
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ require 'delorean_lang'
2
+
3
+ module Marty; module RSpec;
4
+ class Helper
5
+ include Delorean::Model
6
+ # Helper function which increments a global counter. Can be used by
7
+ # tests which run Delorean code to see how many times some code is
8
+ # being called. Works for rule scripts as well.
9
+ delorean_fn :global_inc, sig: 1 do
10
+ |inc|
11
+ @@global_inc ||= 0
12
+
13
+ if inc
14
+ @@global_inc += inc
15
+ else
16
+ @@global_inc = 0
17
+ end
18
+ end
19
+ end
20
+ end end
@@ -0,0 +1,306 @@
1
+ module Marty; module RSpec; module Netzke
2
+ MAX_WAIT_TIME = 5.0
3
+
4
+ def by message, level=0
5
+ wait_for_ready(10)
6
+ pending(message) unless block_given?
7
+ yield
8
+ end
9
+
10
+ alias and_by by
11
+
12
+ ############################################################################
13
+ # navigation helpers
14
+ ############################################################################
15
+
16
+ def ensure_on(path)
17
+ visit(path) unless current_path == path
18
+ end
19
+
20
+ def log_in(username, password)
21
+ wait_for_ready(10)
22
+
23
+ begin
24
+ if first("a[data-qtip='Current user']")
25
+ log_out
26
+ wait_for_ajax
27
+ end
28
+ rescue
29
+ # ignore error
30
+ end
31
+
32
+ find(:xpath, "//span", text: 'Sign in', match: :first, wait: 5).click
33
+ fill_in("login", :with => username)
34
+ fill_in("password", :with => password)
35
+ press("OK")
36
+ wait_for_ajax
37
+ end
38
+
39
+ def log_in_as(username)
40
+ Rails.configuration.marty.auth_source = 'local'
41
+
42
+ ensure_on("/")
43
+ log_in(username, Rails.configuration.marty.local_password)
44
+ ensure_on("/")
45
+ end
46
+
47
+ def log_out
48
+ press("Current user")
49
+ press("Sign out")
50
+ end
51
+
52
+ def press button_name, index_of = 0
53
+ wait_for_element do
54
+ begin
55
+ cmp = first("a[data-qtip='#{button_name}']")
56
+ cmp ||= first(:xpath, ".//a", text: "#{button_name}")
57
+ cmp ||= find(:btn, button_name, match: :first)
58
+ cmp.click
59
+ true
60
+ rescue
61
+ find_by_id(ext_button_id(button_name, index_of), visible: :all).click
62
+ true
63
+ end
64
+ end
65
+ end
66
+
67
+ def popup message = ''
68
+ wait_for_ready
69
+ yield if block_given?
70
+ close_window
71
+ end
72
+
73
+ def close_window
74
+ find(:xpath, '//div[contains(@class, "x-tool-close")]', wait: 5).click
75
+ end
76
+
77
+ ############################################################################
78
+ # stability functions
79
+ ############################################################################
80
+
81
+ def wait_for_ready wait_time = nil
82
+ if wait_time
83
+ find(:status, 'Ready', wait: wait_time)
84
+ else
85
+ find(:status, 'Ready')
86
+ end
87
+ end
88
+
89
+ def wait_for_ajax wait_time = 10
90
+ wait_for_ready(wait_time)
91
+ wait_for_element { !ajax_loading? }
92
+ wait_for_ready
93
+ end
94
+
95
+ def ajax_loading?
96
+ page.execute_script <<-JS
97
+ return Netzke.ajaxIsLoading() || Ext.Ajax.isLoading();
98
+ JS
99
+ end
100
+
101
+ def wait_for_element(seconds_to_wait = 2.0, sleeptime = 0.1)
102
+ res = nil
103
+ start_time = current_time = Time.now
104
+ while !res && current_time - start_time < seconds_to_wait
105
+ begin
106
+ res = yield
107
+ rescue
108
+ ensure
109
+ sleep sleeptime
110
+ current_time = Time.now
111
+ end
112
+ end
113
+ res
114
+ end
115
+
116
+ ############################################################################
117
+ # note that netzke_find doesn't actually find the component (as in Capybara)
118
+ # instead, it prepares the javascript to be run on the component object
119
+ ############################################################################
120
+
121
+ def netzke_find(name, c_type = 'gridpanel')
122
+ case c_type
123
+ when 'combobox'
124
+ Marty::RSpec::Components::NetzkeCombobox.new(name)
125
+ else
126
+ Marty::RSpec::Components::NetzkeGrid.new(name, c_type)
127
+ end
128
+ end
129
+
130
+ def run_js js_str, seconds_to_wait = MAX_WAIT_TIME, sleeptime = 0.1
131
+ result = wait_for_element(seconds_to_wait, sleeptime) do
132
+ page.document.synchronize { @res = page.execute_script(js_str) }
133
+ @res
134
+ end
135
+ result
136
+ end
137
+
138
+ ############################################################################
139
+ # component helpers
140
+ ############################################################################
141
+
142
+ def show_submenu text
143
+ run_js <<-JS
144
+ Ext.ComponentQuery.query('menuitem[text="#{text}"] menu')[0].show()
145
+ JS
146
+ end
147
+
148
+ def ext_button_id title, scope = nil, index_of = 0
149
+ c_str = ext_arg('button{isVisible(true)}', text: "\"#{title}\"")
150
+ run_js <<-JS
151
+ return #{ext_find(c_str, scope, index_of)}.id;
152
+ JS
153
+ end
154
+
155
+ def set_field_value value, field_type='textfield', name=''
156
+ args1 = name.empty? ? "" : "[fieldLabel='#{name}']"
157
+ args2 = name.empty? ? "" : "[name='#{name}']"
158
+ run_js <<-JS
159
+ var field = Ext.ComponentQuery.query("#{field_type}#{args1}")[0];
160
+ field = field || Ext.ComponentQuery.query("#{field_type}#{args2}")[0];
161
+ field.setValue("#{value}");
162
+ return true;
163
+ JS
164
+ end
165
+
166
+ def get_total_pages
167
+ # will get deprecated by Netzke 1.0
168
+ result = find(:xpath, ".//div[contains(@id, 'tbtext-')]",
169
+ text: /^of (\d+)$/, match: :first).text
170
+ result.split(' ')[1].to_i
171
+ end
172
+
173
+ ############################################################################
174
+ # helpers
175
+ ############################################################################
176
+
177
+ def id_of component
178
+ res = run_js <<-JS
179
+ var c = #{component};
180
+ return c.view.id;
181
+ JS
182
+ res
183
+ end
184
+
185
+ def btn_disabled? text
186
+ res = wait_for_element do
187
+ find_by_id(ext_button_id(text))
188
+ end
189
+ !res[:class].match(/disabled/).nil?
190
+ end
191
+
192
+ def click_checkbox(name)
193
+ q = %Q(checkbox[fieldLabel="#{name}"])
194
+ item_id = run_js "return Ext.ComponentQuery.query('#{q}')[0].getItemId();"
195
+ find_by_id(item_id).click
196
+ end
197
+
198
+ def paste text, textarea
199
+ # bit hacky: textarea doesn't like receiving tabs and newlines via fill_in
200
+ simple_escape!(text)
201
+
202
+ find(:xpath, ".//textarea[@name='#{textarea}']")
203
+ run_js <<-JS
204
+ #{ext_var(ext_find(ext_arg('textarea', name: textarea)), 'area')}
205
+ area.setValue("#{text}");
206
+ JS
207
+ end
208
+
209
+ def press_key_in(key, el_id)
210
+ kd = key.downcase
211
+ use_key = ['enter', 'return'].include?(kd) ? kd.to_sym : key
212
+ el = find_by_id("#{el_id}")
213
+ el.native.send_keys(use_key)
214
+ end
215
+
216
+ def simple_escape! text
217
+ text.gsub!(/(\r\n|\n)/, "\\n")
218
+ text.gsub!(/\t/, "\\t")
219
+ end
220
+
221
+ def simple_escape text
222
+ text.gsub(/(\r\n|\n)/, "\\n")
223
+ .gsub(/\t/, "\\t")
224
+ .gsub(/"/, '\"')
225
+ end
226
+
227
+ def type_in(type_s, el_id)
228
+ el = find_by_id("#{el_id}")
229
+ el.native.clear()
230
+ type_s.each_char do |key|
231
+ el.native.send_keys(key)
232
+ end
233
+ el.send_keys(:enter)
234
+ end
235
+
236
+ private
237
+ ############################################################################
238
+ # ExtJS/Netzke helper javascripts:
239
+ # Netzke component lookups, arguments for helper methods
240
+ # (i.e. component) require JS scripts instead of objects
241
+ ############################################################################
242
+
243
+ def ext_arg(component, c_args = {})
244
+ res = component
245
+ c_args.each do |k, v|
246
+ res += "[#{k.to_s}=#{v.to_s}]"
247
+ end
248
+ res
249
+ end
250
+
251
+ def ext_find(ext_arg_str, scope = nil, index = 0)
252
+ scope_str = scope.nil? ? '' : ", #{scope}"
253
+ <<-JS
254
+ Ext.ComponentQuery.query('#{ext_arg_str}'#{scope_str})[#{index}]
255
+ JS
256
+ end
257
+
258
+ def ext_var(ext_find_str, var_name='ext_c')
259
+ <<-JS
260
+ var #{var_name} = #{ext_find_str};
261
+ JS
262
+ end
263
+
264
+ def ext_netzkecombo field
265
+ <<-JS
266
+ #{ext_find(ext_arg('netzkeremotecombo', name: field))}
267
+ JS
268
+ end
269
+
270
+ def ext_combo combo_label, c_name='combo'
271
+ <<-JS
272
+ #{ext_var(ext_find(ext_arg('combobox', fieldLabel: combo_label)), c_name)}
273
+ #{c_name} = #{c_name} ||
274
+ #{ext_find(ext_arg('combobox', name: combo_label))};
275
+ JS
276
+ end
277
+
278
+ def ext_celleditor(grid_name='grid')
279
+ <<-JS
280
+ #{grid_name}.getPlugin('celleditor')
281
+ JS
282
+ end
283
+
284
+ def ext_row(row, grid_name='grid')
285
+ <<-JS
286
+ #{grid_name}.getStore().getAt(#{row})
287
+ JS
288
+ end
289
+
290
+ def ext_col(col, grid_name='grid')
291
+ <<-JS
292
+ #{ext_find(ext_arg('gridcolumn', name: "\"#{col}\""), grid_name)}
293
+ JS
294
+ end
295
+
296
+ def ext_cell_val(row, col, grid, var_str = 'value')
297
+ <<-JS
298
+ #{ext_var(grid, 'grid')}
299
+ #{ext_var(ext_col(col, 'grid'), 'col')}
300
+ #{ext_var(ext_row(row, 'grid'), 'row')}
301
+ var #{var_str} = col.assoc ?
302
+ row.get('association_values')['#{col}'] :
303
+ row.get('#{col}');
304
+ JS
305
+ end
306
+ end end end
@@ -0,0 +1,26 @@
1
+ require Pathname.new(__FILE__).parent.to_s + '/post_run_logger'
2
+
3
+ module Marty; module RSpec; module PerformanceHelper
4
+ include Marty::RSpec::PostRunLogger
5
+
6
+ def calculate_baseline iterations
7
+ Benchmark.measure {
8
+ ActiveRecord::Base.uncached {(0...iterations).each { yield }}
9
+ }
10
+ end
11
+
12
+ def compare_baseline baseline, timings, opts={}
13
+ result_time = timings.map{|t| t.total}.sum
14
+ factor = result_time / baseline.total
15
+
16
+ lb = opts.delete(:lower_bound) || 1.5
17
+ ub = opts.delete(:upper_bound) || 5.0
18
+
19
+ post_run_log ' '+'-'*45,
20
+ " baseline: %.2f, result: %.2f, factor: %.2f" %
21
+ [baseline.total, result_time, factor],
22
+ ' '+'-'*45
23
+
24
+ expect(result_time).to be_between(baseline.total * lb, baseline.total * ub)
25
+ end
26
+ end end end
@@ -0,0 +1,32 @@
1
+ module Marty; module RSpec; module PostRunLogger
2
+ class Storage
3
+ def self.data
4
+ @@data ||= []
5
+ end
6
+
7
+ def self.test_number
8
+ @@num ||= 0
9
+ @@num += 1
10
+ end
11
+
12
+ def self.store_data(name, line)
13
+ data << " #{test_number}) #{name}"
14
+ data << Array(line).map { |string| " #{string}" }
15
+ data << ""
16
+ end
17
+
18
+ def self.dump_data
19
+ unless data.empty?
20
+ puts "\n\nPost Run Logging:\n\n"
21
+ puts data
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ def post_run_log(*log_string)
28
+ Storage.store_data example.example_group.parent_groups.map(&:description).
29
+ reverse.join(' ') + ' ' + example.description,
30
+ log_string
31
+ end
32
+ end end end
@@ -1,4 +1,9 @@
1
- module SpecSetup
1
+ module Marty; module RSpec; module Setup
2
+
3
+ def marty_whodunnit
4
+ Mcfly.whodunnit = Marty::User.find_by_login('marty')
5
+ end
6
+
2
7
  def load_scripts(path, dt)
3
8
  Marty::Script.load_scripts(path, dt)
4
9
  Marty::ScriptSet.clear_cache
@@ -13,11 +18,19 @@ module SpecSetup
13
18
  Marty::DataGrid.create_from_import(*args)
14
19
  end
15
20
 
16
- def marty_whodunnit
17
- Mcfly.whodunnit = Marty::User.find_by_login('marty')
18
- end
19
-
20
21
  def do_import_summary(*args)
21
22
  Marty::DataImporter.do_import_summary(*args)
22
23
  end
23
- end
24
+
25
+ def disable_triggers(table_name, &block)
26
+ begin
27
+ ActiveRecord::Base.connection.
28
+ execute("ALTER TABLE #{table_name} DISABLE TRIGGER USER;")
29
+
30
+ block.call
31
+ ensure
32
+ ActiveRecord::Base.connection.
33
+ execute("ALTER TABLE #{table_name} ENABLE TRIGGER USER;")
34
+ end
35
+ end
36
+ end end end