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

pulibrary / allsearch_api / 8cca27ea-0ec0-4546-b1e5-0dc9f1ef05a3

31 Oct 2025 06:27PM UTC coverage: 99.903% (+0.003%) from 99.9%
8cca27ea-0ec0-4546-b1e5-0dc9f1ef05a3

Pull #389

circleci

sandbergja
Replace uses of the Banner ActiveRecord model with Rom-RB

The ActiveRecord model is still used in tests and a database migration,
maybe those could be migrated to Rom-RB in a separate PR?

A Rom Container is created that uses the same database connections as
ActiveRecord.  This allows rom to use the same database transactions
as activerecord, allowing tests (which use db transactions to stay
isolated from one another) to still pass.
Pull Request #389: Replace uses of the Banner ActiveRecord model with Rom-RB

34 of 39 new or added lines in 4 files covered. (87.18%)

57 existing lines in 13 files now uncovered.

1031 of 1032 relevant lines covered (99.9%)

112.35 hits per line

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

100.0
/app/services/csv_loading_service.rb
1
# frozen_string_literal: true
2

3
require 'csv'
3✔
4
require 'open-uri'
3✔
5

6
# A general class that can be subclassed
7
# to load data from a remote CSV file
8
# into the database
9
class CSVLoadingService
3✔
10
  def run
3✔
11
    fetch_data
85✔
12
    process_data if data_is_valid?
85✔
13
  end
14

15
  private
3✔
16

17
  attr_reader :csv
3✔
18

19
  def fetch_data
3✔
20
    contents = uri.open
85✔
21
    @csv = CSV.new(contents)
85✔
22
  end
23

24
  def process_data
3✔
25
    class_to_load.destroy_all
78✔
26
    csv.each { |row| class_to_load.new_from_csv(row) }
738✔
27
  end
28

29
  # Data is valid if the CSV has not shrunk significantly, and the header row matches the expected headers
30
  def data_is_valid?
3✔
31
    return false if csv_is_much_smaller?
85✔
32

33
    header_row_matches?
82✔
34
  end
35

36
  # If the CSV shrinks by 25% or more, assume something is wrong
37
  def csv_is_much_smaller?
3✔
38
    much_smaller = csv_shrinkage > (existing_records * 0.25)
85✔
39
    return false unless much_smaller
85✔
40

UNCOV
41
    Rails.logger.error("The #{self.class} had a much shorter CSV. " \
3✔
42
                       "The original length was #{existing_records} rows, " \
43
                       "the new length is #{new_csv_length} rows.")
UNCOV
44
    much_smaller
3✔
45
  end
46

47
  def csv_shrinkage
3✔
48
    existing_records - new_csv_length
85✔
49
  end
50

51
  def existing_records
3✔
52
    @existing_records ||= class_to_load.count
173✔
53
  end
54

55
  def new_csv_length
3✔
56
    @new_csv_length ||= begin
88✔
57
      length = csv.readlines.size
85✔
58
      csv.rewind
85✔
59
      length
85✔
60
    end
61
  end
62

63
  # Expect certain headers
64
  def header_row_matches?
3✔
65
    new_headers = csv.readline
82✔
66
    return true if (new_headers & expected_headers) == expected_headers
82✔
67

UNCOV
68
    Rails.logger.error("The #{self.class} did not load the CSV " \
4✔
69
                       "because the headers didn't match. The expected headers are: " \
70
                       "#{expected_headers.to_sentence}. " \
71
                       "The new CSV headers are #{new_headers&.to_sentence}.")
UNCOV
72
    false
4✔
73
  end
74

75
  def uri; end
3✔
76

77
  def class_to_load; end
3✔
78
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