-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtest_select_utils.py
More file actions
1044 lines (879 loc) · 33.5 KB
/
test_select_utils.py
File metadata and controls
1044 lines (879 loc) · 33.5 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
import pytest
from cumulusci.tasks.bulkdata.select_utils import (
OPTIONAL_DEPENDENCIES_AVAILABLE,
SelectOperationExecutor,
SelectStrategy,
add_limit_offset_to_user_filter,
annoy_post_process,
calculate_levenshtein_distance,
determine_field_types,
find_closest_record,
levenshtein_distance,
reorder_records,
split_and_filter_fields,
vectorize_records,
)
# Check for pandas availability
try:
import pandas as pd
PANDAS_AVAILABLE = True
except ImportError:
PANDAS_AVAILABLE = False
def test_standard_generate_query_without_filter():
select_operator = SelectOperationExecutor(SelectStrategy.STANDARD)
sobject = "Contact" # Assuming no declaration for this object
limit = 3
offset = None
query, fields = select_operator.select_generate_query(
sobject=sobject, fields=[], user_filter="", limit=limit, offset=offset
)
assert f"LIMIT {limit}" in query
assert "OFFSET" not in query
assert fields == ["Id"]
def test_standard_generate_query_with_user_filter():
select_operator = SelectOperationExecutor(SelectStrategy.STANDARD)
sobject = "Contact" # Assuming no declaration for this object
limit = 3
offset = None
user_filter = "WHERE Name IN ('Sample Contact')"
query, fields = select_operator.select_generate_query(
sobject=sobject, fields=[], user_filter=user_filter, limit=limit, offset=offset
)
assert "WHERE" in query
assert "Sample Contact" in query
assert "LIMIT" in query
assert "OFFSET" not in query
assert fields == ["Id"]
def test_random_generate_query():
select_operator = SelectOperationExecutor(SelectStrategy.RANDOM)
sobject = "Contact" # Assuming no declaration for this object
limit = 3
offset = None
query, fields = select_operator.select_generate_query(
sobject=sobject, fields=[], user_filter="", limit=limit, offset=offset
)
assert f"LIMIT {limit}" in query
assert "OFFSET" not in query
assert fields == ["Id"]
# Test Cases for standard_post_process
def test_standard_post_process_with_records():
select_operator = SelectOperationExecutor(SelectStrategy.STANDARD)
records = [["001"], ["002"], ["003"]]
num_records = 3
sobject = "Contact"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[],
fields=[],
threshold=None,
)
assert error_message is None
assert len(selected_records) == num_records
assert all(record["success"] for record in selected_records)
assert all(record["created"] is False for record in selected_records)
assert all(record["id"] in ["001", "002", "003"] for record in selected_records)
def test_standard_post_process_with_fewer_records():
select_operator = SelectOperationExecutor(SelectStrategy.STANDARD)
records = [["001"]]
num_records = 3
sobject = "Opportunity"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[],
fields=[],
threshold=None,
)
assert error_message is None
assert len(selected_records) == num_records
assert all(record["success"] for record in selected_records)
assert all(record["created"] is False for record in selected_records)
# Check if records are repeated to match num_records
assert selected_records.count({"id": "001", "success": True, "created": False}) == 3
def test_standard_post_process_with_no_records():
select_operator = SelectOperationExecutor(SelectStrategy.STANDARD)
records = []
num_records = 2
sobject = "Lead"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[],
fields=[],
threshold=None,
)
assert selected_records == []
assert error_message == f"No records found for {sobject} in the target org."
# Test cases for Random Post Process
def test_random_post_process_with_records():
select_operator = SelectOperationExecutor(SelectStrategy.RANDOM)
records = [["001"], ["002"], ["003"]]
num_records = 3
sobject = "Contact"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[],
fields=[],
threshold=None,
)
assert error_message is None
assert len(selected_records) == num_records
assert all(record["success"] for record in selected_records)
assert all(record["created"] is False for record in selected_records)
def test_random_post_process_with_no_records():
select_operator = SelectOperationExecutor(SelectStrategy.RANDOM)
records = []
num_records = 2
sobject = "Lead"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[],
fields=[],
threshold=None,
)
assert selected_records == []
assert error_message == f"No records found for {sobject} in the target org."
def test_similarity_generate_query_no_nesting():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
sobject = "Contact" # Assuming no declaration for this object
limit = 3
offset = None
query, fields = select_operator.select_generate_query(
sobject, ["Name"], [], limit, offset
)
assert fields == ["Id", "Name"]
assert f"LIMIT {limit}" in query
assert "OFFSET" not in query
def test_similarity_generate_query_with_nested_fields():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
sobject = "Event" # Assuming no declaration for this object
limit = 3
offset = None
fields = [
"Subject",
"Who.Contact.Name",
"Who.Contact.Email",
"Who.Lead.Name",
"Who.Lead.Company",
]
query, query_fields = select_operator.select_generate_query(
sobject, fields, [], limit, offset
)
assert "WHERE" not in query # No WHERE clause should be present
assert query_fields == [
"Id",
"Subject",
"Who.Contact.Name",
"Who.Contact.Email",
"Who.Lead.Name",
"Who.Lead.Company",
]
assert f"LIMIT {limit}" in query
assert "TYPEOF Who" in query
assert "WHEN Contact" in query
assert "WHEN Lead" in query
assert "OFFSET" not in query
def test_random_generate_query_with_user_filter():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
sobject = "Contact" # Assuming no declaration for this object
limit = 3
offset = None
user_filter = "WHERE Name IN ('Sample Contact')"
query, fields = select_operator.select_generate_query(
sobject=sobject,
fields=["Name"],
user_filter=user_filter,
limit=limit,
offset=offset,
)
assert "WHERE" in query
assert "Sample Contact" in query
assert "LIMIT" in query
assert "OFFSET" not in query
assert fields == ["Id", "Name"]
def test_levenshtein_distance():
assert levenshtein_distance("kitten", "kitten") == 0 # Identical strings
assert levenshtein_distance("kitten", "sitten") == 1 # One substitution
assert levenshtein_distance("kitten", "kitte") == 1 # One deletion
assert levenshtein_distance("kitten", "sittin") == 2 # Two substitutions
assert levenshtein_distance("kitten", "dog") == 6 # Completely different strings
assert levenshtein_distance("kitten", "") == 6 # One string is empty
assert levenshtein_distance("", "") == 0 # Both strings are empty
assert levenshtein_distance("Kitten", "kitten") == 1 # Case sensitivity
assert levenshtein_distance("kit ten", "kitten") == 1 # Strings with spaces
assert (
levenshtein_distance("levenshtein", "meilenstein") == 4
) # Longer strings with multiple differences
def test_find_closest_record_different_weights():
load_record = ["hello", "world"]
query_records = [
["record1", "hello", "word"], # Levenshtein distance = 1
["record2", "hullo", "word"], # Levenshtein distance = 1
["record3", "hello", "word"], # Levenshtein distance = 1
]
weights = [2.0, 0.5]
# With different weights, the first field will have more impact
closest_record, _ = find_closest_record(load_record, query_records, weights)
assert closest_record == [
"record1",
"hello",
"word",
], "The closest record should be 'record1'."
def test_find_closest_record_basic():
load_record = ["hello", "world"]
query_records = [
["record1", "hello", "word"], # Levenshtein distance = 1
["record2", "hullo", "word"], # Levenshtein distance = 1
["record3", "hello", "word"], # Levenshtein distance = 1
]
weights = [1.0, 1.0]
closest_record, _ = find_closest_record(load_record, query_records, weights)
assert closest_record == [
"record1",
"hello",
"word",
], "The closest record should be 'record1'."
def test_find_closest_record_multiple_matches():
load_record = ["cat", "dog"]
query_records = [
["record1", "bat", "dog"], # Levenshtein distance = 1
["record2", "cat", "dog"], # Levenshtein distance = 0
["record3", "dog", "cat"], # Levenshtein distance = 3
]
weights = [1.0, 1.0]
closest_record, _ = find_closest_record(load_record, query_records, weights)
assert closest_record == [
"record2",
"cat",
"dog",
], "The closest record should be 'record2'."
def test_similarity_post_process_with_records():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
num_records = 1
sobject = "Contact"
load_records = [["Tom Cruise", "62", "Actor"]]
query_records = [
["001", "Bob Hanks", "62", "Actor"],
["002", "Tom Cruise", "63", "Actor"], # Slight difference
["003", "Jennifer Aniston", "30", "Actress"],
]
weights = [1.0, 1.0, 1.0] # Adjust weights to match your data structure
selected_records, _, error_message = select_operator.select_post_process(
load_records=load_records,
query_records=query_records,
num_records=num_records,
sobject=sobject,
weights=weights,
fields=["Name", "Age", "Occupation"],
threshold=None,
)
assert error_message is None
assert len(selected_records) == num_records
assert all(record["success"] for record in selected_records)
assert all(record["created"] is False for record in selected_records)
x = [record["id"] for record in selected_records]
print(x)
assert all(record["id"] in ["002"] for record in selected_records)
def test_similarity_post_process_with_no_records():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
records = []
num_records = 2
sobject = "Lead"
selected_records, _, error_message = select_operator.select_post_process(
load_records=None,
query_records=records,
num_records=num_records,
sobject=sobject,
weights=[1, 1, 1],
fields=[],
threshold=None,
)
assert selected_records == []
assert error_message == f"No records found for {sobject} in the target org."
def test_similarity_post_process_with_no_records__zero_threshold():
select_operator = SelectOperationExecutor(SelectStrategy.SIMILARITY)
load_records = [["Aditya", "Salesforce"], ["Jawad", "Salesforce"]]
query_records = []
num_records = 2
sobject = "Lead"
(
selected_records,
insert_records,
error_message,
) = select_operator.select_post_process(
load_records=load_records,
query_records=query_records,
num_records=num_records,
sobject=sobject,
weights=[1, 1, 1],
fields=["LastName", "Company"],
threshold=0,
)
# Assert that it inserts everything
assert selected_records == [None, None]
assert insert_records[0] == ["Aditya", "Salesforce"]
assert insert_records[1] == ["Jawad", "Salesforce"]
assert error_message is None
def test_calculate_levenshtein_distance_basic():
record1 = ["hello", "world"]
record2 = ["hullo", "word"]
weights = [1.0, 1.0]
# Expected distance based on simple Levenshtein distances
# Levenshtein("hello", "hullo") = 1, Levenshtein("world", "word") = 1
expected_distance = (1 / 5 * 1.0 + 1 / 5 * 1.0) / 2 # Averaged over two fields
result = calculate_levenshtein_distance(record1, record2, weights)
assert result == pytest.approx(
expected_distance
), "Basic distance calculation failed."
# Empty fields
record1 = ["hello", ""]
record2 = ["hullo", ""]
weights = [1.0, 1.0]
# Expected distance based on simple Levenshtein distances
# Levenshtein("hello", "hullo") = 1, Levenshtein("", "") = 0
expected_distance = (1 / 5 * 1.0 + 0 * 1.0) / 2 # Averaged over two fields
result = calculate_levenshtein_distance(record1, record2, weights)
assert result == pytest.approx(
expected_distance
), "Basic distance calculation with empty fields failed."
# Partial empty fields
record1 = ["hello", "world"]
record2 = ["hullo", ""]
weights = [1.0, 1.0]
# Expected distance based on simple Levenshtein distances
# Levenshtein("hello", "hullo") = 1, Levenshtein("world", "") = 5
expected_distance = (
1 / 5 * 1.0 + 5 / 5 * 0.05 * 1.0
) / 2 # Averaged over two fields
result = calculate_levenshtein_distance(record1, record2, weights)
assert result == pytest.approx(
expected_distance
), "Basic distance calculation with partial empty fields failed."
def test_calculate_levenshtein_distance_weighted():
record1 = ["cat", "dog"]
record2 = ["bat", "fog"]
weights = [2.0, 0.5]
# Levenshtein("cat", "bat") = 1, Levenshtein("dog", "fog") = 1
expected_distance = (
1 / 3 * 2.0 + 1 / 3 * 0.5
) / 2.5 # Weighted average over two fields
result = calculate_levenshtein_distance(record1, record2, weights)
assert result == pytest.approx(
expected_distance
), "Weighted distance calculation failed."
def test_calculate_levenshtein_distance_records_length_doesnt_match():
record1 = ["cat", "dog", "cow"]
record2 = ["bat", "fog"]
weights = [2.0, 0.5]
with pytest.raises(ValueError) as e:
calculate_levenshtein_distance(record1, record2, weights)
assert "Records must have the same number of fields." in str(e.value)
def test_calculate_levenshtein_distance_weights_length_doesnt_match():
record1 = ["cat", "dog"]
record2 = ["bat", "fog"]
weights = [2.0, 0.5, 3.0]
with pytest.raises(ValueError) as e:
calculate_levenshtein_distance(record1, record2, weights)
assert "Records must be same size as fields (weights)." in str(e.value)
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_all_numeric_columns():
df_db = pd.DataFrame({"A": ["1", "2", "3"], "B": ["4.5", " 5.5", "6.5"]})
df_query = pd.DataFrame({"A": ["4", "5", ""], "B": ["4.5", "5.5", "6.5"]})
weights = [0.1, 0.2]
expected_output = (
["A", "B"], # numerical_features
[], # boolean_features
[], # categorical_features
[0.1, 0.2], # numerical_weights
[], # boolean_weights
[], # categorical_weights
)
assert determine_field_types(df_db, df_query, weights) == expected_output
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_numeric_columns__one_non_numeric():
df_db = pd.DataFrame({"A": ["1", "2", "3"], "B": ["4.5", "5.5", "6.5"]})
df_query = pd.DataFrame({"A": ["4", "5", "6"], "B": ["abcd", "5.5", "6.5"]})
weights = [0.1, 0.2]
expected_output = (
["A"], # numerical_features
[], # boolean_features
["B"], # categorical_features
[0.1], # numerical_weights
[], # boolean_weights
[0.2], # categorical_weights
)
assert determine_field_types(df_db, df_query, weights) == expected_output
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_all_boolean_columns():
df_db = pd.DataFrame(
{"A": ["true", "false", "true"], "B": ["false", "true", "false"]}
)
df_query = pd.DataFrame(
{"A": ["true", "false", "true"], "B": ["false", "true", "false"]}
)
weights = [0.3, 0.4]
expected_output = (
[], # numerical_features
["A", "B"], # boolean_features
[], # categorical_features
[], # numerical_weights
[0.3, 0.4], # boolean_weights
[], # categorical_weights
)
assert determine_field_types(df_db, df_query, weights) == expected_output
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_all_categorical_columns():
df_db = pd.DataFrame(
{"A": ["apple", "banana", "cherry"], "B": ["dog", "cat", "mouse"]}
)
df_query = pd.DataFrame(
{"A": ["banana", "apple", "cherry"], "B": ["cat", "dog", "mouse"]}
)
weights = [0.5, 0.6]
expected_output = (
[], # numerical_features
[], # boolean_features
["A", "B"], # categorical_features
[], # numerical_weights
[], # boolean_weights
[0.5, 0.6], # categorical_weights
)
assert determine_field_types(df_db, df_query, weights) == expected_output
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_mixed_types():
df_db = pd.DataFrame(
{
"A": ["1", "2", "3"],
"B": ["true", "false", "true"],
"C": ["apple", "banana", "cherry"],
}
)
df_query = pd.DataFrame(
{
"A": ["1", "3", ""],
"B": ["true", "true", "true"],
"C": ["apple", "", "3"],
}
)
weights = [0.7, 0.8, 0.9]
expected_output = (
["A"], # numerical_features
["B"], # boolean_features
["C"], # categorical_features
[0.7], # numerical_weights
[0.8], # boolean_weights
[0.9], # categorical_weights
)
assert determine_field_types(df_db, df_query, weights) == expected_output
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_vectorize_records_mixed_numerical_boolean_categorical():
# Test data with mixed types: numerical and categorical only
db_records = [["1.0", "true", "apple"], ["2.0", "false", "banana"]]
query_records = [["1.5", "true", "apple"], ["2.5", "false", "cherry"]]
weights = [1.0, 1.0, 1.0] # Equal weights for numerical and categorical columns
hash_features = 4 # Number of hashing vectorizer features for categorical columns
final_db_vectors, final_query_vectors = vectorize_records(
db_records, query_records, hash_features, weights
)
# Check the shape of the output vectors
assert final_db_vectors.shape[0] == len(db_records), "DB vectors row count mismatch"
assert final_query_vectors.shape[0] == len(
query_records
), "Query vectors row count mismatch"
# Expected dimensions: numerical (1) + categorical hashed features (4)
expected_feature_count = 2 + hash_features
assert (
final_db_vectors.shape[1] == expected_feature_count
), "DB vectors column count mismatch"
assert (
final_query_vectors.shape[1] == expected_feature_count
), "Query vectors column count mismatch"
def _build_large_annoy_fixture():
"""Build a dataset that forces the ANN path (load*query > 1000)."""
load_records = [["Alice", "Engineer"], ["Bob", "Doctor"]]
query_records = [["q1", "Alice", "Engineer"], ["q2", "Charlie", "Artist"]]
# Add many exact-match records so tests exercise realistic ANN usage.
for i in range(35):
name = f"Employee-{i}"
role = f"Role-{i % 7}"
load_records.append([name, role])
query_records.append([f"q-extra-{i}", name, role])
assert len(load_records) * len(query_records) > 1000
return load_records, query_records
def _build_large_annoy_fixture_polymorphic():
"""Polymorphic-field variant of the large ANN fixture."""
load_records = [
["Alice", "Engineer", "Alice_Contact", "abcd1234"],
["Bob", "Doctor", "Bob_Contact", "qwer1234"],
]
query_records = [
["q1", "Alice", "Engineer", "Alice_Contact"],
["q2", "Charlie", "Artist", "Charlie_Contact"],
]
for i in range(35):
name = f"Employee-{i}"
role = f"Role-{i % 7}"
contact_name = f"Contact-{i}"
contact_id = f"id-{i:04d}"
load_records.append([name, role, contact_name, contact_id])
query_records.append([f"q-extra-{i}", name, role, contact_name])
assert len(load_records) * len(query_records) > 1000
return load_records, query_records
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_annoy_post_process():
# Test data
load_records, query_records = _build_large_annoy_fixture()
weights = [1.0, 1.0, 1.0] # Example weights
closest_records, insert_records = annoy_post_process(
load_records=load_records,
query_records=query_records,
similarity_weights=weights,
all_fields=["Name", "Occupation"],
threshold=None,
)
# Assert ANN output shape and that all load records were matched.
assert len(closest_records) == len(load_records)
assert all(record and "id" in record for record in closest_records)
# No records should be marked for insert without a threshold.
assert not insert_records
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_annoy_post_process__insert_records():
# Test data
load_records, query_records = _build_large_annoy_fixture()
weights = [1.0, 1.0, 1.0] # Example weights
threshold = 0.3
closest_records, insert_records = annoy_post_process(
load_records=load_records,
query_records=query_records,
similarity_weights=weights,
all_fields=["Name", "Occupation"],
threshold=threshold,
)
# Assert threshold behavior without relying on ANN neighbor tie-break order.
assert len(closest_records) == len(load_records)
none_count = sum(record is None for record in closest_records)
assert none_count == len(insert_records)
assert all(candidate in load_records for candidate in insert_records)
def test_annoy_post_process__no_query_records():
# Test data
load_records = [["Alice", "Engineer"], ["Bob", "Doctor"]]
query_records = []
weights = [1.0, 1.0, 1.0] # Example weights
threshold = 0.3
closest_records, insert_records = annoy_post_process(
load_records=load_records,
query_records=query_records,
similarity_weights=weights,
all_fields=["Name", "Occupation"],
threshold=threshold,
)
# Assert the closest records
assert len(closest_records) == 2 # We expect two results (both None)
assert all(rec is None for rec in closest_records) # Both should be None
assert insert_records[0] == [
"Alice",
"Engineer",
] # The first insert record should match the second load record
assert insert_records[1] == [
"Bob",
"Doctor",
] # The first insert record should match the second load record
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_annoy_post_process__insert_records_with_polymorphic_fields():
# Test data
load_records, query_records = _build_large_annoy_fixture_polymorphic()
weights = [1.0, 1.0, 1.0, 1.0] # Example weights
threshold = 0.3
all_fields = ["Name", "Occupation", "Contact.Name", "ContactId"]
closest_records, insert_records = annoy_post_process(
load_records=load_records,
query_records=query_records,
similarity_weights=weights,
all_fields=all_fields,
threshold=threshold,
)
# Assert threshold behavior without relying on ANN neighbor tie-break order.
assert len(closest_records) == len(load_records)
none_count = sum(record is None for record in closest_records)
assert none_count == len(insert_records)
expected_insert_candidates = [
[name, occupation, contact_id]
for name, occupation, _, contact_id in load_records
]
assert all(candidate in expected_insert_candidates for candidate in insert_records)
@pytest.mark.skipif(
not PANDAS_AVAILABLE or not OPTIONAL_DEPENDENCIES_AVAILABLE,
reason="requires optional dependencies for annoy",
)
def test_single_record_match_annoy_post_process():
# Mock data where only the first query record matches the first load record
load_records = [["Alice", "Engineer"], ["Bob", "Doctor"]]
query_records = [["q1", "Alice", "Engineer"]]
weights = [1.0, 1.0, 1.0]
closest_records, insert_records = annoy_post_process(
load_records=load_records,
query_records=query_records,
similarity_weights=weights,
all_fields=["Name", "Occupation"],
threshold=None,
)
# Both the load records should be matched with the only query record we have
assert len(closest_records) == 2
assert closest_records[0]["id"] == "q1"
assert not insert_records
@pytest.mark.parametrize(
"filter_clause, limit_clause, offset_clause, expected",
[
# Test: No existing LIMIT/OFFSET and no new clauses
("SELECT * FROM users", None, None, " SELECT * FROM users"),
# Test: Existing LIMIT and no new limit provided
("SELECT * FROM users LIMIT 100", None, None, "SELECT * FROM users LIMIT 100"),
# Test: Existing OFFSET and no new offset provided
("SELECT * FROM users OFFSET 20", None, None, "SELECT * FROM users OFFSET 20"),
# Test: Existing LIMIT/OFFSET and new clauses provided
(
"SELECT * FROM users LIMIT 100 OFFSET 20",
50,
10,
"SELECT * FROM users LIMIT 50 OFFSET 30",
),
# Test: Existing LIMIT, new limit larger than existing (should keep the smaller one)
("SELECT * FROM users LIMIT 100", 150, None, "SELECT * FROM users LIMIT 100"),
# Test: New limit smaller than existing (should use the new one)
("SELECT * FROM users LIMIT 100", 50, None, "SELECT * FROM users LIMIT 50"),
# Test: Existing OFFSET, adding a new offset (should sum the offsets)
("SELECT * FROM users OFFSET 20", None, 30, "SELECT * FROM users OFFSET 50"),
# Test: Existing LIMIT/OFFSET and new values set to None
(
"SELECT * FROM users LIMIT 100 OFFSET 20",
None,
None,
"SELECT * FROM users LIMIT 100 OFFSET 20",
),
# Test: Removing existing LIMIT and adding a new one
("SELECT * FROM users LIMIT 200", 50, None, "SELECT * FROM users LIMIT 50"),
# Test: Removing existing OFFSET and adding a new one
("SELECT * FROM users OFFSET 40", None, 20, "SELECT * FROM users OFFSET 60"),
# Edge case: Filter clause with mixed cases
(
"SELECT * FROM users LiMiT 100 oFfSeT 20",
50,
10,
"SELECT * FROM users LIMIT 50 OFFSET 30",
),
# Test: Filter clause with trailing/leading spaces
(
" SELECT * FROM users LIMIT 100 OFFSET 20 ",
50,
10,
"SELECT * FROM users LIMIT 50 OFFSET 30",
),
],
)
def test_add_limit_offset_to_user_filter(
filter_clause, limit_clause, offset_clause, expected
):
result = add_limit_offset_to_user_filter(filter_clause, limit_clause, offset_clause)
assert result.strip() == expected.strip()
def test_reorder_records_basic_reordering():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["job", "name"]
expected = [
["Engineer", "Alice"],
["Designer", "Bob"],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_partial_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["age"]
expected = [
[30],
[25],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_missing_fields_in_new_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["nonexistent", "job"]
expected = [
["Engineer"],
["Designer"],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_empty_records():
records = []
original_fields = ["name", "age", "job"]
new_fields = ["job", "name"]
expected = []
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_empty_new_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = []
expected = [
[],
[],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_empty_original_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = []
new_fields = ["job", "name"]
with pytest.raises(KeyError):
reorder_records(records, original_fields, new_fields)
def test_reorder_records_no_common_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["nonexistent_field"]
expected = [
[],
[],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_duplicate_fields_in_new_fields():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["job", "job", "name"]
expected = [
["Engineer", "Engineer", "Alice"],
["Designer", "Designer", "Bob"],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_reorder_records_all_fields_in_order():
records = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
original_fields = ["name", "age", "job"]
new_fields = ["name", "age", "job"]
expected = [
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
]
result = reorder_records(records, original_fields, new_fields)
assert result == expected
def test_split_and_filter_fields_basic_case():
fields = [
"Account.Name",
"Account.Industry",
"Contact.Name",
"AccountId",
"ContactId",
"CreatedDate",
]
load_fields, select_fields = split_and_filter_fields(fields)
assert load_fields == ["AccountId", "ContactId", "CreatedDate"]
assert select_fields == [
"Account.Name",
"Account.Industry",
"Contact.Name",
"CreatedDate",
]
def test_split_and_filter_fields_all_non_lookup_fields():
fields = ["Name", "CreatedDate"]
load_fields, select_fields = split_and_filter_fields(fields)
assert load_fields == ["Name", "CreatedDate"]