From 410bed2ab2ab91b775028c7c4e540482871a9a31 Mon Sep 17 00:00:00 2001 From: claudiawulee <68723604+claudiawulee@users.noreply.github.com> Date: Tue, 30 Apr 2024 15:49:20 -0400 Subject: [PATCH] Switch to use gigabytes instead of gigibytes (#1798) * default base is now 1000 * added test to check for file size with 1000 base * Added a few tests to make sure the base parameter is honored Co-authored-by: Claudia Lee * Adjusted test to use KB instead of KiB --------- Co-authored-by: Hector Correa Co-authored-by: Claudia Lee --- .../number_to_human_size_converter.rb | 17 +++++++++++++++++ spec/models/s3_file_spec.rb | 14 ++++++++++++++ spec/system/work_upload_s3_objects_spec.rb | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 config/initializers/number_to_human_size_converter.rb diff --git a/config/initializers/number_to_human_size_converter.rb b/config/initializers/number_to_human_size_converter.rb new file mode 100644 index 000000000..e3041e580 --- /dev/null +++ b/config/initializers/number_to_human_size_converter.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true +# Source: https://github.com/rails/rails/issues/40054#issuecomment-674449143 +# Additional information: https://massive.io/file-transfer/gb-vs-gib-whats-the-difference/ +module ActiveSupport + module NumberHelper + class NumberToHumanSizeConverter < NumberConverter + private + + # Allows a base to be specified for the conversion + # 1024 was the default and that produces gigibytes + # 1000 produces gigabytes + def base + options[:base] || 1000 + end + end + end +end diff --git a/spec/models/s3_file_spec.rb b/spec/models/s3_file_spec.rb index f70950cc9..cb46bdfa2 100644 --- a/spec/models/s3_file_spec.rb +++ b/spec/models/s3_file_spec.rb @@ -33,6 +33,20 @@ end end + context "display file size" do + it "uses 1000 base by default when calculating display value" do + expect(s3_file.display_size).to eq "10.8 KB" + end + end + + describe "#number_to_human_size" do + it "honors the base if we pass it one" do + expect(s3_file.number_to_human_size(size)).to eq "10.8 KB" + expect(s3_file.number_to_human_size(size, base: 1000)).to eq "10.8 KB" + expect(s3_file.number_to_human_size(size, base: 1024)).to eq "10.5 KB" + end + end + context "safe_id" do it "calculates correct safe_id for files with spaces and non-alpha numeric characters" do expect(s3_file.safe_id).to eq "10-99999-123-abc-#{work.id}-filename--with-spaces--w-----chars-txt" diff --git a/spec/system/work_upload_s3_objects_spec.rb b/spec/system/work_upload_s3_objects_spec.rb index d48630958..04a5b5995 100644 --- a/spec/system/work_upload_s3_objects_spec.rb +++ b/spec/system/work_upload_s3_objects_spec.rb @@ -52,7 +52,7 @@ expect(page).to have_content upload_file_name expect(page).to have_content filename1 expect(page).to have_content filename2 - expect(page).to have_content "Total Size\n31.5 KB" + expect(page).to have_content "Total Size\n32.3 KB" expect(work.reload.pre_curation_uploads.length).to eq(3) end