2008-06-21 14:28:01 +01:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
|
|
|
|
|
|
|
|
module ContextSpecHelper
|
|
|
|
|
|
|
|
|
|
def valid_context_attributes
|
|
|
|
|
{
|
|
|
|
|
:name => "FooBar",
|
|
|
|
|
:position => 1,
|
|
|
|
|
:hide => true,
|
|
|
|
|
:user_id => 1
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
describe "Context validations" do
|
2008-06-21 14:28:01 +01:00
|
|
|
include ContextSpecHelper
|
2008-06-22 15:30:38 +01:00
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
|
@context = Context.new
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-21 14:28:01 +01:00
|
|
|
it "should be valid" do
|
2008-06-22 15:30:38 +01:00
|
|
|
@context.attributes = valid_context_attributes
|
|
|
|
|
@context.should be_valid
|
|
|
|
|
@context.save
|
|
|
|
|
Context.should have(4).records # 3 in fixtures 1 set up here
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
2008-06-22 15:30:38 +01:00
|
|
|
|
|
|
|
|
it "should have two errors with a missing name" do
|
|
|
|
|
@context.attributes = valid_context_attributes.except(:name)
|
|
|
|
|
@context.should_not be_valid
|
|
|
|
|
@context.should have(2).error_on(:name)
|
|
|
|
|
@context.errors.on(:name)[0].should eql("context must have a name")
|
|
|
|
|
@context.errors.on(:name)[1].should eql("context name must be less than 256 characters")
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-21 14:28:01 +01:00
|
|
|
it "should have one error with a name of more than 255 characters" do
|
|
|
|
|
@context.name = "z" * 256
|
2008-06-22 15:30:38 +01:00
|
|
|
@context.should_not be_valid
|
2008-06-21 14:28:01 +01:00
|
|
|
@context.should have(1).error_on(:name)
|
2008-06-22 15:30:38 +01:00
|
|
|
@context.errors.on(:name).should eql("context name must be less than 256 characters")
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
2008-06-22 15:30:38 +01:00
|
|
|
|
2008-06-21 14:28:01 +01:00
|
|
|
it "should have one error with name containing comma" do
|
|
|
|
|
@context.name = "Foo,Bar"
|
2008-06-22 15:30:38 +01:00
|
|
|
@context.should_not be_valid
|
2008-06-21 14:28:01 +01:00
|
|
|
@context.should have(1).error_on(:name)
|
2008-06-22 15:30:38 +01:00
|
|
|
@context.errors.on(:name).should eql("cannot contain the comma (',') character")
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
it "should have one error if name already exists for user" do
|
|
|
|
|
@existing_context = Context.new
|
|
|
|
|
@existing_context.attributes = valid_context_attributes
|
|
|
|
|
@existing_context.save
|
|
|
|
|
@context.attributes = valid_context_attributes
|
|
|
|
|
@context.should_not be_valid
|
|
|
|
|
@context.should have(1).error_on(:name)
|
|
|
|
|
@context.errors.on(:name).should eql("already exists")
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-21 14:28:01 +01:00
|
|
|
it "should have one record in Context model class" do
|
|
|
|
|
@context.name = "FooBar"
|
|
|
|
|
@context.save
|
2008-06-22 15:30:38 +01:00
|
|
|
Context.should have(4).records # 3 in fixture, one set up here
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
2008-06-22 15:30:38 +01:00
|
|
|
|
|
|
|
|
end
|
2008-06-21 14:28:01 +01:00
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
describe "Context model" do
|
|
|
|
|
fixtures :users, :todos, :contexts, :preferences
|
|
|
|
|
|
|
|
|
|
include ContextSpecHelper
|
|
|
|
|
|
|
|
|
|
it "should show hidden" do
|
|
|
|
|
contexts(:call).should be_hide # :hide should be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should be produce correct .summary text for hidden context" do
|
|
|
|
|
contexts(:call).summary(1).should eql("<p>1. Context is Hidden.</p>")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should show not hidden" do
|
|
|
|
|
contexts(:call).hide = false
|
|
|
|
|
contexts(:call).should_not be_hide # :hide should be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should produce correct .summary text for active context" do
|
|
|
|
|
contexts(:call).hide = false
|
|
|
|
|
contexts(:call).summary(1).should eql("<p>1. Context is Active.</p>")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should return .title which matches name" do
|
|
|
|
|
contexts(:agenda).title.should eql(contexts(:agenda).name)
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
it "should .find_by_namepart with exact match" do
|
|
|
|
|
@found = Context.find_by_namepart('agenda')
|
|
|
|
|
@found.should_not eql(nil)
|
|
|
|
|
@found.id.should eql(contexts(:agenda).id)
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
it "should .find_by_namepart with partial match" do
|
|
|
|
|
@found = Context.find_by_namepart('ag')
|
|
|
|
|
@found.should_not eql(nil)
|
|
|
|
|
@found.id.should eql(contexts(:agenda).id)
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
it "should return id with .to_param" do
|
|
|
|
|
Context.find(2).to_param.should eql("2")
|
2008-06-21 14:28:01 +01:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:30:38 +01:00
|
|
|
it "should return feed options" do
|
|
|
|
|
opts = Context.feed_options(users(:admin_user))
|
|
|
|
|
opts[:title].should eql("Tracks Contexts")
|
|
|
|
|
opts[:description].should eql("Lists all the contexts for Admin Schmadmin")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should create null Context with .null_object" do
|
|
|
|
|
@empty = Context.null_object
|
|
|
|
|
@empty.should be_an_instance_of(NullContext)
|
|
|
|
|
@empty.id.should eql(nil)
|
|
|
|
|
@empty.name.should eql('')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should delete todos within context when context deleted" do
|
|
|
|
|
contexts(:agenda).todos.count.should eql(3)
|
|
|
|
|
agenda_todo_ids = contexts(:agenda).todos.collect{|t| t.id }
|
|
|
|
|
contexts(:agenda).destroy
|
|
|
|
|
agenda_todo_ids.each do |todo_id|
|
|
|
|
|
Todo.find(:all).should_not include(todo_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should return correct number of done todos" do
|
|
|
|
|
contexts(:agenda).done_todos.size.should eql(1)
|
|
|
|
|
t = contexts(:agenda).not_done_todos[0]
|
|
|
|
|
t.complete!
|
|
|
|
|
t.save!
|
|
|
|
|
Context.find(contexts(:agenda)).done_todos.size.should eql(2)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should return correct number of not done todos" do
|
|
|
|
|
contexts(:agenda).not_done_todos.size.should eql(2)
|
|
|
|
|
t = contexts(:agenda).not_done_todos[0]
|
|
|
|
|
t.complete!
|
|
|
|
|
t.save!
|
|
|
|
|
Context.find(contexts(:agenda)).not_done_todos.size.should eql(1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|