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"
|
require "rails_helper"
|
||||||
|
|
||||||
describe "user manages images" do
|
describe "user manages images" do
|
||||||
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
it "uploads the image" do
|
it "uploads the image" do
|
||||||
|
stub_clarifai(%w[computer technology])
|
||||||
|
|
||||||
sign_in
|
sign_in
|
||||||
attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
||||||
click_on("Create Image")
|
click_on("Create Image")
|
||||||
@ -19,11 +23,18 @@ describe "user manages images" do
|
|||||||
expect(page).not_to have_css(".image")
|
expect(page).not_to have_css(".image")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "views the image" do
|
it "views the image with tags" do
|
||||||
user = User.create(uid: 1)
|
stub_clarifai(%w[computer technology])
|
||||||
Image.create(user: user, image: "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
|
||||||
|
|
||||||
sign_in
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,11 +9,27 @@ describe Clarifai do
|
|||||||
|
|
||||||
describe ".tags" do
|
describe ".tags" do
|
||||||
it "predicts tags for our image" 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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -65,7 +65,7 @@ RSpec.configure do |config|
|
|||||||
# config.filter_gems_from_backtrace("gem name")
|
# config.filter_gems_from_backtrace("gem name")
|
||||||
|
|
||||||
config.include AuthHelpers, type: :feature
|
config.include AuthHelpers, type: :feature
|
||||||
config.include ClarifaiHelpers, type: :job
|
config.include ClarifaiHelpers
|
||||||
end
|
end
|
||||||
|
|
||||||
Shoulda::Matchers.configure do |config|
|
Shoulda::Matchers.configure do |config|
|
||||||
|
|||||||
@ -1,17 +1,7 @@
|
|||||||
module ClarifaiHelpers
|
module ClarifaiHelpers
|
||||||
def stub_clarifai(tags)
|
def stub_clarifai(tags)
|
||||||
WebMock.
|
clarifai_mock = instance_double(Clarifai)
|
||||||
stub_request(:post, Clarifai::API_URL).
|
allow(clarifai_mock).to receive(:tags).and_return(tags)
|
||||||
to_return(status: 200, body: stub_body(tags).to_json)
|
allow(Clarifai).to receive(:new).and_return(clarifai_mock)
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user