Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

My project #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
max-line-length = 100
# TODO: Make lower 10-15ish
max-complexity = 30
filename = *.py
format = default
exclude =
britedata_py_reports/functools_lru_cache.py
britedata_py_reports/prep.py
britedata_py_reports/nonprep.py
# TODO: Remove
britedata_py_reports/custom_reports/shared_reports/agency_experience_df.py
britedata_py_reports/custom_reports/shared_reports/mutual_boiler_re.py
britedata_py_reports/custom_reports/velocity/agency_commissions_velocity.py
britedata_py_reports/custom_reports/afr/agency_metrics.py
ignore = E203, E402, E501, E711, E712, W503, W504, W605
47 changes: 44 additions & 3 deletions python/gilded_rose.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
# -*- coding: utf-8 -*-

class GildedRose(object):

class GildedRose(object):
def __init__(self, items):
"""Init (constructor).

Parameters
----------
items : list[Item]
Items

Returns
-------
None

"""
self.items = items

def update_quality(self):
"""Used at the end of each day to lower Sell In and Quality for every item.

Returns
-------
None

"""
for item in self.items:
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert":
if (
item.name != "Aged Brie"
and item.name != "Backstage passes to a TAFKAL80ETC concert"
):
if item.quality > 0:
if item.name != "Sulfuras, Hand of Ragnaros":
item.quality = item.quality - 1
Expand All @@ -28,7 +50,10 @@ def update_quality(self):
if item.name != "Backstage passes to a TAFKAL80ETC concert":
if item.quality > 0:
if item.name != "Sulfuras, Hand of Ragnaros":
item.quality = item.quality - 1
if "Conjured" not in item.name:
item.quality = item.quality - 1
elif "Conjured" in item.name:
item.quality = item.quality - 2
else:
item.quality = item.quality - item.quality
else:
Expand All @@ -38,6 +63,22 @@ def update_quality(self):

class Item:
def __init__(self, name, sell_in, quality):
"""Init (constructor).

Parameters
----------
name : str
Item name.
sell_in : int
Number of days we have to sell the item.
quality : int
How valuable the item is.

Returns
-------
None

"""
self.name = name
self.sell_in = sell_in
self.quality = quality
Expand Down
117 changes: 110 additions & 7 deletions python/test_gilded_rose.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,117 @@
# -*- coding: utf-8 -*-
import unittest

from gilded_rose import Item, GildedRose
from gilded_rose import GildedRose, Item


class GildedRoseTest(unittest.TestCase):
def test_foo(self):
items = [Item("foo", 0, 0)]
gilded_rose = GildedRose(items)
gilded_rose.update_quality()
self.assertEquals("fixme", items[0].name)
def setUp(self):
items = [
Item(name="+5 Dexterity Vest", sell_in=10, quality=20),
Item(name="Aged Brie", sell_in=2, quality=0),
Item(name="Elixir of the Mongoose", sell_in=5, quality=7),
Item(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80),
Item(
name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20
),
Item(name="Conjured Mana Cake", sell_in=3, quality=6),
]
self.gilded_rose = GildedRose(items)

def sell_in_and_quality_required(self):
"""
- All items have a sell-in value which denotes the number of days we have
to sell the item
- All items have a quality value which denotes how valuable the item is
"""
self.assertRaises(Exception, Item(name="Aged Brie", sell_in=1))
self.assertRaises(Exception, Item(name="Poutine", quality=100))

def test_end_of_business_daily_cron_job(self):
"""
At the end of each day our system lowers both values for every item
"""
self.gilded_rose.update_quality()
assert self.gilded_rose.items[0].quality == 19
assert self.gilded_rose.items[0].sell_in == 9

def test_conjured_items(self):
"""
Once the sell by date has passed, quality degrades twice as fast
"""
self.gilded_rose.items[5].sell_in = 1
previous_quality = self.gilded_rose.items[5].quality
self.gilded_rose.update_quality()
new_quality = self.gilded_rose.items[5].quality
previous_quality_increase_rate = (
new_quality - previous_quality
) / previous_quality
# Sell date has passed
self.gilded_rose.items[5].sell_in = 0
previous_quality = self.gilded_rose.items[5].quality
self.gilded_rose.update_quality()
new_quality = self.gilded_rose.items[5].quality
new_quality_increase_rate = (new_quality - previous_quality) / previous_quality
increase_in_quality_rate = (
new_quality_increase_rate - previous_quality_increase_rate
) / previous_quality_increase_rate
assert increase_in_quality_rate >= 2

def test_quality_is_never_negative(self):
"""
The quality of an item is never negative
"""
self.gilded_rose.update_quality()
for i in range(0, 10000):
self.gilded_rose.update_quality()
assert self.gilded_rose.items[0].quality >= 0

def test_quality_items_increase_older(self):
"""
"Aged Brie" actually increases in quality the older it gets
"""
previous_quality = self.gilded_rose.items[1].quality
assert previous_quality == 0
self.gilded_rose.update_quality()
new_quality = self.gilded_rose.items[1].quality
assert new_quality > previous_quality

def test_quality_max_limit(self):
"""
The quality of an item is never more than 50
"""
for i in range(0, 1000):
self.gilded_rose.update_quality()
assert self.gilded_rose.items[0].quality <= 50

def test_legendary_items_dont_decrease_quality(self):
"""
"Sulfuras", being a legendary item, never has to be sold or decreases
in quality
"""
previous_quality = self.gilded_rose.items[3].quality
for i in range(0, 1000):
self.gilded_rose.update_quality()
assert self.gilded_rose.items[3].quality == previous_quality

def test_backstage_passes(self):
"""
"Backstage passes" quality increases by 2 when there are 10 days or less
and by 3 when there are 5 days or less
"""
self.gilded_rose.items[4].sell_in = 10
previous_quality = self.gilded_rose.items[4].quality
self.gilded_rose.update_quality()
new_quality = self.gilded_rose.items[4].quality
assert previous_quality - new_quality == -2
self.gilded_rose.update_quality()
assert previous_quality - new_quality == -2
self.gilded_rose.items[4].sell_in = 5
previous_quality = self.gilded_rose.items[4].quality
self.gilded_rose.update_quality()
new_quality = self.gilded_rose.items[4].quality
assert previous_quality - new_quality == -3


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
25 changes: 13 additions & 12 deletions python/texttest_fixture.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

from gilded_rose import *
from gilded_rose import Item, GildedRose

if __name__ == "__main__":
print ("OMGHAI!")
print("OMGHAI!")
items = [
Item(name="+5 Dexterity Vest", sell_in=10, quality=20),
Item(name="Aged Brie", sell_in=2, quality=0),
Item(name="Elixir of the Mongoose", sell_in=5, quality=7),
Item(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80),
Item(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49),
Item(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O
]
Item(name="+5 Dexterity Vest", sell_in=10, quality=20),
Item(name="Aged Brie", sell_in=2, quality=0),
Item(name="Elixir of the Mongoose", sell_in=5, quality=7),
Item(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80),
Item(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49),
Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49),
Item(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O
]

days = 2
import sys

if len(sys.argv) > 1:
days = int(sys.argv[1]) + 1
for day in range(days):
Expand Down