Skip to content

Commit

Permalink
legal checks on object ids support other object ids. [ fix mongoid#87 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 6, 2012
1 parent fc3a9b9 commit 7de7c50
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

### Resolved Issues

* \#87 `Moped::BSON::ObjectId.legal?` now returns true for object ids.

* \#60/\#80 Moped now gracefully handles replica set reconfig and crashes of the
primary and secondary. By default, the node list will be refreshed every
second and the operation will be retried up to 30 times. This is configurable
Expand Down
2 changes: 1 addition & 1 deletion lib/moped/bson/object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def from_time(time)
end

def legal?(str)
/\A\h{24}\Z/ === str
/\A\h{24}\Z/ === str.to_s
end

def from_data(data)
Expand Down
54 changes: 32 additions & 22 deletions spec/moped/bson/object_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
end

describe "unmarshalling" do

let(:marshal_data) do
Marshal.dump(Moped::BSON::ObjectId.from_data(bytes))
Marshal.dump(described_class.from_data(bytes))
end

it "does not attempt to repair the id" do
Expand Down Expand Up @@ -66,16 +67,16 @@
context "when the string is valid" do

it "initializes with the string's bytes" do
Moped::BSON::ObjectId.should_receive(:from_data).with(bytes)
Moped::BSON::ObjectId.from_string "4e4d66343b39b68407000001"
described_class.should_receive(:from_data).with(bytes)
described_class.from_string "4e4d66343b39b68407000001"
end
end

context "when the string is not valid" do

it "raises an error" do
expect {
Moped::BSON::ObjectId.from_string("asadsf")
described_class.from_string("asadsf")
}.to raise_error(Moped::Errors::InvalidObjectId)
end
end
Expand All @@ -85,44 +86,54 @@

context "when the string is too short to be an object id" do
it "returns false" do
Moped::BSON::ObjectId.legal?("a" * 23).should be_false
described_class.legal?("a" * 23).should be_false
end
end

context "when the string contains invalid hex characters" do
it "returns false" do
Moped::BSON::ObjectId.legal?("y" + "a" * 23).should be_false
described_class.legal?("y" + "a" * 23).should be_false
end
end

context "when the string is a valid object id" do
it "returns true" do
Moped::BSON::ObjectId.legal?("a" * 24).should be_true
described_class.legal?("a" * 24).should be_true
end
end

context "when checking against another object id" do

let(:object_id) do
described_class.new
end

it "returns true" do
described_class.legal?(object_id).should be_true
end
end
end

describe ".from_time" do
it "sets the generation time" do
time = Time.at((Time.now.utc - 64800).to_i).utc
Moped::BSON::ObjectId.from_time(time).generation_time.should == time
described_class.from_time(time).generation_time.should == time
end

it "does not include process or sequence information" do
id = Moped::BSON::ObjectId.from_time(Time.now)
id = described_class.from_time(Time.now)
id.to_s.should =~ /\A\h{8}0{16}\Z/
end
end

describe "#initialize" do
context "with no data" do
it "increments the id on each call" do
Moped::BSON::ObjectId.new.should_not eq Moped::BSON::ObjectId.new
described_class.new.should_not eq described_class.new
end

it "increments the id safely across threads" do
ids = 2.times.map { Thread.new { Moped::BSON::ObjectId.new } }
ids = 2.times.map { Thread.new { described_class.new } }
ids[0].value.should_not eq ids[1].value
end
end
Expand All @@ -132,13 +143,13 @@

context "when data is identical" do
it "returns true" do
Moped::BSON::ObjectId.from_data(bytes).should == Moped::BSON::ObjectId.from_data(bytes)
described_class.from_data(bytes).should == described_class.from_data(bytes)
end
end

context "when other is not an object id" do
it "returns false" do
Moped::BSON::ObjectId.new.should_not == nil
described_class.new.should_not == nil
end
end

Expand All @@ -148,13 +159,13 @@

context "when data is identical" do
it "returns true" do
Moped::BSON::ObjectId.from_data(bytes).should eql Moped::BSON::ObjectId.from_data(bytes)
described_class.from_data(bytes).should eql described_class.from_data(bytes)
end
end

context "when other is not an object id" do
it "returns false" do
Moped::BSON::ObjectId.new.should_not eql nil
described_class.new.should_not eql nil
end
end

Expand All @@ -164,13 +175,13 @@

context "when data is identical" do
it "returns the same hash" do
Moped::BSON::ObjectId.from_data(bytes).hash.should eq Moped::BSON::ObjectId.from_data(bytes).hash
described_class.from_data(bytes).hash.should eq described_class.from_data(bytes).hash
end
end

context "when other is not an object id" do
it "returns a different hash" do
Moped::BSON::ObjectId.new.hash.should_not eql Moped::BSON::ObjectId.new.hash
described_class.new.hash.should_not eql described_class.new.hash
end
end

Expand All @@ -179,29 +190,29 @@
describe "#to_s" do

it "returns a hex string representation of the id" do
Moped::BSON::ObjectId.from_data(bytes).to_s.should eq "4e4d66343b39b68407000001"
described_class.from_data(bytes).to_s.should eq "4e4d66343b39b68407000001"
end

end

describe "#inspect" do

it "returns a sane representation of the id" do
Moped::BSON::ObjectId.from_data(bytes).inspect.should eq '"4e4d66343b39b68407000001"'
described_class.from_data(bytes).inspect.should eq '"4e4d66343b39b68407000001"'
end

end

describe "#to_json" do

it "returns a json representation of the id" do
Moped::BSON::ObjectId.from_data(bytes).to_json.should eq('{"$oid": "4e4d66343b39b68407000001"}')
described_class.from_data(bytes).to_json.should eq('{"$oid": "4e4d66343b39b68407000001"}')
end

end

describe "#repair!" do
let(:id) { Moped::BSON::ObjectId.allocate }
let(:id) { described_class.allocate }

context "when the data is a 12-element array" do
it "sets the id's data to the byte string" do
Expand All @@ -225,5 +236,4 @@
end
end
end

end

0 comments on commit 7de7c50

Please sign in to comment.