Spaces:
Paused
Paused
Create tests/test_document_processor.py
Browse files- tests/test_document_processor.py +126 -0
tests/test_document_processor.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import unittest
|
4 |
+
from datetime import datetime
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# إضافة المسار الرئيسي للمشروع إلى PATH
|
8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
9 |
+
|
10 |
+
# استيراد الوحدات المراد اختبارها
|
11 |
+
from modules.document_processor import DocumentProcessor
|
12 |
+
|
13 |
+
class TestDocumentProcessor(unittest.TestCase):
|
14 |
+
"""
|
15 |
+
اختبارات وحدة لمعالج المستندات
|
16 |
+
"""
|
17 |
+
|
18 |
+
def setUp(self):
|
19 |
+
"""
|
20 |
+
إعداد بيئة الاختبار
|
21 |
+
"""
|
22 |
+
self.document_processor = DocumentProcessor()
|
23 |
+
|
24 |
+
# إنشاء ملفات اختبار مؤقتة
|
25 |
+
self.temp_files = {}
|
26 |
+
|
27 |
+
# إنشاء ملف نصي للاختبار
|
28 |
+
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as tmp:
|
29 |
+
tmp.write("""
|
30 |
+
مناقصة لإنشاء مبنى إداري
|
31 |
+
|
32 |
+
المتطلبات:
|
33 |
+
1. يجب أن تكون الشركة مصنفة في مجال المباني الدرجة الثانية على الأقل
|
34 |
+
2. خبرة لا تقل عن 10 سنوات في مجال إنشاء المباني الإدارية
|
35 |
+
3. تنفيذ 3 مشاريع مماثلة خلال الخمس سنوات الماضية
|
36 |
+
|
37 |
+
المحتوى المحلي:
|
38 |
+
يجب ألا تقل نسبة المحتوى المحلي عن 40% من إجمالي قيمة المشروع
|
39 |
+
|
40 |
+
التكلفة التقديرية: 15,000,000 ريال سعودي
|
41 |
+
|
42 |
+
المدة: 24 شهر
|
43 |
+
|
44 |
+
تاريخ البدء: 01/01/2024
|
45 |
+
""".encode('utf-8'))
|
46 |
+
self.temp_files["txt"] = tmp.name
|
47 |
+
|
48 |
+
def tearDown(self):
|
49 |
+
"""
|
50 |
+
تنظيف بيئة الاختبار
|
51 |
+
"""
|
52 |
+
# حذف الملفات المؤقتة
|
53 |
+
for file_path in self.temp_files.values():
|
54 |
+
if os.path.exists(file_path):
|
55 |
+
os.remove(file_path)
|
56 |
+
|
57 |
+
def test_process_txt_document(self):
|
58 |
+
"""
|
59 |
+
اختبار معالجة ملف نصي
|
60 |
+
"""
|
61 |
+
# قراءة محتوى الملف
|
62 |
+
with open(self.temp_files["txt"], 'rb') as f:
|
63 |
+
file_content = f.read()
|
64 |
+
|
65 |
+
# معالجة الملف
|
66 |
+
result = self.document_processor.process_document(
|
67 |
+
file_content,
|
68 |
+
"txt",
|
69 |
+
"test_document.txt"
|
70 |
+
)
|
71 |
+
|
72 |
+
# التحقق من النتائج
|
73 |
+
self.assertIn("text", result)
|
74 |
+
self.assertIn("file_name", result)
|
75 |
+
self.assertEqual(result["file_name"], "test_document.txt")
|
76 |
+
self.assertEqual(result["file_type"], "txt")
|
77 |
+
|
78 |
+
# التحقق من استخراج النص
|
79 |
+
self.assertIn("مناقصة لإنشاء مبنى إداري", result["text"])
|
80 |
+
|
81 |
+
# التحقق من استخراج المتطلبات
|
82 |
+
self.assertIn("requirements", result)
|
83 |
+
requirements = result.get("requirements", [])
|
84 |
+
self.assertGreaterEqual(len(requirements), 1)
|
85 |
+
|
86 |
+
# التحقق من استخراج المحتوى المحلي
|
87 |
+
self.assertIn("local_content", result)
|
88 |
+
local_content = result.get("local_content", {})
|
89 |
+
self.assertIn("percentages", local_content)
|
90 |
+
|
91 |
+
# التحقق من استخراج البيانات المالية
|
92 |
+
self.assertIn("financial_data", result)
|
93 |
+
financial_data = result.get("financial_data", {})
|
94 |
+
self.assertIn("total_cost", financial_data)
|
95 |
+
|
96 |
+
# التحقق من استخراج التواريخ
|
97 |
+
self.assertIn("dates", result)
|
98 |
+
dates = result.get("dates", [])
|
99 |
+
self.assertGreaterEqual(len(dates), 1)
|
100 |
+
|
101 |
+
def test_extract_requirements(self):
|
102 |
+
"""
|
103 |
+
اختبار استخراج المتطلبات
|
104 |
+
"""
|
105 |
+
test_text = """
|
106 |
+
المتطلبات الفنية:
|
107 |
+
1. يجب أن يكون المقاول مصنفاً في مجال المباني الدرجة الأولى
|
108 |
+
2. خبرة لا تقل عن 15 سنة
|
109 |
+
|
110 |
+
المتطلبات المالية:
|
111 |
+
1. ملاءة مالية لا تقل عن 10 مليون ريال
|
112 |
+
"""
|
113 |
+
|
114 |
+
requirements = self.document_processor._extract_requirements(test_text)
|
115 |
+
|
116 |
+
# التحقق من النتائج
|
117 |
+
self.assertGreaterEqual(len(requirements), 2)
|
118 |
+
|
119 |
+
# التحقق من تصنيف المتطلبات
|
120 |
+
categories = set(req["category"] for req in requirements)
|
121 |
+
self.assertIn("فنية", categories)
|
122 |
+
|
123 |
+
def test_extract_local_content_info(self):
|
124 |
+
"""
|
125 |
+
اختبار استخراج معلومات المحتوى المحلي
|
126 |
+
"""
|