Skip to content

Commit

Permalink
[AF-27] Create constants (#28)
Browse files Browse the repository at this point in the history
- Create constants for fixed values of business rules
- Upgrade CHANGELOG and lib version
  • Loading branch information
ricardopacheco authored Mar 6, 2024
1 parent 736b60b commit 9b1aeac
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 69 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## [0.8.1] - 2024-03-06

### Added

- Added configuration constants module to store fixed values referring to business rules.

## [0.8.0] - 2024-03-06

### Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
auction_fun_core (0.8.0)
auction_fun_core (0.8.1)

GEM
remote: https://rubygems.org/
Expand Down
18 changes: 18 additions & 0 deletions lib/auction_fun_core/business/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module AuctionFunCore
module Business
module Configuration
AUCTION_KINDS = Relations::Auctions::KINDS.values
AUCTION_STATUSES = Relations::Auctions::STATUSES.values
AUCTION_MIN_TITLE_LENGTH = 6
AUCTION_MAX_TITLE_LENGTH = 255
AUCTION_STOPWATCH_MIN_VALUE = 15
AUCTION_STOPWATCH_MAX_VALUE = 60
MIN_NAME_LENGTH = 6
MAX_NAME_LENGTH = 128
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 128
end
end
end
6 changes: 2 additions & 4 deletions lib/auction_fun_core/contracts/application_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ module Contracts
# Abstract base class for contracts.
# @abstract
class ApplicationContract < Dry::Validation::Contract
include AuctionFunCore::Business::Configuration

I18N_MACRO_SCOPE = "contracts.errors.custom.macro"
EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+\z/i
MIN_NAME_LENGTH = 6
MAX_NAME_LENGTH = 128
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 128

config.messages.backend = :i18n
config.messages.default_locale = I18n.default_locale
Expand Down
17 changes: 7 additions & 10 deletions lib/auction_fun_core/contracts/auction_context/create_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ module Contracts
module AuctionContext
# Contract class to create new auctions.
class CreateContract < Contracts::ApplicationContract
KINDS = Relations::Auctions::KINDS.values
REQUIRED_FINISHED_AT = KINDS - ["penny"]
MIN_TITLE_LENGTH = 6
MAX_TITLE_LENGTH = 255
STOPWATCH_MIN_VALUE = 15
STOPWATCH_MAX_VALUE = 60
include AuctionFunCore::Business::Configuration

REQUIRED_FINISHED_AT = AUCTION_KINDS - ["penny"]

option :staff_repo, default: proc { Repos::StaffContext::StaffRepository.new }

# @param [Hash] opts Sets an allowed list of parameters, as well as some initial validations.
params do
required(:staff_id).filled(:integer)
required(:title).filled(:string, size?: (MIN_TITLE_LENGTH..MAX_TITLE_LENGTH))
required(:kind).value(included_in?: KINDS)
required(:title).filled(:string, size?: (AUCTION_MIN_TITLE_LENGTH..AUCTION_MAX_TITLE_LENGTH))
required(:kind).value(included_in?: AUCTION_KINDS)
required(:started_at).filled(:time)
optional(:description)
optional(:finished_at).filled(:time)
Expand Down Expand Up @@ -89,11 +86,11 @@ class CreateContract < Contracts::ApplicationContract
key.failure(I18n.t("contracts.errors.filled?")) if !key? && values[:kind] == "penny"

# Must be an integer between 15 and 60.
if key? && values[:kind] == "penny" && !value.between?(STOPWATCH_MIN_VALUE, STOPWATCH_MAX_VALUE)
if key? && values[:kind] == "penny" && !value.between?(AUCTION_STOPWATCH_MIN_VALUE, AUCTION_STOPWATCH_MAX_VALUE)
key.failure(
I18n.t(
"contracts.errors.included_in?.arg.range",
list_left: STOPWATCH_MIN_VALUE, list_right: STOPWATCH_MAX_VALUE
list_left: AUCTION_STOPWATCH_MIN_VALUE, list_right: AUCTION_STOPWATCH_MAX_VALUE
)
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ module Processor
# Contract class for start auctions.
#
class StartContract < Contracts::ApplicationContract
STOPWATCH_MIN_VALUE = 15
STOPWATCH_MAX_VALUE = 60
KINDS = Relations::Auctions::KINDS.values
include AuctionFunCore::Business::Configuration

option :auction_repo, default: proc { Repos::AuctionContext::AuctionRepository.new }

params do
required(:auction_id).filled(:integer)
required(:kind).value(included_in?: KINDS)
required(:kind).value(included_in?: AUCTION_KINDS)
optional(:stopwatch).filled(:integer)
end

Expand All @@ -33,10 +32,10 @@ class StartContract < Contracts::ApplicationContract
key.failure(I18n.t("contracts.errors.filled?")) if !key? && values[:kind] == "penny"

# Must be an integer between 15 and 60.
if key? && values[:kind] == "penny" && !value.between?(STOPWATCH_MIN_VALUE, STOPWATCH_MAX_VALUE)
if key? && values[:kind] == "penny" && !value.between?(AUCTION_STOPWATCH_MIN_VALUE, AUCTION_STOPWATCH_MAX_VALUE)
key.failure(
I18n.t("contracts.errors.included_in?.arg.range",
list_left: STOPWATCH_MIN_VALUE, list_right: STOPWATCH_MAX_VALUE)
list_left: AUCTION_STOPWATCH_MIN_VALUE, list_right: AUCTION_STOPWATCH_MAX_VALUE)
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/auction_fun_core/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module AuctionFunCore
VERSION = "0.8.0"
VERSION = "0.8.1"

# Required class module is a gem dependency
class Version; end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

RSpec.describe AuctionFunCore::Contracts::AuctionContext::CreateContract, type: :contract do
let(:min) { described_class::MIN_TITLE_LENGTH }
let(:max) { described_class::MAX_TITLE_LENGTH }
let(:stopwatch_min_value) { described_class::STOPWATCH_MIN_VALUE }
let(:stopwatch_max_value) { described_class::STOPWATCH_MAX_VALUE }
let(:kinds) { described_class::KINDS.join(", ") }
let(:min) { described_class::AUCTION_MIN_TITLE_LENGTH }
let(:max) { described_class::AUCTION_MAX_TITLE_LENGTH }
let(:stopwatch_min_value) { described_class::AUCTION_STOPWATCH_MIN_VALUE }
let(:stopwatch_max_value) { described_class::AUCTION_STOPWATCH_MAX_VALUE }
let(:kinds) { described_class::AUCTION_KINDS.join(", ") }

describe "#call" do
subject(:contract) { described_class.new.call(attributes) }
Expand Down Expand Up @@ -151,29 +151,8 @@
end
end

describe "stopwatch" do
context "when kind is penny and stopwatch is blank" do
let(:attributes) { {kind: "penny", initial_bid_cents: 1000} }

it "expect failure with error message" do
expect(contract).to be_failure
expect(contract.errors[:stopwatch]).to include(I18n.t("contracts.errors.filled?"))
end
end

context "when kind is penny and stopwatch is not within the allowed range" do
let(:attributes) { {kind: "penny", initial_bid_cents: 1000, stopwatch: 100} }

it "expect failure with error message" do
expect(contract).to be_failure
expect(contract.errors[:stopwatch]).to include(
I18n.t(
"contracts.errors.included_in?.arg.range",
list_left: stopwatch_min_value, list_right: stopwatch_max_value
)
)
end
end
it_behaves_like "validate_stopwatch_contract" do
let(:auction) { Factory.structs[:auction, kind: :penny, initial_bid_cents: 1000] }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe AuctionFunCore::Contracts::AuctionContext::Processor::StartContract, type: :contract do
let(:auction) { Factory[:auction, :default_standard] }
let(:kinds) { described_class::KINDS.join(", ") }
let(:kinds) { described_class::AUCTION_KINDS.join(", ") }

describe "#call" do
subject(:contract) { described_class.new.call(attributes) }
Expand Down Expand Up @@ -42,7 +42,13 @@
end

context "when attributes are valid" do
let(:attributes) { {auction_id: auction.id, kind: auction.kind, stopwatch: 15} }
let(:attributes) do
{
auction_id: auction.id,
kind: auction.kind,
stopwatch: described_class::AUCTION_STOPWATCH_MIN_VALUE
}
end

it "expect return success" do
expect(contract).to be_success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
subject(:contract) { described_class.new.call(attributes) }

context "when params are blank" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect failure with error messages" do
expect(contract).to be_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
subject(:operation) { described_class.new.call(attributes) }

context "when contract are not valid" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect not persist new bid on database" do
expect(bid_repository.count).to be_zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
subject(:operation) { described_class.new.call(attributes) }

context "when contract are not valid" do
let(:attributes) { {} }
let(:attributes) { Dry::Core::Constants::EMPTY_HASH }

it "expect return failure with error messages" do
expect(operation).to be_failure
Expand Down
2 changes: 1 addition & 1 deletion spec/auction_fun_core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

RSpec.describe AuctionFunCore do
it "has a version number" do
expect(AuctionFunCore::VERSION).to eq("0.8.0")
expect(AuctionFunCore::VERSION).to eq("0.8.1")
end
end
2 changes: 1 addition & 1 deletion spec/support/factories/auctions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
f.trait :default_penny do |t|
t.kind { "penny" }

t.stopwatch { 15 }
t.stopwatch { AuctionFunCore::Business::Configuration::AUCTION_STOPWATCH_MIN_VALUE }
t.started_at { 1.hour.from_now }
end

Expand Down
4 changes: 2 additions & 2 deletions spec/support/shared_examples/validate_name_contract.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

shared_examples "validate_name_contract" do |factory_name|
let(:min) { AuctionFunCore::Contracts::ApplicationContract::MIN_NAME_LENGTH }
let(:max) { AuctionFunCore::Contracts::ApplicationContract::MAX_NAME_LENGTH }
let(:min) { AuctionFunCore::Business::Configuration::MIN_NAME_LENGTH }
let(:max) { AuctionFunCore::Business::Configuration::MAX_NAME_LENGTH }
let(:factory) { Factory[factory_name] }

context "when the characters in the name are outside the allowed range" do
Expand Down
4 changes: 2 additions & 2 deletions spec/support/shared_examples/validate_password_contract.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

shared_examples "validate_password_contract" do |factory_name|
let(:min) { AuctionFunCore::Contracts::ApplicationContract::MIN_PASSWORD_LENGTH }
let(:max) { AuctionFunCore::Contracts::ApplicationContract::MAX_PASSWORD_LENGTH }
let(:min) { AuctionFunCore::Business::Configuration::MIN_PASSWORD_LENGTH }
let(:max) { AuctionFunCore::Business::Configuration::MAX_PASSWORD_LENGTH }
let(:factory) { Factory[factory_name] }

context "when password characters are outside the allowed range" do
Expand Down
13 changes: 11 additions & 2 deletions spec/support/shared_examples/validate_stopwatch_contract.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

shared_examples "validate_stopwatch_contract" do
let(:min) { AuctionFunCore::Business::Configuration::AUCTION_STOPWATCH_MIN_VALUE }
let(:max) { AuctionFunCore::Business::Configuration::AUCTION_STOPWATCH_MAX_VALUE }

context "when kind is penny and stopwatch is blank" do
let(:attributes) { {auction_id: auction.id, kind: auction.kind} }

Expand All @@ -11,12 +14,18 @@
end

context "when kind is penny and stopwatch is not within the allowed range" do
let(:attributes) { {auction_id: auction.id, kind: auction.kind, stopwatch: 100} }
let(:attributes) do
{
auction_id: auction.id,
kind: auction.kind,
stopwatch: max + 1
}
end

it "expect failure with error message" do
expect(contract).to be_failure
expect(contract.errors[:stopwatch]).to include(
I18n.t("contracts.errors.included_in?.arg.range", list_left: 15, list_right: 60)
I18n.t("contracts.errors.included_in?.arg.range", list_left: min, list_right: max)
)
end
end
Expand Down

0 comments on commit 9b1aeac

Please sign in to comment.