riot-rails 0.0.10.pre.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG +144 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.markdown +31 -0
  5. data/Rakefile +43 -0
  6. data/VERSION +1 -0
  7. data/lib/riot/action_controller/context_macros/asserts_response.rb +15 -0
  8. data/lib/riot/action_controller/context_middleware.rb +45 -0
  9. data/lib/riot/action_controller/http_methods.rb +19 -0
  10. data/lib/riot/action_controller.rb +4 -0
  11. data/lib/riot/active_record/assertion_macros.rb +3 -0
  12. data/lib/riot/active_record/context_middleware.rb +15 -0
  13. data/lib/riot/active_record/database_macros.rb +58 -0
  14. data/lib/riot/active_record/reflection_macros.rb +106 -0
  15. data/lib/riot/active_record/transactional_middleware.rb +21 -0
  16. data/lib/riot/active_record/validation_macros.rb +187 -0
  17. data/lib/riot/active_record.rb +4 -0
  18. data/lib/riot/rails.rb +1 -0
  19. data/rails/init.rb +1 -0
  20. data/riot-rails.gemspec +130 -0
  21. data/test/action_controller/context_macros/asserts_response_test.rb +35 -0
  22. data/test/action_controller/context_middleware_test.rb +66 -0
  23. data/test/action_controller/delete_request_test.rb +45 -0
  24. data/test/action_controller/get_request_test.rb +37 -0
  25. data/test/action_controller/post_request_test.rb +45 -0
  26. data/test/action_controller/put_request_test.rb +45 -0
  27. data/test/action_controller/restful_delete_request_test.rb +28 -0
  28. data/test/action_controller/restful_get_request_test.rb +25 -0
  29. data/test/action_controller/restful_post_request_test.rb +25 -0
  30. data/test/action_controller/restful_put_request_test.rb +28 -0
  31. data/test/active_record/allowing_values_test.rb +64 -0
  32. data/test/active_record/attribute_is_invalid_test.rb +20 -0
  33. data/test/active_record/belongs_to_test.rb +22 -0
  34. data/test/active_record/context_middleware_test.rb +18 -0
  35. data/test/active_record/has_and_belongs_to_many_test.rb +22 -0
  36. data/test/active_record/has_database_index_on_test.rb +73 -0
  37. data/test/active_record/has_many_test.rb +22 -0
  38. data/test/active_record/has_one_test.rb +22 -0
  39. data/test/active_record/validates_length_of_test.rb +31 -0
  40. data/test/active_record/validates_presence_of_test.rb +14 -0
  41. data/test/active_record/validates_uniqueness_of_test.rb +23 -0
  42. data/test/rails_root/app/controllers/gremlins_controller.rb +21 -0
  43. data/test/rails_root/app/controllers/parties_controller.rb +17 -0
  44. data/test/rails_root/app/controllers/rooms_controller.rb +22 -0
  45. data/test/rails_root/app/views/rendered_templates/foo_bar.html.erb +0 -0
  46. data/test/rails_root/config/database.yml +4 -0
  47. data/test/rails_root/config/environment.rb +46 -0
  48. data/test/rails_root/config/routes.rb +7 -0
  49. data/test/rails_root/db/schema.rb +8 -0
  50. data/test/teststrap.rb +84 -0
  51. data/test/transactional_middleware_test.rb +26 -0
  52. metadata +201 -0
@@ -0,0 +1,25 @@
1
+ require 'teststrap'
2
+
3
+ context "RESTful POST request on resource" do
4
+
5
+ context GremlinsController do
6
+ setup { post("/gremlins", :id => 2) }
7
+
8
+ asserts("request method") { request.request_method }.equals("POST")
9
+ asserts("controller name") { controller.controller_name }.equals("gremlins")
10
+ asserts("action name") { controller.action_name }.equals("create")
11
+ asserts("id param") { controller.params["id"] }.equals("2")
12
+ asserts("response body") { response.body }.equals("makin' money")
13
+ end # on a top level resource
14
+
15
+ context PartiesController do
16
+ setup { post("/gremlins/2/parties", "id" => 3) }
17
+
18
+ asserts("request method") { request.request_method }.equals("POST")
19
+ asserts("controller name") { controller.controller_name }.equals("parties")
20
+ asserts("action name") { controller.action_name }.equals("create")
21
+ asserts("gremlin_id param") { controller.params["gremlin_id"] }.equals("2")
22
+ asserts("id param") { controller.params["id"] }.equals("3")
23
+ asserts("response body") { response.body }.equals("give this monkey what he wants")
24
+ end # on a nested resource
25
+ end # RESTful POST request
@@ -0,0 +1,28 @@
1
+ require 'teststrap'
2
+
3
+ context "RESTful PUT request on resource" do
4
+
5
+ context GremlinsController do
6
+ setup { put("/gremlins/2") }
7
+
8
+ asserts("request method") { request.request_method }.equals("PUT")
9
+ asserts("controller name") { controller.controller_name }.equals("gremlins")
10
+ asserts("action name") { controller.action_name }.equals("update")
11
+ asserts("id param") { controller.params["id"] }.equals("2")
12
+ asserts("response body") { response.body }.equals("savin' money")
13
+ end # on a top level resource
14
+
15
+ context PartiesController do
16
+ setup { put("/gremlins/2/parties/3", "foo" => "bar") }
17
+
18
+ asserts("request method") { request.request_method }.equals("PUT")
19
+ asserts("controller name") { controller.controller_name }.equals("parties")
20
+ asserts("action name") { controller.action_name }.equals("update")
21
+
22
+ asserts("gremlin_id param") { controller.params["gremlin_id"] }.equals("2")
23
+ asserts("id param") { controller.params["id"] }.equals("3")
24
+ asserts("foo param") { controller.params["foo"] }.equals("bar")
25
+
26
+ asserts("response body") { response.body }.equals("i'll put that over here")
27
+ end # on a nested resource
28
+ end # RESTful PUT request
@@ -0,0 +1,64 @@
1
+ require 'teststrap'
2
+
3
+ context "The allow_values_for assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when attribute allows a value") do
8
+ topic.allows_values_for(:email, "a@b.cd").run(Riot::Situation.new)
9
+ end.equals([:pass, ""])
10
+
11
+ should("pass when attribute allows multiple values") do
12
+ topic.allows_values_for(:email, "a@b.cd", "e@f.gh").run(Riot::Situation.new)
13
+ end.equals([:pass, ""])
14
+
15
+ should("fail when attribute is provided a valid and an invalid value") do
16
+ topic.allows_values_for(:email, "a", "e@f.gh").run(Riot::Situation.new)
17
+ end.equals([:fail, %Q{expected :email to allow values ["a"]}, blah, blah])
18
+ end # The allow_values_for assertion macro
19
+
20
+ context "The does_not_allow_values_for assertion macro" do
21
+ setup_test_context
22
+ setup { topic.asserts("room") { Room.new } }
23
+
24
+ should("pass when attribute does not allow a value") do
25
+ topic.does_not_allow_values_for(:email, "a").run(Riot::Situation.new)
26
+ end.equals([:pass, ""])
27
+
28
+ should("pass when attribute does not allow multiple values") do
29
+ topic.does_not_allow_values_for(:email, "a", "e").run(Riot::Situation.new)
30
+ end.equals([:pass, ""])
31
+
32
+ should("fail when attribute is provided a valid and an invalid value") do
33
+ topic.does_not_allow_values_for(:email, "a", "e@f.gh").run(Riot::Situation.new)
34
+ end.equals([:fail, %Q{expected :email not to allow values ["e@f.gh"]}, blah, blah])
35
+ end # The does_not_allow_values_for assertion macro
36
+
37
+ context "The is_invalid_when assertion macro" do
38
+ setup_test_context
39
+ setup { topic.asserts("room") { Room.new } }
40
+
41
+ should("pass when attribute is invalid") do
42
+ topic.is_invalid_when(:email, "fake").run(Riot::Situation.new)
43
+ end.equals([:pass, %Q{attribute :email is invalid}])
44
+
45
+ should("pass when error message equals one in its list of errors") do
46
+ topic.is_invalid_when(:email, "fake", "is invalid").run(Riot::Situation.new)
47
+ end.equals([:pass, %Q{attribute :email is invalid}])
48
+
49
+ should("pass when error message matches one in its list of errors") do
50
+ topic.is_invalid_when(:email, "fake", /invalid/).run(Riot::Situation.new)
51
+ end.equals([:pass, %Q{attribute :email is invalid}])
52
+
53
+ should("fail when attribute is valid") do
54
+ topic.is_invalid_when(:email, "a@b.cd", "is invalid").run(Riot::Situation.new)
55
+ end.equals([:fail, %Q{expected :email to be invalid when value is "a@b.cd"}, blah, blah])
56
+
57
+ should("fail when exact error message not found") do
58
+ topic.is_invalid_when(:email, "fake", "can't be blank").run(Riot::Situation.new)
59
+ end.equals([:fail, %Q{expected :email to be invalid with error message "can't be blank"}, blah, blah])
60
+
61
+ should("fail when error message not matched to returned errors") do
62
+ topic.is_invalid_when(:email, "fake", /blank/).run(Riot::Situation.new)
63
+ end.equals([:fail, %Q{expected :email to be invalid with error message /blank/}, blah, blah])
64
+ end # The is_invalid_when assertion macro
@@ -0,0 +1,20 @@
1
+ require 'teststrap'
2
+
3
+ context "The attribute_is_invalid macro" do
4
+ setup_test_context
5
+
6
+ should("fail when attribute is not invalid") do
7
+ assertion = topic.asserts("room") { Room.new(:location => "barn burner") }
8
+ assertion.attribute_is_invalid(:location, "not yet").run(Riot::Situation.new)
9
+ end.equals([:fail, ":location expected to be invalid", blah, blah])
10
+
11
+ should("fail when attribute is invalid, but the message could not be found") do
12
+ assertion = topic.asserts("room") { Room.new }
13
+ assertion.attribute_is_invalid(:location, "child please").run(Riot::Situation.new)
14
+ end.equals([:fail, %Q{:location is invalid, but "child please" is not a valid error message}, blah, blah])
15
+
16
+ should("pass when attribute is invalid and error message is found") do
17
+ assertion = topic.asserts("room") { Room.new }
18
+ assertion.attribute_is_invalid(:location, "can't be blank").run(Riot::Situation.new)
19
+ end.equals([:pass, %Q{:location is invalid}])
20
+ end # The attribute_is_invalid macro
@@ -0,0 +1,22 @@
1
+ require 'teststrap'
2
+
3
+ context "The belongs_to assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when record has a belongs_to association defined for attribute") do
8
+ topic.belongs_to(:house).run(Riot::Situation.new)
9
+ end.equals([:pass, ":house is a belongs_to association"])
10
+
11
+ should("fail when record does not have a belongs_to association defined for attribute") do
12
+ topic.belongs_to(:someone_else).run(Riot::Situation.new)
13
+ end.equals([:fail, ":someone_else is not a belongs_to association", blah, blah])
14
+
15
+ should("fail when attribute is not a belongs_to, but is a has_one association") do
16
+ topic.belongs_to(:floor).run(Riot::Situation.new)
17
+ end.equals([:fail, ":floor is not a belongs_to association", blah, blah])
18
+
19
+ should("fail when association options are specified, but they do not match the record") do
20
+ topic.belongs_to(:owner, :class_name => "Person").run(Riot::Situation.new)
21
+ end.equals([:fail, %q[expected belongs_to :owner with {:class_name=>"Person"}], blah, blah])
22
+ end # The has_many assertion macro
@@ -0,0 +1,18 @@
1
+ require 'teststrap'
2
+
3
+ context "ActiveRecord middleware" do
4
+ setup_test_context
5
+
6
+ setup do
7
+ situation = Riot::Situation.new
8
+ topic.context(Room) do
9
+ hookup { topic.email = "i.am@chee.se" }
10
+ end.local_run(Riot::SilentReporter.new, situation)
11
+ situation.topic
12
+ end
13
+
14
+ asserts_topic.kind_of(Room)
15
+ asserts(:new_record?)
16
+ asserts(:email).equals("i.am@chee.se")
17
+
18
+ end # ActiveRecord middleware
@@ -0,0 +1,22 @@
1
+ require 'teststrap'
2
+
3
+ context "The has_and_belongs_to_many assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when record has a has_and_belongs_to_many association defined for attribute") do
8
+ topic.has_and_belongs_to_many(:walls).run(Riot::Situation.new)
9
+ end.equals([:pass, ":walls is a has_and_belongs_to_many association"])
10
+
11
+ should("fail when record does not have a has_and_belongs_to_many association defined for attribute") do
12
+ topic.has_and_belongs_to_many(:windows).run(Riot::Situation.new)
13
+ end.equals([:fail, ":windows is not a has_and_belongs_to_many association", blah, blah])
14
+
15
+ should("fail when attribute is not a has_and_belongs_to_many, but is a has_many association") do
16
+ topic.has_and_belongs_to_many(:doors).run(Riot::Situation.new)
17
+ end.equals([:fail, ":doors is not a has_and_belongs_to_many association", blah, blah])
18
+
19
+ should("fail when association options are specified, but they do not match the record") do
20
+ topic.has_and_belongs_to_many(:walls, :join_table => "blueprints").run(Riot::Situation.new)
21
+ end.equals([:fail, %q[expected has_and_belongs_to_many :walls with {:join_table=>"blueprints"}], blah, blah])
22
+ end # The has_and_belongs_to_many assertion macro
@@ -0,0 +1,73 @@
1
+ require 'teststrap'
2
+ require 'ostruct'
3
+
4
+ context "The has_database_index_on macro" do
5
+ setup_test_context
6
+ setup { topic.asserts("room") { Room.new } }
7
+
8
+ hookup do
9
+ MockConnection = Class.new do
10
+ def initialize(index_lookup) @index_lookup = index_lookup; end
11
+ def indexes(table) @index_lookup[table]; end
12
+ end
13
+
14
+ def build_assertion(table, columns, unique=false)
15
+ database_connection = MockConnection.new({table => [ define_index(table, columns, unique) ]})
16
+ # I'll inject your dependency! ;)
17
+ RiotRails::ActiveRecord::HasDatabaseIndexOnMacro.new(database_connection)
18
+ end
19
+
20
+ def define_index(table, columns, unique)
21
+ ActiveRecord::ConnectionAdapters::IndexDefinition.new(table, columns.join(','), unique, columns)
22
+ end
23
+ end
24
+
25
+ should("fail when no index for column") do
26
+ topic.has_database_index_on(:foo).run(Riot::Situation.new)
27
+ end.equals([:fail, %Q{expected index on [:foo]}, blah, blah])
28
+
29
+ should("fail when no index for column set") do
30
+ topic.has_database_index_on(:foo, :bar).run(Riot::Situation.new)
31
+ end.equals([:fail, %Q{expected index on [:foo, :bar]}, blah, blah])
32
+
33
+ should("fail when index found for column, but it's not unique") do
34
+ build_assertion("rooms", ["foo"], false).
35
+ evaluate(Room.new, :foo, :unique => true)
36
+ end.equals([:fail, %Q{expected unique index on [:foo]}, blah, blah])
37
+
38
+ should("fail when index found for column, but it's unique and should not be") do
39
+ build_assertion("rooms", ["foo"], true).
40
+ evaluate(Room.new, :foo, :unique => false)
41
+ end.equals([:fail, %Q{expected index on [:foo]}, blah, blah])
42
+
43
+ should("pass when index found for column") do
44
+ build_assertion("rooms", ["foo"], false).
45
+ evaluate(Room.new, :foo)
46
+ end.equals([:pass, %Q{has index on [:foo]}])
47
+
48
+ should("pass when index found for column set") do
49
+ build_assertion("rooms", ["foo", "bar"], false).
50
+ evaluate(Room.new, :foo, :bar)
51
+ end.equals([:pass, %Q{has index on [:foo, :bar]}])
52
+
53
+ should("pass when unique index found for column") do
54
+ build_assertion("rooms", ["foo"], true).
55
+ evaluate(Room.new, :foo, :unique => true)
56
+ end.equals([:pass, %Q{has unique index on [:foo]}])
57
+
58
+ should("pass when unique index found for column set") do
59
+ build_assertion("rooms", ["foo", "bar"], true).
60
+ evaluate(Room.new, :foo, :bar, :unique => true)
61
+ end.equals([:pass, %Q{has unique index on [:foo, :bar]}])
62
+
63
+ should("pass when non-unique index found for column") do
64
+ build_assertion("rooms", ["foo"], false).
65
+ evaluate(Room.new, :foo, :unique => false)
66
+ end.equals([:pass, %Q{has index on [:foo]}])
67
+
68
+ should("pass when non-unique index found for column set") do
69
+ build_assertion("rooms", ["foo", "bar"], false).
70
+ evaluate(Room.new, :foo, :bar, :unique => false)
71
+ end.equals([:pass, %Q{has index on [:foo, :bar]}])
72
+
73
+ end # The attribute_is_invalid macro
@@ -0,0 +1,22 @@
1
+ require 'teststrap'
2
+
3
+ context "The has_many assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when record has a has_many association defined for attribute") do
8
+ topic.has_many(:doors).run(Riot::Situation.new)
9
+ end.equals([:pass, ":doors is a has_many association"])
10
+
11
+ should("fail when record does not have a has_many association defined for attribute") do
12
+ topic.has_many(:windows).run(Riot::Situation.new)
13
+ end.equals([:fail, ":windows is not a has_many association", blah, blah])
14
+
15
+ should("fail when attribute is not a has_many, but is a has_one association") do
16
+ topic.has_many(:floor).run(Riot::Situation.new)
17
+ end.equals([:fail, ":floor is not a has_many association", blah, blah])
18
+
19
+ should("fail when association options are specified, but they do not match the record") do
20
+ topic.has_many(:doors, :class_name => "Portal").run(Riot::Situation.new)
21
+ end.equals([:fail, %q[expected has_many :doors with {:class_name=>"Portal"}], blah, blah])
22
+ end # The has_many assertion macro
@@ -0,0 +1,22 @@
1
+ require 'teststrap'
2
+
3
+ context "The has_one assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when record has a has_one association defined for attribute") do
8
+ topic.has_one(:floor).run(Riot::Situation.new)
9
+ end.equals([:pass, ":floor is a has_one association"])
10
+
11
+ should("fail when record does not have a has_one association defined for attribute") do
12
+ topic.has_one(:windows).run(Riot::Situation.new)
13
+ end.equals([:fail, ":windows is not a has_one association", blah, blah])
14
+
15
+ should("fail when attribute is not a has_one, but is a has_many association") do
16
+ topic.has_one(:doors).run(Riot::Situation.new)
17
+ end.equals([:fail, ":doors is not a has_one association", blah, blah])
18
+
19
+ should("fail when association options are specified, but they do not match the record") do
20
+ topic.has_one(:floor, :class_name => "Surface").run(Riot::Situation.new)
21
+ end.equals([:fail, %q[expected has_one :floor with {:class_name=>"Surface"}], blah, blah])
22
+ end # The has_one assertion macro
@@ -0,0 +1,31 @@
1
+ require 'teststrap'
2
+
3
+ context "The validates_length_of assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("fail when minimum length causes an error") do
8
+ topic.validates_length_of(:name, (4..15)).run(Riot::Situation.new)
9
+ end.equals([:fail, ":name should be able to be 4 characters", blah, blah])
10
+
11
+ should("fail when value less than minimum value does not cause an error") do
12
+ topic.validates_length_of(:name, (6..15)).run(Riot::Situation.new)
13
+ end.equals([:fail, ":name expected to be more than 5 characters", blah, blah])
14
+
15
+ should("fail when maximum length causes an error") do
16
+ topic.validates_length_of(:name, (5..21)).run(Riot::Situation.new)
17
+ end.equals([:fail, ":name should be able to be 21 characters", blah, blah])
18
+
19
+ should("fail when value greater than maximum value does not cause an error") do
20
+ topic.validates_length_of(:name, (5..19)).run(Riot::Situation.new)
21
+ end.equals([:fail, ":name expected to be less than 20 characters", blah, blah])
22
+
23
+ should("pass when only a value can only be within the specific range") do
24
+ topic.validates_length_of(:name, (5..20)).run(Riot::Situation.new)
25
+ end.equals([:pass, "validates length of :name is within 5..20"])
26
+
27
+ should("pass even when minimum value is zero") do
28
+ topic.validates_length_of(:contents, (0..100)).run(Riot::Situation.new)
29
+ end.equals([:pass, "validates length of :contents is within 0..100"])
30
+
31
+ end # The validates_length_of assertion macro
@@ -0,0 +1,14 @@
1
+ require 'teststrap'
2
+
3
+ context "The validates_presence_of assertion macro" do
4
+ setup_test_context
5
+ setup { topic.asserts("room") { Room.new } }
6
+
7
+ should("pass when attribute requires presence") do
8
+ topic.validates_presence_of(:location).run(Riot::Situation.new)
9
+ end.equals([:pass, "validates presence of :location"])
10
+
11
+ should("fail when attribute does not require presence") do
12
+ topic.validates_presence_of(:contents).run(Riot::Situation.new)
13
+ end.equals([:fail, "expected to validate presence of :contents", blah, blah])
14
+ end # The validates_presence_of assertion macro
@@ -0,0 +1,23 @@
1
+ require 'teststrap'
2
+
3
+ context "The validates_uniqueness_of assertion macro" do
4
+ setup_test_context
5
+
6
+ should("fail without a persisted record") do
7
+ topic.asserts("room") do
8
+ Room.new(:email => "foo@bar.baz")
9
+ end.validates_uniqueness_of(:email).run(Riot::Situation.new)
10
+ end.equals([:fail, "must use a persisted record when testing uniqueness of :email", blah, blah])
11
+
12
+ should("pass with a persisted record") do
13
+ topic.asserts("room") do
14
+ Room.create_with_good_data(:email => "foo@bar.baz")
15
+ end.validates_uniqueness_of(:email).run(Riot::Situation.new)
16
+ end.equals([:pass, ":email is unique"])
17
+
18
+ should("fail with a persisted record but not validating uniqueness") do
19
+ topic.asserts("room") do
20
+ Room.create_with_good_data(:email => "goo@car.caz")
21
+ end.validates_uniqueness_of(:foo).run(Riot::Situation.new)
22
+ end.equals([:fail, "expected to fail because :foo is not unique", blah, blah])
23
+ end # The validates_uniqueness_of assertion macro
@@ -0,0 +1,21 @@
1
+ class GremlinsController < ActionController::Base
2
+ def index
3
+ render :text => "bar"
4
+ end
5
+
6
+ def show
7
+ render :text => "show me the money"
8
+ end
9
+
10
+ def create
11
+ render :text => "makin' money"
12
+ end
13
+
14
+ def update
15
+ render :text => "savin' money"
16
+ end
17
+
18
+ def destroy
19
+ render :text => "spendin' money"
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ class PartiesController < ActionController::Base
2
+ def show
3
+ render :text => "woot"
4
+ end
5
+
6
+ def create
7
+ render :text => "give this monkey what he wants"
8
+ end
9
+
10
+ def update
11
+ render :text => "i'll put that over here"
12
+ end
13
+
14
+ def destroy
15
+ render :text => "all gone"
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ class RoomsController < ActionController::Base
2
+ def index
3
+ render :text => "foo"
4
+ end
5
+
6
+ def create
7
+ render :text => "created #{params.inspect}"
8
+ end
9
+
10
+ def update
11
+ render :text => "updated #{params.inspect}"
12
+ end
13
+
14
+ def destroy
15
+ render :text => "destroyed #{params.inspect}"
16
+ end
17
+
18
+ def echo_params
19
+ serialized_params = (params.keys - ["action"]).sort.map {|k| "#{k}=#{params[k]}" }.join(",")
20
+ render :text => serialized_params
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: "sqlite3"
3
+ database: <%= Rails.root + "db" + "test.db" %>
4
+
@@ -0,0 +1,46 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'sqlite3'
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ # require "action_mailer/railtie"
7
+ # require "active_resource/railtie"
8
+
9
+ module RiotRails
10
+ class Application < Rails::Application
11
+ config.root = File.expand_path(File.join(File.dirname(__FILE__), ".."))
12
+
13
+ config.session_store :cookie_store, {:key => "_riotrails_session"}
14
+ config.secret_token = "i own you." * 3
15
+ end
16
+ end
17
+
18
+ RiotRails::Application.initialize!
19
+
20
+ require File.join(Rails.root, "config", "routes.rb")
21
+
22
+ # Logging stuff
23
+
24
+ class NilIO
25
+ def write(*args); end
26
+ def close(*args); end
27
+ def puts(*args); end
28
+ def path; nil; end
29
+ def fsync; 0; end
30
+ def to_s; "hello"; end
31
+ def sync; true; end
32
+ def sync=(arg); arg; end
33
+ end
34
+
35
+ def shhh(&block)
36
+ orig_out = $stdout
37
+ $stdout = NilIO.new
38
+ yield
39
+ $stdout = orig_out
40
+ end
41
+
42
+ shhh do
43
+ load(File.join(Rails.root, "db", "schema.rb"))
44
+ end
45
+
46
+ ActiveRecord::Base.logger = Logger.new(NilIO.new)
@@ -0,0 +1,7 @@
1
+ RiotRails::Application.routes.draw do |map|
2
+ resources :gremlins do
3
+ resources :parties
4
+ end
5
+
6
+ match ':controller(/:action(/:id(.:format)))'
7
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table :rooms, :force => true do |t|
3
+ t.string :location
4
+ t.string :email
5
+ t.string :contents
6
+ t.string :name
7
+ end
8
+ end
data/test/teststrap.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+ require 'ostruct'
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
+
7
+ #
8
+ # Setup faux Rails environment
9
+
10
+ require(Pathname(__FILE__).dirname + 'rails_root' + 'config' + 'environment')
11
+
12
+ #
13
+ # Model definition
14
+
15
+ class Room < ActiveRecord::Base
16
+ validates_presence_of :location
17
+ validates_format_of :email, :with => /^\w+@\w+\.\w+$/
18
+ validates_uniqueness_of :email
19
+ validates_length_of :name, :within => 5..20
20
+ validates_length_of :contents, :within => 0..100, :allow_blank => true
21
+
22
+ has_many :doors, :class_name => 'Porthole'
23
+ has_one :floor, :class_name => "Substrate"
24
+ has_and_belongs_to_many :walls, :join_table => "floorplans"
25
+ belongs_to :house
26
+ belongs_to :owner, :class_name => 'SomethingElse'
27
+
28
+ def self.create_with_good_data(attributes={})
29
+ create!({:location => "a", :email => "a@b.c", :name => "Junior"}.merge(attributes))
30
+ end
31
+ end
32
+
33
+ #
34
+ # Blah == anything, whatever. Always passes an equality test
35
+
36
+ class Blah
37
+ def ==(o) true; end
38
+ def inspect; "<blah>"; end
39
+ alias :to_s :inspect
40
+ end
41
+
42
+ class Object
43
+ def blah; Blah.new; end
44
+ end
45
+
46
+ #
47
+ # Riot setup
48
+
49
+ require 'riot/rails'
50
+ require 'riot/active_record'
51
+ require 'riot/action_controller'
52
+
53
+ Riot.dots if ENV["TM_MODE"]
54
+
55
+ module RiotRails
56
+ module Context
57
+ def setup_test_context(context_description="test context")
58
+ setup { Riot::Context.new(context_description) {} }
59
+ end
60
+
61
+ def setup_for_assertion_test(&block)
62
+ hookup { topic.setup(&block).run(@situation) }
63
+ end
64
+
65
+ def hookup_for_assertion_test(&block)
66
+ hookup { topic.hookup(&block).run(@situation) }
67
+ end
68
+
69
+ def assertion_test_passes(description, success_message="", &block)
70
+ should("pass #{description}") do
71
+ instance_eval(&block).run(@situation)
72
+ end.equals([:pass, success_message])
73
+ end
74
+
75
+ def assertion_test_fails(description, failure_message, &block)
76
+ should("fail #{description}") do
77
+ instance_eval(&block).run(@situation)
78
+ end.equals([:fail, failure_message, blah, blah])
79
+ end
80
+
81
+ end # Context
82
+ end # RiotRails
83
+
84
+ Riot::Context.instance_eval { include RiotRails::Context }
@@ -0,0 +1,26 @@
1
+ require 'teststrap'
2
+
3
+ context "Transactional middleware" do
4
+ hookup do
5
+ ActiveRecord::Base.instance_eval do
6
+ # Hijacking transaction and just letting stuff fall through
7
+ def transaction(&block) yield; end
8
+ end
9
+ end
10
+
11
+ context "when :transactional is not set" do
12
+ setup { Riot::Context.new("Room") {} }
13
+
14
+ asserts("executing context does not raise errors") do
15
+ topic.local_run(Riot::SilentReporter.new, Riot::Situation.new)
16
+ end
17
+ end # when :transactional is not set
18
+
19
+ context "when :transactional is set to true" do
20
+ setup { Riot::Context.new("Room") { set(:transactional, true) } }
21
+
22
+ asserts("executing context") do
23
+ topic.local_run(Riot::SilentReporter.new, Riot::Situation.new)
24
+ end.raises(ActiveRecord::Rollback)
25
+ end # when :transactional is set to true
26
+ end # Transactional middleware