Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unit_test.rb « ci « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81623b4f6ad90fa5a184a4e46812c506198faedc (plain)
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Ci
  class UnitTest < ApplicationRecord
    extend Gitlab::Ci::Model

    MAX_NAME_SIZE = 255
    MAX_SUITE_NAME_SIZE = 255

    validates :project, :key_hash, :name, :suite_name, presence: true

    has_many :unit_test_failures, class_name: 'Ci::UnitTestFailure'

    belongs_to :project

    scope :by_project_and_keys, -> (project, keys) { where(project_id: project.id, key_hash: keys) }

    class << self
      def find_or_create_by_batch(project, unit_test_attrs)
        # Insert records first. Existing ones will be skipped.
        insert_all(build_insert_attrs(project, unit_test_attrs))

        # Find all matching records now that we are sure they all are persisted.
        by_project_and_keys(project, gather_keys(unit_test_attrs))
      end

      private

      def build_insert_attrs(project, unit_test_attrs)
        # NOTE: Rails 6.1 will add support for insert_all on relation so that
        # we will be able to do project.test_cases.insert_all.
        unit_test_attrs.map do |attrs|
          attrs.merge(
            project_id: project.id,
            name: attrs[:name].truncate(MAX_NAME_SIZE),
            suite_name: attrs[:suite_name].truncate(MAX_SUITE_NAME_SIZE)
          )
        end
      end

      def gather_keys(unit_test_attrs)
        unit_test_attrs.map { |attrs| attrs[:key_hash] }
      end
    end
  end
end