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

pulibrary / pdc_describe / b1776731-62d7-41a7-882d-6a5b4760db75

14 May 2024 12:28PM UTC coverage: 79.223% (-16.7%) from 95.9%
b1776731-62d7-41a7-882d-6a5b4760db75

push

circleci

carolyncole
Adding a submission completion page

fixes #1791

9 of 9 new or added lines in 2 files covered. (100.0%)

567 existing lines in 42 files now uncovered.

2692 of 3398 relevant lines covered (79.22%)

60.03 hits per line

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

74.42
/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|
4✔
10
      filename = file["filename"]
2✔
11
      unless s3_filenames.include?(filename)
2✔
12
        work_changes << { action: "removed", filename:, checksum: file["checksum"] }
1✔
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|
4✔
20
      next if match?(s3_file)
6✔
21
      work_changes << if include?(s3_file)
5✔
UNCOV
22
                        { action: "replaced", filename: s3_file.filename, checksum: s3_file.checksum }
×
23
                      else
24
                        { action: "added", filename: s3_file.filename, checksum: s3_file.checksum }
5✔
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✔
UNCOV
34
    URI.parse(url)
×
35
  end
36

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

41
  def include?(s3_file)
1✔
42
    filenames.include?(s3_file.filename)
5✔
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) }
10✔
47
  end
48

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

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

57
  def self.find_by_filename(work_id:, filename:)
1✔
UNCOV
58
    find_by("work_id = ? AND files @> ?", work_id, JSON.dump([{ filename: }]))
×
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
1✔
74
        true
1✔
UNCOV
75
      elsif checksum1.nil? || checksum2.nil?
×
UNCOV
76
        false
×
UNCOV
77
      elsif checksum1.length < checksum2.length
×
78
        # Decode the first one and then compare
UNCOV
79
        checksum1.unpack("m0").first.unpack("H*").first == checksum2
×
80
      else
81
        # Decode the second one and then compare
UNCOV
82
        checksum1 == checksum2.unpack("m0").first.unpack("H*").first
×
83
      end
84
    rescue ArgumentError
85
      # One of the values was not properly encoded
UNCOV
86
      false
×
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