transactionata 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +62 -0
  5. data/Rakefile +33 -0
  6. data/lib/transactionata/version.rb +3 -0
  7. data/lib/transactionata.rb +38 -0
  8. data/test/helper.rb +9 -0
  9. data/test/rails2/Gemfile +14 -0
  10. data/test/rails2/Rakefile +10 -0
  11. data/test/rails2/app/controllers/application_controller.rb +3 -0
  12. data/test/rails2/app/controllers/posts_controller.rb +83 -0
  13. data/test/rails2/app/controllers/users_controller.rb +83 -0
  14. data/test/rails2/app/helpers/application_helper.rb +3 -0
  15. data/test/rails2/app/helpers/posts_helper.rb +2 -0
  16. data/test/rails2/app/helpers/users_helper.rb +2 -0
  17. data/test/rails2/app/models/post.rb +6 -0
  18. data/test/rails2/app/models/user.rb +9 -0
  19. data/test/rails2/app/views/layouts/application.html.erb +14 -0
  20. data/test/rails2/app/views/layouts/posts.html.erb +17 -0
  21. data/test/rails2/app/views/layouts/users.html.erb +17 -0
  22. data/test/rails2/app/views/posts/edit.html.erb +24 -0
  23. data/test/rails2/app/views/posts/index.html.erb +24 -0
  24. data/test/rails2/app/views/posts/new.html.erb +23 -0
  25. data/test/rails2/app/views/posts/show.html.erb +18 -0
  26. data/test/rails2/app/views/users/edit.html.erb +28 -0
  27. data/test/rails2/app/views/users/index.html.erb +26 -0
  28. data/test/rails2/app/views/users/new.html.erb +27 -0
  29. data/test/rails2/app/views/users/show.html.erb +23 -0
  30. data/test/rails2/config/boot.rb +128 -0
  31. data/test/rails2/config/database.yml +22 -0
  32. data/test/rails2/config/environment.rb +38 -0
  33. data/test/rails2/config/environments/development.rb +17 -0
  34. data/test/rails2/config/environments/production.rb +28 -0
  35. data/test/rails2/config/environments/test.rb +28 -0
  36. data/test/rails2/config/initializers/cookie_verification_secret.rb +7 -0
  37. data/test/rails2/config/initializers/new_rails_defaults.rb +21 -0
  38. data/test/rails2/config/initializers/session_store.rb +15 -0
  39. data/test/rails2/config/locales/en.yml +5 -0
  40. data/test/rails2/config/preinitializer.rb +20 -0
  41. data/test/rails2/config/routes.rb +49 -0
  42. data/test/rails2/db/migrate/20110214150055_create_posts.rb +15 -0
  43. data/test/rails2/db/migrate/20110214150128_create_users.rb +16 -0
  44. data/test/rails2/script/about +4 -0
  45. data/test/rails2/script/console +3 -0
  46. data/test/rails2/script/dbconsole +3 -0
  47. data/test/rails2/script/destroy +3 -0
  48. data/test/rails2/script/generate +3 -0
  49. data/test/rails2/script/performance/benchmarker +3 -0
  50. data/test/rails2/script/performance/profiler +3 -0
  51. data/test/rails2/script/plugin +3 -0
  52. data/test/rails2/script/runner +3 -0
  53. data/test/rails2/script/server +3 -0
  54. data/test/rails2/test/factories/posts_factory.rb +4 -0
  55. data/test/rails2/test/factories/users_factory.rb +7 -0
  56. data/test/rails2/test/fixtures/posts.yml +9 -0
  57. data/test/rails2/test/fixtures/users.yml +1 -0
  58. data/test/rails2/test/functional/posts_controller_test.rb +57 -0
  59. data/test/rails2/test/functional/users_controller_test.rb +57 -0
  60. data/test/rails2/test/integration/ui_test.rb +26 -0
  61. data/test/rails2/test/test_helper.rb +38 -0
  62. data/test/rails2/test/unit/helpers/posts_helper_test.rb +4 -0
  63. data/test/rails2/test/unit/helpers/users_helper_test.rb +4 -0
  64. data/test/rails2/test/unit/post_test.rb +42 -0
  65. data/test/rails2/test/unit/user_test.rb +32 -0
  66. data/test/rails3/Gemfile +37 -0
  67. data/test/rails3/Rakefile +7 -0
  68. data/test/rails3/app/controllers/application_controller.rb +3 -0
  69. data/test/rails3/app/controllers/posts_controller.rb +83 -0
  70. data/test/rails3/app/controllers/users_controller.rb +83 -0
  71. data/test/rails3/app/helpers/application_helper.rb +2 -0
  72. data/test/rails3/app/helpers/posts_helper.rb +2 -0
  73. data/test/rails3/app/helpers/users_helper.rb +2 -0
  74. data/test/rails3/app/models/post.rb +6 -0
  75. data/test/rails3/app/models/user.rb +9 -0
  76. data/test/rails3/app/views/layouts/application.html.erb +14 -0
  77. data/test/rails3/app/views/posts/_form.html.erb +25 -0
  78. data/test/rails3/app/views/posts/edit.html.erb +6 -0
  79. data/test/rails3/app/views/posts/index.html.erb +25 -0
  80. data/test/rails3/app/views/posts/new.html.erb +5 -0
  81. data/test/rails3/app/views/posts/show.html.erb +15 -0
  82. data/test/rails3/app/views/users/_form.html.erb +33 -0
  83. data/test/rails3/app/views/users/edit.html.erb +6 -0
  84. data/test/rails3/app/views/users/index.html.erb +29 -0
  85. data/test/rails3/app/views/users/new.html.erb +5 -0
  86. data/test/rails3/app/views/users/show.html.erb +25 -0
  87. data/test/rails3/config/application.rb +42 -0
  88. data/test/rails3/config/boot.rb +6 -0
  89. data/test/rails3/config/database.yml +22 -0
  90. data/test/rails3/config/environment.rb +5 -0
  91. data/test/rails3/config/environments/development.rb +26 -0
  92. data/test/rails3/config/environments/test.rb +35 -0
  93. data/test/rails3/config/initializers/secret_token.rb +7 -0
  94. data/test/rails3/config/initializers/session_store.rb +8 -0
  95. data/test/rails3/config/locales/en.yml +5 -0
  96. data/test/rails3/config/routes.rb +62 -0
  97. data/test/rails3/config.ru +4 -0
  98. data/test/rails3/db/.gitkeep +0 -0
  99. data/test/rails3/db/migrate/20110212112626_create_posts.rb +14 -0
  100. data/test/rails3/db/migrate/20110212120554_create_users.rb +16 -0
  101. data/test/rails3/db/migrate/20110213220543_add_user_reference_to_post.rb +9 -0
  102. data/test/rails3/script/rails +6 -0
  103. data/test/rails3/test/factories/posts_factory.rb +4 -0
  104. data/test/rails3/test/factories/users_factory.rb +7 -0
  105. data/test/rails3/test/fixtures/posts.yml +9 -0
  106. data/test/rails3/test/fixtures/users.yml +1 -0
  107. data/test/rails3/test/functional/posts_controller_test.rb +57 -0
  108. data/test/rails3/test/functional/users_controller_test.rb +57 -0
  109. data/test/rails3/test/integration/ui_test.rb +26 -0
  110. data/test/rails3/test/performance/browsing_test.rb +9 -0
  111. data/test/rails3/test/test_helper.rb +13 -0
  112. data/test/rails3/test/unit/helpers/posts_helper_test.rb +4 -0
  113. data/test/rails3/test/unit/helpers/users_helper_test.rb +4 -0
  114. data/test/rails3/test/unit/post_test.rb +42 -0
  115. data/test/rails3/test/unit/user_test.rb +32 -0
  116. data/test/test_rails2_integration.rb +26 -0
  117. data/test/test_rails3_integration.rb +26 -0
  118. data/transactionata.gemspec +24 -0
  119. metadata +316 -0
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ doc
7
+ rdoc
8
+ db
9
+ log
10
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in transactionata.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Christoph Olszowka
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = Transactionata
2
+
3
+ Transactional dynamic test data for rails
4
+
5
+ == Synopsis
6
+
7
+ Fixture replacements like Factory Girl are great, but when you have to set up complex test data
8
+ or use Shoulda and your test records are created for each should block, you'll get a huge performance
9
+ hit.
10
+
11
+ Rails' Fixtures are loaded only once and then rolled back via database transaction after every test - why
12
+ not leverage this for your Factory-built test data as well? Transactionata allows you to do so:
13
+
14
+ class UserTest < ActiveSupport::TestCase
15
+ test_data do
16
+ Factory.create(:user, :login => 'colszowka')
17
+ end
18
+
19
+ should "destroy a User" do
20
+ assert User.find_by_login('colszowka').destroy
21
+ end
22
+
23
+ should "have a User" do
24
+ assert User.find_by_login('colszowka')
25
+ end
26
+
27
+ context "The User" do
28
+ subject { @user ||= User.find_by_login!('colszowka') }
29
+
30
+ should "not be a new record" do
31
+ assert !@user.new_record?
32
+ end
33
+ end
34
+ end
35
+
36
+ This will run the code block given to test_data when Fixtures are loaded and 'protect' the data using
37
+ transactions, just like Fixtures do, giving you huge performance gains if you're using factories a lot.
38
+
39
+ For the (FactoryGirl/Shoulda-heavy) Ruby Toolbox 2 test suite, the time for a complete test run went from 5 minutes
40
+ to 50 seconds by just moving test data generation into centralized blocks for each test case using Transactionata.
41
+
42
+ == Usage
43
+
44
+ Add it to your Gemfile and bundle install.
45
+
46
+ gem 'transactionata', :group => :test
47
+
48
+ Then move test data generation to a test_data block as shown above, using finders instead of creating the records
49
+ in the setup/subject blocks.
50
+
51
+ Of course, all of this is completely applicable to default Ruby/Rails unit tests, you don't have to use Shoulda for
52
+ transactional data setup to work.
53
+
54
+ == Compatibility
55
+
56
+ Transactionata's test suite runs against Rails 2.3.10 and 3.0.4 on Ruby 1.9.2, 1.8.7 and REE. Since no black magic is
57
+ involved, it should work on other platforms and versions as well.
58
+
59
+
60
+ == Copyright
61
+
62
+ Copyright (c) 2011 Christoph Olszowka. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/test_*.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ namespace :test do
12
+ desc "Prepares the test rails apps: Install bundle and migrate sqlite db"
13
+ task :prepare do
14
+ Dir['test/rails*'].each do |dir|
15
+ Dir.chdir(dir) do
16
+ system "bundle install" if `bundle check` and not $?.success?
17
+ system "rake db:migrate"
18
+ system "rake db:test:clone_structure"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ task :test => :"test:prepare"
26
+ task :default => :test
27
+
28
+ require 'rake/rdoctask'
29
+ Rake::RDocTask.new do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.rdoc_files.include('README*')
32
+ rdoc.rdoc_files.include('lib/**/*.rb')
33
+ end
@@ -0,0 +1,3 @@
1
+ module Transactionata
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ module Transactionata
2
+ #
3
+ # Hook for creating test data ONCE alongside fixtures that will then be rolled back
4
+ # via transactions after each test, so you can set up complex data via Factories etc.
5
+ # without the speed drop.
6
+ #
7
+ # Please note that you'll have to set up empty fixture files (and load them, see
8
+ # the list of fixtures above) in order to clean up the database before you launch
9
+ # your tests.
10
+ #
11
+ # Usage: In your test class, do:
12
+ # test_data do
13
+ # Factory.create(:foobar)
14
+ # # etc...
15
+ # end
16
+ #
17
+ # The foobar record will be available in all your tests and rolls back even if you
18
+ # modify it in your test cases thanks to transactions.
19
+ #
20
+ def test_data(&blk)
21
+ self.class_eval do
22
+ class << self
23
+ attr_accessor :test_data_block
24
+ end
25
+
26
+ def load_fixtures
27
+ super
28
+ self.class.test_data_block.call
29
+ Fixtures.reset_cache # Required to enforce purging tables for every test file
30
+ end
31
+ end
32
+ self.test_data_block = blk
33
+ end
34
+ end
35
+
36
+ if defined?(ActiveSupport::TestCase)
37
+ ActiveSupport::TestCase.send :extend, Transactionata
38
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'transactionata'
4
+
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,14 @@
1
+ source :gemcutter
2
+ gem "rails", "2.3.10"
3
+ gem "sqlite3-ruby", :require => "sqlite3"
4
+
5
+ group :development do
6
+ # bundler requires these gems in development
7
+ # gem "rails-footnotes"
8
+ end
9
+
10
+ group :test do
11
+ gem 'shoulda', '2.10.3'
12
+ gem 'factory_girl'
13
+ gem 'transactionata', :path => '../../'
14
+ end
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ end
@@ -0,0 +1,83 @@
1
+ class PostsController < ApplicationController
2
+ # GET /posts
3
+ # GET /posts.xml
4
+ def index
5
+ @posts = Post.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @posts }
10
+ end
11
+ end
12
+
13
+ # GET /posts/1
14
+ # GET /posts/1.xml
15
+ def show
16
+ @post = Post.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.xml { render :xml => @post }
21
+ end
22
+ end
23
+
24
+ # GET /posts/new
25
+ # GET /posts/new.xml
26
+ def new
27
+ @post = Post.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @post }
32
+ end
33
+ end
34
+
35
+ # GET /posts/1/edit
36
+ def edit
37
+ @post = Post.find(params[:id])
38
+ end
39
+
40
+ # POST /posts
41
+ # POST /posts.xml
42
+ def create
43
+ @post = Post.new(params[:post])
44
+
45
+ respond_to do |format|
46
+ if @post.save
47
+ format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
48
+ format.xml { render :xml => @post, :status => :created, :location => @post }
49
+ else
50
+ format.html { render :action => "new" }
51
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /posts/1
57
+ # PUT /posts/1.xml
58
+ def update
59
+ @post = Post.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @post.update_attributes(params[:post])
63
+ format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
64
+ format.xml { head :ok }
65
+ else
66
+ format.html { render :action => "edit" }
67
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /posts/1
73
+ # DELETE /posts/1.xml
74
+ def destroy
75
+ @post = Post.find(params[:id])
76
+ @post.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to(posts_url) }
80
+ format.xml { head :ok }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,83 @@
1
+ class UsersController < ApplicationController
2
+ # GET /users
3
+ # GET /users.xml
4
+ def index
5
+ @users = User.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @users }
10
+ end
11
+ end
12
+
13
+ # GET /users/1
14
+ # GET /users/1.xml
15
+ def show
16
+ @user = User.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.xml { render :xml => @user }
21
+ end
22
+ end
23
+
24
+ # GET /users/new
25
+ # GET /users/new.xml
26
+ def new
27
+ @user = User.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @user }
32
+ end
33
+ end
34
+
35
+ # GET /users/1/edit
36
+ def edit
37
+ @user = User.find(params[:id])
38
+ end
39
+
40
+ # POST /users
41
+ # POST /users.xml
42
+ def create
43
+ @user = User.new(params[:user])
44
+
45
+ respond_to do |format|
46
+ if @user.save
47
+ format.html { redirect_to(@user, :notice => 'User was successfully created.') }
48
+ format.xml { render :xml => @user, :status => :created, :location => @user }
49
+ else
50
+ format.html { render :action => "new" }
51
+ format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /users/1
57
+ # PUT /users/1.xml
58
+ def update
59
+ @user = User.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @user.update_attributes(params[:user])
63
+ format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
64
+ format.xml { head :ok }
65
+ else
66
+ format.html { render :action => "edit" }
67
+ format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /users/1
73
+ # DELETE /users/1.xml
74
+ def destroy
75
+ @user = User.find(params[:id])
76
+ @user.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to(users_url) }
80
+ format.xml { head :ok }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,2 @@
1
+ module PostsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,6 @@
1
+ class Post < ActiveRecord::Base
2
+ validate :title, :presence => true
3
+ validate :body, :presence => true
4
+
5
+ belongs_to :user
6
+ end
@@ -0,0 +1,9 @@
1
+ class User < ActiveRecord::Base
2
+ attr_accessor :password_confirmation
3
+ validate :name, :presence => true
4
+ validate :login, :presence => true, :uniqueness => true
5
+ validate :email, :presence => true, :uniqueness => true
6
+ validate :password, :presence => true, :confirmation => true
7
+
8
+ has_many :posts
9
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails3</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>