Akash Singh 3 years ago
parent
commit
9f8346a43a

+ 8 - 3
icopy2xls/__init__.py

@@ -76,7 +76,7 @@ class Mover:
 
             end = self.lines  # if number of rows to be considered from source is not predefined the take all
             if not end:
-                for i in range(source.nrows):
+                for i in range(1, source.nrows):
                     end.append(i)
             new_data = [source.row(row) for row in end]  # create a new list of all data and remove the filtered data
             remove_data = []  # rows not matching filter are stored here which is used later to remove data from new_data
@@ -124,14 +124,19 @@ class Mover:
 
     def process_lines(self, lines):
         line_lis = []
+        if isinstance(lines, int):
+            if not lines < 1:
+                for i in range(1, lines+1):
+                    line_lis.append(i)
+            return line_lis
         lis = [x.strip() for x in lines.strip().split(',')]
         while "" in lis:
             lis.remove("")
         for l in lis:
             if "-" in l:
                 start = int(l.split("-")[0].strip())
-                end = int(l.split("-")[1].strip())
-                for i in range(int(start), int(end)+1):
+                end = int(l.split("-")[1].strip()) + 1
+                for i in range(int(start), int(end)):
                     if i not in line_lis:
                         line_lis.append(i)
             elif l.strip().isnumeric():

BIN
tests/TestInput/Financial Sample - Output.xlsx


BIN
tests/TestInput/Financial Sample.xlsx


BIN
tests/TestInput/Financial Sample_1.xlsx


+ 106 - 0
tests/test_icopy.py

@@ -0,0 +1,106 @@
+import os
+import sys
+
+myPath = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0, myPath + '/../')
+
+from icopy2xls import Mover
+
+input_file_1 = "tests/TestInput/Financial Sample.xlsx"
+input_file_2 = "tests/TestInput/Financial Sample_1.xlsx"
+input_sheet = "Sheet1"
+destination_file = "tests/TestInput/Financial Sample - Output.xlsx"
+output_file = "tests/TestOutput/Financial Sample - Result.xlsx"
+output_sheet = "Sheet1"
+
+if os.path.exists(output_file):
+    os.remove(output_file)
+
+with open(destination_file, 'rb')as file:
+    data = file.read()
+
+with open(output_file, 'wb')as file:
+    file.write(data)
+
+mover = Mover(
+    source_file_path=input_file_1,
+    source_sheet=input_sheet,
+    destination_file_path=output_file,
+    destination_sheet=output_sheet
+    )
+
+
+def test_basic():
+    mover.move()
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1401 and sht.ncols == 12
+
+
+def test_filters():
+    mover.move(filters={"Country": "Canada"})
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1541 and sht.ncols == 12
+
+
+def test_filters_multiple():
+    mover.move(filters={"Country": "Canada", "Segment": "Government"})
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1601 and sht.ncols == 12
+
+
+def test_filters_value_list():
+    mover.move(filters={"Country": ["Canada", "France"]})
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1881 and sht.ncols == 12
+
+
+def test_process_lines():
+    lines = "3,7-13,25,300-310,1"
+    processed = mover.process_lines(lines)
+    expected = [1, 3, 7, 8, 9, 10, 11, 12, 13, 25, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310]
+    assert len(processed) == 21 and sorted(processed) == expected
+
+
+def test_lines():
+    move = Mover(
+        source_file_path=input_file_1,
+        source_sheet=input_sheet,
+        destination_file_path=output_file,
+        destination_sheet=output_sheet,
+        lines="1-10"
+    )
+    move.move()
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1891 and sht.ncols == 12
+
+
+def test_add_missing():
+    move = Mover(
+        source_file_path=input_file_1,
+        source_sheet=input_sheet,
+        destination_file_path=output_file,
+        destination_sheet=output_sheet,
+        lines="1-10"
+    )
+    move.move(add_missing_columns=True)
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1901 and sht.ncols == 16
+
+
+def test_multiple_inputs():
+    move = Mover(
+        source_file_path=[input_file_1, input_file_2],
+        source_sheet=input_sheet,
+        destination_file_path=output_file,
+        destination_sheet=output_sheet,
+        lines="10,21-30"
+    )
+    move.move()
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1923 and sht.ncols == 16
+
+
+def test_read_xlsx():
+    sht = mover.read_xlsx(output_file, output_sheet)
+    assert sht.nrows == 1923 and sht.ncols == 16
+