-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunittest_dynamic_database.py
2807 lines (2402 loc) · 125 KB
/
unittest_dynamic_database.py
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 all the necessary modules
import math
from typing import Union
import unittest
import datetime
from pathlib import Path
from dynamic_database import DynamicDatabase, Repository, Theorem, AnnotatedTactic, Annotation, PremiseFile, Premise
from lean_dojo.data_extraction.lean import Pos, LeanGitRepo
import generate_benchmark_lean4
import lean_dojo
import json
import shutil
import random
from loguru import logger
from unittest.mock import Mock, patch
from dynamic_database import DynamicDatabase, Repository, Theorem, AnnotatedTactic
from prover.proof_search import Status, SearchResult
from dynamic_database import parse_pos
from typing import Tuple
import os
from unittest.mock import patch, MagicMock
RAID_DIR = os.environ.get('RAID_DIR')
DATA_DIR = "datasets_new_unittest"
MERGED_DATA_DIR = "datasets_merged_unittest"
PROOF_LOG_FILE_NAME = "proof_logs_unittest/proof_log_unittest.log"
class TestDynamicDatabaseCore(unittest.TestCase):
"""
Unit tests for the DynamicDatabase class and related functionality.
This test suite covers the following aspects of the DynamicDatabase:
- Repository operations (add, update, get)
- Theorem operations (adding, retrieving, updating)
- Premise file operations
- JSON serialization and deserialization
- Repository properties (total_theorems, etc.)
- Special case handling (empty strings, None values)
- Theorem difficulty rating calculation and updating
- Converting sorry theorems to proven theorems
- Handling of duplicate repositories
- Repository equality
The tests use a combination of simple and complex test cases to verify that
all aspects of the database function correctly, including edge cases.
"""
def setUp(self):
self.db = DynamicDatabase()
self.repo = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()},
)
def assertDatetimeEqual(self, dt1, dt2):
"""
Assert that two datetimes are equal, ignoring microseconds.
This is important because serialization and deserialization of datetimes
may lose microsecond precision.
"""
self.assertEqual(dt1.replace(microsecond=0), dt2.replace(microsecond=0))
def test_parse_pos_bad_position(self):
with self.assertRaises(ValueError):
parse_pos("invalid_pos")
def test_get_update_theorem_in_repo(self):
theorem = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
self.repo.proven_theorems.append(theorem)
self.db.add_repository(self.repo)
retrieved_theorem = self.repo.get_theorem("test_theorem", "test.lean")
self.assertEqual(retrieved_theorem, theorem)
updated_theorem = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement="Updated statement"
)
self.repo.update_theorem(updated_theorem)
retrieved_theorem = self.repo.get_theorem("test_theorem", "test.lean")
self.assertEqual(retrieved_theorem.theorem_statement, "Updated statement")
non_existent_theorem = Theorem(
full_name="non_existent",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
with self.assertRaises(ValueError):
self.repo.update_theorem(non_existent_theorem)
def test_get_premise_file_in_repo(self):
premise_file = PremiseFile(
path=Path("test.lean"),
imports=[],
premises=[]
)
self.repo.premise_files.append(premise_file)
self.db.add_repository(self.repo)
retrieved_premise_file = self.repo.get_premise_file("test.lean")
self.assertEqual(retrieved_premise_file, premise_file)
def test_get_file_traced(self):
self.repo.files_traced.append(Path("test.lean"))
self.db.add_repository(self.repo)
retrieved_file = self.repo.get_file_traced("test.lean")
self.assertEqual(retrieved_file, Path("test.lean"))
def test_update_pr_url(self):
self.repo.pr_url = "https://github.com/test/repo/pull/1"
self.db.add_repository(self.repo)
self.repo.pr_url = "https://github.com/test/repo/pull/2"
self.db.update_repository(self.repo)
updated_repo = self.db.get_repository(self.repo.url, self.repo.commit)
self.assertEqual(updated_repo.pr_url, "https://github.com/test/repo/pull/2")
def test_difficulty_rating_in_theorem(self):
theorem = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
difficulty_rating=0.7
)
self.repo.proven_theorems.append(theorem)
self.db.add_repository(self.repo)
retrieved_theorem = self.repo.get_theorem("test_theorem", "test.lean")
self.assertEqual(retrieved_theorem.difficulty_rating, 0.7)
def test_validation_in_from_dict(self):
with self.assertRaises(ValueError):
DynamicDatabase.from_dict({})
with self.assertRaises(ValueError):
Repository.from_dict({})
with self.assertRaises(ValueError):
Theorem.from_dict({}, "url", "commit")
with self.assertRaises(ValueError):
PremiseFile.from_dict({})
with self.assertRaises(ValueError):
Premise.from_dict({})
with self.assertRaises(ValueError):
Annotation.from_dict({})
with self.assertRaises(ValueError):
AnnotatedTactic.from_dict({})
def test_empty_path_to_data_in_from_dict_repository(self):
data = {
"url": "https://github.com/test/repo",
"name": "Test Repo",
"commit": "abc123",
"lean_version": "3.50.3",
"lean_dojo_version": "1.8.4",
"metadata": {"date_processed": datetime.datetime.now().isoformat()},
"theorems_folder": "",
"premise_files_corpus": "",
"files_traced": ""
}
with self.assertRaises(ValueError):
Repository.from_dict(data)
def test_to_dict_for_all(self):
theorem = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
theorem_dict = theorem.to_dict()
self.assertIsInstance(theorem_dict, dict)
self.assertEqual(theorem_dict["full_name"], "test_theorem")
premise_file = PremiseFile(
path=Path("test.lean"),
imports=[],
premises=[]
)
premise_file_dict = premise_file.to_dict()
self.assertIsInstance(premise_file_dict, dict)
self.assertEqual(premise_file_dict["path"], "test.lean")
repo_dict = self.repo.to_dict()
self.assertIsInstance(repo_dict, dict)
self.assertEqual(repo_dict["url"], "https://github.com/test/repo")
annotation = Annotation(
full_name="test_annotation",
def_path="test/path.lean",
def_pos=Pos(1, 1),
def_end_pos=Pos(2, 1)
)
annotation_dict = annotation.to_dict()
self.assertIsInstance(annotation_dict, dict)
self.assertEqual(annotation_dict["full_name"], "test_annotation")
self.assertEqual(annotation_dict["def_path"], "test/path.lean")
self.assertEqual(annotation_dict["def_pos"], "(1, 1)")
self.assertEqual(annotation_dict["def_end_pos"], "(2, 1)")
annotated_tactic = AnnotatedTactic(
tactic="test_tactic",
annotated_tactic=("test_tactic", [annotation]),
state_before="test_state_before",
state_after="test_state_after"
)
annotated_tactic_dict = annotated_tactic.to_dict()
self.assertIsInstance(annotated_tactic_dict, dict)
self.assertEqual(annotated_tactic_dict["tactic"], "test_tactic")
self.assertEqual(annotated_tactic_dict["state_before"], "test_state_before")
self.assertEqual(annotated_tactic_dict["state_after"], "test_state_after")
premise = Premise(
full_name="test_premise",
code="test_code",
start=Pos(1, 1),
end=Pos(2, 1),
kind="theorem"
)
premise_dict = premise.to_dict()
self.assertIsInstance(premise_dict, dict)
self.assertEqual(premise_dict["full_name"], "test_premise")
self.assertEqual(premise_dict["code"], "test_code")
self.assertEqual(premise_dict["start"], "(1, 1)")
self.assertEqual(premise_dict["end"], "(2, 1)")
self.assertEqual(premise_dict["kind"], "theorem")
def test_empty_string_and_none_json_serialization(self):
empty_theorem = Theorem(
full_name="",
file_path=Path(""),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement=None,
difficulty_rating=None
)
self.repo.proven_theorems.append(empty_theorem)
self.db.add_repository(self.repo)
json_file = "empty_none_test.json"
self.db.to_json(json_file)
loaded_db = DynamicDatabase.from_json(json_file)
loaded_repo = loaded_db.get_repository(self.repo.url, self.repo.commit)
loaded_theorem = loaded_repo.proven_theorems[-1]
self.assertEqual(loaded_theorem.full_name, "")
self.assertEqual(str(loaded_theorem.file_path), ".")
self.assertEqual(loaded_theorem.url, "https://github.com/test/repo")
self.assertEqual(loaded_theorem.commit, "abc123")
self.assertIsNone(loaded_theorem.theorem_statement)
self.assertIsNone(loaded_theorem.difficulty_rating)
def test_complex_json_serialization(self):
theorem1 = Theorem(
full_name="theorem1",
file_path=Path("test1.lean"),
start=Pos(1, 1),
end=Pos(10, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement="theorem1 : 2 + 2 = 4",
traced_tactics=[
AnnotatedTactic(
tactic="rw [add_comm]",
annotated_tactic=("rw [add_comm]", [
Annotation(
full_name="add_comm",
def_path="src/add_comm.lean",
def_pos=Pos(5, 1),
def_end_pos=Pos(7, 1)
)
]),
state_before="⊢ 2 + 2 = 4",
state_after="⊢ 2 + 2 = 4"
)
],
difficulty_rating=0.7
)
theorem2 = Theorem(
full_name="theorem2",
file_path=Path("test2.lean"),
start=Pos(1, 1),
end=Pos(5, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement="theorem2 : ∀ x y : ℕ, x + y = y + x",
traced_tactics=[],
difficulty_rating=None
)
premise_file = PremiseFile(
path=Path("premise.lean"),
imports=["import data.nat.basic"],
premises=[
Premise(
full_name="nat_add_comm",
code="theorem nat_add_comm : ∀ a b : ℕ, a + b = b + a := sorry",
start=Pos(1, 1),
end=Pos(1, 60),
kind="theorem"
)
]
)
complex_repo = Repository(
url="https://github.com/test/complex-repo",
name="Complex Test Repo",
commit="complex123",
lean_version="4.0.0",
lean_dojo_version="1.0.0",
metadata={
"date_processed": datetime.datetime.now(),
"extra_info": {"key1": "value1", "key2": 2}
},
proven_theorems=[theorem1],
sorry_theorems_unproved=[theorem2],
premise_files=[premise_file],
files_traced=[Path("test1.lean"), Path("test2.lean")],
pr_url="https://github.com/test/complex-repo/pull/1"
)
self.db.add_repository(complex_repo)
json_file = "complex_test_database.json"
self.db.to_json(json_file)
loaded_db = DynamicDatabase.from_json(json_file)
self.assertEqual(len(loaded_db.repositories), len(self.db.repositories))
loaded_repo = loaded_db.get_repository("https://github.com/test/complex-repo", "complex123")
self.assertIsNotNone(loaded_repo)
self.assertEqual(loaded_repo.name, "Complex Test Repo")
self.assertEqual(loaded_repo.lean_version, "4.0.0")
self.assertEqual(loaded_repo.pr_url, "https://github.com/test/complex-repo/pull/1")
# Check theorems
self.assertEqual(len(loaded_repo.proven_theorems), 1)
self.assertEqual(len(loaded_repo.sorry_theorems_unproved), 1)
loaded_theorem1 = loaded_repo.proven_theorems[0]
self.assertEqual(loaded_theorem1.full_name, "theorem1")
self.assertEqual(loaded_theorem1.theorem_statement, "theorem1 : 2 + 2 = 4")
self.assertEqual(len(loaded_theorem1.traced_tactics), 1)
self.assertEqual(loaded_theorem1.difficulty_rating, 0.7)
loaded_theorem2 = loaded_repo.sorry_theorems_unproved[0]
self.assertEqual(loaded_theorem2.full_name, "theorem2")
self.assertIsNone(loaded_theorem2.difficulty_rating)
# Check premise files
self.assertEqual(len(loaded_repo.premise_files), 1)
loaded_premise_file = loaded_repo.premise_files[0]
self.assertEqual(str(loaded_premise_file.path), "premise.lean")
self.assertEqual(len(loaded_premise_file.premises), 1)
# Check metadata
self.assertIn("extra_info", loaded_repo.metadata)
self.assertEqual(loaded_repo.metadata["extra_info"]["key1"], "value1")
self.assertEqual(loaded_repo.metadata["extra_info"]["key2"], 2)
# Check files traced
self.assertEqual(len(loaded_repo.files_traced), 2)
self.assertIn(Path("test1.lean"), loaded_repo.files_traced)
self.assertIn(Path("test2.lean"), loaded_repo.files_traced)
def test_is_same_theorem(self):
theorem1 = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
theorem2 = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
theorem3 = Theorem(
full_name="other_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
self.assertTrue(theorem1.is_same_theorem(theorem2))
self.assertFalse(theorem1.is_same_theorem(theorem3))
def test_repository_properties(self):
theorem1 = Theorem(
full_name="test_theorem1",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
theorem2 = Theorem(
full_name="test_theorem2",
file_path=Path("test.lean"),
start=Pos(3, 1),
end=Pos(4, 1),
url="https://github.com/test/repo",
commit="abc123"
)
self.repo.proven_theorems.append(theorem1)
self.repo.sorry_theorems_unproved.append(theorem2)
self.assertEqual(self.repo.total_theorems, 2)
self.assertEqual(self.repo.num_proven_theorems, 1)
self.assertEqual(self.repo.num_sorry_theorems, 1)
self.assertEqual(self.repo.num_sorry_theorems_unproved, 1)
self.assertEqual(self.repo.num_sorry_theorems_proved, 0)
def test_get_all_theorems(self):
theorem1 = Theorem(
full_name="test_theorem1",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
theorem2 = Theorem(
full_name="test_theorem2",
file_path=Path("test.lean"),
start=Pos(3, 1),
end=Pos(4, 1),
url="https://github.com/test/repo",
commit="abc123"
)
self.repo.proven_theorems.append(theorem1)
self.repo.sorry_theorems_unproved.append(theorem2)
all_theorems = self.repo.get_all_theorems
self.assertEqual(len(all_theorems), 2)
self.assertIn(theorem1, all_theorems)
self.assertIn(theorem2, all_theorems)
def test_empty_repository(self):
empty_repo = Repository(
url="https://github.com/empty/repo",
name="Empty Repo",
commit="empty123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
self.db.add_repository(empty_repo)
self.assertEqual(empty_repo.total_theorems, 0)
self.assertEqual(empty_repo.num_proven_theorems, 0)
self.assertEqual(empty_repo.num_sorry_theorems, 0)
self.assertEqual(len(empty_repo.premise_files), 0)
self.assertEqual(len(empty_repo.files_traced), 0)
def test_theorem_with_empty_traced_tactics(self):
theorem = Theorem(
full_name="term_style_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
traced_tactics=[]
)
self.repo.proven_theorems.append(theorem)
self.db.add_repository(self.repo)
retrieved_theorem = self.repo.get_theorem("term_style_theorem", "test.lean")
self.assertEqual(len(retrieved_theorem.traced_tactics), 0)
def test_none_values(self):
theorem = Theorem(
full_name="none_value_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement=None,
difficulty_rating=None
)
self.repo.proven_theorems.append(theorem)
self.repo.pr_url = None
self.db.add_repository(self.repo)
retrieved_theorem = self.repo.get_theorem("none_value_theorem", "test.lean")
self.assertIsNone(retrieved_theorem.theorem_statement)
self.assertIsNone(retrieved_theorem.difficulty_rating)
self.assertIsNone(self.repo.pr_url)
def test_empty_strings(self):
theorem = Theorem(
full_name="",
file_path=Path(""),
start=Pos(1, 1),
end=Pos(2, 1),
url="",
commit=""
)
self.repo.proven_theorems.append(theorem)
self.db.add_repository(self.repo)
retrieved_theorem = self.repo.get_theorem("", "")
self.assertEqual(retrieved_theorem.full_name, "")
self.assertEqual(str(retrieved_theorem.file_path), ".")
self.assertEqual(retrieved_theorem.url, "")
self.assertEqual(retrieved_theorem.commit, "")
theorem2 = Theorem(
full_name="",
file_path=Path(""),
start=Pos(1, 1),
end=Pos(2, 1),
url="new_url",
commit=""
)
self.repo.update_theorem(theorem2)
retrieved_theorem = self.repo.get_theorem("", "") # Should be theorem2
self.assertEqual(retrieved_theorem.full_name, "")
self.assertEqual(str(retrieved_theorem.file_path), ".")
self.assertEqual(retrieved_theorem.url, "new_url")
self.assertEqual(retrieved_theorem.commit, "")
def test_datetime_serialization(self):
original_date = datetime.datetime.now()
self.repo.metadata["date_processed"] = original_date
self.db.add_repository(self.repo)
json_file = "test_datetime.json"
self.db.to_json(json_file)
loaded_db = DynamicDatabase.from_json(json_file)
loaded_repo = loaded_db.get_repository(self.repo.url, self.repo.commit)
self.assertDatetimeEqual(original_date, loaded_repo.metadata["date_processed"])
def test_duplicate_url_different_commit(self):
repo1 = Repository(
url="https://github.com/test/repo",
name="Test Repo 1",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
repo2 = Repository(
url="https://github.com/test/repo",
name="Test Repo 2",
commit="def456",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now() + datetime.timedelta(days=1)}
)
# Add a theorem to both repositories
common_theorem = Theorem(
full_name="common_theorem",
file_path=Path("common.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
theorem_statement="Old version"
)
repo1.proven_theorems.append(common_theorem)
updated_common_theorem = Theorem(
full_name="common_theorem",
file_path=Path("common.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="def456",
theorem_statement="New version"
)
repo2.proven_theorems.append(updated_common_theorem)
# Add unique theorems to each repository
repo1.proven_theorems.append(Theorem(
full_name="unique_to_repo1",
file_path=Path("repo1.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
))
repo2.proven_theorems.append(Theorem(
full_name="unique_to_repo2",
file_path=Path("repo2.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="def456"
))
self.db.add_repository(repo1)
self.db.add_repository(repo2)
self.assertEqual(len(self.db.repositories), 2)
dst_dir = Path(RAID_DIR) / DATA_DIR / "test_duplicate_url"
self.db.generate_merged_dataset(dst_dir)
with open(dst_dir / "random" / "train.json", 'r') as f:
data = json.load(f)
# Check that both repositories are represented
self.assertEqual(len(data), 3)
# Check that the common theorem is from the most recent repository
common_theorem_in_dataset = next(t for t in data if t["full_name"] == "common_theorem")
self.assertEqual(common_theorem_in_dataset["theorem_statement"], "New version")
self.assertEqual(common_theorem_in_dataset["commit"], "def456")
# Check that unique theorems from both repositories are present
self.assertTrue(any(t["full_name"] == "unique_to_repo1" for t in data))
self.assertTrue(any(t["full_name"] == "unique_to_repo2" for t in data))
with open(dst_dir / "metadata.json", 'r') as f:
metadata = json.load(f)
self.assertEqual(len(metadata["repositories"]), 2)
self.assertTrue(any(repo["commit"] == "abc123" for repo in metadata["repositories"]))
self.assertTrue(any(repo["commit"] == "def456" for repo in metadata["repositories"]))
def test_change_sorry_to_proven(self):
theorem = Theorem(
full_name="sorry_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123"
)
self.repo.sorry_theorems_unproved.append(theorem)
self.db.add_repository(self.repo)
self.repo.change_sorry_to_proven(theorem, PROOF_LOG_FILE_NAME)
self.assertEqual(len(self.repo.sorry_theorems_unproved), 0)
self.assertEqual(len(self.repo.sorry_theorems_proved), 1)
self.assertEqual(self.repo.sorry_theorems_proved[0].full_name, "sorry_theorem")
not_found_theorem = Theorem(
full_name="not_found_theorem",
file_path=Path("test.lean"),
start=Pos(3, 1),
end=Pos(4, 1),
url="https://github.com/test/repo",
commit="abc123"
)
with self.assertRaises(ValueError):
self.repo.change_sorry_to_proven(not_found_theorem, PROOF_LOG_FILE_NAME)
with self.assertRaises(ValueError):
self.repo.change_sorry_to_proven(theorem, PROOF_LOG_FILE_NAME)
def test_add_repository_duplicate(self):
repo = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()},
)
# Add the repository for the first time
self.db.add_repository(repo)
self.assertEqual(len(self.db.repositories), 1)
# Try to add the same repository again
self.db.add_repository(repo)
self.assertEqual(len(self.db.repositories), 1, "Repository should not be added twice")
# Verify that the repository details are unchanged
added_repo = self.db.get_repository("https://github.com/test/repo", "abc123")
self.assertIsNotNone(added_repo)
self.assertEqual(added_repo.name, "Test Repo")
self.assertEqual(added_repo.commit, "abc123")
def test_repository_equality(self):
repo1 = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
repo2 = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
repo3 = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="def456",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
self.assertEqual(repo1, repo2)
self.assertNotEqual(repo1, repo3)
def test_add_repository_duplicate(self):
repo = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
self.db.add_repository(repo)
self.assertEqual(len(self.db.repositories), 1)
self.db.add_repository(repo)
self.assertEqual(len(self.db.repositories), 1)
def test_update_repository_duplicate(self):
repo = Repository(
url="https://github.com/test/repo",
name="Test Repo",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()}
)
self.db.add_repository(repo)
self.assertEqual(len(self.db.repositories), 1)
self.db.update_repository(repo)
added_repo = self.db.get_repository("https://github.com/test/repo", "abc123")
self.assertEqual(len(self.db.repositories), 1)
self.assertEqual(added_repo.name, "Test Repo")
self.assertEqual(added_repo.commit, "abc123")
self.assertEqual(added_repo.lean_version, "3.50.3")
added_repo.name = "Updated Repo"
added_repo.lean_version = "3.50.4"
self.db.update_repository(added_repo)
updated_repo = self.db.get_repository("https://github.com/test/repo", "abc123")
self.assertEqual(len(self.db.repositories), 1)
self.assertEqual(updated_repo.name, "Updated Repo")
self.assertEqual(updated_repo.commit, "abc123")
self.assertEqual(added_repo.lean_version, "3.50.4")
self.db.update_repository(added_repo)
updated_repo = self.db.get_repository("https://github.com/test/repo", "abc123")
self.assertEqual(len(self.db.repositories), 1)
self.assertEqual(updated_repo.name, "Updated Repo")
self.assertEqual(updated_repo.commit, "abc123")
self.assertEqual(added_repo.lean_version, "3.50.4")
def test_update_theorem_difficulty(self):
theorem = Theorem(
full_name="test_theorem",
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
difficulty_rating=None
)
self.repo.proven_theorems.append(theorem)
self.db.add_repository(self.repo)
# Calculate and update difficulty
difficulty = 10
theorem.difficulty_rating = difficulty
# Verify the difficulty has been updated
updated_theorem = self.repo.get_theorem("test_theorem", "test.lean")
self.assertIsNotNone(updated_theorem.difficulty_rating)
self.assertEqual(updated_theorem.difficulty_rating, difficulty)
# Test updating difficulty of an existing theorem
new_difficulty = 0.8
theorem.difficulty_rating = new_difficulty
updated_theorem = self.repo.get_theorem("test_theorem", "test.lean")
self.assertEqual(updated_theorem.difficulty_rating, new_difficulty)
self.db.update_repository(self.repo)
json_file = "theorem_difficulty_test.json"
self.db.to_json(json_file)
# Read the JSON file and verify its contents
loaded_db = DynamicDatabase.from_json(json_file)
loaded_repo = loaded_db.get_repository("https://github.com/test/repo", "abc123")
self.assertIsNotNone(loaded_repo)
loaded_theorem = loaded_repo.get_theorem("test_theorem", "test.lean")
self.assertIsNotNone(loaded_theorem)
def create_theorem(self, name, tactics):
return Theorem(
full_name=name,
file_path=Path("test.lean"),
start=Pos(1, 1),
end=Pos(2, 1),
url="https://github.com/test/repo",
commit="abc123",
traced_tactics=tactics
)
def _calculate_difficulty(self, theorem: Theorem) -> Union[float, None]:
proof_steps = theorem.traced_tactics
if any('sorry' in step.tactic for step in proof_steps):
return float('inf') # Hard (no proof)
if len(proof_steps) == 0:
return None # To be distributed later
return math.exp(len(proof_steps))
def test_calculate_and_update_difficulty(self):
# Test case 1: Theorem with 'sorry'
sorry_theorem = self.create_theorem("sorry_theorem", [
AnnotatedTactic(tactic="sorry", annotated_tactic=("sorry", []), state_before="", state_after="")
])
self.repo.sorry_theorems_unproved.append(sorry_theorem)
# Test case 2: Theorem with no tactics
empty_theorem = self.create_theorem("empty_theorem", [])
self.repo.proven_theorems.append(empty_theorem)
# Test case 3: Theorem with proven sorry
normal_theorem = self.create_theorem("proven_sorry_theorem", [
AnnotatedTactic(tactic="tactic1", annotated_tactic=("tactic1", []), state_before="", state_after=""),
AnnotatedTactic(tactic="tactic2", annotated_tactic=("tactic2", []), state_before="", state_after="")
])
self.repo.proven_theorems.append(normal_theorem)
# Test case 4: Theorem with normal teactics
normal_theorem = self.create_theorem("normal_theorem", [
AnnotatedTactic(tactic="tactic1", annotated_tactic=("tactic1", []), state_before="before", state_after="no goals"),
AnnotatedTactic(tactic="tactic2", annotated_tactic=("tactic2", []), state_before="before2", state_after="no goals")
])
self.repo.proven_theorems.append(normal_theorem)
self.db.add_repository(self.repo)
json_file = "theorem_difficulty_test.json"
self.db.to_json(json_file)
for theorem in self.repo.get_all_theorems:
difficulty = self._calculate_difficulty(theorem)
theorem.difficulty_rating = difficulty
self.db.update_repository(self.repo)
sorry_theorem = self.repo.get_theorem("sorry_theorem", "test.lean")
self.assertEqual(sorry_theorem.difficulty_rating, float('inf'))
empty_theorem = self.repo.get_theorem("empty_theorem", "test.lean")
self.assertIsNone(empty_theorem.difficulty_rating)
normal_theorem = self.repo.get_theorem("normal_theorem", "test.lean")
self.assertEqual(normal_theorem.difficulty_rating, math.exp(2))
# Test JSON serialization and deserialization
self.db.to_json(json_file)
loaded_db = DynamicDatabase.from_json(json_file)
loaded_repo = loaded_db.get_repository("https://github.com/test/repo", "abc123")
self.assertIsNotNone(loaded_repo)
loaded_sorry_theorem = loaded_repo.get_theorem("sorry_theorem", "test.lean")
self.assertEqual(loaded_sorry_theorem.difficulty_rating, float('inf'))
loaded_empty_theorem = loaded_repo.get_theorem("empty_theorem", "test.lean")
self.assertIsNone(loaded_empty_theorem.difficulty_rating)
loaded_normal_theorem = loaded_repo.get_theorem("normal_theorem", "test.lean")
self.assertEqual(loaded_normal_theorem.difficulty_rating, math.exp(2))
class TestDynamicDatabaseSimpleLean(unittest.TestCase):
def setUp(self):
self.db = DynamicDatabase()
self.simple_lean_repo = self.create_simple_lean_repo()
self.db.add_repository(self.simple_lean_repo)
def create_simple_lean_repo(self):
url = "https://github.com/Adarsh321123/SimpleLean"
commit = "99a5078e1614e61f0d9cc234ca246c8744a4e660"
lean_git_repo = LeanGitRepo(url, commit)
dir_name = url.split("/")[-1].replace('.git', '') + "_" + commit
dst_dir = RAID_DIR + "/" + DATA_DIR + "/" + dir_name + "_updated"
config = lean_git_repo.get_config("lean-toolchain")
v = generate_benchmark_lean4.get_lean4_version_from_config(config["content"])
data = {
"url": lean_git_repo.url,
"name": "/".join(lean_git_repo.url.split("/")[-2:]),
"commit": lean_git_repo.commit,
"lean_version": v,
"lean_dojo_version": lean_dojo.__version__,
"metadata": {
"date_processed": datetime.datetime.now(),
},
"theorems_folder": dst_dir + "/random",
"premise_files_corpus": dst_dir + "/corpus.jsonl",
"files_traced": dst_dir + "/traced_files.jsonl",
}
repo = Repository.from_dict(data)
return repo
def test_empty_repo(self):
self.assertEqual(len(self.simple_lean_repo.proven_theorems), 0)
self.assertEqual(len(self.simple_lean_repo.sorry_theorems_proved), 0)
self.assertEqual(len(self.simple_lean_repo.sorry_theorems_unproved), 0)
def test_generate_dataset_with_empty_repo(self):
dst_dir = Path(RAID_DIR) / DATA_DIR / "simple_lean_generated"
self.db.generate_merged_dataset(dst_dir)
self.assertTrue(dst_dir.exists())
self.assertTrue((dst_dir / "random").exists())
self.assertTrue((dst_dir / "novel_premises").exists())
for split in ['train', 'val', 'test']:
with open(dst_dir / "random" / f"{split}.json", 'r') as f:
data = json.load(f)
self.assertEqual(len(data), 0)
class TestDynamicDatabaseUnicode(unittest.TestCase):
""""
Unit test class for testing Unicode handling in the DynamicDatabase class.
This test suite focuses on verifying that the DynamicDatabase correctly handles
Unicode characters during serialization and deserialization operations, and
that Unicode content can be properly manipulated within the database.
The tests include:
1. Serializing and deserializing a database with Unicode content
2. Modifying a theorem with Unicode content and verifying the changes persist
The test database includes:
- A repository with a Unicode name
- Theorems with Unicode mathematical symbols (∀, ℕ, ℝ, etc.)
- Complex mathematical expressions using Unicode symbols
This ensures the database can correctly handle international character sets
and mathematical notation when saving to and loading from JSON files.
"""
def setUp(self):
self.db = DynamicDatabase()
self.unicode_repo = self.create_unicode_sample_repo()
self.db.add_repository(self.unicode_repo)
def assertDatetimeEqual(self, dt1, dt2):
"""
Assert that two datetimes are equal, ignoring microseconds.
This is important because serialization and deserialization of datetimes
may lose microsecond precision.
"""
self.assertEqual(dt1.replace(microsecond=0), dt2.replace(microsecond=0))
def create_unicode_sample_repo(self):
repo = Repository(
url="https://github.com/example/repo",
name="Example Repo with Unicode ユニコード",
commit="abc123",
lean_version="3.50.3",
lean_dojo_version="1.8.4",
metadata={"date_processed": datetime.datetime.now()},
)
theorem1 = Theorem(
full_name="example.commutative_addition",