File size: 8,805 Bytes
394963a
d9e6017
 
 
 
 
 
394963a
 
 
d9e6017
394963a
 
 
d9e6017
65933cd
d9e6017
 
 
 
65933cd
 
 
d9e6017
 
65933cd
 
 
d9e6017
f503159
d9e6017
 
65933cd
d9e6017
65933cd
d9e6017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65933cd
d9e6017
 
 
 
 
 
 
 
 
 
 
 
 
 
394963a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9e6017
394963a
 
 
 
 
 
 
 
 
 
 
 
 
 
d9e6017
394963a
 
 
 
 
 
bc8e3e9
65933cd
394963a
d9e6017
 
65933cd
 
 
 
 
d9e6017
394963a
d9e6017
65933cd
d9e6017
 
 
394963a
d9e6017
 
394963a
65933cd
d9e6017
 
65933cd
d9e6017
65933cd
 
 
 
d9e6017
65933cd
d9e6017
 
 
65933cd
394963a
65933cd
394963a
65933cd
394963a
 
65933cd
 
 
394963a
d9e6017
65933cd
 
d9e6017
65933cd
 
 
 
 
d9e6017
 
65933cd
d9e6017
 
 
 
394963a
 
65933cd
 
394963a
65933cd
 
 
 
d9e6017
65933cd
d9e6017
65933cd
d9e6017
 
 
 
 
 
 
65933cd
d9e6017
 
65933cd
d9e6017
65933cd
 
 
d9e6017
65933cd
d9e6017
65933cd
 
d9e6017
65933cd
 
 
 
 
 
d9e6017
65933cd
 
 
d9e6017
65933cd
d9e6017
65933cd
d9e6017
65933cd
d9e6017
 
65933cd
 
 
394963a
 
d9e6017
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python3
"""Test the complete upload flow: upload β†’ create caption β†’ submit caption"""

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import requests
import time
import io
from app.database import SessionLocal
from app import crud, models

def test_database_connection():
    """Test basic database connectivity and table creation"""
    db = SessionLocal()
    try:
        print("Testing database connection...")
        
        sources = db.query(models.Source).all()
        print(f"Found {len(sources)} sources in database")
        
        test_img = models.Images(
            file_key="test_key",
            sha256="test_sha",
            source="OTHER",
            event_type="OTHER",
            epsg="4326",
            image_type="crisis_map"
        )
        
        db.add(test_img)
        db.commit()
        print(f"Created test image with ID: {test_img.image_id}")
        
        db.delete(test_img)
        db.commit()
        print("Test completed successfully - database is working")
        
    except Exception as e:
        print(f"Database test failed: {e}")
        db.rollback()
    finally:
        db.close()

def test_crud_functions():
    """Test CRUD functions"""
    db = SessionLocal()
    try:
        print("Testing CRUD functions...")
        
        img = crud.create_image(
            db=db,
            src="OTHER",
            type_code="OTHER",
            key="test_crud_key",
            sha="test_crud_sha",
            countries=["US", "CA"],
            epsg="4326",
            image_type="crisis_map"
        )
        print(f"CRUD create_image successful: {img.image_id}")
        
        caption = crud.create_caption(
            db=db,
            image_id=img.image_id,
            title="Test CRUD Caption",
            prompt="Test CRUD prompt",
            model_code="STUB_MODEL",
            raw_json={"test_crud": True},
            text="This is a test CRUD caption"
        )
        print(f"CRUD create_caption successful for image: {caption.image_id}")
        
        db.delete(img)
        db.commit()
        print("CRUD test completed successfully")
        
    except Exception as e:
        print(f"CRUD test failed: {e}")
        db.rollback()
    finally:
        db.close()

def test_complete_upload_flow():
    """Test the complete upload flow: upload β†’ create caption β†’ submit caption"""
    print("=== Testing Complete Upload Flow ===")
    
    test_content = b"test image data for upload flow"
    test_filename = "test_upload.jpg"
    
    print("1. Uploading image via API...")
    files = {'file': (test_filename, io.BytesIO(test_content), 'image/jpeg')}
    data = {
        'source': 'OTHER',
        'type': 'OTHER', 
        'countries': ['US'],
        'epsg': '4326',
        'image_type': 'crisis_map'
    }
    
    try:
        response = requests.post('http://localhost:8080/api/images/', files=files, data=data)
        print(f"Upload response status: {response.status_code}")
        
        if response.status_code == 200:
            upload_result = response.json()
            image_id = upload_result['image_id']
            print(f"Upload successful! Image ID: {image_id}")
            
            print("2. Creating caption via API...")
            caption_data = {
                'title': 'Test Caption',
                'prompt': 'Describe this test image'
            }
            
            caption_response = requests.post(
                f'http://localhost:8080/api/images/{image_id}/caption',
                data=caption_data
            )
            print(f"Caption response status: {caption_response.status_code}")
            
            if caption_response.status_code == 200:
                caption_result = caption_response.json()
                caption_id = caption_result['image_id']
                print(f"Caption created successfully! Caption ID: {caption_id}")
                
                print("3. Submitting caption via API...")
                submit_data = {
                    'title': 'Test Caption',
                    'edited': 'This is an edited test caption',
                    'accuracy': 85,
                    'context': 90,
                    'usability': 80
                }
                
                submit_response = requests.put(
                    f'http://localhost:8080/api/images/{image_id}/caption',
                    json=submit_data
                )
                print(f"Submit response status: {submit_response.status_code}")
                
                if submit_response.status_code == 200:
                    print("Caption submitted successfully!")
                    
                    print("4. Verifying in database...")
                    db = SessionLocal()
                    try:
                        db_caption = crud.get_caption(db, image_id)
                        if db_caption:
                            print(f"SUCCESS: Caption found in database for image: {db_caption.image_id}")
                            print(f"Title: {db_caption.title}")
                            print(f"Edited: {db_caption.edited}")
                            print(f"Accuracy: {db_caption.accuracy}")
                        else:
                            print("ERROR: Caption not found in database")
                    finally:
                        db.close()
                else:
                    print(f"Caption submission failed: {submit_response.text}")
            else:
                print(f"Caption creation failed: {caption_response.text}")
        else:
            print(f"Upload failed: {response.text}")
            
    except Exception as e:
        print(f"Upload flow test failed: {e}")
        import traceback
        traceback.print_exc()

def test_deletion_logic():
    """Test the deletion logic for images"""
    print("=== Testing Deletion Logic ===")
    
    test_content = b"test image data for deletion test"
    test_filename = "test_deletion.jpg"
    
    print("1. Uploading image via API...")
    files = {'file': (test_filename, io.BytesIO(test_content), 'image/jpeg')}
    data = {
        'source': 'OTHER',
        'type': 'OTHER', 
        'countries': ['US'],
        'epsg': '4326',
        'image_type': 'crisis_map'
    }
    
    try:
        response = requests.post('http://localhost:8080/api/images/', files=files, data=data)
        print(f"Upload response status: {response.status_code}")
        
        if response.status_code == 200:
            upload_result = response.json()
            image_id = upload_result['image_id']
            print(f"Upload successful! Image ID: {image_id}")
            
            print("2. Creating caption via API...")
            caption_data = {
                'title': 'Test Caption for Deletion',
                'prompt': 'Describe this test image'
            }
            
            caption_response = requests.post(
                f'http://localhost:8080/api/images/{image_id}/caption',
                data=caption_data
            )
            print(f"Caption response status: {caption_response.status_code}")
            
            if caption_response.status_code == 200:
                print("Caption created successfully!")
                
                print("3. Testing image deletion...")
                delete_response = requests.delete(
                    f'http://localhost:8080/api/images/{image_id}'
                )
                print(f"Delete response status: {delete_response.status_code}")
                
                if delete_response.status_code == 200:
                    print("Image deleted successfully!")
                    
                    print("4. Verifying image deletion...")
                    db = SessionLocal()
                    try:
                        db_image = crud.get_image(db, image_id)
                        if db_image:
                            print("ERROR: Image still exists when it should have been deleted")
                        else:
                            print("SUCCESS: Image completely removed from database")
                    finally:
                        db.close()
                else:
                    print(f"Image deletion failed: {delete_response.text}")
            else:
                print(f"Caption creation failed: {caption_response.text}")
        else:
            print(f"Upload failed: {response.text}")
            
    except Exception as e:
        print(f"Deletion logic test failed: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_database_connection()
    test_crud_functions()
    test_complete_upload_flow()
    test_deletion_logic()