Skip to content

Commit 1664c0c

Browse files
committed
Adding script to test slicing issue.
1 parent a4665da commit 1664c0c

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

test_slicing_issue.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
This script tests the PyDomo SDK's data upload functionality using two different methods of slicing pandas DataFrames before uploading them as CSVs.
3+
4+
The first method, `test_slicing_issue_old_method`, uses the current implementation of thee `stream_upload` method.
5+
6+
The second method, `test_slicing_issue_new_method`, uses a new `stream_upload` method where the slicing issue has been fixed.
7+
8+
Each method tries to create and update a Domo dataset using a test DataFrame. If dataset creation or update fails, the test will fail. The output of the script will indicate whether each method succeeded or failed, and any errors encountered will be printed to the console.
9+
"""
10+
11+
import os
12+
import unittest
13+
import pandas as pd
14+
import math
15+
from pydomo import Domo
16+
from pydomo.utilities import UtilitiesClient
17+
18+
DOMO_CLIENT_ID = os.getenv('DOMO_CLIENT_ID')
19+
DOMO_CLIENT_SECRET = os.getenv('DOMO_CLIENT_SECRET')
20+
DOMO_API_HOST = 'api.domo.com'
21+
22+
def stream_upload_new(self, ds_id, df_up, warn_schema_change=True):
23+
domoSchema = self.domo_schema(ds_id)
24+
dataSchema = self.data_schema(df_up)
25+
26+
stream_id = self.get_stream_id(ds_id)
27+
28+
if self.identical(domoSchema, dataSchema) == False:
29+
new_schema = {'schema': {'columns': dataSchema}}
30+
url = '/v1/datasets/{ds}'.format(ds=ds_id)
31+
change_result = self.transport.put(url, new_schema)
32+
if warn_schema_change:
33+
print('Schema Updated')
34+
35+
exec_info = self.stream.create_execution(stream_id)
36+
exec_id = exec_info['id']
37+
38+
chunksz = self.estimate_chunk_rows(df_up)
39+
start = 0
40+
df_rows = len(df_up.index)
41+
end = df_rows
42+
if df_rows > chunksz:
43+
end = chunksz
44+
45+
for i in range(math.ceil(df_rows / chunksz)):
46+
df_sub = df_up.iloc[start:end] # Removed the comma here
47+
csv = df_sub.to_csv(header=False, index=False)
48+
self.stream.upload_part(stream_id, exec_id, start, csv)
49+
start = end
50+
end = end + chunksz
51+
if end > df_rows:
52+
end = df_rows
53+
54+
result = self.stream.commit_execution(stream_id, exec_id)
55+
return result
56+
57+
# Patch the UtilitiesClient class stream_upload method
58+
UtilitiesClient.stream_upload_new = stream_upload_new
59+
60+
class TestSlicingIssue(unittest.TestCase):
61+
62+
def setUp(self):
63+
self.domo = Domo(client_id=DOMO_CLIENT_ID, client_secret=DOMO_CLIENT_SECRET, api_host=DOMO_API_HOST)
64+
data = {
65+
'col1': [1, 2, 3, None],
66+
'col2': ['a', 'b', '', None],
67+
'col3': [1.1, 2.2, 3.3, None],
68+
'col4': [True, False, True, None],
69+
'col5': ['x', '', 'z', 'a'],
70+
'col6': [6, None, 8, 9],
71+
'col7': [None, None, None, None],
72+
'col8': ['', '', '', ''],
73+
'col9': ['test1', 'test2', 'test3', 'test4'],
74+
'col10': [10, 20, 30, 40],
75+
'col11': [None, 'b', None, 'd'],
76+
'col12': [12.1, None, 12.3, 12.4],
77+
'col13': [True, True, False, False],
78+
'col14': ['', 'b', 'c', 'd'],
79+
'col15': [15, 15, 15, 15],
80+
'col16': [None, None, None, None],
81+
'col17': ['a', 'b', 'c', 'd'],
82+
'col18': [18, 19, 20, 21],
83+
'col19': [None, 'y', None, 'z'],
84+
'col20': [20, 21, 22, 23]
85+
}
86+
self.df = pd.DataFrame(data)
87+
88+
def test_slicing_issue_old_method(self):
89+
print("Testing with current stream_upload method...")
90+
try:
91+
dataset_old = self.domo.ds_create(self.df,'TEST_SLICING_ISSUE_OLD', 'Test dataset for slicing issue in PyDomo SDK (old method)')
92+
self.domo.ds_update(dataset_old, self.df)
93+
print("Old method succeeded.")
94+
except Exception as e:
95+
self.fail(f"Old method failed with error: {e}")
96+
97+
def test_slicing_issue_new_method(self):
98+
print("Testing revised stream_upload method...")
99+
try:
100+
self.domo.utilities.stream_upload = self.domo.utilities.stream_upload_new
101+
dataset_new = self.domo.ds_create(self.df,'TEST_SLICING_ISSUE_NEW', 'Test dataset for slicing issue in PyDomo SDK (new method)')
102+
self.domo.ds_update(dataset_new, self.df)
103+
print("New method succeeded.")
104+
except Exception as e:
105+
self.fail(f"New method failed with error: {e}")
106+
107+
if __name__ == '__main__':
108+
unittest.main()

0 commit comments

Comments
 (0)