Class: GitlabQuality::TestTooling::CodeCoverage::TestFileMappingData

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_quality/test_tooling/code_coverage/test_file_mapping_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(test_to_sources, tests_to_categories: {}) ⇒ TestFileMappingData

Returns a new instance of TestFileMappingData.

Parameters:

  • test_to_sources (Hash<String, Array<String>>)

    Test files mapped to all source files they cover

  • tests_to_categories (Hash<String, Array<String>>) (defaults to: {})

    Test files mapped to their feature categories



11
12
13
14
# File 'lib/gitlab_quality/test_tooling/code_coverage/test_file_mapping_data.rb', line 11

def initialize(test_to_sources, tests_to_categories: {})
  @test_to_sources = test_to_sources
  @tests_to_categories = tests_to_categories
end

Instance Method Details

#as_db_tableArray<Hash<Symbol, String>>

Rows for shared.test_file_mappings, one per test-and-source pair.

Carries no group, stage or section on purpose. Those describe the category rather than the mapping and change whenever stages.yml is reorganised, so storing them on every mapping multiplies rows for no gain.

Readers resolve ownership from shared.category_owners through category. That table keeps every historical row, up to 57 for one category, so a plain join fans out; dedupe it with ORDER BY category, timestamp DESC LIMIT 1 BY category.

Examples:

Return value

[
  { test_file: "spec/models/user_spec.rb", source_file: "app/models/user.rb",
    category: "team_planning" },
  ...
]

Returns:

  • (Array<Hash<Symbol, String>>)

    Mapping data formatted for database insertion



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab_quality/test_tooling/code_coverage/test_file_mapping_data.rb', line 33

def as_db_table
  @test_to_sources.flat_map do |test_file, source_files|
    category = @tests_to_categories[test_file]&.first || ''

    source_files.map do |source_file|
      {
        test_file: test_file,
        source_file: source_file,
        category: category
      }
    end
  end
end