-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing some more issues and adding tests.
- Loading branch information
Elad Meidar
committed
Nov 12, 2009
1 parent
a75bb20
commit 3f75ce0
Showing
11 changed files
with
260 additions
and
5 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
lib/plain_view/connection_adapters/abstract/schema_statements.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'active_record/view' | ||
class VUserCompany < ActiveRecord::View | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |