From 4b6958de4a6a4872107a020d16c55faa70fa5fde Mon Sep 17 00:00:00 2001
From: brodriguez8774 <brodriguez8774@gmail.com>
Date: Sun, 22 Sep 2019 22:32:22 -0400
Subject: [PATCH] Implement methods to give knapsack class the initial data

---
 resources/knapsack.py       | 41 ++++++++++++++++++++++-
 tests/resources/knapsack.py | 66 +++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/resources/knapsack.py b/resources/knapsack.py
index a06b84c..a2329d7 100644
--- a/resources/knapsack.py
+++ b/resources/knapsack.py
@@ -25,4 +25,43 @@ class Knapsack():
     Knapsack algorithm.
     """
     def __init__(self):
-        pass
+        self._item_set_ = None
+        self._max_weight_ = None
+
+    def set_item_set(self, item_set):
+        """
+        Sets group of items to choose from.
+        :param item_set: Set of items, essentially in JSON format.
+        """
+        # Check that value is List type.
+        if not isinstance(item_set, list):
+            raise TypeError('Arg must be array of dict objects.')
+
+        # Check all items in item set.
+        for item in item_set:
+            # Check that array values are Dict type.
+            if not isinstance(item, dict):
+                raise TypeError('Arg must be array of dict objects.')
+
+            if 'benefit' not in item.keys():
+                raise KeyError('Key "benefit" must be present in all items.')
+
+            if 'cost' not in item.keys():
+                raise KeyError('Key "cost" must be present in all items.')
+
+        self._item_set_ = item_set
+
+    def set_max_weight(self, max_weight):
+        """
+        Sets upper bound to limit algorithm choices.
+        :param max_weight: Upper bound of algorithm's "bag".
+        """
+        # Check that value is an Int.
+        if not isinstance(max_weight, int):
+            raise TypeError('Arg must be of type Int.')
+
+        # Check that value is greater than 0.
+        if max_weight <= 0:
+            raise ValueError('Arg must be greater than 0.')
+
+        self._max_weight_ = max_weight
diff --git a/tests/resources/knapsack.py b/tests/resources/knapsack.py
index 61a4b9b..3ba9d20 100644
--- a/tests/resources/knapsack.py
+++ b/tests/resources/knapsack.py
@@ -19,5 +19,71 @@ class TestKnapsack(unittest.TestCase):
     def setUp(self):
         self.knapsack = Knapsack()
 
+        self.item_set_1 = [
+            {
+                'benefit': 12,
+                'cost': 4
+            },
+            {
+                'benefit': 32,
+                'cost': 8
+            },
+            {
+                'benefit': 40,
+                'cost': 2
+            },
+            {
+                'benefit': 30,
+                'cost': 6
+            },
+            {
+                'benefit': 50,
+                'cost': 1
+            }
+        ]
+
     def test_knapsack_creation(self):
         self.assertTrue(isinstance(self.knapsack, Knapsack))
+
+    def test_set_item_set_success(self):
+        self.knapsack.set_item_set(self.item_set_1)
+        self.assertEqual(self.knapsack._item_set_, self.item_set_1)
+
+    def test_set_item_set_failure(self):
+        with self.subTest('Arg is not array of dicts'):
+            with self.assertRaises(TypeError):
+                self.knapsack.set_item_set(None)
+
+            with self.assertRaises(TypeError):
+                self.knapsack.set_item_set([1, 2, 3])
+
+        with self.subTest('Dict item must have "benefit" and "cost" keys.'):
+            with self.assertRaises(KeyError):
+                self.knapsack.set_item_set([{'benefit': 1}])
+
+            with self.assertRaises(KeyError):
+                self.knapsack.set_item_set([{'cost': 1}])
+
+    def test_set_max_weight_success(self):
+        self.knapsack.set_max_weight(1)
+        self.assertEqual(self.knapsack._max_weight_, 1)
+
+        self.knapsack.set_max_weight(2)
+        self.assertEqual(self.knapsack._max_weight_, 2)
+
+    def test_set_max_weight_failure(self):
+        with self.subTest('Arg is not of type Int'):
+            with self.assertRaises(TypeError):
+                self.knapsack.set_max_weight(None)
+
+            with self.assertRaises(TypeError):
+                self.knapsack.set_max_weight('Test')
+
+        with self.subTest('Arg is not greater than 0'):
+            with self.assertRaises(ValueError):
+                self.knapsack.set_max_weight(0)
+
+            with self.assertRaises(ValueError):
+                self.knapsack.set_max_weight(-1)
+
+
-- 
GitLab