ソースを参照

added test for file renamer

marton levente 8 ヶ月 前
コミット
3f8155c404
1 ファイル変更37 行追加11 行削除
  1. 37 11
      tests/test_anafoauth.py

+ 37 - 11
tests/test_anafoauth.py

@@ -3,6 +3,7 @@
 import platform
 import unittest
 import os
+from typing import Union
 
 from lxml import etree
 
@@ -101,20 +102,45 @@ class efactTest(unittest.TestCase):
         #         self.efactoauth.validate_invoice(os.path.join(root, file))
         self.efactoauth.validate_invoice('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
 
-    def test_renamer(self):
-        tree = etree.parse('c:/Users/levi/FACT1/MZK/2023_01_03/MZK2414.xml')
-        root = tree.getroot()
-        parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-        is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-        partie_id = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-        print(parties[1].text, is_date[0].text, partie_id[3].text)
-        # for partie in parties:
-        #     print(partie.getparent())
+    def test_renamer(self, dir_path, kind: Union[str, 'seller', 'buyer']):  # @UndefinedVariable
+        if kind == 'seller':
+            account = 'AccountingCustomerParty'
+        else:
+            account = 'AccountingSupplierParty'
+        for root_f, _dirs, files in os.walk(dir_path):
+            for file in files:
+                if file.endswith('.xml') and '_' not in file:
+                    # print(os.path.join(root, file))
+                    tree = etree.parse(os.path.join(root_f, file))
+                    root = tree.getroot()
+                    parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                    for partie in parties:
+                        parent_p_type = partie.getparent().getparent().getparent()
+                        if etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
+                            # print(partie.text)
+                            file_partie = partie.text
+
+                    is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                    is_date = is_date[0].text
+
+                    partie_ids = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                    for partie_id in partie_ids:
+                        parent_p_type = partie_id.getparent().getparent().getparent()
+                        parent_t_type = partie_id.getparent()
+                        # print(parent_t_type)
+                        if etree.QName(parent_t_type).localname == 'PartyTaxScheme' and etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
+                            # print(partie_id.text)
+                            file_id = partie_id.text
+                    new_name = '{}_{}_{}_{}.xml'.format(file.replace('.xml', ''), file_id, file_partie, is_date)
+                    current_path = os.path.join(root_f, file)
+                    print(new_name)
+                    new_path = os.path.join(root_f, new_name)
+                    os.rename(current_path, new_path)
 
 
 if __name__ == "__main__":
     # unittest.main()
     # efactTest().test_add_invoice()
-    efactTest().test_get_all_messages()
-    # efactTest().test_renamer()
+    # efactTest().test_get_all_messages()
+    efactTest().test_renamer('c:/Users/levi/git/anafapi/tests/17259191/', kind='seller')
     # anoauth.get_token()