From aff57f3180614428c48d581026598737393eb26e Mon Sep 17 00:00:00 2001 From: Amadeus Folego Date: Sat, 2 May 2015 19:14:45 -0300 Subject: [PATCH] Add basic testing functionality --- .test.env | 3 ++ README.md | 12 +++++++ Rakefile | 15 ++++++--- bin/bs | 6 ++-- models.rb | 2 +- test/routes/communities_test.rb | 57 +++++++++++++++++++++++++++++++++ test/test_helper.rb | 18 +++++++++++ 7 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 .test.env create mode 100644 test/routes/communities_test.rb create mode 100644 test/test_helper.rb diff --git a/.test.env b/.test.env new file mode 100644 index 0000000..8c8da17 --- /dev/null +++ b/.test.env @@ -0,0 +1,3 @@ +SECRET=abad1dea # < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo; +RACK_ENV=test # staging|production +DATABASE_URL=sqlite:///tmp/test.sqlite # DATABASE_URL=postgres://[user]:[password]@[host]:[port]/yogurt_development diff --git a/README.md b/README.md index 248de5b..271f7e8 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,17 @@ locally (without `bundle exec` if you're using gst). Visit `/communities`. +Testing +------- + +Use the `.test.env` file to configure the testing environment. + +Migrate with: `BS=.test.env bin/bs bundle exec rake db:migrate`. + +Test with: `BS=.test.env bin/bs bundle exec rake test`. + +Individually: `BS=.test.env bin/bs ruby test/routes/communities_test.rb`. + Structure --------- @@ -106,6 +117,7 @@ Structure - **Assets**: `./assets` - **Views**: `./views` - **Migrations**: `./db/migrations` +- **Tests**: `./db/tests` Examples -------- diff --git a/Rakefile b/Rakefile index 258d15e..6f2e0e9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = Dir.glob('test/**/*_test.rb') +end + namespace :assets do desc "Precompile the assets" task :precompile do @@ -9,15 +15,16 @@ end namespace :db do desc "Run migrations" task :migrate, [:version] do |t, args| - require "sequel" + require "./models" + Sequel.extension :migration - db = Sequel.connect(ENV.fetch("DATABASE_URL")) + if args[:version] puts "Migrating to version #{args[:version]}" - Sequel::Migrator.run(db, "db/migrations", target: args[:version].to_i) + Sequel::Migrator.run(DB, "db/migrations", target: args[:version].to_i) else puts "Migrating to latest" - Sequel::Migrator.run(db, "db/migrations") + Sequel::Migrator.run(DB, "db/migrations") end end diff --git a/bin/bs b/bin/bs index ab68001..d6b3aa0 100755 --- a/bin/bs +++ b/bin/bs @@ -1,7 +1,9 @@ #!/bin/bash -if [[ -f .env ]]; then - eval "$(awk '/^[A-Z]/ { print "export " $0 }' .env)" +BS=${BS:-'.env'} + +if [[ -f $BS ]]; then + eval "$(awk '/^[A-Z]/ { print "export " $0 }' $BS)" if [[ $# -gt 0 ]]; then diff --git a/models.rb b/models.rb index 04d4ce2..acdfd45 100644 --- a/models.rb +++ b/models.rb @@ -4,7 +4,7 @@ require './config/unreloader' DB = Sequel.connect(ENV['DATABASE_URL']).tap do |config| - config.loggers << Logger.new($stdout) + config.loggers << Logger.new($stdout) unless env.test? end # See: http://permalink.gmane.org/gmane.comp.lang.ruby.sequel/2118 diff --git a/test/routes/communities_test.rb b/test/routes/communities_test.rb new file mode 100644 index 0000000..87880da --- /dev/null +++ b/test/routes/communities_test.rb @@ -0,0 +1,57 @@ +require './test/test_helper' + +class CommunitiesTest < MiniTest::Test + include Rack::Test::Methods + + def app + Yogurt.app + end + + def test_index_shows_created_community + community_name = "A nice community" + community = Community.create name: community_name + + get "/communities" + + assert last_response.ok? + assert last_response.body.include? community_name + end + + def test_edit_existing_community + community_name = "A nice community" + community = Community.create name: community_name + + get "/communities/#{community.id}/edit" + + assert last_response.ok? + assert last_response.body.include? community_name + end + + def test_edit_absent_community + get "/communities/999/edit" + + refute last_response.ok? + assert last_response.status == 404 + end + + def test_delete_existing_community + community_name = "A nice community" + community = Community.create name: community_name + + id = community.id + + delete "/communities/#{id}" + + follow_redirect! + + assert Community[id].nil? + assert_equal last_request.path, "/communities" + end + + def test_delete_absent_community + delete "/communities/999" + + refute last_response.ok? + assert last_response.status == 404 + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..43bc89a --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,18 @@ +require "minitest/autorun" +require "rack/test" +require "pry" + +require './models.rb' +require './yogurt.rb' + +class MiniTest::Test + alias_method :_original_run, :run + + def run(*args, &block) + result = nil + Sequel::Model.db.transaction(:rollback => :always, :auto_savepoint=>true) do + result = _original_run(*args, &block) + end + result + end +end