Skip to content

Commit

Permalink
Add a seed_once with specs to test it.
Browse files Browse the repository at this point in the history
  • Loading branch information
queso committed Jan 20, 2010
1 parent 2e5dedf commit 941e97e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/seed-fu.rb
Expand Up @@ -73,6 +73,10 @@ class ActiveRecord::Base
def self.seed(*constraints, &block)
SeedFu::Seeder.plant(self, *constraints, &block)
end

def self.seed_once(*constraints, &block)
SeedFu::Seeder.plant(self, *constraints, true, &block)
end

def self.seed_many(*constraints)
seeds = constraints.pop
Expand Down
45 changes: 44 additions & 1 deletion spec/seed_fu_spec.rb
Expand Up @@ -55,6 +55,50 @@
SeededModel.find_by_login("harry").first_name.should == "Harry"
end

it "should update, not create, if constraints are met" do
SeededModel.seed(:id) do |s|
s.id = 1
s.login = "bob"
s.first_name = "Bob"
s.last_name = "Bobson"
s.title = "Peon"
end

SeededModel.seed(:id) do |s|
s.id = 1
s.login = "bob"
s.first_name = "Robert"
s.last_name = "Bobson"
s.title = "Peon"
end

bob = SeededModel.find_by_id(1)
bob.first_name.should == "Robert"
bob.last_name.should == "Bobson"
end

it "should create but not update with seed_once" do
SeededModel.seed_once(:id) do |s|
s.id = 1
s.login = "bob"
s.first_name = "Bob"
s.last_name = "Bobson"
s.title = "Peon"
end

SeededModel.seed_once(:id) do |s|
s.id = 1
s.login = "bob"
s.first_name = "Robert"
s.last_name = "Bobson"
s.title = "Peon"
end

bob = SeededModel.find_by_id(1)
bob.first_name.should == "Bob"
bob.last_name.should == "Bobson"
end

#it "should raise an error if constraints are not unique" do
# SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
# SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
Expand All @@ -66,7 +110,6 @@
#end

it "should default to an id constraint"
it "should update, not create, if constraints are met"
it "should require that all constraints are defined"
it "should raise an error if validation fails"
it "should retain fields that aren't specifically altered in the seeding"
Expand Down

0 comments on commit 941e97e

Please sign in to comment.