11#!/usr/bin/env python3
22"""
3- Simple test script to verify the Storage API endpoints work correctly.
3+ Unit tests to verify the Storage API endpoints work correctly.
44"""
55
6+ import unittest
67import requests
78import json
89
9- BASE_URL = "http://localhost:5001"
10-
11- def test_root_endpoint ():
12- """Test the root endpoint"""
13- print ("Testing root endpoint..." )
14- try :
15- response = requests .get (f"{ BASE_URL } /" )
16- print (f"Status: { response .status_code } " )
17- print (f"Response: { response .json ()} " )
18- return response .status_code == 200
19- except Exception as e :
20- print (f"Error: { e } " )
21- return False
22-
23- def test_storage_entry ():
24- """Test storage entry endpoints"""
25- print ("\n Testing storage entry endpoints..." )
10+ class TestStorageAPI (unittest .TestCase ):
11+ """Test cases for Storage API endpoints"""
12+
13+ def setUp (self ):
14+ """Set up test configuration"""
15+ self .base_url = "http://localhost:5001"
2616
27- # Save an entry
28- try :
29- response = requests .post (f"{ BASE_URL } /entries/test_key" ,
17+ def test_root_endpoint (self ):
18+ """Test the root endpoint"""
19+ response = requests .get (f"{ self .base_url } /" )
20+ self .assertEqual (response .status_code , 200 )
21+ self .assertIsInstance (response .json (), dict )
22+
23+ def test_storage_entry_operations (self ):
24+ """Test storage entry endpoints"""
25+ # Save an entry
26+ response = requests .post (f"{ self .base_url } /entries/test_key" ,
3027 json = {"value" : "test_value" })
31- print ( f"Save entry status: { response .status_code } " )
28+ self . assertEqual ( response .status_code , 200 )
3229
3330 # Fetch the entry
34- response = requests .get (f"{ BASE_URL } /entries/test_key" )
35- print (f"Fetch entry status: { response .status_code } " )
36- print (f"Fetch entry response: { response .json ()} " )
37-
38- # Test default value
39- response = requests .get (f"{ BASE_URL } /entries/missing_key?default=default_val" )
40- print (f"Default value response: { response .json ()} " )
31+ response = requests .get (f"{ self .base_url } /entries/test_key" )
32+ self .assertEqual (response .status_code , 200 )
33+ data = response .json ()
34+ self .assertIn ("value" , data )
4135
42- return True
43- except Exception as e :
44- print (f"Error: { e } " )
45- return False
46-
47- def test_storage_file ():
48- """Test storage file endpoints"""
49- print ("\n Testing storage file endpoints..." )
36+ # Test default value for missing key
37+ response = requests .get (f"{ self .base_url } /entries/missing_key?default=default_val" )
38+ self .assertEqual (response .status_code , 200 )
39+ data = response .json ()
40+ self .assertIn ("value" , data )
5041
51- try :
42+ def test_storage_file_operations (self ):
43+ """Test storage file endpoints"""
5244 # Save a file
53- response = requests .post (f"{ BASE_URL } /storage/test/example.txt" ,
45+ response = requests .post (f"{ self . base_url } /storage/test/example.txt" ,
5446 json = {"content" : "Hello, World!" })
55- print ( f"Save file status: { response .status_code } " )
47+ self . assertEqual ( response .status_code , 200 )
5648
5749 # Fetch the file
58- response = requests .get (f"{ BASE_URL } /storage/test/example.txt" )
59- print (f"Fetch file status: { response .status_code } " )
60- print (f"Fetch file response: { response .json ()} " )
50+ response = requests .get (f"{ self .base_url } /storage/test/example.txt" )
51+ self .assertEqual (response .status_code , 200 )
52+ data = response .json ()
53+ self .assertIn ("content" , data )
6154
6255 # List files
63- response = requests .get (f"{ BASE_URL } /storage/test/" )
64- print (f"List files status: { response .status_code } " )
65- print (f"List files response: { response .json ()} " )
66-
67- return True
68- except Exception as e :
69- print (f"Error: { e } " )
70- return False
71-
72- def test_storage_rename ():
73- """Test storage rename endpoint"""
74- print ("\n Testing storage rename endpoint..." )
56+ response = requests .get (f"{ self .base_url } /storage/test/" )
57+ self .assertEqual (response .status_code , 200 )
58+ data = response .json ()
59+ self .assertIsInstance (data , (list , dict ))
7560
76- try :
61+ def test_storage_rename_operation (self ):
62+ """Test storage rename endpoint"""
7763 # Create a test file
78- response = requests .post (f"{ BASE_URL } /storage/test_rename_file.txt" ,
64+ response = requests .post (f"{ self . base_url } /storage/test_rename_file.txt" ,
7965 json = {"content" : "Content to rename" })
80- print ( f"Create test file status: { response .status_code } " )
66+ self . assertEqual ( response .status_code , 200 )
8167
8268 # Rename the file
83- response = requests .post (f"{ BASE_URL } /storage/rename" ,
69+ response = requests .post (f"{ self . base_url } /storage/rename" ,
8470 json = {"old_path" : "test_rename_file.txt" , "new_path" : "renamed_test_file.txt" })
85- print (f"Rename file status: { response .status_code } " )
86- print (f"Rename response: { response .json ()} " )
71+ self .assertEqual (response .status_code , 200 )
8772
8873 # Verify old file is gone
89- response = requests .get (f"{ BASE_URL } /storage/test_rename_file.txt" )
90- print ( f"Old file fetch status: { response .status_code } (should be 404)" )
74+ response = requests .get (f"{ self . base_url } /storage/test_rename_file.txt" )
75+ self . assertEqual ( response .status_code , 404 )
9176
9277 # Verify new file exists
93- response = requests .get (f"{ BASE_URL } /storage/renamed_test_file.txt" )
94- print (f"New file fetch status: { response .status_code } " )
95- print (f"New file content: { response .json ()} " )
96-
97- return True
98- except Exception as e :
99- print (f"Error: { e } " )
100- return False
78+ response = requests .get (f"{ self .base_url } /storage/renamed_test_file.txt" )
79+ self .assertEqual (response .status_code , 200 )
80+ data = response .json ()
81+ self .assertIn ("content" , data )
10182
10283if __name__ == "__main__" :
103- print ("Testing Storage API endpoints..." )
104-
105- tests = [
106- test_root_endpoint ,
107- test_storage_entry ,
108- test_storage_file ,
109- test_storage_rename
110- ]
111-
112- results = []
113- for test in tests :
114- results .append (test ())
115-
116- print (f"\n Test results: { sum (results )} /{ len (results )} passed" )
84+ unittest .main ()
0 commit comments