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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

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

39.53
/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✔
UNCOV
9
    existing_files.each do |file|
×
UNCOV
10
      filename = file["filename"]
×
UNCOV
11
      unless s3_filenames.include?(filename)
×
UNCOV
12
        work_changes << { action: "removed", filename:, checksum: file["checksum"] }
×
13
      end
14
    end
15
  end
16

17
  def snapshot_modifications(work_changes, s3_files)
1✔
18
    # check for modifications
UNCOV
19
    s3_files.each do |s3_file|
×
UNCOV
20
      next if match?(s3_file)
×
UNCOV
21
      work_changes << if include?(s3_file)
×
UNCOV
22
                        { action: "replaced", filename: s3_file.filename, checksum: s3_file.checksum }
×
23
                      else
UNCOV
24
                        { action: "added", filename: s3_file.filename, checksum: s3_file.checksum }
×
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✔
UNCOV
38
    files.map { |file| file["filename"] }
×
39
  end
40

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

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

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

53
  def store_files(s3_files)
1✔
UNCOV
54
    self.files = s3_files.map { |file| { "filename" => file.filename, "checksum" => file.checksum } }
×
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✔
UNCOV
73
      if checksum1 == checksum2
×
UNCOV
74
        true
×
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