Skip to content

Commit

Permalink
Add partial unit test coverage for annotate
Browse files Browse the repository at this point in the history
  • Loading branch information
blowmage committed Aug 23, 2016
1 parent ef4a544 commit 6a61d0c
Show file tree
Hide file tree
Showing 11 changed files with 1,561 additions and 14 deletions.
25 changes: 16 additions & 9 deletions google-cloud-language/lib/google/cloud/language/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module Language
# annotation.thing #=> Some Result
#
class Document
##
# @private The gRPC Service object.
attr_accessor :service

##
# @private Creates a new Document instance.
def initialize
Expand Down Expand Up @@ -76,6 +80,9 @@ def format
return :html if html?
end

##
# Update the Document's format. Accepted values are `:text` or `:html`.
#
def format= new_format
@grpc.type = :PLAIN_TEXT if new_format.to_s == "text"
@grpc.type = :HTML if new_format.to_s == "html"
Expand Down Expand Up @@ -118,11 +125,11 @@ def language
end

##
# The Document's language.
# Update the Document's language. ISO and BCP-47 language codes are
# accepted.
#
def language= new_language
new_language = new_language.to_s unless new_language.nil?
@grpc.language = new_language
@grpc.language = new_language.to_s
end

##
Expand Down Expand Up @@ -153,9 +160,9 @@ def language= new_language
def annotate text: false, entities: false, sentiment: false,
encoding: nil
ensure_service!
grpc = @service.annotate to_grpc, text: text, entities: entities,
sentiment: sentiment,
encoding: encoding
grpc = service.annotate to_grpc, text: text, entities: entities,
sentiment: sentiment,
encoding: encoding
Annotation.from_grpc grpc
end
alias_method :mark, :annotate
Expand All @@ -182,7 +189,7 @@ def annotate text: false, entities: false, sentiment: false,
#
def entities encoding: nil
ensure_service!
grpc = @service.entities to_grpc, encoding: encoding
grpc = service.entities to_grpc, encoding: encoding
Annotation::Entities.from_grpc grpc
end

Expand All @@ -206,7 +213,7 @@ def entities encoding: nil
#
def sentiment
ensure_service!
grpc = @service.sentiment to_grpc
grpc = service.sentiment to_grpc
Annotation::Sentiment.from_grpc grpc
end

Expand Down Expand Up @@ -250,7 +257,7 @@ def self.from_source source, service, format: nil, language: nil
##
# Raise an error unless an active language project object is available.
def ensure_service!
fail "Must have active connection" unless @service
fail "Must have active connection" unless service
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
end
let(:entity_json) { entity_hash.to_json }
let(:entity_grpc) { Google::Cloud::Language::V1beta1::Entity.decode_json entity_json }
let(:entity_grpc) { Google::Cloud::Language::V1beta1::Entity.decode_json entity_json }
let(:entity) { Google::Cloud::Language::Annotation::Entity.from_grpc entity_grpc }

it "has attributes" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
describe Google::Cloud::Language::Annotation::TextSpan do
let(:text_span_hash) do
{
"content": "Hello world!",
"beginOffset": -1
content: "Hello world!",
beginOffset: -1
}
end
let(:text_span_json) { text_span_hash.to_json }
let(:text_span_grpc) { Google::Cloud::Language::V1beta1::TextSpan.decode_json text_span_json }
let(:text_span_grpc) { Google::Cloud::Language::V1beta1::TextSpan.decode_json text_span_json }
let(:text_span) { Google::Cloud::Language::Annotation::TextSpan.from_grpc text_span_grpc }

it "has attributes" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
end
let(:token_json) { token_hash.to_json }
let(:token_grpc) { Google::Cloud::Language::V1beta1::Token.decode_json token_json }
let(:token_grpc) { Google::Cloud::Language::V1beta1::Token.decode_json token_json }
let(:token) { Google::Cloud::Language::Annotation::Token.from_grpc token_grpc }

it "has attributes" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# 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
#
# http://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 "helper"

describe Google::Cloud::Language::Document, :full_html_annotation, :mock_language do
let(:doc) { language.document html_content, format: :html }

it "runs full annotation" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: html_content, type: :HTML)
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
annotation = doc.annotate
mock.verify

assert_html_annotation annotation
end

it "runs full annotation with en language options" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: html_content, type: :HTML, language: "en")
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
doc.language = "en"
annotation = doc.annotate
mock.verify

assert_html_annotation annotation
end

def assert_html_annotation annotation
annotation.language.must_equal "en"

annotation.sentiment.must_be_kind_of Google::Cloud::Language::Annotation::Sentiment
annotation.sentiment.language.must_equal "en"
annotation.sentiment.polarity.must_be_close_to 1.0
annotation.sentiment.magnitude.must_be_close_to 1.899999976158142

annotation.entities.must_be_kind_of ::Array
annotation.entities.class.must_equal Google::Cloud::Language::Annotation::Entities
annotation.entities.each do |entity|
entity.must_be_kind_of Google::Cloud::Language::Annotation::Entity
end
annotation.entities.count.must_equal 2
annotation.entities.language.must_equal "en"
annotation.entities.unknown.map(&:name).must_equal []
annotation.entities.people.map(&:name).must_equal ["chris"]
annotation.entities.locations.map(&:name).must_equal ["utah"]
annotation.entities.places.map(&:name).must_equal ["utah"]
annotation.entities.organizations.map(&:name).must_equal []
annotation.entities.events.map(&:name).must_equal []
annotation.entities.artwork.map(&:name).must_equal []
annotation.entities.goods.map(&:name).must_equal []
annotation.entities.other.map(&:name).must_equal []

annotation.sentences.each do |sentence|
sentence.must_be_kind_of Google::Cloud::Language::Annotation::TextSpan
end
annotation.sentences.map(&:text).must_equal html_sentences

annotation.tokens.each do |token|
token.must_be_kind_of Google::Cloud::Language::Annotation::Token
end
annotation.tokens.count.must_equal 24
token = annotation.tokens.first
token.text.must_equal "Hello"
token.part_of_speech.must_equal :X
token.head_token_index.must_equal 0
token.label.must_equal :ROOT
token.lemma.must_equal "Hello"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# 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
#
# http://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 "helper"

describe Google::Cloud::Language::Document, :full_text_annotation, :mock_language do
let(:doc) { language.document text_content }

it "runs full annotation with content and empty options" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: text_content, type: :PLAIN_TEXT)
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
annotation = doc.annotate
mock.verify

assert_text_annotation annotation
end

it "runs full annotation with content and TEXT format options" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: text_content, type: :PLAIN_TEXT)
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
doc.text!
annotation = doc.annotate
mock.verify

assert_text_annotation annotation
end

it "runs full annotation with content and TEXT format and en language options" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: text_content, type: :PLAIN_TEXT, language: "en")
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
doc.text!
doc.language = :en
annotation = doc.annotate
mock.verify

assert_text_annotation annotation
end

it "runs full annotation with content and en language options" do
grpc_doc = Google::Cloud::Language::V1beta1::Document.new(
content: text_content, type: :PLAIN_TEXT, language: "en")
features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new(
extract_syntax: true, extract_entities: true, extract_document_sentiment: true)
grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json

mock = Minitest::Mock.new
mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8]

doc.service.mocked_service = mock
doc.language = :en
annotation = doc.annotate
mock.verify

assert_text_annotation annotation
end

def assert_text_annotation annotation
annotation.language.must_equal "en"

annotation.sentiment.language.must_equal "en"
annotation.sentiment.polarity.must_equal 1.0
annotation.sentiment.magnitude.must_equal 2.0999999046325684

annotation.entities.count.must_equal 3
annotation.entities.language.must_equal "en"
annotation.entities.unknown.map(&:name).must_equal []
annotation.entities.people.map(&:name).must_equal ["Chris", "Mike"]
annotation.entities.locations.map(&:name).must_equal ["Utah"]
annotation.entities.places.map(&:name).must_equal ["Utah"]
annotation.entities.organizations.map(&:name).must_equal []
annotation.entities.events.map(&:name).must_equal []
annotation.entities.artwork.map(&:name).must_equal []
annotation.entities.goods.map(&:name).must_equal []
annotation.entities.other.map(&:name).must_equal []

annotation.sentences.map(&:text).must_equal text_sentences
annotation.tokens.count.must_equal 24
token = annotation.tokens.first
token.text.must_equal "Hello"
token.part_of_speech.must_equal :X
token.head_token_index.must_equal 0
token.label.must_equal :ROOT
token.lemma.must_equal "Hello"
end
end
Loading

0 comments on commit 6a61d0c

Please sign in to comment.