improving todo creation tests and implement context, project and tag setting/creation

This commit is contained in:
Stefan Richter 2011-10-10 22:25:51 +02:00
parent 3180164ed0
commit 9eae8a7068
2 changed files with 110 additions and 44 deletions

View file

@ -143,19 +143,6 @@ class Todo < ActiveRecord::Base
end end
end end
def predecessor_dependencies=(params)
value = params[:predecessor_dependencies]
if !value.nil?
if value.class == Array
value.each do |attrs|
predecessor_dependencies.build(attrs)
end
else
predecessor_dependencies.build(value)
end
end
end
def save_predecessors def save_predecessors
unless @predecessor_array.nil? # Only save predecessors if they changed unless @predecessor_array.nil? # Only save predecessors if they changed
current_array = self.predecessors current_array = self.predecessors
@ -190,19 +177,6 @@ class Todo < ActiveRecord::Base
self.activate! self.activate!
end end
def successor_dependencies=(params)
value = params[:successor_dependencies]
if !value.nil?
if value.class == Array
value.each do |attrs|
successor_dependencies.build(attrs)
end
else
successor_dependencies.build(value)
end
end
end
# Returns true if t is equal to self or a successor of self # Returns true if t is equal to self or a successor of self
def is_successor?(todo) def is_successor?(todo)
if self == todo if self == todo
@ -314,6 +288,8 @@ class Todo < ActiveRecord::Base
end end
def add_predecessor(t) def add_predecessor(t)
return if t.nil?
@predecessor_array = predecessors @predecessor_array = predecessors
@predecessor_array << t @predecessor_array << t
end end
@ -336,6 +312,62 @@ class Todo < ActiveRecord::Base
self[:notes] = value self[:notes] = value
end end
# XML API fixups
def predecessor_dependencies=(params)
value = params[:predecessor]
if !value.nil?
if value.class == Array
value.each do |ele|
if ele.is_a? String
add_predecessor(self.user.todos.find_by_id(ele.to_i)) unless ele.blank?
else
predecessor_dependencies.build(value)
end
end
else
if ele.is_a? String
add_predecessor(self.user.todos.find_by_id(ele.to_i)) unless ele.blank?
else
predecessor_dependencies.build(value)
end
end
end
end
alias_method :original_context=, :context=
def context=(value)
if value.is_a? Context
self.original_context=(value)
else
self.original_context=(Context.create(value))
end
end
alias_method :original_project=, :project=
def project=(value)
if value.is_a? Project
self.original_project=(value)
else
self.original_project=(Project.create(value))
end
end
alias_method :original_tags=, :tags=
def tags=(params)
value = params[:tag]
if !value.nil?
if value.class == Array
value.each do |attrs|
tags.build(attrs)
end
else
tags.build(value)
end
end
end
# Rich Todo API # Rich Todo API
def self.from_rich_message(user, default_context_id, description, notes) def self.from_rich_message(user, default_context_id, description, notes)

View file

@ -8,8 +8,8 @@ class TodoXmlApiTest < ActionController::IntegrationTest
def setup def setup
assert_test_environment_ok assert_test_environment_ok
@user = users(:other_user) @user = users(:admin_user)
@password = 'sesame' @password = 'abracadabra'
end end
def test_get_tickler_succeeds def test_get_tickler_succeeds
@ -51,39 +51,73 @@ class TodoXmlApiTest < ActionController::IntegrationTest
end end
def test_post_create_todo_with_dependencies def test_post_create_todo_with_dependencies
old_count = @user.todos.count
authenticated_post_xml_to_todo_create " authenticated_post_xml_to_todo_create "
<todo> <todo>
<description>this will succeed 2</description> <description>this will succeed 2</description>
<context_id type='integer'>10</context_id> <context_id>8</context_id>
<project_id type='integer'>4</project_id> <project_id>1</project_id>
<predecessor_dependencies> <predecessor_dependencies>
<predecessor>12</predecessor> <predecessor>5</predecessor>
<predecessor>6</predecessor>
</predecessor_dependencies> </predecessor_dependencies>
<successor_dependencies>
<successor>12</successor>
</successor_dependencies>
</todo>" </todo>"
assert_response :success assert_response :success
assert_equal @user.todos.count, old_count + 1 todo = @user.todos.find_by_description("this will succeed 2")
assert_not_nil todo
assert !todo.uncompleted_predecessors.empty?
end end
def test_post_create_todo_with_tags def test_post_create_todo_with_tags
old_count = @user.todos.count
authenticated_post_xml_to_todo_create " authenticated_post_xml_to_todo_create "
<todo> <todo>
<description>this will succeed 2</description> <description>this will succeed 3</description>
<context_id type='integer'>10</context_id> <context_id type='integer'>8</context_id>
<project_id type='integer'>4</project_id> <project_id type='integer'>1</project_id>
<tags> <tags>
<tag><name>starred</name></tag> <tag><name>starred</name></tag>
<tag><name>starred2</name></tag>
</tags> </tags>
</todo>" </todo>"
puts @response.body
assert_response :success assert_response :success
assert_equal @user.todos.count, old_count + 1 todo = @user.todos.find_by_description("this will succeed 3")
assert_not_nil todo
assert !todo.starred?
end
def test_post_create_todo_with_new_context
authenticated_post_xml_to_todo_create "
<todo>
<description>this will succeed 4</description>
<project_id>1</project_id>
<context>
<name>@SomeNewContext</name>
</context>
</todo>"
assert_response :success
todo = @user.todos.find_by_description("this will succeed 4")
assert_not_nil todo
assert_not_nil todo.context
assert_equal todo.context.name, "@SomeNewContext"
end
def test_post_create_todo_with_new_context
authenticated_post_xml_to_todo_create "
<todo>
<description>this will succeed 5</description>
<context_id>8</context_id>
<project>
<name>Make even more money</name>
</project>
</todo>"
assert_response :success
todo = @user.todos.find_by_description("this will succeed 5")
assert_not_nil todo
assert_not_nil todo.project
assert_equal todo.project.name, "Make even more money"
end end
def test_post_create_todo_with_wrong_project_and_context_id def test_post_create_todo_with_wrong_project_and_context_id
@ -101,7 +135,7 @@ class TodoXmlApiTest < ActionController::IntegrationTest
private private
def authenticated_post_xml_to_todo_create(postdata = @@valid_postdata, user = users(:other_user).login, password = 'sesame') def authenticated_post_xml_to_todo_create(postdata = @@valid_postdata, user = @user.login, password = @password)
authenticated_post_xml "/todos", user, password, postdata authenticated_post_xml "/todos", user, password, postdata
end end