Fix tests

This commit is contained in:
Juan José Mata
2026-02-11 19:01:58 +01:00
parent 0eedd533bb
commit 8276800859
2 changed files with 21 additions and 12 deletions

View File

@@ -10,11 +10,12 @@ class ProcessPdfJob < ApplicationJob
pdf_import.update!(status: :importing)
begin
pdf_import.process_with_ai
upload_to_vector_store(pdf_import)
process_result = pdf_import.process_with_ai
document_type = resolve_document_type(pdf_import, process_result)
upload_to_vector_store(pdf_import, document_type: document_type)
# For bank statements, extract transactions and generate import rows
if pdf_import.bank_statement?
if bank_statement_document?(document_type)
Rails.logger.info("ProcessPdfJob: Extracting transactions for bank statement import #{pdf_import.id}")
pdf_import.extract_transactions
Rails.logger.info("ProcessPdfJob: Extracted #{pdf_import.extracted_transactions.size} transactions")
@@ -33,7 +34,7 @@ class ProcessPdfJob < ApplicationJob
# Bank statements with rows go to pending for user review/publish
# Non-bank statements are marked complete (no further action needed)
final_status = pdf_import.bank_statement? && pdf_import.rows_count > 0 ? :pending : :complete
final_status = bank_statement_document?(document_type) && pdf_import.rows_count > 0 ? :pending : :complete
pdf_import.update!(status: final_status)
rescue StandardError => e
sanitized_error = sanitize_error_message(e)
@@ -60,18 +61,28 @@ class ProcessPdfJob < ApplicationJob
end
end
def upload_to_vector_store(pdf_import)
def upload_to_vector_store(pdf_import, document_type:)
filename = pdf_import.pdf_file.filename.to_s
file_content = pdf_import.pdf_file_content
family_document = pdf_import.family.upload_document(
file_content: file_content,
filename: filename,
metadata: { "type" => pdf_import.document_type }
metadata: { "type" => document_type }
)
return if family_document
Rails.logger.warn("ProcessPdfJob: Vector store upload failed for import #{pdf_import.id}")
end
def resolve_document_type(pdf_import, process_result)
return process_result.document_type if process_result.respond_to?(:document_type) && process_result.document_type.present?
pdf_import.reload.document_type
end
def bank_statement_document?(document_type)
document_type == "bank_statement"
end
end

View File

@@ -35,10 +35,9 @@ class ProcessPdfJobTest < ActiveJob::TestCase
test "uploads non-bank PDF to vector store with classified type metadata" do
pdf_content = attach_pdf!(@import)
process_result = Struct.new(:document_type).new("financial_document")
@import.stubs(:process_with_ai) do
@import.update!(ai_summary: "A tax return", document_type: "financial_document")
end
@import.expects(:process_with_ai).once.returns(process_result)
@import.stubs(:send_next_steps_email)
@import.expects(:extract_transactions).never
@@ -56,10 +55,9 @@ class ProcessPdfJobTest < ActiveJob::TestCase
test "uploads bank statement PDF to vector store with classified type metadata" do
pdf_content = attach_pdf!(@import)
process_result = Struct.new(:document_type).new("bank_statement")
@import.stubs(:process_with_ai) do
@import.update!(ai_summary: "A bank statement", document_type: "bank_statement")
end
@import.expects(:process_with_ai).once.returns(process_result)
@import.expects(:extract_transactions).once
@import.expects(:generate_rows_from_extracted_data).once do
@import.update_column(:rows_count, 1)