From eb53865ff91288da6625a12f19540427736a1ea4 Mon Sep 17 00:00:00 2001 From: bsag Date: Sun, 22 Jun 2008 15:30:38 +0100 Subject: [PATCH] Context model specs completed. Using fixtures for some examples currently, but might convert to mocks in future. Specs now offer same coverage as Test::Unit tests for Context model. --- spec/fixtures/contexts.yml | 25 ++++-- spec/fixtures/preferences.yml | 34 ++++++++ spec/fixtures/todos.yml | 57 ++++++++++++++ spec/fixtures/users.yml | 27 +++++++ spec/models/context_spec.rb | 141 +++++++++++++++++++++++++++------- 5 files changed, 250 insertions(+), 34 deletions(-) create mode 100644 spec/fixtures/preferences.yml create mode 100644 spec/fixtures/todos.yml create mode 100644 spec/fixtures/users.yml diff --git a/spec/fixtures/contexts.yml b/spec/fixtures/contexts.yml index 5bf02933..a0513ccc 100644 --- a/spec/fixtures/contexts.yml +++ b/spec/fixtures/contexts.yml @@ -1,7 +1,22 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -# one: -# column: value -# -# two: -# column: value +agenda: + id: 2 + name: agenda + position: 2 + hide: false + user: admin_user + +call: + id: 3 + name: call + position: 3 + hide: true + user: admin_user + +email: + id: 4 + name: email + position: 4 + hide: false + user: admin_user diff --git a/spec/fixtures/preferences.yml b/spec/fixtures/preferences.yml new file mode 100644 index 00000000..66d41ee8 --- /dev/null +++ b/spec/fixtures/preferences.yml @@ -0,0 +1,34 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +admin_user_prefs: + user: admin_user + staleness_starts: 7 + date_format: "%d/%m/%Y" + title_date_format: "%A, %d %B %Y" + show_number_completed: 5 + show_completed_projects_in_sidebar: true + show_hidden_contexts_in_sidebar: true + show_hidden_projects_in_sidebar: true + admin_email: butshesagirl@rousette.org.uk + week_starts: 1 + due_style: 0 + refresh: 0 + time_zone: "London" + verbose_action_descriptors: true + show_project_on_todo_done: false + +other_user_prefs: + user: jane + staleness_starts: 7 + date_format: "%d/%m/%Y" + title_date_format: "%A, %d %B %Y" + show_number_completed: 5 + show_completed_projects_in_sidebar: true + show_hidden_contexts_in_sidebar: true + show_hidden_projects_in_sidebar: true + admin_email: butshesagirl@rousette.org.uk + week_starts: 1 + due_style: 0 + refresh: 0 + time_zone: "London" + verbose_action_descriptors: false + show_project_on_todo_done: true diff --git a/spec/fixtures/todos.yml b/spec/fixtures/todos.yml new file mode 100644 index 00000000..412feac8 --- /dev/null +++ b/spec/fixtures/todos.yml @@ -0,0 +1,57 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +<% + +def today + Time.zone.now.beginning_of_day.to_s(:db) +end + +def next_week + 1.week.from_now.beginning_of_day.to_s(:db) +end + +def last_week + 1.week.ago.beginning_of_day.to_s(:db) +end + +def two_weeks_ago + 2.weeks.ago.beginning_of_day.to_s(:db) +end + +def two_weeks_hence + 2.weeks.from_now.beginning_of_day.to_s(:db) +end + +%> + +billgates: + id: 2 + context_id: 2 + project_id: 2 + description: Call Bill Gates to find out how much he makes per day + notes: ~ + state: active + due: <%= two_weeks_hence %> + completed_at: ~ + user: admin_user + +dinoexterm: + id: 3 + context_id: 2 + project_id: 3 + description: Call dinosaur exterminator + notes: Ask him if I need to hire a skip for the corpses. + state: active + due: <%= two_weeks_hence %> + completed_at: ~ + user: admin_user + +buymilk: + id: 4 + context_id: 2 + project_id: ~ + description: Buy milk + notes: ~ + state: completed + due: ~ + completed_at: <%= today %> + user: admin_user diff --git a/spec/fixtures/users.yml b/spec/fixtures/users.yml new file mode 100644 index 00000000..5d027653 --- /dev/null +++ b/spec/fixtures/users.yml @@ -0,0 +1,27 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +admin_user: + login: admin + crypted_password: <%= Digest::SHA1.hexdigest("#{Tracks::Config.salt}--abracadabra--") %> + token: <%= Digest::SHA1.hexdigest("adminSat Feb 25 17:14:00 GMT 20060.236961325863376") %> + is_admin: true + first_name: Admin + last_name: Schmadmin + auth_type: database + +other_user: + login: jane + crypted_password: <%= Digest::SHA1.hexdigest("#{Tracks::Config.salt}--sesame--") %> + token: <%= Digest::SHA1.hexdigest("janeSun Feb 19 14:42:45 GMT 20060.408173979260027") %> + is_admin: false + first_name: Jane + last_name: Doe + auth_type: database + +ldap_user: + login: john + crypted_password: test + token: <%= Digest::SHA1.hexdigest("johnSun Feb 19 14:42:45 GMT 20060.408173979260027") %> + is_admin: false + first_name: John + last_name: Deere + auth_type: ldap diff --git a/spec/models/context_spec.rb b/spec/models/context_spec.rb index 74ece6d0..ffb6b121 100644 --- a/spec/models/context_spec.rb +++ b/spec/models/context_spec.rb @@ -13,56 +13,139 @@ module ContextSpecHelper end - describe Context do +describe "Context validations" do include ContextSpecHelper - - before(:each) do - @context = Context.new - end - - it "should be valid" do - @context.name = "FooBar" - @context.should be_valid + + before(:each) do + @context = Context.new end - + + it "should be valid" do + @context.attributes = valid_context_attributes + @context.should be_valid + @context.save + Context.should have(4).records # 3 in fixtures 1 set up here + end + + 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 + it "should have one error with a name of more than 255 characters" do @context.name = "z" * 256 + @context.should_not be_valid @context.should have(1).error_on(:name) + @context.errors.on(:name).should eql("context name must be less than 256 characters") end - + it "should have one error with name containing comma" do @context.name = "Foo,Bar" + @context.should_not be_valid @context.should have(1).error_on(:name) + @context.errors.on(:name).should eql("cannot contain the comma (',') character") end + 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 + it "should have one record in Context model class" do @context.name = "FooBar" @context.save - Context.should have(1).record + Context.should have(4).records # 3 in fixture, one set up here + end + +end + +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 hidden" do - @context.attributes = valid_context_attributes - @context.should be_hide # :hide should be true + it "should be produce correct .summary text for hidden context" do + contexts(:call).summary(1).should eql("

1. Context is Hidden.

") + 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("

1. Context is Active.

") + end + + it "should return .title which matches name" do + contexts(:agenda).title.should eql(contexts(:agenda).name) end - it "should be produce correct summary text for hidden context" do - @context.attributes = valid_context_attributes - @context.summary(1).should eql("

1. Context is Hidden.

") + 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) end - it "should be active" do - @context.attributes = valid_context_attributes - @context.hide = false - @context.save - @context.should_not be_hide # :hide should be true + 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) end - it "should produce correct summary text for active context" do - @context.attributes = valid_context_attributes - @context.hide = false - @context.save - @context.summary(1).should eql("

1. Context is Active.

") + it "should return id with .to_param" do + Context.find(2).to_param.should eql("2") end - end + 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