mirror of
https://github.com/danbee/my-images
synced 2025-03-04 08:49:05 +00:00
Refactor Clarifai specs
This commit is contained in:
parent
74284024c8
commit
59dd4645c4
@ -1,7 +1,11 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe "user manages images" do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
it "uploads the image" do
|
||||
stub_clarifai(%w[computer technology])
|
||||
|
||||
sign_in
|
||||
attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
||||
click_on("Create Image")
|
||||
@ -19,11 +23,18 @@ describe "user manages images" do
|
||||
expect(page).not_to have_css(".image")
|
||||
end
|
||||
|
||||
it "views the image" do
|
||||
user = User.create(uid: 1)
|
||||
Image.create(user: user, image: "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
||||
it "views the image with tags" do
|
||||
stub_clarifai(%w[computer technology])
|
||||
|
||||
sign_in
|
||||
find(".image").click
|
||||
attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
||||
perform_enqueued_jobs do
|
||||
click_on("Create Image")
|
||||
end
|
||||
page.find(".image").click
|
||||
|
||||
%w[computer technology].each do |tag|
|
||||
expect(page).to have_content(tag)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -9,11 +9,27 @@ describe Clarifai do
|
||||
|
||||
describe ".tags" do
|
||||
it "predicts tags for our image" do
|
||||
stub_clarifai(%w[computer technology])
|
||||
stub_api(%w[computer technology])
|
||||
|
||||
clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg")
|
||||
clarifai = Clarifai.new("spec/fixtures/spectrum.jpg")
|
||||
|
||||
expect(clarifai_image.tags).to eq(%w[computer technology])
|
||||
expect(clarifai.tags).to eq(%w[computer technology])
|
||||
end
|
||||
end
|
||||
|
||||
def stub_api(tags)
|
||||
WebMock.
|
||||
stub_request(:post, Clarifai::API_URL).
|
||||
to_return(status: 200, body: stub_body(tags).to_json)
|
||||
end
|
||||
|
||||
def stub_body(tags)
|
||||
{ "outputs": [{ "data": { "concepts": tag_structure(tags) } }] }
|
||||
end
|
||||
|
||||
def tag_structure(tags)
|
||||
tags.map do |tag|
|
||||
{ "name": tag, "value": 0.95 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -65,7 +65,7 @@ RSpec.configure do |config|
|
||||
# config.filter_gems_from_backtrace("gem name")
|
||||
|
||||
config.include AuthHelpers, type: :feature
|
||||
config.include ClarifaiHelpers, type: :job
|
||||
config.include ClarifaiHelpers
|
||||
end
|
||||
|
||||
Shoulda::Matchers.configure do |config|
|
||||
|
||||
@ -1,17 +1,7 @@
|
||||
module ClarifaiHelpers
|
||||
def stub_clarifai(tags)
|
||||
WebMock.
|
||||
stub_request(:post, Clarifai::API_URL).
|
||||
to_return(status: 200, body: stub_body(tags).to_json)
|
||||
end
|
||||
|
||||
def stub_body(tags)
|
||||
{ "outputs": [{ "data": { "concepts": tag_structure(tags) } }] }
|
||||
end
|
||||
|
||||
def tag_structure(tags)
|
||||
tags.map do |tag|
|
||||
{ "name": tag, "value": 0.95 }
|
||||
end
|
||||
clarifai_mock = instance_double(Clarifai)
|
||||
allow(clarifai_mock).to receive(:tags).and_return(tags)
|
||||
allow(Clarifai).to receive(:new).and_return(clarifai_mock)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user