Class: PromptEngine::TestCasesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/prompt_engine/test_cases_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 17

def create
  @test_case = @eval_set.test_cases.build(test_case_params)

  if @test_case.save
    redirect_to prompt_eval_set_path(@prompt, @eval_set), notice: "Test case was successfully created."
  else
    flash.now[:alert] = "Please fix the errors below."
    render :new
  end
end

#destroyObject



40
41
42
43
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 40

def destroy
  @test_case.destroy
  redirect_to prompt_eval_set_path(@prompt, @eval_set), notice: "Test case was successfully deleted."
end

#editObject



28
29
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 28

def edit
end

#importObject



45
46
47
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 45

def import
  # Display the import form
end

#import_createObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 84

def import_create
  imported_data = session[:imported_test_cases]

  unless imported_data.present?
    redirect_to import_prompt_eval_set_test_cases_path(@prompt, @eval_set),
      alert: "No import data found. Please upload a file again."
    return
  end

  success_count = 0
  errors = []

  imported_data.each_with_index do |data, index|
    test_case = @eval_set.test_cases.build(
      input_variables: data[:input_variables],
      expected_output: data[:expected_output],
      description: data[:description]
    )

    if test_case.save
      success_count += 1
    else
      errors << "Row #{index + 1}: #{test_case.errors.full_messages.join(", ")}"
    end
  end

  # Clear the session data
  session.delete(:imported_test_cases)

  if errors.any?
    flash[:alert] = "Import completed with errors: #{errors.join("; ")}"
  else
    flash[:notice] = "Successfully imported #{success_count} test cases."
  end

  redirect_to prompt_eval_set_path(@prompt, @eval_set)
end

#import_previewObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 49

def import_preview
  unless params[:file].present?
    redirect_to import_prompt_eval_set_test_cases_path(@prompt, @eval_set),
      alert: "Please select a file to import."
    return
  end

  @imported_data = []
  @errors = []

  begin
    file_content = params[:file].read
    file_type = detect_file_type(params[:file])

    if file_type == :csv
      parse_csv(file_content)
    elsif file_type == :json
      parse_json(file_content)
    else
      @errors << "Unsupported file format. Please upload a CSV or JSON file."
    end
  rescue => e
    @errors << "Error reading file: #{e.message}"
  end

  if @errors.any?
    flash.now[:alert] = @errors.join(", ")
    render :import
  else
    # Store the imported data in session for the create action
    session[:imported_test_cases] = @imported_data
    render :import_preview
  end
end

#newObject



9
10
11
12
13
14
15
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 9

def new
  @test_case = @eval_set.test_cases.build
  # Pre-populate with prompt's parameters
  @test_case.input_variables = @prompt.parameters.each_with_object({}) do |param, hash|
    hash[param.name] = param.default_value
  end
end

#updateObject



31
32
33
34
35
36
37
38
# File 'app/controllers/prompt_engine/test_cases_controller.rb', line 31

def update
  if @test_case.update(test_case_params)
    redirect_to prompt_eval_set_path(@prompt, @eval_set), notice: "Test case was successfully updated."
  else
    flash.now[:alert] = "Please fix the errors below."
    render :edit
  end
end