spec/mongoid/token_spec.rb in mongoid_token-1.1.0 vs spec/mongoid/token_spec.rb in mongoid_token-1.1.1

- old
+ new

@@ -18,10 +18,12 @@ include Mongoid::Document include Mongoid::Token field :url token :length => 3, :contains => :alphanumeric + + validates :url, :presence => true end class FailLink include Mongoid::Document include Mongoid::Token @@ -35,10 +37,26 @@ field :name token :length => 8, :contains => :alpha, :field_name => :vid end +class Image + include Mongoid::Document + include Mongoid::Token + + field :url + token :length => 8, :contains => :fixed_numeric_no_leading_zeros +end + +class Event + include Mongoid::Document + include Mongoid::Token + + field :name + token :length => 8, :contains => :alpha_lower +end + class Node include Mongoid::Document include Mongoid::Token field :name @@ -58,15 +76,19 @@ describe Mongoid::Token do before :each do @account = Account.create(:name => "Involved Pty. Ltd.") @link = Link.create(:url => "http://involved.com.au") @video = Video.create(:name => "Nyan nyan") + @image = Image.create(:url => "http://involved.com.au/image.png") + @event = Event.create(:name => "Super cool party!") Account.create_indexes Link.create_indexes FailLink.create_indexes Video.create_indexes + Image.create_indexes + Event.create_indexes Node.create_indexes end it "should have a token field" do @account.attributes.include?('token').should == true @@ -76,10 +98,11 @@ it "should have a token of correct length" do @account.token.length.should == 16 @link.token.length.should == 3 @video.vid.length.should == 8 + @image.token.length.should == 8 end it "should only generate unique tokens" do Link.create_indexes 1000.times do @@ -104,10 +127,15 @@ 50.times do |index| @video = Video.create(:name => "A test video") @video.vid.gsub(/[A-Za-z]/, "").length.should == 0 end + + 50.times do |index| + @event = Event.create(:name => "Super cool party!2") + @event.token.gsub(/[a-z]/, "").length.should == 0 + end end it "should create the only after the first save" do @account = Account.new(:name => "Smith & Co. LLC") @account.token.should be_nil @@ -143,19 +171,18 @@ @account.token = nil @account.save! @account.token.should_not be_nil end - it "should fail with an exception after 3 retries (by default)" do + it "should fail with an exception after 3 retries" do Link.destroy_all Link.create_indexes @first_link = Link.create(:url => "http://involved.com.au") Link.any_instance.stub(:generate_token).and_return(@first_link.token) @link = Link.new(:url => "http://fail.com") - #lambda{ @link.save! }.should raise_error(Mongoid::Token::CollisionRetriesExceeded) expect { @link.save! }.to raise_error(Mongoid::Token::CollisionRetriesExceeded) Link.count.should == 1 Link.where(:token => @first_link.token).count.should == 1 end @@ -163,23 +190,28 @@ it "tries to resolve collisions when instantiated with create!" do link = Link.create!(url: "http://example.com/1") Link.any_instance.stub(:generate_token).and_return(link.token) - expect { Link.create!(url: "http://example.com/2") } - .to raise_error(Mongoid::Token::CollisionRetriesExceeded) + expect { Link.create!(url: "http://example.com/2") }.to raise_error(Mongoid::Token::CollisionRetriesExceeded) end + it "should not raise a custom error if an operation failure is thrown for another reason" do + link = Link.new + lambda{ link.save! }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded) + link.valid?.should == false + end + it "should not raise a custom exception if retries are set to zero" do FailLink.destroy_all FailLink.create_indexes @first_link = FailLink.create(:url => "http://involved.com.au") Link.any_instance.stub(:generate_token).and_return(@first_link.token) @link = FailLink.new(:url => "http://fail.com") - lambda{ @link.save }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded) + lambda{ @link.save! }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded) end it "should create unique indexes on embedded documents" do @cluster = Cluster.create(:name => "CLUSTER_001") 5.times do |index| @@ -187,8 +219,17 @@ end @cluster.nodes.each do |node| node.attributes.include?('token').should == true node.token.match(/[0-9]{8}/).should_not == nil + end + end + + describe "with :fixed_numeric_not_null" do + it "should not start with 0" do + 1000.times do + image = Image.create(:url => "http://something.com/image.png") + image.token.should_not start_with "0" + end end end end