forked from agentic-review-benchmarks/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_annotation_service.py
More file actions
1347 lines (1126 loc) · 54 KB
/
test_annotation_service.py
File metadata and controls
1347 lines (1126 loc) · 54 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from unittest.mock import create_autospec, patch
import pytest
from faker import Faker
from werkzeug.exceptions import NotFound
from models import Account
from models.model import MessageAnnotation
from services.annotation_service import AppAnnotationService
from services.app_service import AppService
class TestAnnotationService:
"""Integration tests for AnnotationService using testcontainers."""
@pytest.fixture
def mock_external_service_dependencies(self):
"""Mock setup for external service dependencies."""
with (
patch("services.account_service.FeatureService") as mock_account_feature_service,
patch("services.annotation_service.FeatureService") as mock_feature_service,
patch("services.annotation_service.add_annotation_to_index_task") as mock_add_task,
patch("services.annotation_service.update_annotation_to_index_task") as mock_update_task,
patch("services.annotation_service.delete_annotation_index_task") as mock_delete_task,
patch("services.annotation_service.enable_annotation_reply_task") as mock_enable_task,
patch("services.annotation_service.disable_annotation_reply_task") as mock_disable_task,
patch("services.annotation_service.batch_import_annotations_task") as mock_batch_import_task,
patch("services.annotation_service.current_account_with_tenant") as mock_current_account_with_tenant,
):
# Setup default mock returns
mock_account_feature_service.get_features.return_value.billing.enabled = False
mock_add_task.delay.return_value = None
mock_update_task.delay.return_value = None
mock_delete_task.delay.return_value = None
mock_enable_task.delay.return_value = None
mock_disable_task.delay.return_value = None
mock_batch_import_task.delay.return_value = None
# Create mock user that will be returned by current_account_with_tenant
mock_user = create_autospec(Account, instance=True)
yield {
"account_feature_service": mock_account_feature_service,
"feature_service": mock_feature_service,
"add_task": mock_add_task,
"update_task": mock_update_task,
"delete_task": mock_delete_task,
"enable_task": mock_enable_task,
"disable_task": mock_disable_task,
"batch_import_task": mock_batch_import_task,
"current_account_with_tenant": mock_current_account_with_tenant,
"current_user": mock_user,
}
def _create_test_app_and_account(self, db_session_with_containers, mock_external_service_dependencies):
"""
Helper method to create a test app and account for testing.
Args:
db_session_with_containers: Database session from testcontainers infrastructure
mock_external_service_dependencies: Mock dependencies
Returns:
tuple: (app, account) - Created app and account instances
"""
fake = Faker()
# Setup mocks for account creation
mock_external_service_dependencies[
"account_feature_service"
].get_system_features.return_value.is_allow_register = True
# Create account and tenant first
from services.account_service import AccountService, TenantService
account = AccountService.create_account(
email=fake.email(),
name=fake.name(),
interface_language="en-US",
password=fake.password(length=12),
)
TenantService.create_owner_tenant_if_not_exist(account, name=fake.company())
tenant = account.current_tenant
# Setup app creation arguments
app_args = {
"name": fake.company(),
"description": fake.text(max_nb_chars=100),
"mode": "chat",
"icon_type": "emoji",
"icon": "🤖",
"icon_background": "#FF6B6B",
"api_rph": 100,
"api_rpm": 10,
}
# Create app
app_service = AppService()
app = app_service.create_app(tenant.id, app_args, account)
# Setup current_user mock
self._mock_current_user(mock_external_service_dependencies, account.id, tenant.id)
return app, account
def _mock_current_user(self, mock_external_service_dependencies, account_id, tenant_id):
"""
Helper method to mock the current user for testing.
"""
mock_external_service_dependencies["current_user"].id = account_id
mock_external_service_dependencies["current_user"].current_tenant_id = tenant_id
# Configure current_account_with_tenant to return (user, tenant_id)
mock_external_service_dependencies["current_account_with_tenant"].return_value = (
mock_external_service_dependencies["current_user"],
tenant_id,
)
def _create_test_conversation(self, app, account, fake):
"""
Helper method to create a test conversation with all required fields.
"""
from extensions.ext_database import db
from models.model import Conversation
conversation = Conversation(
app_id=app.id,
app_model_config_id=None,
model_provider=None,
model_id="",
override_model_configs=None,
mode=app.mode,
name=fake.sentence(),
inputs={},
introduction="",
system_instruction="",
system_instruction_tokens=0,
status="normal",
invoke_from="console",
from_source="console",
from_end_user_id=None,
from_account_id=account.id,
)
db.session.add(conversation)
db.session.flush()
return conversation
def _create_test_message(self, app, conversation, account, fake):
"""
Helper method to create a test message with all required fields.
"""
import json
from extensions.ext_database import db
from models.model import Message
message = Message(
app_id=app.id,
model_provider=None,
model_id="",
override_model_configs=None,
conversation_id=conversation.id,
inputs={},
query=fake.sentence(),
message=json.dumps([{"role": "user", "text": fake.sentence()}]),
message_tokens=0,
message_unit_price=0,
message_price_unit=0.001,
answer=fake.text(max_nb_chars=200),
answer_tokens=0,
answer_unit_price=0,
answer_price_unit=0.001,
parent_message_id=None,
provider_response_latency=0,
total_price=0,
currency="USD",
invoke_from="console",
from_source="console",
from_end_user_id=None,
from_account_id=account.id,
)
db.session.add(message)
db.session.commit()
return message
def test_insert_app_annotation_directly_success(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test successful direct insertion of app annotation.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Setup annotation data
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
# Insert annotation directly
annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
# Verify annotation was created correctly
assert annotation.app_id == app.id
assert annotation.question == annotation_args["question"]
assert annotation.content == annotation_args["answer"]
assert annotation.account_id == account.id
assert annotation.hit_count == 0
assert annotation.id is not None
# Verify annotation was saved to database
from extensions.ext_database import db
db.session.refresh(annotation)
assert annotation.id is not None
# Verify add_annotation_to_index_task was called (when annotation setting exists)
# Note: In this test, no annotation setting exists, so task should not be called
mock_external_service_dependencies["add_task"].delay.assert_not_called()
def test_insert_app_annotation_directly_app_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test direct insertion of app annotation when app is not found.
"""
fake = Faker()
non_existent_app_id = fake.uuid4()
# Mock random current user to avoid dependency issues
self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())
# Setup annotation data
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
# Try to insert annotation with non-existent app
with pytest.raises(NotFound, match="App not found"):
AppAnnotationService.insert_app_annotation_directly(annotation_args, non_existent_app_id)
def test_update_app_annotation_directly_success(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test successful direct update of app annotation.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# First, create an annotation
original_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
annotation = AppAnnotationService.insert_app_annotation_directly(original_args, app.id)
# Update the annotation
updated_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
updated_annotation = AppAnnotationService.update_app_annotation_directly(updated_args, app.id, annotation.id)
# Verify annotation was updated correctly
assert updated_annotation.id == annotation.id
assert updated_annotation.app_id == app.id
assert updated_annotation.question == updated_args["question"]
assert updated_annotation.content == updated_args["answer"]
assert updated_annotation.account_id == account.id
# Verify original values were changed
assert updated_annotation.question != original_args["question"]
assert updated_annotation.content != original_args["answer"]
# Verify update_annotation_to_index_task was called (when annotation setting exists)
# Note: In this test, no annotation setting exists, so task should not be called
mock_external_service_dependencies["update_task"].delay.assert_not_called()
def test_up_insert_app_annotation_from_message_new(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test creating new annotation from message.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create a conversation and message first
conversation = self._create_test_conversation(app, account, fake)
message = self._create_test_message(app, conversation, account, fake)
# Setup annotation data with message_id
annotation_args = {
"message_id": message.id,
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
# Insert annotation from message
annotation = AppAnnotationService.up_insert_app_annotation_from_message(annotation_args, app.id)
# Verify annotation was created correctly
assert annotation.app_id == app.id
assert annotation.conversation_id == conversation.id
assert annotation.message_id == message.id
assert annotation.question == annotation_args["question"]
assert annotation.content == annotation_args["answer"]
assert annotation.account_id == account.id
# Verify add_annotation_to_index_task was called (when annotation setting exists)
# Note: In this test, no annotation setting exists, so task should not be called
mock_external_service_dependencies["add_task"].delay.assert_not_called()
def test_up_insert_app_annotation_from_message_update(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test updating existing annotation from message.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create a conversation and message first
conversation = self._create_test_conversation(app, account, fake)
message = self._create_test_message(app, conversation, account, fake)
# Create initial annotation
initial_args = {
"message_id": message.id,
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
initial_annotation = AppAnnotationService.up_insert_app_annotation_from_message(initial_args, app.id)
# Update the annotation
updated_args = {
"message_id": message.id,
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
updated_annotation = AppAnnotationService.up_insert_app_annotation_from_message(updated_args, app.id)
# Verify annotation was updated correctly (same ID)
assert updated_annotation.id == initial_annotation.id
assert updated_annotation.question == updated_args["question"]
assert updated_annotation.content == updated_args["answer"]
assert updated_annotation.question != initial_args["question"]
assert updated_annotation.content != initial_args["answer"]
# Verify add_annotation_to_index_task was called (when annotation setting exists)
# Note: In this test, no annotation setting exists, so task should not be called
mock_external_service_dependencies["add_task"].delay.assert_not_called()
def test_up_insert_app_annotation_from_message_app_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test creating annotation from message when app is not found.
"""
fake = Faker()
non_existent_app_id = fake.uuid4()
# Mock random current user to avoid dependency issues
self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())
# Setup annotation data
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
# Try to insert annotation with non-existent app
with pytest.raises(NotFound, match="App not found"):
AppAnnotationService.up_insert_app_annotation_from_message(annotation_args, non_existent_app_id)
def test_get_annotation_list_by_app_id_success(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test successful retrieval of annotation list by app ID.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create multiple annotations
annotations = []
for i in range(3):
annotation_args = {
"question": f"Question {i}: {fake.sentence()}",
"answer": f"Answer {i}: {fake.text(max_nb_chars=200)}",
}
annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
annotations.append(annotation)
# Get annotation list
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword=""
)
# Verify results
assert len(annotation_list) == 3
assert total == 3
# Verify all annotations belong to the correct app
for annotation in annotation_list:
assert annotation.app_id == app.id
assert annotation.account_id == account.id
def test_get_annotation_list_by_app_id_with_keyword(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test retrieval of annotation list with keyword search.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create annotations with specific keywords
unique_keyword = f"unique_{fake.uuid4()[:8]}"
annotation_args = {
"question": f"Question with {unique_keyword} keyword",
"answer": f"Answer with {unique_keyword} keyword",
}
AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
# Create another annotation without the keyword
other_args = {
"question": "Different question without special term",
"answer": "Different answer without special content",
}
AppAnnotationService.insert_app_annotation_directly(other_args, app.id)
# Search with keyword
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword=unique_keyword
)
# Verify only matching annotations are returned
assert len(annotation_list) == 1
assert total == 1
assert unique_keyword in annotation_list[0].question or unique_keyword in annotation_list[0].content
def test_get_annotation_list_by_app_id_with_special_characters_in_keyword(
self, db_session_with_containers, mock_external_service_dependencies
):
r"""
Test retrieval of annotation list with special characters in keyword to verify SQL injection prevention.
This test verifies:
- Special characters (%, _, \) in keyword are properly escaped
- Search treats special characters as literal characters, not wildcards
- SQL injection via LIKE wildcards is prevented
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create annotations with special characters in content
annotation_with_percent = {
"question": "Question with 50% discount",
"answer": "Answer about 50% discount offer",
}
AppAnnotationService.insert_app_annotation_directly(annotation_with_percent, app.id)
annotation_with_underscore = {
"question": "Question with test_data",
"answer": "Answer about test_data value",
}
AppAnnotationService.insert_app_annotation_directly(annotation_with_underscore, app.id)
annotation_with_backslash = {
"question": "Question with path\\to\\file",
"answer": "Answer about path\\to\\file location",
}
AppAnnotationService.insert_app_annotation_directly(annotation_with_backslash, app.id)
# Create annotation that should NOT match (contains % but as part of different text)
annotation_no_match = {
"question": "Question with 100% different",
"answer": "Answer about 100% different content",
}
AppAnnotationService.insert_app_annotation_directly(annotation_no_match, app.id)
# Test 1: Search with % character - should find exact match only
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword="50%"
)
assert total == 1
assert len(annotation_list) == 1
assert "50%" in annotation_list[0].question or "50%" in annotation_list[0].content
# Test 2: Search with _ character - should find exact match only
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword="test_data"
)
assert total == 1
assert len(annotation_list) == 1
assert "test_data" in annotation_list[0].question or "test_data" in annotation_list[0].content
# Test 3: Search with \ character - should find exact match only
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword="path\\to\\file"
)
assert total == 1
assert len(annotation_list) == 1
assert "path\\to\\file" in annotation_list[0].question or "path\\to\\file" in annotation_list[0].content
# Test 4: Search with % should NOT match 100% (verifies escaping works)
annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(
app.id, page=1, limit=10, keyword="50%"
)
# Should only find the 50% annotation, not the 100% one
assert total == 1
assert all("50%" in (item.question or "") or "50%" in (item.content or "") for item in annotation_list)
def test_get_annotation_list_by_app_id_app_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test retrieval of annotation list when app is not found.
"""
fake = Faker()
non_existent_app_id = fake.uuid4()
# Mock random current user to avoid dependency issues
self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())
# Try to get annotation list with non-existent app
with pytest.raises(NotFound, match="App not found"):
AppAnnotationService.get_annotation_list_by_app_id(non_existent_app_id, page=1, limit=10, keyword="")
def test_delete_app_annotation_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful deletion of app annotation.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create an annotation first
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
annotation_id = annotation.id
# Delete the annotation
AppAnnotationService.delete_app_annotation(app.id, annotation_id)
# Verify annotation was deleted
from extensions.ext_database import db
deleted_annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
assert deleted_annotation is None
# Verify delete_annotation_index_task was called (when annotation setting exists)
# Note: In this test, no annotation setting exists, so task should not be called
mock_external_service_dependencies["delete_task"].delay.assert_not_called()
def test_delete_app_annotation_app_not_found(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test deletion of app annotation when app is not found.
"""
fake = Faker()
non_existent_app_id = fake.uuid4()
annotation_id = fake.uuid4()
# Mock random current user to avoid dependency issues
self._mock_current_user(mock_external_service_dependencies, fake.uuid4(), fake.uuid4())
# Try to delete annotation with non-existent app
with pytest.raises(NotFound, match="App not found"):
AppAnnotationService.delete_app_annotation(non_existent_app_id, annotation_id)
def test_delete_app_annotation_annotation_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test deletion of app annotation when annotation is not found.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
non_existent_annotation_id = fake.uuid4()
# Try to delete non-existent annotation
with pytest.raises(NotFound, match="Annotation not found"):
AppAnnotationService.delete_app_annotation(app.id, non_existent_annotation_id)
def test_enable_app_annotation_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful enabling of app annotation.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Setup enable arguments
enable_args = {
"score_threshold": 0.8,
"embedding_provider_name": "openai",
"embedding_model_name": "text-embedding-ada-002",
}
# Enable annotation
result = AppAnnotationService.enable_app_annotation(enable_args, app.id)
# Verify result structure
assert "job_id" in result
assert "job_status" in result
assert result["job_status"] == "waiting"
assert result["job_id"] is not None
# Verify task was called
mock_external_service_dependencies["enable_task"].delay.assert_called_once()
def test_disable_app_annotation_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful disabling of app annotation.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Disable annotation
result = AppAnnotationService.disable_app_annotation(app.id)
# Verify result structure
assert "job_id" in result
assert "job_status" in result
assert result["job_status"] == "waiting"
assert result["job_id"] is not None
# Verify task was called
mock_external_service_dependencies["disable_task"].delay.assert_called_once()
def test_enable_app_annotation_cached_job(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test enabling app annotation when job is already cached.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Mock Redis to return cached job
from extensions.ext_redis import redis_client
cached_job_id = fake.uuid4()
enable_app_annotation_key = f"enable_app_annotation_{app.id}"
redis_client.set(enable_app_annotation_key, cached_job_id)
# Setup enable arguments
enable_args = {
"score_threshold": 0.8,
"embedding_provider_name": "openai",
"embedding_model_name": "text-embedding-ada-002",
}
# Enable annotation (should return cached job)
result = AppAnnotationService.enable_app_annotation(enable_args, app.id)
# Verify cached result
assert cached_job_id == result["job_id"].decode("utf-8")
assert result["job_status"] == "processing"
# Verify task was not called again
mock_external_service_dependencies["enable_task"].delay.assert_not_called()
# Clean up
redis_client.delete(enable_app_annotation_key)
def test_get_annotation_hit_histories_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful retrieval of annotation hit histories.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create an annotation first
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
# Add some hit histories
for i in range(3):
AppAnnotationService.add_annotation_history(
annotation_id=annotation.id,
app_id=app.id,
annotation_question=annotation.question,
annotation_content=annotation.content,
query=f"Query {i}: {fake.sentence()}",
user_id=account.id,
message_id=fake.uuid4(),
from_source="console",
score=0.8 + (i * 0.1),
)
# Get hit histories
hit_histories, total = AppAnnotationService.get_annotation_hit_histories(
app.id, annotation.id, page=1, limit=10
)
# Verify results
assert len(hit_histories) == 3
assert total == 3
# Verify all histories belong to the correct annotation
for history in hit_histories:
assert history.annotation_id == annotation.id
assert history.app_id == app.id
assert history.account_id == account.id
def test_add_annotation_history_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful addition of annotation history.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create an annotation first
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
# Get initial hit count
initial_hit_count = annotation.hit_count
# Add annotation history
query = fake.sentence()
message_id = fake.uuid4()
score = 0.85
AppAnnotationService.add_annotation_history(
annotation_id=annotation.id,
app_id=app.id,
annotation_question=annotation.question,
annotation_content=annotation.content,
query=query,
user_id=account.id,
message_id=message_id,
from_source="console",
score=score,
)
# Verify hit count was incremented
from extensions.ext_database import db
db.session.refresh(annotation)
assert annotation.hit_count == initial_hit_count + 1
# Verify history was created
from models.model import AppAnnotationHitHistory
history = (
db.session.query(AppAnnotationHitHistory)
.where(
AppAnnotationHitHistory.annotation_id == annotation.id, AppAnnotationHitHistory.message_id == message_id
)
.first()
)
assert history is not None
assert history.app_id == app.id
assert history.account_id == account.id
assert history.question == query
assert history.score == score
assert history.source == "console"
def test_get_annotation_by_id_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful retrieval of annotation by ID.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create an annotation
annotation_args = {
"question": fake.sentence(),
"answer": fake.text(max_nb_chars=200),
}
created_annotation = AppAnnotationService.insert_app_annotation_directly(annotation_args, app.id)
# Get annotation by ID
retrieved_annotation = AppAnnotationService.get_annotation_by_id(created_annotation.id)
# Verify annotation was retrieved correctly
assert retrieved_annotation is not None
assert retrieved_annotation.id == created_annotation.id
assert retrieved_annotation.app_id == app.id
assert retrieved_annotation.question == annotation_args["question"]
assert retrieved_annotation.content == annotation_args["answer"]
assert retrieved_annotation.account_id == account.id
def test_batch_import_app_annotations_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful batch import of app annotations.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create CSV content
csv_content = "Question 1,Answer 1\nQuestion 2,Answer 2\nQuestion 3,Answer 3"
# Mock FileStorage
from io import BytesIO
from werkzeug.datastructures import FileStorage
file_storage = FileStorage(
stream=BytesIO(csv_content.encode("utf-8")), filename="annotations.csv", content_type="text/csv"
)
mock_external_service_dependencies["feature_service"].get_features.return_value.billing.enabled = False
# Mock pandas to return expected DataFrame
import pandas as pd
with patch("services.annotation_service.pd") as mock_pd:
mock_df = pd.DataFrame(
{0: ["Question 1", "Question 2", "Question 3"], 1: ["Answer 1", "Answer 2", "Answer 3"]}
)
mock_pd.read_csv.return_value = mock_df
# Batch import annotations
result = AppAnnotationService.batch_import_app_annotations(app.id, file_storage)
# Verify result structure
assert "job_id" in result
assert "job_status" in result
assert result["job_status"] == "waiting"
assert result["job_id"] is not None
# Verify task was called
mock_external_service_dependencies["batch_import_task"].delay.assert_called_once()
def test_batch_import_app_annotations_empty_file(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test batch import with empty CSV file.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create empty CSV content
csv_content = ""
# Mock FileStorage
from io import BytesIO
from werkzeug.datastructures import FileStorage
file_storage = FileStorage(
stream=BytesIO(csv_content.encode("utf-8")), filename="annotations.csv", content_type="text/csv"
)
# Mock pandas to return empty DataFrame
import pandas as pd
with patch("services.annotation_service.pd") as mock_pd:
mock_df = pd.DataFrame()
mock_pd.read_csv.return_value = mock_df
# Batch import annotations
result = AppAnnotationService.batch_import_app_annotations(app.id, file_storage)
# Verify error result
assert "error_msg" in result
assert "empty" in result["error_msg"].lower()
def test_batch_import_app_annotations_quota_exceeded(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test batch import when quota is exceeded.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create CSV content
csv_content = "Question 1,Answer 1\nQuestion 2,Answer 2\nQuestion 3,Answer 3"
# Mock FileStorage
from io import BytesIO
from werkzeug.datastructures import FileStorage
file_storage = FileStorage(
stream=BytesIO(csv_content.encode("utf-8")), filename="annotations.csv", content_type="text/csv"
)
# Mock pandas to return DataFrame
import pandas as pd
with patch("services.annotation_service.pd") as mock_pd:
mock_df = pd.DataFrame(
{0: ["Question 1", "Question 2", "Question 3"], 1: ["Answer 1", "Answer 2", "Answer 3"]}
)
mock_pd.read_csv.return_value = mock_df
# Mock FeatureService to return billing enabled with quota exceeded
mock_external_service_dependencies["feature_service"].get_features.return_value.billing.enabled = True
mock_external_service_dependencies[
"feature_service"
].get_features.return_value.annotation_quota_limit.limit = 1
mock_external_service_dependencies[
"feature_service"
].get_features.return_value.annotation_quota_limit.size = 0
# Batch import annotations
result = AppAnnotationService.batch_import_app_annotations(app.id, file_storage)
# Verify error result
assert "error_msg" in result
assert "limit" in result["error_msg"].lower()
def test_get_app_annotation_setting_by_app_id_enabled(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test getting enabled app annotation setting by app ID.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create annotation setting
from extensions.ext_database import db
from models.dataset import DatasetCollectionBinding
from models.model import AppAnnotationSetting
# Create a collection binding first
collection_binding = DatasetCollectionBinding(
provider_name="openai",
model_name="text-embedding-ada-002",
type="annotation",
collection_name=f"annotation_collection_{fake.uuid4()}",
)
collection_binding.id = str(fake.uuid4())
db.session.add(collection_binding)
db.session.flush()
# Create annotation setting
annotation_setting = AppAnnotationSetting(
app_id=app.id,
score_threshold=0.8,
collection_binding_id=collection_binding.id,
created_user_id=account.id,
updated_user_id=account.id,
)
db.session.add(annotation_setting)
db.session.commit()
# Get annotation setting
result = AppAnnotationService.get_app_annotation_setting_by_app_id(app.id)
# Verify result structure
assert result["enabled"] is True
assert result["id"] == annotation_setting.id
assert result["score_threshold"] == 0.8
assert result["embedding_model"]["embedding_provider_name"] == "openai"
assert result["embedding_model"]["embedding_model_name"] == "text-embedding-ada-002"
def test_get_app_annotation_setting_by_app_id_disabled(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test getting disabled app annotation setting by app ID.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Get annotation setting (no setting exists)
result = AppAnnotationService.get_app_annotation_setting_by_app_id(app.id)
# Verify result structure
assert result["enabled"] is False
def test_update_app_annotation_setting_success(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test successful update of app annotation setting.
"""
fake = Faker()
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Create annotation setting first
from extensions.ext_database import db
from models.dataset import DatasetCollectionBinding
from models.model import AppAnnotationSetting
# Create a collection binding first
collection_binding = DatasetCollectionBinding(
provider_name="openai",
model_name="text-embedding-ada-002",
type="annotation",
collection_name=f"annotation_collection_{fake.uuid4()}",