Skip to content

Commit

Permalink
Add basic testing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
badosu committed May 2, 2015
1 parent b131027 commit aff57f3
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .test.env
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand All @@ -106,6 +117,7 @@ Structure
- **Assets**: `./assets`
- **Views**: `./views`
- **Migrations**: `./db/migrations`
- **Tests**: `./db/tests`

Examples
--------
Expand Down
15 changes: 11 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions bin/bs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions test/routes/communities_test.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aff57f3

Please sign in to comment.