Skip to content

Commit

Permalink
fixing some more issues and adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Meidar committed Nov 12, 2009
1 parent a75bb20 commit 3f75ce0
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module PlainView
module ConnectionAdapters # :nodoc:
module SchemaStatements
def self.included(base)
base.alias_method_chain :drop_table, :cascade
end

# Create a view.
# The +options+ hash can include the following keys:
Expand Down
6 changes: 6 additions & 0 deletions lib/plain_view/connection_adapters/mysql_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module PlainView
module ConnectionAdapters
module SchemaStatements
def self.included(base)
base.alias_method_chain :drop_table, :cascade
end
end

module MysqlAdapter
def self.included(base)
if base.private_method_defined?(:supports_views?)
Expand Down
2 changes: 1 addition & 1 deletion lib/plain_view/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def view(view, stream)
v.puts " do |v|"


v.puts " v.select , #{@connection.view_select_statement(view).dump}"
v.puts " v.select #{@connection.view_select_statement(view).dump}"

v.puts " end"
v.puts
Expand Down
3 changes: 3 additions & 0 deletions test/app/models/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Company < ActiveRecord::Base
belongs_to :user, :foreign_key => 'owner_id'
end
3 changes: 3 additions & 0 deletions test/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
has_one :company, :foreign_key => 'owner_id'
end
3 changes: 3 additions & 0 deletions test/app/models/v_user_company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'active_record/view'
class VUserCompany < ActiveRecord::View
end
38 changes: 38 additions & 0 deletions test/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
sqlite:
adapter: sqlite
database: ':memory:'

sqlite3:
adapter: sqlite3
database: ':memory:'

postgresql:
adapter: postgresql
username: postgres
password: postgres
database: plainview_plugin_test
min_messages: ERROR

mysql:
adapter: mysql
host: localhost
username: rails
password:
database: plainview_plugin_test

oracle:
adapter: oracle
host: localhost/xe
username: rails
password:

oci:
adapter: oci
host: localhost/xe
username: rails
password:

sqlserver:
adapter: sqlserver
mode: ODBC
dsn: 'DRIVER=/usr/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=Your_Server_Name;DATABASE=Your_Database_Name;Port=1433;uid=Your_UID;pwd=Your_PWD;'
30 changes: 30 additions & 0 deletions test/db/schema.SQLite.out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 0) do

create_table "companies", :force => true do |t|
t.string "name"
t.integer "owner_id"
end

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.string "address"
t.integer "age"
end

create_view "v_user_company" do |v|
v.select "CREATE VIEW \"v_user_company\" AS SELECT * FROM \"users\""
end

end
13 changes: 13 additions & 0 deletions test/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.string :name
t.string :email
t.string :address
t.integer :age
end

create_table :companies, :force => true do |t|
t.string :name
t.integer :owner_id
end
end
113 changes: 113 additions & 0 deletions test/schema_dumper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
require "test_helper"
require 'active_record/schema_dumper'

class SchemaDumperTest < Test::Unit::TestCase
def setup
ActiveRecord::Base.connection.execute('drop view if exists v_user_company')
end

def teardown
ActiveRecord::Base.connection.execute('drop view if exists v_user_company')
end

def test_view
create_user_company_view
stream = StringIO.new
dumper = ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
stream.rewind

expected_schema = "create_view \"v_user_company\" do |v|\n v.select \"CREATE VIEW \\\"v_user_company\\\" AS SELECT * FROM \\\"users\\\"\"\n end\n\nend\n"

assert_match expected_schema, stream.read #File.open(File.dirname(__FILE__) + "/schema.#{$connection}.expected.rb", 'r').readlines,
end

def test_dump_and_load
create_user_company_view
schema_file = File.dirname(__FILE__) + "/db/schema.#{ActiveRecord::Base.connection.adapter_name}.out.rb"
assert_nothing_raised do
File.open(schema_file, "w+") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end

assert_nothing_raised do
load(schema_file)
end
end

# def test_union
# Person.create(:first_name => 'Joe', :last_name => 'User', :ssn => '123456789')
# Person2.create(:first_name => 'Jane', :last_name => 'Doe', :ssn => '222334444')
#
# select_stmt = <<-HERE
# select first_name, last_name, ssn from people
# UNION
# select first_name, last_name, ssn from people2
# HERE
#
# ActiveRecord::Base.connection.create_view(:v_profile, select_stmt, :force => true) do |v|
# v.column :first_name
# v.column :last_name
# v.column :ssn
# end
#
# assert_dump_and_load_succeed
# end
# def test_symbol_ignore
# ActiveRecord::SchemaDumper.ignore_views << :v_people
# create_people_view
# assert_dump_and_load_succeed
# ActiveRecord::SchemaDumper.ignore_views.pop
# end
# def test_regex_ignore
# ActiveRecord::SchemaDumper.ignore_views << Regexp.new(/v_people/)
# create_people_view
# assert_dump_and_load_succeed
# ActiveRecord::SchemaDumper.ignore_views.pop
# end
# def test_non_allowed_object_raises_error
# create_people_view
# ActiveRecord::SchemaDumper.ignore_views << 0
# begin
# schema_file = File.dirname(__FILE__) + "/schema.#{$connection}.out.rb"
# File.open(schema_file, "w") do |file|
# assert_raise(StandardError) do
# ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
# end
# end
# ensure
# ActiveRecord::SchemaDumper.ignore_views.pop
# end
# end
#
# def test_logging_error
# ActiveRecord::SchemaDumper.ignore_views << 0
# old_logger = ActiveRecord::Base.logger
#
# begin
# mock_logger = flexmock('logger', :error => nil)
# mock_logger.should_receive(:error)
# ActiveRecord::Base.logger = mock_logger
# schema_file = File.dirname(__FILE__) + "/schema.#{$connection}.out.rb"
# File.open(schema_file, "w") do |file|
# ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
# end
# ensure
# ActiveRecord::SchemaDumper.ignore_views.pop
# ActiveRecord::Base.logger = old_logger
# end
# end
#
# def assert_dump_and_load_succeed
# schema_file = File.dirname(__FILE__) + "/schema.#{$connection}.out.rb"
# assert_nothing_raised do
# File.open(schema_file, "w") do |file|
# ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
# end
# end
#
# assert_nothing_raised do
# load(schema_file)
# end
# end
end
51 changes: 50 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
require 'rubygems'
require 'active_record'
require 'active_support'
require 'active_support/test_case'
require 'active_support/test_case'
require 'test/unit'

require 'lib/plain_view'
config = YAML::load(File.open('test/config/database.yml'))

db_adapter = ENV['DB']

# no db passed, try one of these fine config-free DBs before nuking everyone to hell.
db_adapter ||=
begin
require 'rubygems'
require 'sqlite'
'sqlite'
rescue MissingSourceFile
begin
require 'sqlite3'
'sqlite3'
rescue MissingSourceFile
end
end

if db_adapter.nil?
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
end

puts "Using #{db_adapter}"

# Connect
ActiveRecord::Base.establish_connection(config[db_adapter])

# Load schema
load 'test/db/schema.rb'

# load models
Dir['test/app/models/**/*.rb'].each do |f|
require f
puts "Loaded #{f}"
end


class Test::Unit::TestCase
def create_user_company_view
ActiveRecord::Base.connection.create_view :v_user_company do |v|
v.base_model :user
v.select :include => :company
end
end
end

0 comments on commit 3f75ce0

Please sign in to comment.