def __func(): form = forms.ImportCaseForm(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_case_export_path if os.path.isfile(filename): try: with open(filename, 'r') as cases_file: cases_file = cases_file.read() cases_file = cases_file.replace('\n', '') cases = json.loads(cases_file) case_subscription.add_cases(cases) for case in cases.keys(): running_context.db.session.add( running_context.CaseSubscription(name=case)) running_context.CaseSubscription.update(case) running_context.db.session.commit() return { "cases": case_subscription.subscriptions_as_json() }, SUCCESS except (OSError, IOError) as e: current_app.logger.error( 'Error importing cases from file {0}: {1}'.format( filename, format_exception_message(e))) return {"error": "Error reading file."}, IO_ERROR except ValueError as e: current_app.logger.error( 'Error importing cases from file {0}: Invalid JSON {1}'. format(filename, format_exception_message(e))) return {"error": "Invalid JSON file."}, INVALID_INPUT_ERROR else: current_app.logger.debug( 'Cases successfully imported from {0}'.format(filename)) return {"error": "File does not exist."}, IO_ERROR
def __func(): data = request.get_json() filename = (data['filename'] if (data is not None and 'filename' in data and data['filename']) else core.config.paths.default_case_export_path) if os.path.isfile(filename): try: with open(filename, 'r') as cases_file: cases_file = cases_file.read() cases_file = cases_file.replace('\n', '') cases = json.loads(cases_file) case_subscription.add_cases(cases) for case in cases: db.session.add(running_context.CaseSubscription(name=case)) running_context.CaseSubscription.update(case) db.session.commit() return {"cases": case_subscription.subscriptions}, SUCCESS except (OSError, IOError) as e: current_app.logger.error('Error importing cases from file ' '{0}: {1}'.format( filename, format_exception_message(e))) return {"error": "Error reading file."}, IO_ERROR except ValueError as e: current_app.logger.error( 'Error importing cases from file {0}: ' 'Invalid JSON {1}'.format(filename, format_exception_message(e))) return {"error": "Invalid JSON file."}, INVALID_INPUT_ERROR else: current_app.logger.debug( 'Cases successfully imported from {0}'.format(filename)) return {"error": "File does not exist."}, IO_ERROR
def import_cases(): form = forms.ImportCaseForm(request.form) filename = form.filename.data if form.filename.data else core.config.paths.default_case_export_path if os.path.isfile(filename): try: with open(filename, 'r') as cases_file: cases_file = cases_file.read() cases_file = cases_file.replace('\n', '') cases = json.loads(cases_file) case_subscription.add_cases(cases) for case in cases.keys(): running_context.db.session.add( running_context.CaseSubscription(name=case)) running_context.CaseSubscription.update(case) running_context.db.session.commit() return json.dumps({ "status": "success", "cases": case_subscription.subscriptions_as_json() }) except (OSError, IOError): return json.dumps({"status": "error reading file"}) except ValueError: return json.dumps({"status": "file contains invalid JSON"}) else: return json.dumps({"status": "error: file does not exist"})
def test_delete_cases_some_existing(self): subs.set_subscriptions(self.cases1) subs.add_cases(self.cases2) subs.delete_cases(['case2', 'case3']) expected_cases = { 'case1': self.cases1['case1'], 'case4': self.cases2['case4'] } self.assertInMemoryCasesAreCorrect(expected_cases) self.assertDatabaseCasesAreCorrect(set(expected_cases.keys()))
def test_add_case_with_existing_cases_sets(self): subs.set_subscriptions(self.cases1) subs.add_cases(self.cases2) self.assertInMemoryCasesAreCorrect(self.cases1) self.assertDatabaseCasesAreCorrect( {'case1', 'case2', 'case3', 'case4'})
def test_add_case_no_existing_cases_sets(self): subs.add_cases(self.cases1) self.assertInMemoryCasesAreCorrect(self.cases1) self.assertDatabaseCasesAreCorrect({'case1', 'case2'})