Skip to content

Commit 1131d3c

Browse files
authored
Create tdmh.test.py
1 parent 65f7e96 commit 1131d3c

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/tdmh.test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# tdmh.test.py
2+
3+
import unittest
4+
from tdmh import TransDimensionalMarketHub
5+
6+
class TestTransDimensionalMarketHub(unittest.TestCase):
7+
def setUp(self):
8+
"""Set up a new TransDimensionalMarketHub instance for testing."""
9+
self.tdmh = TransDimensionalMarketHub()
10+
11+
def test_list_item_success(self):
12+
"""Test successful listing of an item."""
13+
self.tdmh.list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 50)
14+
self.assertIn("item_001", self.tdmh.market_data)
15+
self.assertEqual(self.tdmh.market_data["item_001"]["name"], "Galactic Crystal")
16+
self.assertEqual(self.tdmh.market_data["item_001"]["price"], 100)
17+
self.assertEqual(self.tdmh.market_data["item_001"]["quantity"], 50)
18+
19+
def test_trade_item_success(self):
20+
"""Test successful trading of an item."""
21+
self.tdmh.list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 50)
22+
self.tdmh.trade_item("item_001", "User _1", 5)
23+
self.assertEqual(self.tdmh.market_data["item_001"]["quantity"], 45)
24+
self.assertEqual(len(self.tdmh.transaction_log), 1)
25+
self.assertEqual(self.tdmh.transaction_log[0]['item_name'], "Galactic Crystal")
26+
self.assertEqual(self.tdmh.transaction_log[0]['buyer'], "User _1")
27+
self.assertEqual(self.tdmh.transaction_log[0]['quantity'], 5)
28+
29+
def test_trade_item_insufficient_stock(self):
30+
"""Test trading an item with insufficient stock."""
31+
self.tdmh.list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 2)
32+
with self.assertRaises(ValueError) as context:
33+
self.tdmh.trade_item("item_001", "User _1", 3)
34+
self.assertEqual(str(context.exception), "Insufficient stock for item 'Galactic Crystal'. Available: 2, Requested: 3.")
35+
36+
def test_trade_item_not_available(self):
37+
"""Test trading an item that is not available."""
38+
with self.assertRaises(ValueError) as context:
39+
self.tdmh.trade_item("item_999", "User _1", 1)
40+
self.assertEqual(str(context.exception), "Item not available.")
41+
42+
def test_update_price_success(self):
43+
"""Test successful price update of an item."""
44+
self.tdmh.list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 50)
45+
self.tdmh.update_price("item_001", 120)
46+
self.assertEqual(self.tdmh.market_data["item_001"]["price"], 120)
47+
48+
def test_update_price_item_not_found(self):
49+
"""Test updating the price of an item that is not found."""
50+
with self.assertRaises(ValueError) as context:
51+
self.tdmh.update_price("item_999", 120)
52+
self.assertEqual(str(context.exception), "Item not found.")
53+
54+
def test_check_item_details_success(self):
55+
"""Test successful retrieval of item details."""
56+
self.tdmh.list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 50)
57+
details = self.tdmh.check_item_details("item_001")
58+
self.assertEqual(details["name"], "Galactic Crystal")
59+
self.assertEqual(details["price"], 100)
60+
61+
def test_check_item_details_not_found(self):
62+
"""Test checking details of an item that is not found."""
63+
with self.assertRaises(ValueError) as context:
64+
self.tdmh.check_item_details("item_999")
65+
self.assertEqual(str(context.exception), "Item not found.")
66+
67+
def test_get_transaction_log(self):
68+
"""Test retrieval of transaction log."""
69+
self.tdmh .list_item("item_001", "Galactic Crystal", 100, "A rare crystal from the Andromeda galaxy.", 50)
70+
self.tdmh.trade_item("item_001", "User _1", 5)
71+
log = self.tdmh.get_transaction_log()
72+
self.assertEqual(len(log), 1)
73+
self.assertEqual(log[0]['item_name'], "Galactic Crystal")
74+
self.assertEqual(log[0]['buyer'], "User _1")
75+
self.assertEqual(log[0]['quantity'], 5)
76+
77+
if __name__ == "__main__":
78+
unittest.main()

0 commit comments

Comments
 (0)