Skip to content

Commit

Permalink
added test for run, cleaned up outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarin committed Oct 12, 2023
1 parent 8179fcb commit 28023ed
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 46 deletions.
62 changes: 33 additions & 29 deletions lib/reports/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@

module Reports
class Dynamic
attr_reader :base, :decorations, :restrictions, :verbose
def initialize(base: "", decorations: [], restrictions: [], verbosity: false)
log "new dynamic report!"
attr_reader :base, :decorations, :restrictions
def initialize(base: "", decorations: [], restrictions: [], log_toggle: 0)
@base = validate_clusterable(base)
@decorations = add_decorations(decorations)
@restrictions = add_restrictions(restrictions)
@verbose = validate_verbosity(verbosity)
@log_toggle = log_toggle
log [
"new dynamic report!",
"base: #{base}",
"decorations: #{decorations.inspect}",
"restrictions: #{restrictions.inspect}",
"output_file: #{output_file}"
].join("\n")
end

def self.model
Expand Down Expand Up @@ -98,14 +104,16 @@ def add_restrictions(restrictions)

# Puts it all together.
def run
outf header
records do |rec|
outf rec.to_s
File.open(output_file, "w") do |outf|
outf.puts header
records do |rec|
outf.puts rec.join("\t")
end
end
end

def header
@decorations.join("\t")
@decorations.map(&:key).join("\t")
end

# Runs the query and yields the records
Expand Down Expand Up @@ -135,23 +143,24 @@ def records
end
end

# Because we may have been started by Sidekiq, which cannot pass Ruby's true/false.
def validate_verbosity(str_or_bool)
if str_or_bool.is_a? TrueClass
true
elsif str_or_bool.is_a? FalseClass
false
elsif str_or_bool == "true"
true
elsif str_or_bool == "false"
false
else
raise "heck 1"
end
def output_dir
FileUtils.mkdir_p(
File.join(
Settings.dynamic_reports || "/tmp",
"dynamic",
base
)
).first # returns an array, so
end

def verbose?
@verbose
def output_file
if @output_file.nil?
ymd = Time.now.strftime("%Y-%m-%d")
rand_str = SecureRandom.hex
id = [ymd, rand_str].join("-")
@output_file = File.join(output_dir, id) + ".tsv"
end
@output_file
end

private
Expand Down Expand Up @@ -192,12 +201,7 @@ def get_field(str)

# Route messages to the log
def log(msg)
puts "## " + msg if verbose?
end

# Route messages to an output location.
def outf(msg)
puts "outf >> " + msg
puts("## " + msg) if @log_toggle == 1
end
end

Expand Down
54 changes: 37 additions & 17 deletions spec/reports/dynamic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
let(:base_com) { "commitments" }
let(:base_hti) { "ht_items" }
let(:base_ocn) { "ocn_resolutions" }
let(:log_t) { 0 } # set to 1 for all the logs

let(:dec1) { ["holdings.ocn"] }
let(:dec2) { ["holdings.ocn", "holdings.local_id"] }
let(:res1) { [{"holdings.organization" => "umich"}] }
let(:res2) { [{"holdings.organization" => "umich"}, {"holdings.status" => "CH"}] }
let(:chattiness) { true }

let(:min_params) { {base: base_hol, decorations: dec1} }
let(:basic_params) { {base: base_hol, decorations: dec1, restrictions: res1} }
let(:min_params) { {base: base_hol, decorations: dec1, log_toggle: log_t} }
let(:basic_params) { {base: base_hol, decorations: dec1, restrictions: res1, log_toggle: log_t} }
let(:dynamic_ok) { described_class.new(**min_params) }

before(:each) do
Expand Down Expand Up @@ -71,6 +71,16 @@
end
end

describe "#header" do
it "is based on @decorations" do
params = min_params
expect(described_class.new(**params).header).to eq "ocn"

params[:decorations] << "holdings.local_id"
expect(described_class.new(**params).header).to eq "ocn\tlocal_id"
end
end

describe "#records" do
it "returns matching records" do
1.upto(3).each do |i|
Expand Down Expand Up @@ -147,7 +157,6 @@
restrictions = [{base_field => field_val}]
report = described_class.new(
base: params[:base],
verbosity: chattiness,
decorations: params[:decorations],
restrictions: restrictions
)
Expand All @@ -158,7 +167,6 @@
# that we know is going to result in zero records, does it really?
report = described_class.new(
base: params[:base],
verbosity: chattiness,
decorations: params[:decorations],
restrictions: restrictions << test_case[:fail]
)
Expand All @@ -167,19 +175,31 @@
end
end
end
describe "#validate_verbosity" do
it "sets verbosity correctly" do
test_expectations = {
true => true,
false => false,
"true" => true,
"false" => false
}
test_expectations.each do |param, result|
expect(dynamic_ok.validate_verbosity(param)).to be result
report = described_class.new(**min_params.merge({verbosity: param}))
expect(report.verbose?).to be result

describe "outputs" do
it "auto-generates its own output dir" do
dir = dynamic_ok.output_dir
expect(Dir.exist?(dir)).to be true
end
it "auto-generates its own file when running" do
# So, does not exist at first, but the path is known
file = dynamic_ok.output_file
expect(File.exist?(file)).to be false
cluster_tap_save(build(:holding))
dynamic_ok.run
# We ran, path is the same, file now exists.
expect(File.exist?(file)).to be true
end
end

describe "#run" do
it "puts it all together" do
buf = []
1.upto(10) do |_i|
buf << build(:holding)
end
cluster_tap_save(*buf)
dynamic_ok.run
end
end
end

0 comments on commit 28023ed

Please sign in to comment.