Class: ProjectTestsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/project_tests_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #api_authenticate!, #current_project, #expire_revision!, #followed_projects, #no_cache, #read_revision, #require_login, #return_or_cache_revision!, #revision, #save_current_project, #set_current_project, #unfurling?

Methods included from UrlHelper

#feature_path, #github_commit_range_url, #github_commit_url, #github_project_url, #github_url?, #goldmine_case_number_url, #link_to_project_feature, #new_release_url, #releases_path

Instance Method Details

#indexObject



3
4
5
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
# File 'app/controllers/project_tests_controller.rb', line 3

def index
  @project = Project.find_by_slug! params[:slug]

  head = params.fetch :at, @project.head_sha
  commits = params.fetch(:limit, 500).to_i

  @commits = Houston.benchmark("[project_tests#index] fetch commits") {
    @project.repo.ancestors(head, including_self: true, limit: commits) }
  @runs = @project.test_runs.where(sha: @commits.map(&:sha))

  @tests = @project.tests.order(:suite, :name)
    .joins(<<-SQL)
      LEFT JOIN LATERAL (
        SELECT COUNT(*) FROM test_results
        WHERE test_results.test_run_id IN (#{@runs.pluck(:id).join(",")})
        AND test_results.status='pass'
        AND test_results.test_id=tests.id
      ) "passes" ON TRUE
      LEFT JOIN LATERAL (
        SELECT COUNT(*) FROM test_results
        WHERE test_results.test_run_id IN (#{@runs.pluck(:id).join(",")})
        AND test_results.status='fail'
        AND test_results.test_id=tests.id
      ) "fails" ON TRUE
    SQL
    .where("passes.count + fails.count > 0")
    .pluck("tests.id", "tests.suite", "tests.name", "passes.count", "fails.count")
end

#showObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/project_tests_controller.rb', line 32

def show
  @project = Project.find_by_slug! params[:slug]
  @test = @project.tests.find params[:id]
  @totals = Hash[@test.test_results.group(:status).pluck(:status, "COUNT(*)")]

  begin
    head = params.fetch :at, @project.head_sha
    stop_shas = @test.introduced_in_shas
    @commits = Houston.benchmark("[project_tests#show] fetch commits") {
      @project.repo.ancestors(head, including_self: true, limit: 100, hide: stop_shas) }

    if @commits.any?
      @runs = @project.test_runs.where(sha: @commits.map(&:sha))

      @commits.each do |commit|
        def commit.date
          @date ||= committed_at.to_date
        end
        def commit.time
          committed_at
        end
      end

      @results = @test.test_results.where(test_run_id: @runs.map(&:id))
        .joins(:test_run)
        .select("test_runs.sha", "test_results.*")
        .index_by { |result| result[:sha] }
      @runs = @runs.index_by(&:sha)
    end
  rescue Houston::Adapters::VersionControl::CommitNotFound
    @commits = []
    @exception = $!
  end

  if request.xhr?
    if @commits.any?
      render partial: "project_tests/commits"
    else
      head 204
    end
  end
end