Commit a6fc1473 authored by John E. Vincent's avatar John E. Vincent

finish specs for Host

parent abddba1b
......@@ -4,6 +4,7 @@ gem "sinatra-namespace", "= 0.6.1"
gem "ohm", "= 0.1.3"
gem "ohm-contrib", "= 0.1.0"
gem "haml", "= 3.0.25"
gem "rake", "= 0.8.7"
group :development do
gem "sinatra-reloader", "= 0.5.0"
gem "rspec", "= 2.4.0"
......
......@@ -16,6 +16,7 @@ GEM
ohm-contrib (0.1.0)
ohm
rack (1.2.1)
rake (0.8.7)
rcov (0.9.9)
rcov (0.9.9-java)
redis (2.1.1)
......@@ -54,6 +55,7 @@ DEPENDENCIES
json-jruby (= 1.4.6)
ohm (= 0.1.3)
ohm-contrib (= 0.1.0)
rake (= 0.8.7)
rcov (= 0.9.9)
rspec (= 2.4.0)
sinatra (= 1.1.2)
......
......@@ -14,7 +14,8 @@ class Host < Ohm::Model
index :status
def validate
assert_present :name, :status
assert_present :name
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
......
......@@ -4,12 +4,12 @@ describe "Host", :reset_redis => true do
it "should create a new Host with no services" do
hostname = "host1.domain.com"
status = "up"
host = Host.create(:name => hostname, :status => status)
hoststatus = "up"
host = Host.create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.name.should == hostname
host.status.should == status
host.status.should == hoststatus
host.services.size.should == 0
end
......@@ -31,4 +31,85 @@ describe "Host", :reset_redis => true do
host.services[1].host_id.should == host.id
end
it "should create a Host via find_or_create" do
hostname = "host3.domain.com"
hoststatus = "up"
host = Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.is_new?.should == true
end
it "should update a Host via find_or_create" do
hostname = "host3.domain.com"
hoststatus = "pending"
newstatus = "down"
host = Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.is_new?.should == true
sleep 3
host1 = Host.find_or_create(:name => hostname, :status => newstatus)
host1.save
host1.is_new?.should == false
end
it "should delete a Host" do
hostname = "host3.domain.com"
hoststatus = "pending"
host = Host.create(:name => hostname, :status => hoststatus)
host.save
host.delete
Host.find(:name => hostname).empty?.should == true
end
it "should not create a new Host with missing status" do
hostname ="host3.domain.com"
host = Host.create(:name => hostname)
host.valid?.should == false
host.errors.should == [[:status, :not_present], [:status, :not_member]]
end
it "should not create a new Host with missing hostname" do
status = "up"
host = Host.create(:status => status)
host.valid?.should == false
host.errors.should == [[:name, :not_present]]
end
it "should not create a new Host with an invalid status" do
hostname = "host3.domain.com"
status = "online"
host = Host.create(:name => hostname, :status => status)
host.valid?.should == false
host.errors.should == [[:status, :not_member]]
end
it "should not create a duplicate Host" do
hostname = "host1.domain.com"
status = "up"
host1 = Host.create(:name => hostname, :status => status)
host1.save
host2 = Host.create(:name => hostname, :status => status)
host2.valid?.should == false
host2.errors.should == [[:name, :not_unique]]
end
it "should return all Hosts" do
hostname1 = "host1.domain.com"
status1 = "up"
hostname2 = "host2.domain.com"
status2= "down"
host1 = Host.create(:name => hostname1, :status => status1)
host2 = Host.create(:name => hostname2, :status => status2)
host1.save
host2.save
Hosts.all.class.should == Array
Hosts.all.size.should == 2
Hosts.all.first.name.should == hostname1
Hosts.all.first.status.should == status1
Hosts.all.last.name.should == hostname2
Hosts.all.last.status.should == status2
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment