Skip to content

Commit

Permalink
Adding Faker::Commerce.rating
Browse files Browse the repository at this point in the history
  • Loading branch information
bradly committed Aug 11, 2023
1 parent f7146f0 commit 85ab1f7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/default/commerce.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Faker::Commerce.product_name #=> "Practical Granite Shirt"
Faker::Commerce.price #=> 44.6
Faker::Commerce.price(range: 0..10.0, as_string: true) #=> "2.18"

# Produces a Float between 0 and 5 by with step of 0.1 by default
# Keyword arguments: range, as_string, step, as float
Faker::Commerce.rating #=> 4.6
Faker::Commerce.rating(range: 0..10.0, decimals: 2) #=> 8.92

# Generate a random promotion code.
# Keyword arguments: digits
Faker::Commerce.promotion_code #=> "AmazingDeal829102"
Expand Down
19 changes: 19 additions & 0 deletions lib/faker/default/commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ def vendor
fetch('commerce.vendor')
end

##
# Produces a random product rating.
#
# @param range [Range] A range to generate the rating number within. Defaults to 0..5.0.
# @param decimals [float] Max number of decimals to generate. Defaults to 1
#
# @return [Float]
#
# @example
# Faker::Commerce.rating #=> 3.8
# Faker::Commerce.rating(range: 0..10.0, decimals: 2) #=> 8.92
#
# @faker.version next
def rating(range: 0..5.0, decimals: 1)
random_value = rand(range)
multiplier = 10.0**decimals
(random_value * multiplier).floor / multiplier
end

private

def categories(num)
Expand Down
2 changes: 1 addition & 1 deletion lib/locales/en/commerce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ en:
product: [Chair, Car, Computer, Gloves, Pants, Shirt, Table, Shoes, Hat, Plate, Knife, Bottle, Coat, Lamp, Keyboard, Bag, Bench, Clock, Watch, Wallet]
promotion_code:
adjective: ['Amazing', 'Awesome', 'Cool', 'Good', 'Great', 'Incredible', 'Killer', 'Premium', 'Special', 'Stellar', 'Sweet']
noun: ['Code', 'Deal', 'Discount', 'Price', 'Promo', 'Promotion', 'Sale', 'Savings']
noun: ['Code', 'Deal', 'Discount', 'Price', 'Rating', 'Promo', 'Promotion', 'Sale', 'Savings']
brand: ['Samsung',
'Dell',
'Nike',
Expand Down
23 changes: 23 additions & 0 deletions test/faker/default/test_faker_commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,27 @@ def test_brand
def test_vendor
assert_match(/[A-Z][a-z]+\.?/, @tester.vendor)
end

def test_rating
assert_includes 0..5, @tester.rating
assert_instance_of Float, @tester.rating(range: 1..2)
assert_includes 1..2, @tester.rating(range: 1..2)
end

def test_rating_with_custom_decimals
value = @tester.rating(decimals: 2)

assert_equal value, value.round(2)
end

def test_rating_is_float
assert @tester.rating.is_a? Float
end

def test_rating_with_srand
Faker::Config.random = Random.new(12_345)
rating = @tester.rating

assert_in_delta(4.6, rating)
end
end

0 comments on commit 85ab1f7

Please sign in to comment.