• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / e7d05f65-9563-4150-9745-e81c8d801b90

10 Jul 2024 08:25PM UTC coverage: 95.769% (-0.1%) from 95.872%
e7d05f65-9563-4150-9745-e81c8d801b90

Pull #1848

circleci

jrgriffiniii
Ensuring that at least one file upload is provided for draft Works.
Pull Request #1848: Ensuring that at least one file upload is provided for draft Works

2 of 2 new or added lines in 1 file covered. (100.0%)

4 existing lines in 3 files now uncovered.

3282 of 3427 relevant lines covered (95.77%)

217.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.35
/app/models/upload_snapshot.rb
1
# frozen_string_literal: true
2
class UploadSnapshot < ApplicationRecord
1✔
3
  belongs_to :work
1✔
4
  attr_writer :upload
1✔
5

6
  alias_attribute :existing_files, :files
1✔
7

8
  def snapshot_deletions(work_changes, s3_filenames)
1✔
9
    existing_files.each do |file|
38✔
10
      filename = file["filename"]
35✔
11
      unless s3_filenames.include?(filename)
35✔
12
        work_changes << { action: "removed", filename:, checksum: file["checksum"] }
22✔
13
      end
14
    end
15
  end
16

17
  def snapshot_modifications(work_changes, s3_files)
1✔
18
    # check for modifications
19
    s3_files.each do |s3_file|
38✔
20
      next if match?(s3_file)
55✔
21
      work_changes << if include?(s3_file)
44✔
22
                        { action: "replaced", filename: s3_file.filename, checksum: s3_file.checksum }
2✔
23
                      else
24
                        { action: "added", filename: s3_file.filename, checksum: s3_file.checksum }
42✔
25
                      end
26
    end
27
  end
28

29
  def upload
1✔
UNCOV
30
    @upload ||= uploads.find { |s3_file| filenames.include?(s3_file.filename) }
×
31
  end
32

33
  def uri
1✔
34
    URI.parse(url)
1✔
35
  end
36

37
  def filenames
1✔
38
    files.map { |file| file["filename"] }
90✔
39
  end
40

41
  def include?(s3_file)
1✔
42
    filenames.include?(s3_file.filename)
46✔
43
  end
44

45
  def index(s3_file)
1✔
46
    files.index { |file| file["filename"] == s3_file.filename && checksum_compare(file["checksum"], s3_file.checksum) }
149✔
47
  end
48

49
  def match?(s3_file)
1✔
50
    index(s3_file).present?
63✔
51
  end
52

53
  def store_files(s3_files)
1✔
54
    self.files = s3_files.map { |file| { "filename" => file.filename, "checksum" => file.checksum } }
90✔
55
  end
56

57
  def self.find_by_filename(work_id:, filename:)
1✔
58
    find_by("work_id = ? AND files @> ?", work_id, JSON.dump([{ filename: }]))
3✔
59
  end
60

61
  private
1✔
62

63
    def uploads
1✔
UNCOV
64
      work.uploads
×
65
    end
66

67
    # Compares two checksums. Accounts for the case in which one of them is
68
    # a plain MD5 value and the other has been encoded with base64.
69
    # See also
70
    #   https://ruby-doc.org/core-2.7.0/Array.html#method-i-pack
71
    #   https://ruby-doc.org/core-2.7.0/String.html#method-i-unpack
72
    def checksum_compare(checksum1, checksum2)
1✔
73
      if checksum1 == checksum2
22✔
74
        true
14✔
75
      elsif checksum1.nil? || checksum2.nil?
8✔
76
        false
1✔
77
      elsif checksum1.length < checksum2.length
7✔
78
        # Decode the first one and then compare
79
        checksum1.unpack("m0").first.unpack("H*").first == checksum2
5✔
80
      else
81
        # Decode the second one and then compare
82
        checksum1 == checksum2.unpack("m0").first.unpack("H*").first
2✔
83
      end
84
    rescue ArgumentError
85
      # One of the values was not properly encoded
86
      false
1✔
87
    end
88
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc