Skip to content

Commit

Permalink
add tests for rails blog app
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr-ch committed Oct 29, 2018
1 parent 0314c9b commit d96a802
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 1 deletion.
78 changes: 78 additions & 0 deletions test/truffle/ecosystem/blog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash

# get the absolute path of the executable and resolve symlinks
SELF_PATH=$(cd "$(dirname "$0")" && pwd -P)/$(basename "$0")
while [ -h "$SELF_PATH" ]; do
# 1) cd to directory of the symlink
# 2) cd to the directory of where the symlink points
# 3) get the pwd
# 4) append the basename
DIR=$(dirname "$SELF_PATH")
SYM=$(readlink "$SELF_PATH")
SELF_PATH=$(cd "$DIR" && cd "$(dirname "$SYM")" && pwd)/$(basename "$SYM")
done

ecosystem=$(dirname "$SELF_PATH")
truffle=$(dirname "$ecosystem")
test=$(dirname "$truffle")
home=$(dirname "$test")

function jt {
ruby "${home}/tool/jt.rb" "$@"
}

function truffleruby {
jt ruby -S "$@"
}

set -xe

jt gem-test-pack
truffleruby gem install truffleruby-gem-test-pack/gem-cache/bundler-1.16.5.gem --local

# backup bin/rake, which gets overwritten
cp bin/rake bin/rake-original

cd test/truffle/ecosystem/blog

truffleruby bundle config --local cache_path ../../../../truffleruby-gem-test-pack/gem-cache
truffleruby bundle config --local without postgresql mysql
truffleruby bundle config --local build.nokogiri --use-system-libraries

truffleruby bundle install --local --no-cache

truffleruby bin/rails db:setup
truffleruby bin/rails log:clear tmp:clear

truffleruby bin/rails test

if [ -f tmp/pids/server.pid ]
then
kill "$(cat tmp/pids/server.pid)" || true
rm tmp/pids/server.pid
fi

port=57085
truffleruby bundle exec bin/rails server --port="$port" &

function kill_server {
kill %1
kill "$(cat tmp/pids/server.pid)"
}

set +x
url="http://localhost:$port/posts.json"
while ! curl -s "$url";
do
echo -n .
sleep 1
done
set -x

test "$(curl -s "$url")" = '[]'

kill_server

# put back the original
cd "$home"
mv -f bin/rake-original bin/rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class PostsController < ApplicationController

def index
@posts = Post.order(:created_at).all

respond_to do |format|
format.json { render json: @posts }
format.html
end
end

def show
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'test_helper'

class PostsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get '/posts'
assert_response :success

assert_match /Post 1/, @response.body
assert_match /Post 2/, @response.body
end

test "should get show" do
get post_path id: 1
assert_response :success
assert_match /Post 1/, @response.body
end

test "should get new" do
get new_post_path
assert_response :success
end

test "should post create" do
new_body = "= Title\nauthor\n\nBody"
post '/posts', params: { body: new_body }
assert_equal 3, Post.count
assert_redirected_to '/posts/3'
end

test "should delete destroy" do
delete post_path id: 1
assert_redirected_to action: :index
assert_equal Post.all, [Post.find(2)]
end

test "should get edit" do
get post_path id: 1
assert_response :success
end

test "should put update" do
new_body = "= Title\nauthor\n\nBody"
put post_path id: 1, body: new_body
assert_redirected_to post_path(1)
assert_equal Post.find(1).body, new_body
end

test "should delete destroy_all" do
delete destroy_all_posts_path
assert_redirected_to action: :index
assert_predicate Post.all, :empty?
end

end
Empty file.
18 changes: 18 additions & 0 deletions test/truffle/ecosystem/blog/test/fixtures/post.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

post1:
id: 1
body: |
= Post 1
An author
Body of the post 1.
post2:
id: 2
body: |
= Post 2
An author
Body of the post 2.
Empty file.
19 changes: 19 additions & 0 deletions test/truffle/ecosystem/blog/test/models/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
test "title is parsed" do
assert_equal Post.find(1).title, "Post 1"
end

test "author is parsed" do
assert_equal Post.find(1).author, "An author"
end

test "title and author are required" do
post = Post.new(body: "")
assert_not post.valid?
assert_equal post.errors.details,
:title => [{ :error => :blank }],
:author => [{ :error => :blank }]
end
end
2 changes: 1 addition & 1 deletion tool/jt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
MRI_TEST_CEXT_DIR = "#{TRUFFLERUBY_DIR}/test/mri/tests/cext-c"
MRI_TEST_CEXT_LIB_DIR = "#{TRUFFLERUBY_DIR}/.ext/c"

TRUFFLERUBY_GEM_TEST_PACK_VERSION = "965f5e29f433a69557c55bff5832de28710ce31a"
TRUFFLERUBY_GEM_TEST_PACK_VERSION = "f23314cbf560d8578b0c2cbd972b83575ba93cd5"

JDEBUG_PORT = 51819
JDEBUG = "-J-agentlib:jdwp=transport=dt_socket,server=y,address=#{JDEBUG_PORT},suspend=y"
Expand Down

0 comments on commit d96a802

Please sign in to comment.