Skip to content

Commit

Permalink
Added tests and change the relation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandervanzuriak committed Dec 12, 2024
1 parent 7abfad7 commit fb2f9d7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
8 changes: 6 additions & 2 deletions app/services/calculators/calculation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ def initialize(calculator, inputs)
def perform
@calculator.formulas.map do |formula|
result = @dentaku.evaluate(formula.expression, @inputs).round(2)

{ label: formula.label, result: result, unit: formula.unit, relation: formula.relation.downcase }
{
label: formula.label,
result: result,
unit: formula.unit,
relation: formula.relation
}
end
end
end
6 changes: 3 additions & 3 deletions app/validators/relation_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ def validate(record)
def relation_is_correct(record)
return if record.formulas.blank?

if record.formulas.first.relation == "Previous"
if record.formulas.first.relation == "previous"
record.formulas.first.errors.add(:relation, "The first formula cannot have a relation of 'Previous' because there is no previous formula.")
end

if record.formulas.last.relation == "Next"
if record.formulas.last.relation == "next"
record.formulas.last.errors.add(:relation, "The last formula cannot have a relation of 'Next' because there is no next formula.")
end

last_relation = nil
record.formulas.each do |formula|
if last_relation == "Next" && formula.relation == "Previous"
if last_relation == "next" && formula.relation == "previous"
formula.errors.add(:relation, "cannot have the same relation as the previous formula.")
end
last_relation = formula.relation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

<%= f.input :uk_unit, label: "Uk Unit Label:" %>
<%= f.input :en_unit, label: "Unit Label:" %>
<%= f.input :relation, as: :select, label: "Relation of formula (optinal):",
collection: ["Next", "Previous"], include_blank: "None"
%>
<%= f.input :relation, as: :select, label: "Relation of formula (optional):",
collection: { "None": nil, "Next": "next", "Previous": "previous"}, default: "None" %>

<%= link_to_remove_association "- Remove Formula", f, class: "text-red-500 underline" %>
</div>
Expand Down
10 changes: 5 additions & 5 deletions spec/helpers/calculators/calculation_service_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:calculator) { instance_double("Calculator", formulas: formulas) }
let(:formulas) do
[
Formula.new(en_label: "Addition", en_unit: "units", uk_label: "Додавання", uk_unit: "одиниці", expression: "x + y"),
Formula.new(en_label: "Addition", en_unit: "units", uk_label: "Додавання", uk_unit: "одиниці", expression: "x + y", relation: "next"),
Formula.new(en_label: "Multiplication", en_unit: "units", uk_label: "Множення", uk_unit: "одиниці", expression: "x * y")
]
end
Expand All @@ -28,8 +28,8 @@

it "returns results with English labels and units" do
expect(subject).to eq([
{ label: "Addition", result: 8, unit: "units" },
{ label: "Multiplication", result: 15, unit: "units" }
{ label: "Addition", result: 8, unit: "units", relation: "next" },
{ label: "Multiplication", result: 15, unit: "units", relation: nil }
])
end
end
Expand All @@ -39,8 +39,8 @@

it "returns results with Ukrainian labels and units" do
expect(subject).to eq([
{ label: "Додавання", result: 8, unit: "одиниці" },
{ label: "Множення", result: 15, unit: "одиниці" }
{ label: "Додавання", result: 8, unit: "одиниці", relation: "next" },
{ label: "Множення", result: 15, unit: "одиниці", relation: nil }
])
end
end
Expand Down
56 changes: 56 additions & 0 deletions spec/validators/relation_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "rails_helper"

RSpec.describe RelationValidator do
let(:calculator) { build(:calculator) }

describe "validations" do
let(:formula_1) { build(:formula, relation: "previous") }
let(:formula_2) { build(:formula, relation: "next") }
let(:formula_3) { build(:formula, relation: nil) }

context "when the first formula has a relation of 'Previous'" do
before do
calculator.formulas = [formula_1]
calculator.valid?
end

it "adds an error to the first formula" do
expect(formula_1.errors[:relation]).to include("The first formula cannot have a relation of 'Previous' because there is no previous formula.")
end
end

context "when the last formula has a relation of 'Next'" do
before do
calculator.formulas = [formula_2]
calculator.valid?
end

it "adds an error to the last formula" do
expect(formula_2.errors[:relation]).to include("The last formula cannot have a relation of 'Next' because there is no next formula.")
end
end

context "when formulas have consecutive relations 'Next' and 'Previous'" do
before do
calculator.formulas = [formula_2, formula_1]
calculator.valid?
end

it "adds an error to the second formula" do
expect(formula_1.errors[:relation]).to include("cannot have the same relation as the previous formula.")
end
end

context "when all formulas have valid relations" do
before do
calculator.formulas = [formula_3, formula_1]
calculator.valid?
end

it "does not add errors to the formulas" do
expect(formula_1.errors[:relation]).to be_empty
expect(formula_3.errors[:relation]).to be_empty
end
end
end
end

0 comments on commit fb2f9d7

Please sign in to comment.