1+ #!/usr/bin/env python3
2+ """
3+ Simple test runner to identify failing tests without pytest version requirements.
4+ """
5+
6+ import sys
7+ import os
8+ import traceback
9+ import tempfile
10+ import json
11+ from pathlib import Path
12+
13+ # Add src to path
14+ sys .path .insert (0 , 'src' )
15+
16+ def test_singleton_basic ():
17+ """Test singleton pattern basic functionality."""
18+ print ("Testing singleton pattern..." )
19+ try :
20+ from patterns .singleton import singleton
21+
22+ @singleton
23+ class TestService :
24+ def __init__ (self ):
25+ self .value = 42
26+
27+ s1 = TestService ()
28+ s2 = TestService ()
29+ assert s1 is s2
30+ print ("✅ Singleton basic test passed" )
31+ return True
32+ except Exception as e :
33+ print (f"❌ Singleton basic test failed: { e } " )
34+ traceback .print_exc ()
35+ return False
36+
37+ def test_repository_sqlite ():
38+ """Test SQLite repository functionality."""
39+ print ("Testing SQLite repository..." )
40+ try :
41+ from patterns .repository import SqliteUserRepository , User
42+
43+ repo = SqliteUserRepository (':memory:' )
44+ user = User (id = None , name = 'Test User' , email = 'test@example.com' )
45+ saved_user = repo .save (user )
46+
47+ assert saved_user .id == 1
48+ assert saved_user .name == 'Test User'
49+
50+ found_user = repo .find_by_id (1 )
51+ assert found_user is not None
52+ assert found_user .name == 'Test User'
53+
54+ print ("✅ SQLite repository test passed" )
55+ return True
56+ except Exception as e :
57+ print (f"❌ SQLite repository test failed: { e } " )
58+ traceback .print_exc ()
59+ return False
60+
61+ def test_repository_json_file ():
62+ """Test JSON file repository functionality."""
63+ print ("Testing JSON file repository..." )
64+ try :
65+ from patterns .repository import JsonFileUserRepository , User
66+
67+ # Test with new file
68+ with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.json' ) as f :
69+ temp_path = f .name
70+
71+ try :
72+ repo = JsonFileUserRepository (temp_path )
73+ assert len (repo ._users ) == 0
74+ assert repo ._next_id == 1
75+
76+ user = User (id = None , name = 'Test User' , email = 'test@example.com' )
77+ saved_user = repo .save (user )
78+ assert saved_user .id == 1
79+
80+ print ("✅ JSON file repository new file test passed" )
81+
82+ # Test with existing file
83+ with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.json' ) as f :
84+ test_data = {
85+ 'users' : [
86+ {
87+ 'id' : 1 ,
88+ 'name' : 'Alice' ,
89+ 'email' : 'alice@example.com' ,
90+ 'created_at' : '2023-01-01T12:00:00' ,
91+ }
92+ ],
93+ 'next_id' : 2 ,
94+ }
95+ json .dump (test_data , f )
96+ temp_path2 = f .name
97+
98+ try :
99+ repo2 = JsonFileUserRepository (temp_path2 )
100+ assert len (repo2 ._users ) == 1
101+ assert repo2 ._next_id == 2
102+ assert repo2 ._users [0 ].name == 'Alice'
103+ print ("✅ JSON file repository existing file test passed" )
104+
105+ finally :
106+ os .unlink (temp_path2 )
107+
108+ finally :
109+ os .unlink (temp_path )
110+
111+ return True
112+
113+ except Exception as e :
114+ print (f"❌ JSON file repository test failed: { e } " )
115+ traceback .print_exc ()
116+ return False
117+
118+ def test_factory_pattern ():
119+ """Test factory pattern functionality."""
120+ print ("Testing factory pattern..." )
121+ try :
122+ from patterns .factory import ShapeFactory , NotificationFactory
123+
124+ # Test shape factory
125+ circle = ShapeFactory .create_shape ('circle' , radius = 5 )
126+ assert circle .radius == 5
127+ assert abs (circle .area () - 78.53981633974483 ) < 0.001
128+
129+ # Test notification factory
130+ email = NotificationFactory .create_notifier ('email' )
131+ assert email .smtp_server == 'smtp.gmail.com'
132+
133+ print ("✅ Factory pattern test passed" )
134+ return True
135+ except Exception as e :
136+ print (f"❌ Factory pattern test failed: { e } " )
137+ traceback .print_exc ()
138+ return False
139+
140+ def test_observer_pattern ():
141+ """Test observer pattern functionality."""
142+ print ("Testing observer pattern..." )
143+ try :
144+ from patterns .observer import WeatherStation , CurrentConditionsDisplay
145+
146+ station = WeatherStation ()
147+ display = CurrentConditionsDisplay ()
148+
149+ station .register_observer (display )
150+ assert len (station ._observers ) == 1
151+
152+ station .set_measurements (25.0 , 65.0 , 1013.0 )
153+ # Should not raise any exceptions
154+
155+ print ("✅ Observer pattern test passed" )
156+ return True
157+ except Exception as e :
158+ print (f"❌ Observer pattern test failed: { e } " )
159+ traceback .print_exc ()
160+ return False
161+
162+ def main ():
163+ """Run all tests."""
164+ print ("🤖 Running simple test suite..." )
165+ print ("=" * 50 )
166+
167+ tests = [
168+ test_singleton_basic ,
169+ test_repository_sqlite ,
170+ test_repository_json_file ,
171+ test_factory_pattern ,
172+ test_observer_pattern ,
173+ ]
174+
175+ passed = 0
176+ failed = 0
177+
178+ for test in tests :
179+ try :
180+ if test ():
181+ passed += 1
182+ else :
183+ failed += 1
184+ except Exception as e :
185+ print (f"❌ Test { test .__name__ } failed with exception: { e } " )
186+ failed += 1
187+ print ()
188+
189+ print ("=" * 50 )
190+ print (f"Results: { passed } passed, { failed } failed" )
191+
192+ if failed == 0 :
193+ print ("🎉 All tests passed!" )
194+ return True
195+ else :
196+ print (f"❌ { failed } tests failed" )
197+ return False
198+
199+ if __name__ == "__main__" :
200+ success = main ()
201+ sys .exit (0 if success else 1 )
0 commit comments