File: unit_test.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (45 lines) | stat: -rw-r--r-- 1,525 bytes parent folder | download
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
# frozen_string_literal: true

module Ci
  class UnitTest < Ci::ApplicationRecord
    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) }
    scope :deletable, -> { where('NOT EXISTS (?)', Ci::UnitTestFailure.select(1).where("#{Ci::UnitTestFailure.table_name}.unit_test_id = #{table_name}.id")) }

    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