File size: 12,071 Bytes
31f0e50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""

Unit Tests for Database ORM Models.



Tests Conversation, Message, and ExtractedIntelligence models.

"""

import pytest
from datetime import datetime

from app.database.models import Conversation, Message, ExtractedIntelligence


class TestConversationModel:
    """Tests for Conversation model."""
    
    def test_initialization(self):
        """Test Conversation can be initialized."""
        conv = Conversation(session_id="test-session-123")
        
        assert conv is not None
        assert conv.session_id == "test-session-123"
    
    def test_default_values(self):
        """Test Conversation default values."""
        conv = Conversation(session_id="test-123")
        
        assert conv.language == "en"
        assert conv.persona is None
        assert conv.scam_detected is False
        assert conv.confidence == 0.0
        assert conv.turn_count == 0
    
    def test_custom_values(self):
        """Test Conversation with custom values."""
        conv = Conversation(
            session_id="sess-custom",
            language="hi",
            persona="elderly",
            scam_detected=True,
            confidence=0.95,
            turn_count=5
        )
        
        assert conv.language == "hi"
        assert conv.persona == "elderly"
        assert conv.scam_detected is True
        assert conv.confidence == 0.95
        assert conv.turn_count == 5
    
    def test_timestamps_set(self):
        """Test timestamps are set on initialization."""
        conv = Conversation(session_id="test")
        
        assert conv.created_at is not None
        assert conv.updated_at is not None
        assert isinstance(conv.created_at, datetime)
        assert isinstance(conv.updated_at, datetime)
    
    def test_id_is_none_initially(self):
        """Test id is None before database insert."""
        conv = Conversation(session_id="test")
        
        assert conv.id is None
    
    def test_to_dict(self):
        """Test to_dict method."""
        conv = Conversation(
            session_id="sess-dict-test",
            language="en",
            persona="eager",
            scam_detected=True,
            confidence=0.8,
            turn_count=3
        )
        
        result = conv.to_dict()
        
        assert isinstance(result, dict)
        assert result["session_id"] == "sess-dict-test"
        assert result["language"] == "en"
        assert result["persona"] == "eager"
        assert result["scam_detected"] is True
        assert result["confidence"] == 0.8
        assert result["turn_count"] == 3
        assert "created_at" in result
        assert "updated_at" in result
    
    def test_to_dict_includes_id(self):
        """Test to_dict includes id field."""
        conv = Conversation(session_id="test")
        result = conv.to_dict()
        
        assert "id" in result


class TestMessageModel:
    """Tests for Message model."""
    
    def test_initialization(self):
        """Test Message can be initialized."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="scammer",
            message="Hello, you won!"
        )
        
        assert msg is not None
        assert msg.conversation_id == 1
        assert msg.turn_number == 1
        assert msg.sender == "scammer"
        assert msg.message == "Hello, you won!"
    
    def test_timestamp_set(self):
        """Test timestamp is set on initialization."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="agent",
            message="Test"
        )
        
        assert msg.timestamp is not None
        assert isinstance(msg.timestamp, datetime)
    
    def test_id_is_none_initially(self):
        """Test id is None before database insert."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="scammer",
            message="Test"
        )
        
        assert msg.id is None
    
    def test_to_dict(self):
        """Test to_dict method."""
        msg = Message(
            conversation_id=5,
            turn_number=3,
            sender="agent",
            message="How can I claim?"
        )
        
        result = msg.to_dict()
        
        assert isinstance(result, dict)
        assert result["conversation_id"] == 5
        assert result["turn_number"] == 3
        assert result["sender"] == "agent"
        assert result["message"] == "How can I claim?"
        assert "timestamp" in result
    
    def test_to_dict_includes_id(self):
        """Test to_dict includes id field."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="scammer",
            message="Test"
        )
        result = msg.to_dict()
        
        assert "id" in result
    
    def test_scammer_sender(self):
        """Test message from scammer."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="scammer",
            message="Scam message"
        )
        
        assert msg.sender == "scammer"
    
    def test_agent_sender(self):
        """Test message from agent."""
        msg = Message(
            conversation_id=1,
            turn_number=1,
            sender="agent",
            message="Agent response"
        )
        
        assert msg.sender == "agent"


class TestExtractedIntelligenceModel:
    """Tests for ExtractedIntelligence model."""
    
    def test_initialization(self):
        """Test ExtractedIntelligence can be initialized."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel is not None
        assert intel.conversation_id == 1
    
    def test_default_empty_lists(self):
        """Test default empty lists for all fields."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel.upi_ids == []
        assert intel.bank_accounts == []
        assert intel.ifsc_codes == []
        assert intel.phone_numbers == []
        assert intel.phishing_links == []
    
    def test_default_confidence(self):
        """Test default extraction confidence is 0.0."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel.extraction_confidence == 0.0
    
    def test_custom_values(self):
        """Test ExtractedIntelligence with custom values."""
        intel = ExtractedIntelligence(
            conversation_id=5,
            upi_ids=["scammer@paytm", "fraud@ybl"],
            bank_accounts=["123456789012"],
            ifsc_codes=["SBIN0001234"],
            phone_numbers=["+919876543210"],
            phishing_links=["http://fake.com"],
            extraction_confidence=0.9
        )
        
        assert intel.conversation_id == 5
        assert len(intel.upi_ids) == 2
        assert "scammer@paytm" in intel.upi_ids
        assert len(intel.bank_accounts) == 1
        assert len(intel.ifsc_codes) == 1
        assert len(intel.phone_numbers) == 1
        assert len(intel.phishing_links) == 1
        assert intel.extraction_confidence == 0.9
    
    def test_timestamp_set(self):
        """Test created_at timestamp is set."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel.created_at is not None
        assert isinstance(intel.created_at, datetime)
    
    def test_id_is_none_initially(self):
        """Test id is None before database insert."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel.id is None
    
    def test_to_dict(self):
        """Test to_dict method."""
        intel = ExtractedIntelligence(
            conversation_id=10,
            upi_ids=["test@upi"],
            bank_accounts=["999888777666"],
            extraction_confidence=0.75
        )
        
        result = intel.to_dict()
        
        assert isinstance(result, dict)
        assert result["conversation_id"] == 10
        assert result["upi_ids"] == ["test@upi"]
        assert result["bank_accounts"] == ["999888777666"]
        assert result["extraction_confidence"] == 0.75
        assert "created_at" in result
    
    def test_to_dict_includes_all_fields(self):
        """Test to_dict includes all intelligence fields."""
        intel = ExtractedIntelligence(conversation_id=1)
        result = intel.to_dict()
        
        assert "id" in result
        assert "upi_ids" in result
        assert "bank_accounts" in result
        assert "ifsc_codes" in result
        assert "phone_numbers" in result
        assert "phishing_links" in result
    
    def test_has_intelligence_true(self):
        """Test has_intelligence returns True when data exists."""
        intel = ExtractedIntelligence(
            conversation_id=1,
            upi_ids=["test@paytm"]
        )
        
        assert intel.has_intelligence() is True
    
    def test_has_intelligence_false(self):
        """Test has_intelligence returns False when empty."""
        intel = ExtractedIntelligence(conversation_id=1)
        
        assert intel.has_intelligence() is False
    
    def test_has_intelligence_with_bank_account(self):
        """Test has_intelligence with bank account."""
        intel = ExtractedIntelligence(
            conversation_id=1,
            bank_accounts=["123456789012"]
        )
        
        assert intel.has_intelligence() is True
    
    def test_has_intelligence_with_phone(self):
        """Test has_intelligence with phone number."""
        intel = ExtractedIntelligence(
            conversation_id=1,
            phone_numbers=["+919876543210"]
        )
        
        assert intel.has_intelligence() is True
    
    def test_has_intelligence_with_phishing_link(self):
        """Test has_intelligence with phishing link."""
        intel = ExtractedIntelligence(
            conversation_id=1,
            phishing_links=["http://scam.com"]
        )
        
        assert intel.has_intelligence() is True


class TestModelIntegration:
    """Integration tests for model relationships."""
    
    def test_message_references_conversation(self):
        """Test Message references Conversation by ID."""
        conv = Conversation(session_id="test-integration")
        conv.id = 100  # Simulate database-assigned ID
        
        msg = Message(
            conversation_id=conv.id,
            turn_number=1,
            sender="scammer",
            message="Test"
        )
        
        assert msg.conversation_id == conv.id
    
    def test_intelligence_references_conversation(self):
        """Test ExtractedIntelligence references Conversation by ID."""
        conv = Conversation(session_id="test-intel")
        conv.id = 200  # Simulate database-assigned ID
        
        intel = ExtractedIntelligence(conversation_id=conv.id)
        
        assert intel.conversation_id == conv.id
    
    def test_multiple_messages_per_conversation(self):
        """Test multiple messages can reference same conversation."""
        conv_id = 50
        
        msg1 = Message(
            conversation_id=conv_id,
            turn_number=1,
            sender="scammer",
            message="First message"
        )
        
        msg2 = Message(
            conversation_id=conv_id,
            turn_number=1,
            sender="agent",
            message="First response"
        )
        
        msg3 = Message(
            conversation_id=conv_id,
            turn_number=2,
            sender="scammer",
            message="Second message"
        )
        
        assert msg1.conversation_id == msg2.conversation_id == msg3.conversation_id