|
| 1 | +import unittest |
| 2 | +import uuid |
| 3 | +import requests |
| 4 | + |
| 5 | +API_URL = "http://localhost:8000/transaction" |
| 6 | + |
| 7 | +class TestTransactionRoutesDocker(unittest.TestCase): |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + self.transaction_id = None |
| 11 | + |
| 12 | + def test_create_transaction_success(self): |
| 13 | + transaction_data = { |
| 14 | + "amount": 100.0, |
| 15 | + "method": "credit_card", |
| 16 | + "date": "2025-03-30T12:00:00", |
| 17 | + "description": "Test transaction" |
| 18 | + } |
| 19 | + response = requests.post(f"{API_URL}/", json=transaction_data) |
| 20 | + self.assertEqual(response.status_code, 201) |
| 21 | + self.transaction_id = response.json()["id"] |
| 22 | + |
| 23 | + def test_get_all_transactions(self): |
| 24 | + response = requests.get(API_URL) |
| 25 | + self.assertEqual(response.status_code, 200) |
| 26 | + self.assertIsInstance(response.json(), list) |
| 27 | + |
| 28 | + def test_get_single_transaction_not_found(self): |
| 29 | + random_id = uuid.uuid4() |
| 30 | + response = requests.get(f"{API_URL}/{random_id}") |
| 31 | + self.assertEqual(response.status_code, 404) |
| 32 | + |
| 33 | + def test_get_single_transaction_success(self): |
| 34 | + transaction_data = { |
| 35 | + "amount": 50.0, |
| 36 | + "method": "debit_card", |
| 37 | + "date": "2025-03-30T14:00:00", |
| 38 | + "description": "Another transaction" |
| 39 | + } |
| 40 | + create_response = requests.post(f"{API_URL}/", json=transaction_data) |
| 41 | + transaction_id = create_response.json()["id"] |
| 42 | + |
| 43 | + response = requests.get(f"{API_URL}/{transaction_id}") |
| 44 | + self.assertEqual(response.status_code, 200) |
| 45 | + self.assertEqual(response.json()["id"], transaction_id) |
| 46 | + |
| 47 | + def test_delete_transaction_success(self): |
| 48 | + transaction_data = { |
| 49 | + "amount": 75.0, |
| 50 | + "method": "paypal", |
| 51 | + "date": "2025-03-30T16:00:00", |
| 52 | + "description": "Transaction to delete" |
| 53 | + } |
| 54 | + create_response = requests.post(f"{API_URL}/", json=transaction_data) |
| 55 | + transaction_id = create_response.json()["id"] |
| 56 | + |
| 57 | + response = requests.delete(f"{API_URL}/{transaction_id}") |
| 58 | + self.assertEqual(response.status_code, 204) |
| 59 | + |
| 60 | + def test_delete_transaction_not_found(self): |
| 61 | + random_id = uuid.uuid4() |
| 62 | + response = requests.delete(f"{API_URL}/{random_id}") |
| 63 | + self.assertEqual(response.status_code, 404) |
0 commit comments