Files
growstuff/spec/tasks/packets_needing_ocr_spec.rb
T
Brenda Wallace 1701f77723 Add OCR task for seed packet photos and related specs
- Introduced a Rake task `packets:needing_ocr` to list photos of seed packets that lack text, outputting results as JSON lines.
- Created a Python script `read_packets.py` to process the output of the Rake task, downloading images and performing OCR to extract text.
- Updated model specs for `Crop` to ensure proper handling of photos associated with different seeds.
- Refactored API request specs for `Photos` and `Seeds`, removing unnecessary attributes from the response.
- Added tests for the new Rake task to ensure correct functionality and output format.
2026-10-28 20:23:35 +13:00

61 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
require 'rake'
describe 'packets:needing_ocr' do
let(:seed) { create(:seed) }
let(:lines) { output.lines.map { |line| JSON.parse(line) } }
let(:output) { capture_task }
def capture_task
Rake::Task['packets:needing_ocr'].reenable
original = $stdout
$stdout = StringIO.new
Rake::Task['packets:needing_ocr'].invoke
$stdout.string
ensure
$stdout = original
end
before do
Rails.application.load_tasks unless Rake::Task.task_defined?('packets:needing_ocr')
end
context 'with a back and a front, added back first' do
let!(:back) { create(:photo, owner: seed.owner).tap { |photo| seed.photo_associations.create!(photo:, label: 'back') } }
let!(:front) { create(:photo, owner: seed.owner).tap { |photo| seed.photo_associations.create!(photo:, label: 'front') } }
it('lists the oldest photo first') { expect(lines.pluck('photo_id')).to eq [back.id.to_s, front.id.to_s] }
it('says which side each is') { expect(lines.pluck('label')).to eq %w(back front) }
it('says which seed') { expect(lines.first['seed_ids']).to eq [seed.id] }
it('gives the address of the image') { expect(lines.first['image_url']).to eq back.fullsize_url }
end
context 'with a photo that is not of the packet' do
before { seed.photo_associations.create!(photo: create(:photo, owner: seed.owner)) }
it('lists nothing') { expect(lines).to be_empty }
end
context 'with a packet photo whose seed already has its text' do
let(:seed) { create(:seed, packet_text: 'Sow by: December 2026') }
before { seed.photo_associations.create!(photo: create(:photo, owner: seed.owner), label: 'back') }
it('lists nothing') { expect(lines).to be_empty }
end
context 'with a limit' do
before do
2.times { seed.photo_associations.create!(photo: create(:photo, owner: seed.owner), label: nil) }
create(:photo, owner: seed.owner).tap { |photo| seed.photo_associations.create!(photo:, label: 'front') }
other = create(:seed, owner: seed.owner)
create(:photo, owner: seed.owner).tap { |photo| other.photo_associations.create!(photo:, label: 'back') }
stub_const('ENV', ENV.to_hash.merge('LIMIT' => '1'))
end
it('lists only that many') { expect(lines.size).to eq 1 }
end
end