ArvindSelvaraj commited on
Commit
34e8385
1 Parent(s): 00eb694

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +12 -20
backend.py CHANGED
@@ -1,4 +1,3 @@
1
- import json
2
  import csv
3
  import io
4
  import requests
@@ -64,7 +63,6 @@ def generate_testcases(user_story):
64
  if chunk.choices[0].delta.content is not None:
65
  test_cases_text += chunk.choices[0].delta.content
66
 
67
-
68
  # Ensure the entire response is captured before cleaning
69
  if test_cases_text.strip() == "":
70
  return [{"test_case": "No test cases generated or output was empty."}]
@@ -89,40 +87,34 @@ def generate_testcases(user_story):
89
  print(f"API request failed: {str(e)}")
90
  return []
91
 
92
- # Add options for multiple test case formats
93
- def export_test_cases(test_cases, format='json'):
94
  if not test_cases:
95
  return "No test cases to export."
96
 
97
  # Convert test cases (which are currently strings) into a structured format for CSV
98
- structured_test_cases = [{'Test Case': case} for case in test_cases]
99
 
100
- if format == 'json':
101
- # Improve JSON export to be line-by-line formatted
102
- return json.dumps(test_cases, indent=4, separators=(',', ': ')) # More readable format
103
- elif format == 'csv':
104
  if isinstance(test_cases, list) and isinstance(test_cases[0], dict):
105
  output = io.StringIO()
106
- csv_writer = csv.DictWriter(output, fieldnames=test_cases[0].keys(), quoting=csv.QUOTE_ALL)
107
  csv_writer.writeheader()
108
- csv_writer.writerows(test_cases)
109
  return output.getvalue()
110
  else:
111
  raise ValueError("Test cases must be a list of dictionaries for CSV export.")
112
 
113
- # 2. Save test cases as a downloadable file
114
- def save_test_cases_as_file(test_cases, format='json'):
115
  if not test_cases:
116
  return "No test cases to save."
117
 
118
- if format == 'json':
119
- with open('test_cases.json', 'w') as f:
120
- json.dump(test_cases, f)
121
- elif format == 'csv':
122
  with open('test_cases.csv', 'w', newline='') as file:
123
- dict_writer = csv.DictWriter(file, fieldnames=test_cases[0].keys())
124
  dict_writer.writeheader()
125
- dict_writer.writerows(test_cases)
126
  else:
127
  return f"Unsupported format: {format}"
128
- return f'{format} file saved'
 
 
1
  import csv
2
  import io
3
  import requests
 
63
  if chunk.choices[0].delta.content is not None:
64
  test_cases_text += chunk.choices[0].delta.content
65
 
 
66
  # Ensure the entire response is captured before cleaning
67
  if test_cases_text.strip() == "":
68
  return [{"test_case": "No test cases generated or output was empty."}]
 
87
  print(f"API request failed: {str(e)}")
88
  return []
89
 
90
+ # Export test cases in CSV format
91
+ def export_test_cases(test_cases, format='csv'):
92
  if not test_cases:
93
  return "No test cases to export."
94
 
95
  # Convert test cases (which are currently strings) into a structured format for CSV
96
+ structured_test_cases = [{'Test Case': case.get('test_case', case)} for case in test_cases]
97
 
98
+ if format == 'csv':
 
 
 
99
  if isinstance(test_cases, list) and isinstance(test_cases[0], dict):
100
  output = io.StringIO()
101
+ csv_writer = csv.DictWriter(output, fieldnames=structured_test_cases[0].keys(), quoting=csv.QUOTE_ALL)
102
  csv_writer.writeheader()
103
+ csv_writer.writerows(structured_test_cases)
104
  return output.getvalue()
105
  else:
106
  raise ValueError("Test cases must be a list of dictionaries for CSV export.")
107
 
108
+ # Save test cases as a CSV file
109
+ def save_test_cases_as_file(test_cases, format='csv'):
110
  if not test_cases:
111
  return "No test cases to save."
112
 
113
+ if format == 'csv':
 
 
 
114
  with open('test_cases.csv', 'w', newline='') as file:
115
+ dict_writer = csv.DictWriter(file, fieldnames=['Test Case'])
116
  dict_writer.writeheader()
117
+ dict_writer.writerows([{'Test Case': case.get('test_case', case)} for case in test_cases])
118
  else:
119
  return f"Unsupported format: {format}"
120
+ return f'{format} file saved'