amrita2 1.9.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +112 -0
- data/init.rb +6 -0
- data/lib/amrita2/gettext.rb +116 -0
- data/lib/amrita2/macro.rb +153 -0
- data/lib/amrita2/rails_bridge.rb +172 -26
- data/lib/amrita2/template.rb +2634 -234
- data/lib/amrita2/testsupport.rb +171 -0
- data/lib/amrita2/version.rb +3 -3
- data/lib/amrita2.rb +1 -0
- data/sample/depot/app/controllers/admin_controller.rb +59 -0
- data/sample/depot/app/controllers/application.rb +20 -0
- data/sample/depot/app/controllers/info_controller.rb +19 -0
- data/sample/depot/app/controllers/login_controller.rb +85 -0
- data/sample/depot/app/controllers/store_controller.rb +68 -0
- data/sample/depot/app/helpers/admin_helper.rb +7 -0
- data/sample/depot/app/helpers/application_helper.rb +10 -0
- data/sample/depot/app/helpers/ar_form.rb +169 -0
- data/sample/depot/app/helpers/form_tag.rb +24 -0
- data/sample/depot/app/helpers/info_helper.rb +7 -0
- data/sample/depot/app/helpers/standard_form.rb +73 -0
- data/sample/depot/app/helpers/store_helper.rb +14 -0
- data/sample/depot/app/models/cart.rb +36 -0
- data/sample/depot/app/models/cart_item.rb +26 -0
- data/sample/depot/app/models/line_item.rb +34 -0
- data/sample/depot/app/models/order.rb +57 -0
- data/sample/depot/app/models/product.rb +41 -0
- data/sample/depot/app/models/user.rb +83 -0
- data/sample/depot/config/boot.rb +49 -0
- data/sample/depot/config/environment.rb +83 -0
- data/sample/depot/config/environments/development.rb +24 -0
- data/sample/depot/config/environments/production.rb +24 -0
- data/sample/depot/config/environments/test.rb +24 -0
- data/sample/depot/config/routes.rb +10 -0
- data/sample/depot/db/migrate/001_create_products.rb +18 -0
- data/sample/depot/db/migrate/002_add_price.rb +14 -0
- data/sample/depot/db/migrate/003_add_test_data.rb +68 -0
- data/sample/depot/db/migrate/004_add_sessions.rb +20 -0
- data/sample/depot/db/migrate/005_create_orders.rb +21 -0
- data/sample/depot/db/migrate/006_create_line_items.rb +27 -0
- data/sample/depot/db/migrate/007_create_users.rb +18 -0
- data/sample/depot/db/schema.rb +45 -0
- data/sample/depot/public/dispatch.rb +15 -0
- data/sample/depot/test/functional/admin_controller_test.rb +54 -0
- data/sample/depot/test/functional/info_controller_test.rb +23 -0
- data/sample/depot/test/functional/login_controller_test.rb +74 -0
- data/sample/depot/test/functional/store_controller_test.rb +57 -0
- data/sample/depot/test/integration/dsl_user_stories_test.rb +126 -0
- data/sample/depot/test/integration/user_stories_test.rb +70 -0
- data/sample/depot/test/performance/order_speed_test.rb +58 -0
- data/sample/depot/test/test_helper.rb +16 -0
- data/sample/depot/test/unit/cart_test.rb +39 -0
- data/sample/depot/test/unit/cart_test1.rb +31 -0
- data/sample/depot/test/unit/line_item_test.rb +15 -0
- data/sample/depot/test/unit/order_test.rb +15 -0
- data/sample/depot/test/unit/product_test.rb +98 -0
- data/sample/depot/vendor/plugins/amrita2/init.rb +6 -0
- data/sample/hello/hello.rb +22 -0
- data/sample/login_engine/app/controllers/application.rb +16 -0
- data/sample/login_engine/app/controllers/user_controller.rb +265 -0
- data/sample/login_engine/app/helpers/application_helper.rb +3 -0
- data/sample/login_engine/app/helpers/form_tag.rb +16 -0
- data/sample/login_engine/app/helpers/two_columns.rb +24 -0
- data/sample/login_engine/app/helpers/two_columns_form.rb +47 -0
- data/sample/login_engine/app/helpers/user_helper.rb +88 -0
- data/sample/login_engine/app/models/user.rb +7 -0
- data/sample/login_engine/app/models/user_notify.rb +75 -0
- data/sample/login_engine/config/boot.rb +45 -0
- data/sample/login_engine/config/environment.rb +140 -0
- data/sample/login_engine/config/environments/development.rb +21 -0
- data/sample/login_engine/config/environments/production.rb +18 -0
- data/sample/login_engine/config/environments/test.rb +19 -0
- data/sample/login_engine/config/routes.rb +23 -0
- data/sample/login_engine/db/migrate/001_create_users.rb +25 -0
- data/sample/login_engine/db/schema.rb +25 -0
- data/sample/login_engine/lib/config.rb +113 -0
- data/sample/login_engine/lib/hpricot_test_extension.rb +80 -0
- data/sample/login_engine/lib/login_engine/authenticated_system.rb +113 -0
- data/sample/login_engine/lib/login_engine/authenticated_user.rb +155 -0
- data/sample/login_engine/lib/login_engine.rb +62 -0
- data/sample/login_engine/public/dispatch.rb +10 -0
- data/sample/login_engine/test/functional/amrita2_test.rb +267 -0
- data/sample/login_engine/test/functional/user_controller_test.rb +544 -0
- data/sample/login_engine/test/mocks/mail.rb +14 -0
- data/sample/login_engine/test/mocks/time.rb +19 -0
- data/sample/login_engine/test/test_helper.rb +31 -0
- data/sample/login_engine/test/unit/user_test.rb +116 -0
- data/sample/login_engine/vendor/plugins/amrita2/init.rb +6 -0
- data/specs/attribute.rb +201 -0
- data/specs/datatypes.rb +231 -0
- data/specs/dictionary.rb +68 -0
- data/specs/erb_cdata.rb +187 -0
- data/specs/filters.rb +513 -0
- data/specs/gettext/erb_gettext.rb +42 -0
- data/specs/gettext/gettext_util.rb +65 -0
- data/specs/gettext/static_text.rb +103 -0
- data/specs/impl/code_generator.rb +87 -0
- data/specs/impl/dynamic_element.rb +92 -0
- data/specs/impl/hash_delegator.rb +57 -0
- data/specs/impl/parse_opt.rb +34 -0
- data/specs/impl/preprocess.rb +823 -0
- data/specs/impl/testsupport.rb +89 -0
- data/specs/inlineruby.rb +429 -0
- data/specs/intro.rb +654 -0
- data/specs/loop.rb +203 -0
- data/specs/macro.rb +532 -0
- data/specs/sample.rb +34 -0
- data/specs/sanitize.rb +110 -0
- data/specs/template.rb +189 -0
- data/specs/trace.rb +97 -0
- metadata +138 -19
- data/lib/amrita2/core.rb +0 -1897
- data/lib/amrita2/rd.rb +0 -314
@@ -0,0 +1,20 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class AddSessions < ActiveRecord::Migration
|
7
|
+
def self.up
|
8
|
+
create_table :sessions do |t|
|
9
|
+
t.column :session_id, :string
|
10
|
+
t.column :data, :text
|
11
|
+
t.column :updated_at, :datetime
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :sessions, :session_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :sessions
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class CreateOrders < ActiveRecord::Migration
|
7
|
+
|
8
|
+
def self.up
|
9
|
+
create_table :orders do |t|
|
10
|
+
t.column :name, :string
|
11
|
+
t.column :address, :text
|
12
|
+
t.column :email, :string
|
13
|
+
t.column :pay_type, :string, :limit => 10
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def self.down
|
19
|
+
drop_table :orders
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class CreateLineItems < ActiveRecord::Migration
|
7
|
+
def self.up
|
8
|
+
create_table :line_items do |t|
|
9
|
+
t.column :product_id, :integer, :null => false
|
10
|
+
t.column :order_id, :integer, :null => false
|
11
|
+
t.column :quantity, :integer, :null => false
|
12
|
+
t.column :total_price, :decimal, :null => false, :precision => 8, :scale => 2
|
13
|
+
end
|
14
|
+
|
15
|
+
execute "alter table line_items
|
16
|
+
add constraint fk_line_item_products
|
17
|
+
foreign key (product_id) references products(id)"
|
18
|
+
|
19
|
+
execute "alter table line_items
|
20
|
+
add constraint fk_line_item_orders
|
21
|
+
foreign key (order_id) references orders(id)"
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.down
|
25
|
+
drop_table :line_items
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class CreateUsers < ActiveRecord::Migration
|
7
|
+
def self.up
|
8
|
+
create_table :users do |t|
|
9
|
+
t.column :name, :string
|
10
|
+
t.column :hashed_password, :string
|
11
|
+
t.column :salt, :string
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :users
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# This file is autogenerated. Instead of editing this file, please use the
|
2
|
+
# migrations feature of ActiveRecord to incrementally modify your database, and
|
3
|
+
# then regenerate this schema definition.
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define(:version => 7) do
|
6
|
+
|
7
|
+
create_table "line_items", :force => true do |t|
|
8
|
+
t.column "product_id", :integer, :null => false
|
9
|
+
t.column "order_id", :integer, :null => false
|
10
|
+
t.column "quantity", :integer, :null => false
|
11
|
+
t.column "total_price", :decimal, :precision => 8, :scale => 2, :null => false
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index "line_items", ["product_id"], :name => "fk_line_item_products"
|
15
|
+
add_index "line_items", ["order_id"], :name => "fk_line_item_orders"
|
16
|
+
|
17
|
+
create_table "orders", :force => true do |t|
|
18
|
+
t.column "name", :string
|
19
|
+
t.column "address", :text
|
20
|
+
t.column "email", :string
|
21
|
+
t.column "pay_type", :string, :limit => 10
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table "products", :force => true do |t|
|
25
|
+
t.column "title", :string
|
26
|
+
t.column "description", :text
|
27
|
+
t.column "image_url", :string
|
28
|
+
t.column "price", :decimal, :precision => 8, :scale => 2, :default => 0.0
|
29
|
+
end
|
30
|
+
|
31
|
+
create_table "sessions", :force => true do |t|
|
32
|
+
t.column "session_id", :string
|
33
|
+
t.column "data", :text
|
34
|
+
t.column "updated_at", :datetime
|
35
|
+
end
|
36
|
+
|
37
|
+
add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
|
38
|
+
|
39
|
+
create_table "users", :force => true do |t|
|
40
|
+
t.column "name", :string
|
41
|
+
t.column "hashed_password", :string
|
42
|
+
t.column "salt", :string
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/Users/dave/ruby1.8/bin/ruby
|
2
|
+
#---
|
3
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
4
|
+
# We make no guarantees that this code is fit for any purpose.
|
5
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
6
|
+
#---
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
|
9
|
+
|
10
|
+
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
|
11
|
+
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
|
12
|
+
require "dispatcher"
|
13
|
+
|
14
|
+
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
|
15
|
+
Dispatcher.dispatch
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
7
|
+
require 'admin_controller'
|
8
|
+
require 'amrita2/testsupport'
|
9
|
+
|
10
|
+
# Re-raise errors caught by the controller.
|
11
|
+
class AdminController; def rescue_action(e) raise e end; end
|
12
|
+
|
13
|
+
class AdminControllerTest < Test::Unit::TestCase
|
14
|
+
include Amrita2::RailsTestHelper
|
15
|
+
fixtures :products
|
16
|
+
|
17
|
+
def setup
|
18
|
+
User.create(:name=>'u1', :password=>'abc')
|
19
|
+
User.create(:name=>'u2', :password=>'efg')
|
20
|
+
@controller = AdminController.new
|
21
|
+
@request = ActionController::TestRequest.new
|
22
|
+
@response = ActionController::TestResponse.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_new
|
26
|
+
get :new
|
27
|
+
session[:user_id] = User.find_by_name('u1').id
|
28
|
+
flash[:notice] = 'Please log in'
|
29
|
+
compare_result do
|
30
|
+
get :new
|
31
|
+
@response.body
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_list
|
36
|
+
get :new
|
37
|
+
session[:user_id] = User.find_by_name('u1').id
|
38
|
+
flash[:notice] = 'Please log in'
|
39
|
+
compare_result do
|
40
|
+
get :list
|
41
|
+
@response.body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_show
|
46
|
+
get :new
|
47
|
+
session[:user_id] = User.find_by_name('u1').id
|
48
|
+
flash[:notice] = 'Please log in'
|
49
|
+
compare_result do
|
50
|
+
get :show, :id=>1
|
51
|
+
@response.body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
7
|
+
require 'info_controller'
|
8
|
+
|
9
|
+
# Re-raise errors caught by the controller.
|
10
|
+
class InfoController; def rescue_action(e) raise e end; end
|
11
|
+
|
12
|
+
class InfoControllerTest < Test::Unit::TestCase
|
13
|
+
def setup
|
14
|
+
@controller = InfoController.new
|
15
|
+
@request = ActionController::TestRequest.new
|
16
|
+
@response = ActionController::TestResponse.new
|
17
|
+
end
|
18
|
+
|
19
|
+
# Replace this with your real tests.
|
20
|
+
def test_truth
|
21
|
+
assert true
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
8
|
+
require 'login_controller'
|
9
|
+
|
10
|
+
# Re-raise errors caught by the controller.
|
11
|
+
class LoginController; def rescue_action(e) raise e end; end
|
12
|
+
|
13
|
+
class LoginControllerTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
|
16
|
+
fixtures :users
|
17
|
+
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@controller = LoginController.new
|
21
|
+
@request = ActionController::TestRequest.new
|
22
|
+
@response = ActionController::TestResponse.new
|
23
|
+
end
|
24
|
+
|
25
|
+
# Replace this with your real tests.
|
26
|
+
def test_truth
|
27
|
+
assert true
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
if false
|
32
|
+
|
33
|
+
def test_index
|
34
|
+
get :index
|
35
|
+
assert_response :success
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def test_index_without_user
|
42
|
+
get :index
|
43
|
+
assert_redirected_to :action => "login"
|
44
|
+
assert_equal "Please log in", flash[:notice]
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def test_index_with_user
|
50
|
+
get :index, {}, { :user_id => users(:dave).id }
|
51
|
+
assert_response :success
|
52
|
+
assert_template "index"
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def test_login
|
58
|
+
dave = users(:dave)
|
59
|
+
post :login, :name => dave.name, :password => 'secret'
|
60
|
+
assert_redirected_to :action => "index"
|
61
|
+
assert_equal dave.id, session[:user_id]
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
def test_bad_password
|
67
|
+
dave = users(:dave)
|
68
|
+
post :login, :name => dave.name, :password => 'wrong'
|
69
|
+
assert_template "login"
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
7
|
+
require 'amrita2/testsupport'
|
8
|
+
require 'store_controller'
|
9
|
+
|
10
|
+
# Re-raise errors caught by the controller.
|
11
|
+
class StoreController; def rescue_action(e) raise e end; end
|
12
|
+
|
13
|
+
class StoreControllerTest < Test::Unit::TestCase
|
14
|
+
include Amrita2::RailsTestHelper
|
15
|
+
fixtures :products
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@controller = StoreController.new
|
19
|
+
@request = ActionController::TestRequest.new
|
20
|
+
@response = ActionController::TestResponse.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_index
|
24
|
+
compare_result do
|
25
|
+
get :index
|
26
|
+
assert_response(:success)
|
27
|
+
@response.body
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_post_to_add_to_cart
|
32
|
+
get :index
|
33
|
+
ruby_id = products(:ruby_book).id
|
34
|
+
assert_select "form[action=/store/add_to_cart/#{ruby_id}][method=post]"
|
35
|
+
|
36
|
+
post :add_to_cart, :id=>ruby_id
|
37
|
+
|
38
|
+
compare_result do
|
39
|
+
get :index
|
40
|
+
assert_response(:success)
|
41
|
+
@response.body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_checkout
|
46
|
+
ruby_id = products(:ruby_book).id
|
47
|
+
get :index
|
48
|
+
post :add_to_cart, :id=>ruby_id
|
49
|
+
|
50
|
+
compare_result do
|
51
|
+
get :checkout
|
52
|
+
assert_response(:success)
|
53
|
+
@response.body
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require "#{File.dirname(__FILE__)}/../test_helper"
|
7
|
+
|
8
|
+
class DslUserStoriesTest < ActionController::IntegrationTest
|
9
|
+
fixtures :products
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
DAVES_DETAILS = {
|
14
|
+
:name => "Dave Thomas",
|
15
|
+
:address => "123 The Street",
|
16
|
+
:email => "dave@pragprog.com",
|
17
|
+
:pay_type => "check"
|
18
|
+
}
|
19
|
+
|
20
|
+
|
21
|
+
MIKES_DETAILS = {
|
22
|
+
:name => "Mike Clark",
|
23
|
+
:address => "345 The Avenue",
|
24
|
+
:email => "mike@pragmaticstudio.com",
|
25
|
+
:pay_type => "cc"
|
26
|
+
}
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
def setup
|
31
|
+
LineItem.delete_all
|
32
|
+
Order.delete_all
|
33
|
+
@ruby_book = products(:ruby_book)
|
34
|
+
@rails_book = products(:rails_book)
|
35
|
+
end
|
36
|
+
|
37
|
+
# A user goes to the store index page. They select a product,
|
38
|
+
# adding it to their cart. They then check out, filling in
|
39
|
+
# their details on the checkout form. When they submit,
|
40
|
+
# an order is created in the database containing
|
41
|
+
# their information, along with a single line item
|
42
|
+
# corresponding to the product they added to their cart.
|
43
|
+
|
44
|
+
|
45
|
+
def test_buying_a_product
|
46
|
+
dave = regular_user
|
47
|
+
dave.get "/store/index"
|
48
|
+
dave.is_viewing "index"
|
49
|
+
dave.buys_a @ruby_book
|
50
|
+
dave.has_a_cart_containing @ruby_book
|
51
|
+
dave.checks_out DAVES_DETAILS
|
52
|
+
dave.is_viewing "index"
|
53
|
+
check_for_order DAVES_DETAILS, @ruby_book
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
def test_two_people_buying
|
59
|
+
dave = regular_user
|
60
|
+
mike = regular_user
|
61
|
+
dave.buys_a @ruby_book
|
62
|
+
mike.buys_a @rails_book
|
63
|
+
dave.has_a_cart_containing @ruby_book
|
64
|
+
dave.checks_out DAVES_DETAILS
|
65
|
+
mike.has_a_cart_containing @rails_book
|
66
|
+
check_for_order DAVES_DETAILS, @ruby_book
|
67
|
+
mike.checks_out MIKES_DETAILS
|
68
|
+
check_for_order MIKES_DETAILS, @rails_book
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def regular_user
|
74
|
+
open_session do |user|
|
75
|
+
def user.is_viewing(page)
|
76
|
+
assert_response :success
|
77
|
+
assert_template page
|
78
|
+
end
|
79
|
+
|
80
|
+
def user.buys_a(product)
|
81
|
+
xml_http_request "/store/add_to_cart", :id => product.id
|
82
|
+
assert_response :success
|
83
|
+
end
|
84
|
+
|
85
|
+
def user.has_a_cart_containing(*products)
|
86
|
+
cart = session[:cart]
|
87
|
+
assert_equal products.size, cart.items.size
|
88
|
+
for item in cart.items
|
89
|
+
assert products.include?(item.product)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def user.checks_out(details)
|
94
|
+
post "/store/checkout"
|
95
|
+
assert_response :success
|
96
|
+
assert_template "checkout"
|
97
|
+
|
98
|
+
post_via_redirect "/store/save_order",
|
99
|
+
:order => { :name => details[:name],
|
100
|
+
:address => details[:address],
|
101
|
+
:email => details[:email],
|
102
|
+
:pay_type => details[:pay_type]
|
103
|
+
}
|
104
|
+
assert_response :success
|
105
|
+
assert_template "index"
|
106
|
+
assert_equal 0, session[:cart].items.size
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
def check_for_order(details, *products)
|
113
|
+
order = Order.find_by_name(details[:name])
|
114
|
+
assert_not_nil order
|
115
|
+
|
116
|
+
assert_equal details[:name], order.name
|
117
|
+
assert_equal details[:address], order.address
|
118
|
+
assert_equal details[:email], order.email
|
119
|
+
assert_equal details[:pay_type], order.pay_type
|
120
|
+
|
121
|
+
assert_equal products.size, order.line_items.size
|
122
|
+
for line_item in order.line_items
|
123
|
+
assert products.include?(line_item.product)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require "#{File.dirname(__FILE__)}/../test_helper"
|
7
|
+
|
8
|
+
class UserStoriesTest < ActionController::IntegrationTest
|
9
|
+
fixtures :products
|
10
|
+
|
11
|
+
# A user goes to the index page. They select a product, adding it to their
|
12
|
+
# cart, and check out, filling in their details on the checkout form. When
|
13
|
+
# they submit, an order is created containing their information, along with a
|
14
|
+
# single line item corresponding to the product they added to their cart.
|
15
|
+
|
16
|
+
def test_buying_a_product
|
17
|
+
|
18
|
+
LineItem.delete_all
|
19
|
+
Order.delete_all
|
20
|
+
ruby_book = products(:ruby_book)
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
get "/store/index"
|
25
|
+
assert_response :success
|
26
|
+
assert_template "index"
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
xml_http_request "/store/add_to_cart", :id => ruby_book.id
|
31
|
+
assert_response :success
|
32
|
+
|
33
|
+
cart = session[:cart]
|
34
|
+
assert_equal 1, cart.items.size
|
35
|
+
assert_equal ruby_book, cart.items[0].product
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
post "/store/checkout"
|
40
|
+
assert_response :success
|
41
|
+
assert_template "checkout"
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
post_via_redirect "/store/save_order",
|
46
|
+
:order => { :name => "Dave Thomas",
|
47
|
+
:address => "123 The Street",
|
48
|
+
:email => "dave@pragprog.com",
|
49
|
+
:pay_type => "check" }
|
50
|
+
assert_response :success
|
51
|
+
assert_template "index"
|
52
|
+
assert_equal 0, session[:cart].items.size
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
orders = Order.find(:all)
|
57
|
+
assert_equal 1, orders.size
|
58
|
+
order = orders[0]
|
59
|
+
|
60
|
+
assert_equal "Dave Thomas", order.name
|
61
|
+
assert_equal "123 The Street", order.address
|
62
|
+
assert_equal "dave@pragprog.com", order.email
|
63
|
+
assert_equal "check", order.pay_type
|
64
|
+
|
65
|
+
assert_equal 1, order.line_items.size
|
66
|
+
line_item = order.line_items[0]
|
67
|
+
assert_equal ruby_book, line_item.product
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
7
|
+
require 'store_controller'
|
8
|
+
|
9
|
+
# Re-raise errors caught by the controller.
|
10
|
+
class OrderController; def rescue_action(e) raise e end; end
|
11
|
+
|
12
|
+
class OrderSpeedTest < Test::Unit::TestCase
|
13
|
+
|
14
|
+
DAVES_DETAILS = {
|
15
|
+
:name => "Dave Thomas",
|
16
|
+
:address => "123 The Street",
|
17
|
+
:email => "dave@pragprog.com",
|
18
|
+
:pay_type => "check"
|
19
|
+
}
|
20
|
+
|
21
|
+
|
22
|
+
self.fixture_path = File.join(File.dirname(__FILE__), "../fixtures/performance")
|
23
|
+
fixtures :products
|
24
|
+
|
25
|
+
|
26
|
+
def setup
|
27
|
+
@controller = StoreController.new
|
28
|
+
@request = ActionController::TestRequest.new
|
29
|
+
@response = ActionController::TestResponse.new
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
def test_100_orders
|
35
|
+
Order.delete_all
|
36
|
+
LineItem.delete_all
|
37
|
+
|
38
|
+
|
39
|
+
@controller.logger.silence do
|
40
|
+
|
41
|
+
elapsed_time = Benchmark.realtime do
|
42
|
+
100.downto(1) do |prd_id|
|
43
|
+
cart = Cart.new
|
44
|
+
cart.add_product(Product.find(prd_id))
|
45
|
+
post :save_order,
|
46
|
+
{ :order => DAVES_DETAILS },
|
47
|
+
{ :cart => cart }
|
48
|
+
assert_redirected_to :action => :index
|
49
|
+
end
|
50
|
+
end
|
51
|
+
assert_equal 100, Order.count
|
52
|
+
assert elapsed_time < 3.00
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
|
7
|
+
ENV["RAILS_ENV"] = "test"
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
9
|
+
require 'test_help'
|
10
|
+
|
11
|
+
class Test::Unit::TestCase
|
12
|
+
self.use_transactional_fixtures = true
|
13
|
+
self.use_instantiated_fixtures = false
|
14
|
+
|
15
|
+
# Add more helper methods to be used by all tests here...
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
8
|
+
|
9
|
+
class CartTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
fixtures :products
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def test_add_unique_products
|
16
|
+
cart = Cart.new
|
17
|
+
rails_book = products(:rails_book)
|
18
|
+
ruby_book = products(:ruby_book)
|
19
|
+
cart.add_product rails_book
|
20
|
+
cart.add_product ruby_book
|
21
|
+
assert_equal 2, cart.items.size
|
22
|
+
assert_equal rails_book.price + ruby_book.price, cart.total_price
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
def test_add_duplicate_product
|
28
|
+
cart = Cart.new
|
29
|
+
rails_book = products(:rails_book)
|
30
|
+
cart.add_product rails_book
|
31
|
+
cart.add_product rails_book
|
32
|
+
assert_equal 2*rails_book.price, cart.total_price
|
33
|
+
assert_equal 1, cart.items.size
|
34
|
+
assert_equal 2, cart.items[0].quantity
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
39
|
+
|