Skip to content

Commit

Permalink
* Add acceptance/smoke tests for vision helpers (#2720)
Browse files Browse the repository at this point in the history
* Add image_context as parameter to helpers
* Alias ProductSearchClient class methods to instance methods
  • Loading branch information
TheRoyalTnetennba committed Dec 14, 2018
1 parent 71acf42 commit bd9c15b
Show file tree
Hide file tree
Showing 11 changed files with 854 additions and 138 deletions.
22 changes: 22 additions & 0 deletions google-cloud-vision/Rakefile
Expand Up @@ -143,6 +143,8 @@ end
task :generate_partials do
require "google/cloud/vision"
Google::Cloud::Vision::AVAILABLE_VERSIONS.each do |version|

# Prepare ImageAnnotator helpers
require "google/cloud/vision/#{version}/image_annotator_client"
helper_hash = {}
Google::Cloud::Vision.const_get(version.capitalize)::Feature::Type.constants.each do |feature_type|
Expand All @@ -151,16 +153,36 @@ task :generate_partials do
method_name += "_detection" unless method_name.include? "detection"
helper_hash[method_name] = feature_type
end

# Prepare ProductSearch helpers
require "google/cloud/vision/#{version}/product_search_client"
class_methods = Google::Cloud::Vision.const_get(version.capitalize)::ProductSearchClient.methods false
params = Hash[class_methods.collect { |method_name| [method_name, ""] } ]
params.each do |method_name, _|
args = Google::Cloud::Vision.const_get(version.capitalize)::ProductSearchClient.method(method_name).parameters
params[method_name] = args.map { |arg| arg.last }
end

# Generate ImageAnnotator and ProductSearch helpers
file_path = "./lib/google/cloud/vision/#{version}/helpers.rb"
File.open(file_path, "w") do |f|
config = ERB.new(File.read("./synth/helpers.rb.erb"))
f.write(config.result(binding))
end

# Generate ImageAnnotator and ProductSearch tests
file_path = "./test/google/cloud/vision/#{version}/helpers_test.rb"
File.open(file_path, "w") do |f|
config = ERB.new(File.read("./synth/helpers_test.rb.erb"))
f.write(config.result(binding))
end

# Generate acceptance tests
file_path = "./acceptance/google/cloud/vision/#{version}/helpers_smoke_test.rb"
File.open(file_path, "w") do |f|
config = ERB.new(File.read("./synth/helpers_smoke_test.rb.erb"))
f.write(config.result(binding))
end
end
end

Expand Down
@@ -0,0 +1,112 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "minitest/autorun"
require "minitest/spec"

require "google/cloud/vision"
require "google/cloud/vision/v1/image_annotator_client"

describe Google::Cloud::Vision::V1::ImageAnnotatorClient do
let(:gs_image) { "gs://gapic-toolkit/President_Barack_Obama.jpg" }
let(:image_annotator_client) { Google::Cloud::Vision::ImageAnnotator.new(version: :v1) }
let(:options) do
{
images: [],
image: "gs://gapic-toolkit/President_Barack_Obama.jpg",
max_results: 10,
async: false,
mime_type: nil,
batch_size: 10,
destination: nil,
image_context: {}
}
end

it "can successfully make face_detection requests" do
response = image_annotator_client.face_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make landmark_detection requests" do
response = image_annotator_client.landmark_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make logo_detection requests" do
response = image_annotator_client.logo_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make label_detection requests" do
response = image_annotator_client.label_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make text_detection requests" do
response = image_annotator_client.text_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make document_text_detection requests" do
response = image_annotator_client.document_text_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make safe_search_detection requests" do
response = image_annotator_client.safe_search_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make image_properties_detection requests" do
response = image_annotator_client.image_properties_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make web_detection requests" do
response = image_annotator_client.web_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make product_search_detection requests" do
response = image_annotator_client.product_search_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make object_localization_detection requests" do
response = image_annotator_client.object_localization_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make crop_hints_detection requests" do
response = image_annotator_client.crop_hints_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make label_detection requests and verify response" do
response = image_annotator_client.label_detection **options
labels = response.responses.map do |response|
response.label_annotations.map { |label| label.description }
end.flatten
assert_includes(labels, "suit")
end

it "can successfully make face_detection requests and verify response" do
response = image_annotator_client.face_detection **options
labels = response.responses.map do |response|
response.face_annotations.map { |annotation| annotation.joy_likelihood }
end.flatten
assert_includes(labels, :VERY_LIKELY)
end
end
@@ -0,0 +1,112 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "minitest/autorun"
require "minitest/spec"

require "google/cloud/vision"
require "google/cloud/vision/v1p3beta1/image_annotator_client"

describe Google::Cloud::Vision::V1p3beta1::ImageAnnotatorClient do
let(:gs_image) { "gs://gapic-toolkit/President_Barack_Obama.jpg" }
let(:image_annotator_client) { Google::Cloud::Vision::ImageAnnotator.new(version: :v1p3beta1) }
let(:options) do
{
images: [],
image: "gs://gapic-toolkit/President_Barack_Obama.jpg",
max_results: 10,
async: false,
mime_type: nil,
batch_size: 10,
destination: nil,
image_context: {}
}
end

it "can successfully make face_detection requests" do
response = image_annotator_client.face_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make landmark_detection requests" do
response = image_annotator_client.landmark_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make logo_detection requests" do
response = image_annotator_client.logo_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make label_detection requests" do
response = image_annotator_client.label_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make text_detection requests" do
response = image_annotator_client.text_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make document_text_detection requests" do
response = image_annotator_client.document_text_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make safe_search_detection requests" do
response = image_annotator_client.safe_search_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make image_properties_detection requests" do
response = image_annotator_client.image_properties_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make web_detection requests" do
response = image_annotator_client.web_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make product_search_detection requests" do
response = image_annotator_client.product_search_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make object_localization_detection requests" do
response = image_annotator_client.object_localization_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make crop_hints_detection requests" do
response = image_annotator_client.crop_hints_detection image: gs_image
refute_empty(response.responses)
end

it "can successfully make label_detection requests and verify response" do
response = image_annotator_client.label_detection **options
labels = response.responses.map do |response|
response.label_annotations.map { |label| label.description }
end.flatten
assert_includes(labels, "suit")
end

it "can successfully make face_detection requests and verify response" do
response = image_annotator_client.face_detection **options
labels = response.responses.map do |response|
response.face_annotations.map { |annotation| annotation.joy_likelihood }
end.flatten
assert_includes(labels, :VERY_LIKELY)
end
end

0 comments on commit bd9c15b

Please sign in to comment.