From 8d9324592b12dc1691f052165150ec14238584f1 Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sat, 23 Nov 2019 21:38:22 -0500 Subject: [PATCH 1/6] Apply black, flake8 also --- .flake8 | 16 ++++++++++++++++ python/gilded_rose.py | 7 +++++-- python/test_gilded_rose.py | 4 +++- python/texttest_fixture.py | 25 +++++++++++++------------ 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000000..d94abe2fc3 --- /dev/null +++ b/.flake8 @@ -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 diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea6486..6c7ca3b4a7 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,13 +1,16 @@ # -*- coding: utf-8 -*- -class GildedRose(object): +class GildedRose(object): def __init__(self, items): self.items = items def update_quality(self): 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 diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 2c66711bda..7fad116399 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -3,6 +3,7 @@ from gilded_rose import Item, GildedRose + class GildedRoseTest(unittest.TestCase): def test_foo(self): items = [Item("foo", 0, 0)] @@ -10,5 +11,6 @@ def test_foo(self): gilded_rose.update_quality() self.assertEquals("fixme", items[0].name) -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() diff --git a/python/texttest_fixture.py b/python/texttest_fixture.py index 86af5ef78d..fbf8b26d03 100644 --- a/python/texttest_fixture.py +++ b/python/texttest_fixture.py @@ -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): From 850cdc11ec2518923d92bf75bb8210a205536c9b Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sat, 23 Nov 2019 21:46:13 -0500 Subject: [PATCH 2/6] Add initial blank failing tests --- python/test_gilded_rose.py | 64 +++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 7fad116399..4d4f8ca9c5 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest -from gilded_rose import Item, GildedRose +from gilded_rose import GildedRose, Item class GildedRoseTest(unittest.TestCase): @@ -11,6 +11,68 @@ def test_foo(self): gilded_rose.update_quality() self.assertEquals("fixme", items[0].name) + def test_1(self): + """ + All items have a sell-in value which denotes the number of days we have to sell the item + """ + pass + + def test_2(self): + """ + All items have a quality value which denotes how valuable the item is + """ + pass + + def test_3(self): + """ + All items have a quality value which denotes how valuable the item is + """ + pass + + def test_4(self): + """ + At the end of each day our system lowers both values for every item + """ + pass + + def test_5(self): + """ + Once the sell by date has passed, quality degrades twice as fast + """ + + def test_6(self): + """ + The quality of an item is never negative + """ + + def test_7(self): + """ + "Aged Brie" actually increases in quality the older it gets + """ + + def test_8(self): + """ + The quality of an item is never more than 50 + """ + + def test_9(self): + """ + "Sulfuras", being a legendary item, never has to be sold or decreases in quality + """ + + def test_10(self): + """ + "Backstage passes", like aged brie, increases in quality as its sell-in value approaches; quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but quality drops to 0 after the concert + """ + + def test_11(self): + """ + """ + + def test_12(self): + """ + """ + if __name__ == "__main__": unittest.main() From 19eba02165cb587295a077e7f28d3fdee127ae75 Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sat, 23 Nov 2019 23:54:25 -0500 Subject: [PATCH 3/6] Add initial passing legacy tests --- python/gilded_rose.py | 35 ++++++++++++++ python/test_gilded_rose.py | 95 +++++++++++++++++++++++--------------- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 6c7ca3b4a7..a628f25051 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -3,9 +3,28 @@ 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" @@ -41,6 +60,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 diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 4d4f8ca9c5..7b21bd4c88 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -5,73 +5,94 @@ 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 test_1(self): - """ - All items have a sell-in value which denotes the number of days we have to sell the item - """ - pass - - def test_2(self): - """ - All items have a quality value which denotes how valuable the item is - """ - pass + 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 test_3(self): + def sell_in_and_quality_required(self): """ - All items have a quality value which denotes how valuable the item is + - 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 """ - pass + self.assertRaises(Exception, Item(name="Aged Brie", sell_in=1)) + self.assertRaises(Exception, Item(name="Poutine", quality=100)) - def test_4(self): + def test_end_of_business_daily_cron_job(self): """ At the end of each day our system lowers both values for every item """ - pass + self.gilded_rose.update_quality() + assert self.gilded_rose.items[0].quality == 19 + assert self.gilded_rose.items[0].sell_in == 9 - def test_5(self): + def test_conjured_items(self): """ Once the sell by date has passed, quality degrades twice as fast """ + raise Exception("Implement new feature") - def test_6(self): + 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_7(self): + 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_8(self): + 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_9(self): - """ - "Sulfuras", being a legendary item, never has to be sold or decreases in quality - """ - - def test_10(self): - """ - "Backstage passes", like aged brie, increases in quality as its sell-in value approaches; quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but quality drops to 0 after the concert - """ - - def test_11(self): + def test_legendary_items_dont_decrease_quality(self): """ + "Sulfuras", being a legendary item, never has to be sold or decreases + in quality """ + for i in range(0, 1000): + self.gilded_rose.update_quality() + assert self.gilded_rose.items[0].quality <= 50 - def test_12(self): + 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__": From e7241bd9a880b74e12ec527b5d649bb7ca1fe630 Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sat, 23 Nov 2019 23:56:34 -0500 Subject: [PATCH 4/6] Update sulfuras case --- python/test_gilded_rose.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 7b21bd4c88..5769595d9f 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -72,9 +72,10 @@ 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[0].quality <= 50 + assert self.gilded_rose.items[3].quality == previous_quality def test_backstage_passes(self): """ From bf846c33023f828b973790d5a73789b90d36b4b8 Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sun, 24 Nov 2019 00:22:28 -0500 Subject: [PATCH 5/6] Add failing test for conjured items --- python/test_gilded_rose.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 5769595d9f..ee47c77c8c 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -38,7 +38,19 @@ def test_conjured_items(self): """ Once the sell by date has passed, quality degrades twice as fast """ - raise Exception("Implement new feature") + self.gilded_rose.items[4].sell_in = 1 + previous_quality = self.gilded_rose.items[4].quality + self.gilded_rose.update_quality() + new_quality = self.gilded_rose.items[4].quality + previous_quality_increase_rate = (new_quality - previous_quality) / previous_quality + # Sell date has passed + previous_quality = self.gilded_rose.items[4].quality + self.gilded_rose.update_quality() + new_quality = self.gilded_rose.items[4].quality + new_quality_increase_rate = (previous_quality - new_quality) / previous_quality + increase_in_quality_rate = (new_quality_increase_rate - previous_quality_increase_rate) / previous_quality + self.assertEqual(increase_in_quality_rate / previous_quality, 1, "{} is not 1") + assert (new_quality_increase_rate - previous_quality_increase_rate) / previous_quality == 1 def test_quality_is_never_negative(self): """ From da51d0850c8d695494f867629dbe31238d0318e1 Mon Sep 17 00:00:00 2001 From: Ivan R Date: Sun, 24 Nov 2019 00:56:22 -0500 Subject: [PATCH 6/6] Add logic for conjured items --- python/gilded_rose.py | 5 ++++- python/test_gilded_rose.py | 29 +++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index a628f25051..2f892c936a 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -50,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: diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index ee47c77c8c..e40cfdae3f 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -5,14 +5,15 @@ class GildedRoseTest(unittest.TestCase): - 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="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) @@ -38,19 +39,23 @@ def test_conjured_items(self): """ Once the sell by date has passed, quality degrades twice as fast """ - self.gilded_rose.items[4].sell_in = 1 - previous_quality = self.gilded_rose.items[4].quality + 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[4].quality - previous_quality_increase_rate = (new_quality - previous_quality) / previous_quality + new_quality = self.gilded_rose.items[5].quality + previous_quality_increase_rate = ( + new_quality - previous_quality + ) / previous_quality # Sell date has passed - previous_quality = self.gilded_rose.items[4].quality + 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[4].quality - new_quality_increase_rate = (previous_quality - new_quality) / previous_quality - increase_in_quality_rate = (new_quality_increase_rate - previous_quality_increase_rate) / previous_quality - self.assertEqual(increase_in_quality_rate / previous_quality, 1, "{} is not 1") - assert (new_quality_increase_rate - previous_quality_increase_rate) / previous_quality == 1 + 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): """