Module: DBUtils
- Includes:
- RSpecConfigurationHelper
- Included in:
- Rspec2db
- Defined in:
- lib/rspec2db/db/schema.rb,
lib/rspec2db/utils/db_utils.rb,
lib/rspec2db/db/build_execution_stats.rb
Class Method Summary collapse
- .build_stats(build_id, file_name, limit, suite, config, reporter_url = 'http://localhost/') ⇒ Object
- .create_rspec_db(dbconfig) ⇒ Object
- .migrate_rspec_db(dbconfig) ⇒ Object
Instance Method Summary collapse
- #connect_to_db(config) ⇒ Object
- #create_test_case(test_run, example_group, example, backtrace = nil, screenshot_event = nil) ⇒ Object
- #create_test_run(test_suite, config, global_file_lock = '/tmp/.rspec2db.yaml') ⇒ Object
- #create_test_suite(config) ⇒ Object
- #update_test_run(test_run, summary, global_file_lock = nil) ⇒ Object
Methods included from RSpecConfigurationHelper
check_rspec_options, #extract_rspec_core_version, generate_local_config, insert_rspec2db_formatter, load_config, load_local_config, #load_snippet_extractor, override_default_config, #print_example_failed_content
Class Method Details
.build_stats(build_id, file_name, limit, suite, config, reporter_url = 'http://localhost/') ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rspec2db/db/build_execution_stats.rb', line 6 def self.build_stats(build_id, file_name, limit, suite, config, reporter_url = 'http://localhost/') ActiveRecord::Base.establish_connection(config) if limit == nil @query = "select tr.* from test_runs tr, test_suites ts where ts.id = tr.test_suites_id and tr.build LIKE '#{build_id}' and ts.suite LIKE '#{suite}' order by tr.created_at desc limit 1" elsif limit == 'all' @query = "select tr.* from test_runs tr, test_suites ts where ts.id = tr.test_suites_id and tr.build LIKE '#{build_id}' and ts.suite LIKE '#{suite}' order by tr.created_at desc" else raise Exception, 'Invalid parameter value.' end report = "Execution stats:\n" report << "----------------\n" @build = "" @success_rate = [] @test_steps_count = 0 @test_steps_pass_count = 0 @test_steps_failed_count = 0 @duration = 0 @test_run_id = 0 @test_runs = TestRun.find_by_sql(@query) @test_runs.each do |test_run| @success_rate << ((test_run.example_count.to_i - test_run.failure_count.to_i).to_f / test_run.example_count.to_f) * 100 @test_steps_count = @test_steps_count + test_run.example_count @test_steps_pass_count = @test_steps_pass_count + (test_run.example_count.to_i - test_run.failure_count.to_i) @test_steps_failed_count = @test_steps_failed_count + test_run.failure_count if test_run.duration>@duration @duration = test_run.duration end @build = test_run.build @test_run_id = test_run.id end @rate = ( @test_steps_pass_count.to_f / @test_steps_count.to_f ) * 100 @formatted_rate = sprintf('%.2f', @rate.to_f) @formatted_duration = sprintf('%.2f', @duration.to_f) if reporter_url == nil File.open(file_name, 'w') { |f| f.write("#{report}Build name: #{@build}\nDuration: #{@formatted_duration}s\nSuccess rate: #{@formatted_rate}%\nTest steps count: #{@test_steps_count}\nTest steps passed: #{@test_steps_pass_count}\nTest steps failed: #{@test_steps_failed_count}\n") } else File.open(file_name, 'w') { |f| f.write("#{report}Build name: #{@build}\nDuration: #{@formatted_duration}s\nSuccess rate: #{@formatted_rate}%\nTest steps count: #{@test_steps_count}\nTest steps passed: #{@test_steps_pass_count}\nTest steps failed: #{@test_steps_failed_count}\nTest reporter page: #{reporter_url}/test-runs/#{@test_run_id}/test-cases\n") } end sql = "select count(tc.test_group) from test_suites ts, test_runs tr, test_cases tc where ts.id=tr.test_suites_id and tr.id=tc.test_runs_id and tr.build='#{build_id}' and ts.suite='#{suite}' group by tc.test_group" number_of_scripts = TestRun.find_by_sql(sql) File.open(file_name, 'a') { |f| f.puts "Number of test scripts: #{number_of_scripts.count}"} sql_failed="select count(tc.test_group) from test_suites ts, test_runs tr, test_cases tc where ts.id=tr.test_suites_id and tr.id=tc.test_runs_id and tr.build='#{build_id}' and ts.suite='#{suite}' and tc.execution_result='failed' group by tc.test_group" number_of_failed_scripts=TestRun.find_by_sql(sql_failed) File.open(file_name, 'a') { |f| f.puts "Number of failed scripts: #{number_of_failed_scripts.count}"} end |
.create_rspec_db(dbconfig) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rspec2db/db/schema.rb', line 6 def self.create_rspec_db(dbconfig) ActiveRecord::Base.establish_connection(dbconfig) ActiveRecord::Schema.define(version: 1) do create_table :test_suites, force: :cascade do |t| t.string :suite t. end create_table :test_runs, force: :cascade do |t| t.float :duration t.integer :example_count t.integer :failure_count t.integer :pending_count t.string :build t.string :computer_name t.string :environment t.string :git_hash t.string :git_branch t. t.references :test_suites end create_table :test_cases, force: :cascade do |t| t.string :test_group t.string :context t.string :description t.string :execution_result t.string :screenshot_path t.string :screenshot_url t.text :exception t.string :pending_message t.float :duration t.text :backtrace t.text :metadata t. t.references :test_runs end end end |
.migrate_rspec_db(dbconfig) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/rspec2db/db/schema.rb', line 47 def self.migrate_rspec_db(dbconfig) ActiveRecord::Base.establish_connection(dbconfig) migrations_path = Bundler.rubygems.find_name('rspec2db').first.full_gem_path + '/lib/rspec2db/db/migrations' migration_context = ActiveRecord::MigrationContext.new(migrations_path) migration_context.migrate end |
Instance Method Details
#connect_to_db(config) ⇒ Object
12 13 14 15 |
# File 'lib/rspec2db/utils/db_utils.rb', line 12 def connect_to_db(config) ActiveRecord::Base.establish_connection(config['dbconnection']) ActiveRecord::Base.default_timezone = :local end |
#create_test_case(test_run, example_group, example, backtrace = nil, screenshot_event = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rspec2db/utils/db_utils.rb', line 49 def create_test_case(test_run, example_group, example, backtrace = nil, screenshot_event = nil) example test_case = TestCase.create( test_runs_id: test_run.id, test_group: example_group.top_level_description, description: example.description, execution_result: example.execution_result.status, duration: example.execution_result.run_time, pending_message: example.execution_result..to_s, exception: example.execution_result.exception.to_s) if backtrace && !example.execution_result.exception.nil? && !example.execution_result.exception.backtrace.nil? File.open('/tmp/output', 'w'){ |w| w.write(example.execution_result.exception.backtrace) } test_case.update_attributes( backtrace: example.execution_result.exception.backtrace.join('\n'), metadata: print_example_failed_content(example) ) end if !example_group.top_level? # check for detecting Context (as opposed to Describe group) test_case.update_attributes( context: example_group.description ) end if screenshot_event[:example] && screenshot_event[:example] == example.description screenshot_event.delete :example test_case.update_attributes screenshot_event screenshot_event = {} end test_case end |
#create_test_run(test_suite, config, global_file_lock = '/tmp/.rspec2db.yaml') ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rspec2db/utils/db_utils.rb', line 26 def create_test_run(test_suite, config, global_file_lock = '/tmp/.rspec2db.yaml') test_run_hash = { build: config['options']['build'], test_suites_id: test_suite.id, git_hash: config['options']['git_commit'], git_branch: config['options']['git_branch'], environment: config['options']['environment'] } global_lock = File.new(global_file_lock, File::CREAT | File::TRUNC) begin global_lock.flock(File::LOCK_EX) test_run = TestRun.where(test_run_hash).first || TestRun.create(test_run_hash) global_lock.flock(File::LOCK_UN) rescue Exception => e puts e. puts e.backtrace ensure global_lock.flock(File::LOCK_UN) end test_run end |
#create_test_suite(config) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/rspec2db/utils/db_utils.rb', line 17 def create_test_suite(config) begin test_suite = TestSuite.find_or_create_by(suite: config['options']['suite']) rescue ActiveRecord::RecordNotUnique => active_record_error puts active_record_error test_suite = TestSuite.find_by(suite: config['options']['suite']) end end |
#update_test_run(test_run, summary, global_file_lock = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rspec2db/utils/db_utils.rb', line 81 def update_test_run(test_run, summary, global_file_lock = nil) global_lock = File.new(global_file_lock, File::CREAT | File::TRUNC) begin global_lock.flock(File::LOCK_EX) test_run.reload test_run.increment(:example_count, summary.example_count) .increment(:failure_count, summary.failure_count) .increment(:pending_count, summary.pending_count) .increment(:duration, summary.duration) .save! global_lock.flock(File::LOCK_UN) rescue Exception => e puts e. puts e.backtrace ensure global_lock.flock(File::LOCK_UN) end end |