Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

Commit

Permalink
update Document#id= to handle values that are (or can be converted to…
Browse files Browse the repository at this point in the history
…) Strings and are compliant
  • Loading branch information
javmorin committed Mar 29, 2012
1 parent c5b7d64 commit 97d323d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
13 changes: 4 additions & 9 deletions lib/aws_cloud_search/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@ def add_field(name, value)

# The id field must conform to a special format
def id=(id)
if id.is_a? String
raise ArgumentError.new("Document id must match the regex [a-z0-9][a-z0-9_]*$") unless id =~ /^[a-z0-9][a-z0-9_]*$/
@id = id.downcase
elsif id == nil
@id = id
else
raise ArgumentError.new("Invalid Type: document id must be of type string.")
end
raise ArgumentError.new("Invalid ID: Document id must be a String or respond to #to_s") if (id.nil? || !id.respond_to?(:to_s))
@id = id.to_s
raise ArgumentError.new("Invalid ID: Document id must match the regex [a-z0-9][a-z0-9_]*$") unless @id =~ /^[a-z0-9][a-z0-9_]*$/
end

# Resets the fields.
Expand Down Expand Up @@ -80,4 +75,4 @@ def to_json
end

end
end
end
46 changes: 29 additions & 17 deletions spec/aws_cloud_search/document_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
require 'spec_helper'

describe AwsCloudSearch::Document do
before(:each) do
@doc = AwsCloudSearch::Document.new
end
let (:doc) { AwsCloudSearch::Document.new }

it "should accept values of the proper type" do
expect { @doc.id='123abc' }.to_not raise_error
expect { @doc.lang='en' }.to_not raise_error
expect { @doc.version=123 }.to_not raise_error
end
context "#id=" do
it "should accept a String-able value (Integer)" do
expect { doc.id = 123456789 }.to_not raise_error
end

it "should accept a compliant String" do
expect { doc.id = "abcdef" }.to_not raise_error
end

it "should throw an exception when given values of the wrong type" do
expect { @doc.id=123 }.to raise_error(Exception)
expect { @doc.lang=123 }.to raise_error(Exception)
expect { @doc.version='abc123' }.to raise_error(Exception)
it "should not accept a non-compliant String" do
expect { doc.id = 'AZ12' }.to raise_error(ArgumentError)
expect { doc.id = '!@#$%^&*()AZ' }.to raise_error(ArgumentError)
expect { doc.id = '_abc123' }.to raise_error(ArgumentError)
end

it "should not accept nil" do
expect { doc.id = nil }.to raise_error(ArgumentError)
end
end

it "should reject an incorrectly formatted id" do
expect { @doc.id='AZ12'}.to raise_error(Exception)
expect { @doc.id='!@#$%^&*()AZ'}.to raise_error(Exception)
expect { @doc.id= '_abc123'}.to raise_error(Exception)
context "#type_attr_accessor attributes" do
it "should accept values of proper type" do
expect { doc.lang = 'abcd' }.to_not raise_error
expect { doc.version = 1234 }.to_not raise_error
end

it "should not accept values of incorrect type" do
expect { doc.lang = 1234 }.to raise_error(ArgumentError)
expect { doc.version "abcd" }.to raise_error(ArgumentError)
end
end

end
end

0 comments on commit 97d323d

Please sign in to comment.