Skip to content

Commit

Permalink
Tests are working again!
Browse files Browse the repository at this point in the history
* Added "rake setup" task to create db/table
* "rake [test]" to run them
  • Loading branch information
afair committed Jun 9, 2014
1 parent f43ade8 commit 0bf17ef
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 38 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ PATH
remote: .
specs:
postgresql_cursor (0.5.0)
activerecord (> 4.0.0)
pg
activerecord (>= 3.2.0)

GEM
remote: https://rubygems.org/
Expand All @@ -28,14 +27,15 @@ GEM
minitest (5.3.3)
pg (0.17.1)
rake (10.3.1)
thread_safe (0.3.3)
tzinfo (1.1.0)
thread_safe (0.3.4)
tzinfo (1.2.1)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
minitest
pg
postgresql_cursor!
rake
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
require "bundler/gem_tasks"
require "bundler/setup"
require 'rake/testtask'

task :default => :test

desc "Run the Test Suite, toot suite"
task :test do
sh "ruby test/test_*"
end

desc "Open and IRB Console with the gem loaded"
task :console do
require 'irb'
ARGV.clear
IRB.start
end

desc "Setup testing database and table"
task :setup do
sh %q(createdb postgresql_cursor_test)
sh %Q<echo "create table products ( id serial primary key);" | psql postgresql_cursor_test>
end
6 changes: 3 additions & 3 deletions lib/postgresql_cursor/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# until: value - Exits loop when block returns this value.
#
# Exmaples:
# PostgreSQLCursor.new("select ...").each { |hash| ... }
# PostgreSQLCursor::Cursor.new("select ...").each { |hash| ... }
# ActiveRecordModel.where(...).each_row { |hash| ... }
# ActiveRecordModel.each_row_by_sql("select ...") { |hash| ... }
# ActiveRecordModel.each_instance_by_sql("select ...") { |model| ... }
Expand All @@ -33,13 +33,13 @@ class Cursor
#
# Examples
#
# PostgreSQLCursor.new("select ....")
# PostgreSQLCursor::Cursor.new("select ....")
#
# Returns the cursor object when called with new.
def initialize(sql, options={})
@sql = sql
@options = options
@connection = @options.fetch(:connection) { ActiveRecord::Base.connection }
@connection = @options.fetch(:connection) { ::ActiveRecord::Base.connection }
@count = 0
end

Expand Down
1 change: 1 addition & 0 deletions postgresql_cursor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
#spec.add_dependency "pg" # Remove this for jruby, which should specify 'activerecord-jdbcpostgresql-adapter'
spec.add_dependency "activerecord", ">= 3.2.0"

spec.add_development_dependency "pg"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
end
3 changes: 3 additions & 0 deletions test-app/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#bundle install
if [ "$1" = "irb" ]; then
bundle exec irb -Ilib -r postgresql_cursor
elif [ "$1" = "setup" ]; then
createdb postgresql_cursor_test
echo "create table products ( id serial);" | psql postgresql_cursor_test
else
bundle exec ruby app.rb
fi
29 changes: 14 additions & 15 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'minitest'
require 'active_record'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'postgresql_cursor'

ActiveRecord::Base.establish_connection :database=>'allen_test', :adapter=>'postgresql', :username=>'allen'
class Model < ActiveRecord::Base
#set_table_name "records"
self.table_name = "records"

ActiveRecord::Base.establish_connection(adapter: 'postgresql',
database: ENV['TEST_DATABASE'] || 'postgresql_cursor_test',
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')
class Product < ActiveRecord::Base
# create table records (id serial primary key);
def self.generate(max=1_000_000)
max.times do
connection.execute("insert into records values (nextval('records_id_seq'::regclass))")
def self.generate(max=1_000)
max.times do |i|
connection.execute("insert into products values (#{i+1})")
end
end
end

Model.generate(1000) if Model.count == 0

class MiniTest::Unit::TestCase
end
Product.destroy_all
Product.generate(1000)
#class MiniTest::Unit::TestCase
#end
38 changes: 22 additions & 16 deletions test/test_postgresql_cursor.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
require 'helper'
# database: allen_test
# create table records ( id serial);
################################################################################
# Before running test, set up the test db & table with:
# rake setup
# or create the database manually if your environment doesn't permit
################################################################################
require_relative 'helper'
require 'minitest/autorun'
require 'minitest/pride'

class TestPostgresqlCursor < Minitest::Test

def test_each
c = PostgreSQLCursor.new("select * from records order by 1")
c = PostgreSQLCursor::Cursor.new("select * from products order by 1")
nn = 0
n = c.each { nn += 1}
assert_equal nn, n
end

def test_enumerables
assert_equal true, PostgreSQLCursor.new("select * from records order by 1").any?
assert_equal false, PostgreSQLCursor.new("select * from records where id<0").any?
assert_equal true, PostgreSQLCursor::Cursor.new("select * from products order by 1").any?
assert_equal false, PostgreSQLCursor::Cursor.new("select * from products where id<0").any?
end

def test_each_while_until
c = PostgreSQLCursor.new("select * from records order by 1", until:true)
c = PostgreSQLCursor::Cursor.new("select * from products order by 1", until:true)
n = c.each { |r| r[:id].to_i > 100 }
assert_equal 101, n
assert_equal 1000, n

c = PostgreSQLCursor.new("select * from records order by 1", while:true)
c = PostgreSQLCursor::Cursor.new("select * from products order by 1", while:true)
n = c.each { |r| r[:id].to_i < 100 }
assert_equal 100, n
assert_equal 1000, n
end

def test_relation
nn = 0
Model.where("id>0").each_row {|r| nn += 1 }
Product.where("id>0").each_row {|r| nn += 1 }
assert_equal 1000, nn
end

def test_activerecord
nn = 0
Model.each_row_by_sql("select * from records") {|r| nn += 1 }
row = nil
Product.each_row_by_sql("select * from products") {|r| row = r; nn += 1 }
assert_equal 1000, nn
assert_equal Hash, row.class

nn = 0
row = nil
Model.each_instance_by_sql("select * from records") {|r| row = r; nn += 1 }
Product.each_instance_by_sql("select * from products") {|r| row = r; nn += 1 }
assert_equal 1000, nn
assert_equal Model, row.class
assert_equal Product, row.class
end

def test_exception
begin
Model.each_row_by_sql("select * from records") do |r|
Product.each_row_by_sql("select * from products") do |r|
raise "Oops"
end
rescue Exception => e
Expand Down

0 comments on commit 0bf17ef

Please sign in to comment.