use fewer sigfigs for ounces; add spec to option_value_namer

This commit is contained in:
Andy Brett
2020-08-12 10:38:38 -07:00
parent 2fe9f4abc8
commit 55e448897f
5 changed files with 18 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ angular.module("admin.products").factory "VariantUnitManager", ->
453.6:
name: 'lb'
system: 'imperial'
28.34952:
28.35:
name: 'oz'
system: 'imperial'
'volume':

View File

@@ -32,7 +32,7 @@ module ProductImport
{
'g' => { scale: 1, unit: 'weight' },
'kg' => { scale: 1000, unit: 'weight' },
'oz' => { scale: 28.34952, unit: 'weight' },
'oz' => { scale: 28.35, unit: 'weight' },
'lb' => { scale: 453.6, unit: 'weight' },
't' => { scale: 1_000_000, unit: 'weight' },
'ml' => { scale: 0.001, unit: 'volume' },

View File

@@ -39,7 +39,6 @@ module OpenFoodNetwork
if @variant.unit_value.present?
if %w(weight volume).include? @variant.product.variant_unit
value, unit_name = option_value_value_unit_scaled
else
value = @variant.unit_value
unit_name = pluralize(@variant.product.variant_unit_name, value)
@@ -66,7 +65,7 @@ module OpenFoodNetwork
units = {
'weight' => {
1.0 => { 'name' => 'g', 'system' => 'metric' },
28.34952 => { 'name' => 'oz', 'system' => 'imperial' },
28.35 => { 'name' => 'oz', 'system' => 'imperial' },
453.6 => { 'name' => 'lb', 'system' => 'imperial' },
1000.0 => { 'name' => 'kg', 'system' => 'metric' },
1_000_000.0 => { 'name' => 'T', 'system' => 'metric' }

View File

@@ -29,7 +29,7 @@ describe "VariantUnitManager", ->
describe "unitScales", ->
it "returns a sorted set of scales for unit type weight", ->
expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.34952, 453.6, 1000.0, 1000000.0]
expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.35, 453.6, 1000.0, 1000000.0]
it "returns a sorted set of scales for unit type volume", ->
expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 1.0, 1000.0]
@@ -37,13 +37,13 @@ describe "VariantUnitManager", ->
describe "compatibleUnitScales", ->
it "returns a sorted set of compatible scales based on the scale and unit type provided", ->
expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0]
expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.34952, 453.6]
expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.35, 453.6]
describe "variantUnitOptions", ->
it "returns an array of options", ->
expect(VariantUnitManager.variantUnitOptions()).toEqual [
["Weight (g)", "weight_1"],
["Weight (oz)", "weight_28.34952"],
["Weight (oz)", "weight_28.35"],
["Weight (lb)", "weight_453.6"],
["Weight (kg)", "weight_1000"],
["Weight (T)", "weight_1000000"],

View File

@@ -83,12 +83,21 @@ module OpenFoodNetwork
expect(subject.send(:option_value_value_unit)).to eq [1, 'kg']
end
it "returns only values that are in the same measurement systems" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
allow(v).to receive(:product) { p }
allow(v).to receive(:unit_value) { 500 }
# 500g would convert to > 1 pound, but we don't want the namer to use
# pounds since it's in a different measurement system.
expect(subject.send(:option_value_value_unit)).to eq [500, 'g']
end
it "generates values for all weight scales" do
[[1.0, 'g'], [1000.0, 'kg'], [1_000_000.0, 'T']].each do |scale, unit|
[[1.0, 'g'], [28.35, 'oz'], [453.6, 'lb'], [1000.0, 'kg'], [1_000_000.0, 'T']].each do |scale, unit|
p = double(:product, variant_unit: 'weight', variant_unit_scale: scale)
allow(v).to receive(:product) { p }
allow(v).to receive(:unit_value) { 100 * scale }
expect(subject.send(:option_value_value_unit)).to eq [100, unit]
allow(v).to receive(:unit_value) { 10.0 * scale }
expect(subject.send(:option_value_value_unit)).to eq [10, unit]
end
end