mirror of
https://github.com/danbee/my-images
synced 2025-03-04 08:49:05 +00:00
Refactor Clarifai spec
This commit is contained in:
parent
d933f8aa3f
commit
e326c7d2bd
@ -1,32 +1,19 @@
|
|||||||
require "spec_helper"
|
|
||||||
ENV["CLARIFAI_API_KEY"] = "1234"
|
ENV["CLARIFAI_API_KEY"] = "1234"
|
||||||
|
|
||||||
|
require "spec_helper"
|
||||||
require "clarifai"
|
require "clarifai"
|
||||||
|
require_relative "../support/clarifai_helpers"
|
||||||
|
|
||||||
describe Clarifai do
|
describe Clarifai do
|
||||||
|
include ClarifaiHelpers
|
||||||
|
|
||||||
describe ".tags" do
|
describe ".tags" do
|
||||||
it "predicts tags for our image" do
|
it "predicts tags for our image" do
|
||||||
WebMock.
|
stub_clarifai(%w[computer technology])
|
||||||
stub_request(:post, Clarifai::API_URL).
|
|
||||||
to_return(status: 200, body: stub_body.to_json)
|
|
||||||
|
|
||||||
clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg")
|
clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg")
|
||||||
|
|
||||||
expect(clarifai_image.tags).to eq(["computer", "technology"])
|
expect(clarifai_image.tags).to eq(%w[computer technology])
|
||||||
end
|
|
||||||
|
|
||||||
def stub_body
|
|
||||||
{
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"concepts": [
|
|
||||||
{ "name": "computer", "value": 0.96887743 },
|
|
||||||
{ "name": "technology", "value": 0.96544206 },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
17
spec/support/clarifai_helpers.rb
Normal file
17
spec/support/clarifai_helpers.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user