Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests.

  • Loading branch information...
commit 6e40944c51d9d3aeaf26344a271da4e3018514e0 1 parent 3f75ce0
@eladmeidar authored
View
6 README.textile
@@ -52,8 +52,8 @@ although i was unable to actually test it on all of them, help is appreciated he
h3. Tests
-Not yet actually, there will be some soon.
+Run @rake@. select the db you want to test by using an env var
+<pre>> DB=mysql rake</pre>
+don't forget to update the plugin's @database.yml@ in @test/config@
h3. Todos
-
-Tests!
View
6 lib/plain_view/connection_adapters/abstract/schema_definitions.rb
@@ -2,7 +2,7 @@ module PlainView
module ConnectionAdapters #:nodoc:
# Abstract definition of a View
class ViewDefinition
- attr_accessor :columns, :select_query, :base_model, :algorithm, :security, :check_option
+ attr_accessor :columns, :select_query, :base_model, :raw_sql, :algorithm, :security, :check_option
ALGO_TYPES = [:merge, :temptable]
SECURITY_TYPES = [:definer, :invoker]
@@ -18,6 +18,10 @@ def use_algorithm(algo_type)
@algorithm = algo_type.to_s.upcase if ALGO_TYPES.include?(algo_type.to_sym)
end
+ def use_raw_sql(raw_sql)
+ @raw_sql = raw_sql
+ end
+
def has_algorithm?
@algorithm.present?
end
View
39 lib/plain_view/connection_adapters/abstract/schema_statements.rb
@@ -16,24 +16,29 @@ def create_view(name, options={})
if options[:force]
drop_view(name) rescue nil
end
-
- create_sql = "CREATE "
- if view_definition.has_algorithm?
- create_sql << "ALGORITHM=#{view_definition.algorithm} "
- end
- if view_definition.has_security?
- create_sql << "SQL SECURITY #{view_definition.security} "
- end
- create_sql << "VIEW #{quote_table_name(name)} "
- if supports_view_columns_definition? && !view_definition.to_sql.blank?
- create_sql << "("
- create_sql << view_definition.to_sql
- create_sql << ") "
- end
- create_sql << "AS #{view_definition.select_query}"
- if view_definition.has_check_option?
- create_sql << " WITH #{view_definition.check_option} CHECK OPTION"
+
+ if view_definition.raw_sql.present?
+ create_sql = view_definition.raw_sql
+ else
+ create_sql = "CREATE "
+ if view_definition.has_algorithm?
+ create_sql << "ALGORITHM=#{view_definition.algorithm} "
+ end
+ if view_definition.has_security?
+ create_sql << "SQL SECURITY #{view_definition.security} "
+ end
+ create_sql << "VIEW #{quote_table_name(name)} "
+ if supports_view_columns_definition? && !view_definition.to_sql.blank?
+ create_sql << "("
+ create_sql << view_definition.to_sql
+ create_sql << ") "
+ end
+ create_sql << "AS #{view_definition.select_query}"
+ if view_definition.has_check_option?
+ create_sql << " WITH #{view_definition.check_option} CHECK OPTION"
+ end
end
+
execute create_sql
end
end
View
4 lib/plain_view/connection_adapters/mysql_adapter.rb
@@ -23,6 +23,7 @@ def base_tables(name = nil) #:nodoc:
execute("SHOW FULL TABLES WHERE TABLE_TYPE='BASE TABLE'").each{|row| tables << row[0]}
tables
end
+
alias nonview_tables base_tables
def views(name = nil) #:nodoc:
@@ -47,8 +48,9 @@ def structure_dump
# Get the view select statement for the specified table.
def view_select_statement(view, name=nil)
begin
+
row = execute("SHOW CREATE VIEW #{view}", name).each do |row|
- return convert_statement(row[1]) if row[0] == view
+ return row[1] #convert_statement(row[1]) if row[0] == view
end
rescue ActiveRecord::StatementInvalid => e
raise "No view called #{view} found"
View
6 lib/plain_view/schema_dumper.rb
@@ -20,7 +20,7 @@ def trailer_with_views(stream)
def dump_with_views(stream)
dump_without_views(stream)
begin
- if @connection.supports_views?
+ if @connection.send(:supports_views?)
views(stream)
end
rescue => e
@@ -57,10 +57,10 @@ def view(view, stream)
v = StringIO.new
v.print " create_view #{view.inspect}"
- v.puts " do |v|"
+ v.puts ", :force => true do |v|"
- v.puts " v.select #{@connection.view_select_statement(view).dump}"
+ v.puts " v.use_raw_sql '#{@connection.view_select_statement(view)}'"
v.puts " end"
v.puts
View
2  test/config/database.yml
@@ -16,7 +16,7 @@ postgresql:
mysql:
adapter: mysql
host: localhost
- username: rails
+ username: root
password:
database: plainview_plugin_test
View
30 test/db/schema.MySQL.out.rb
@@ -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_companies", :force => true do |v|
+ v.use_raw_sql 'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_user_companies` AS select `users`.`email` AS `email`,`companies`.`name` AS `company_name` from (`users` left join `companies` on((`users`.`id` = `companies`.`owner_id`)))'
+ end
+
+end
View
4 test/db/schema.SQLite.out.rb
@@ -23,8 +23,8 @@
t.integer "age"
end
- create_view "v_user_company" do |v|
- v.select "CREATE VIEW \"v_user_company\" AS SELECT * FROM \"users\""
+ create_view "v_user_companies", :force => true do |v|
+ v.use_raw_sql 'CREATE VIEW "v_user_companies" AS SELECT users.email as email, companies.name as company_name FROM "users" LEFT JOIN companies on users.id = companies.owner_id'
end
end
View
91 test/schema_dumper_test.rb
@@ -3,11 +3,11 @@
class SchemaDumperTest < Test::Unit::TestCase
def setup
- ActiveRecord::Base.connection.execute('drop view if exists v_user_company')
+ ActiveRecord::Base.connection.execute('drop view if exists v_user_companies')
end
def teardown
- ActiveRecord::Base.connection.execute('drop view if exists v_user_company')
+ ActiveRecord::Base.connection.execute('drop view if exists v_user_companies')
end
def test_view
@@ -15,10 +15,10 @@ def test_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"
+ # puts "#{stream.readlines.join}"
+ expected_schema= 'create_view "v_user_companies", :force => true do |v|'
- assert_match expected_schema, stream.read #File.open(File.dirname(__FILE__) + "/schema.#{$connection}.expected.rb", 'r').readlines,
+ assert_match expected_schema, stream.readlines.join
end
def test_dump_and_load
@@ -29,85 +29,12 @@ def test_dump_and_load
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
-
+
+ ActiveRecord::Base.connection.drop_view(:v_user_companies)
+
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
View
5 test/test_helper.rb
@@ -44,9 +44,10 @@
class Test::Unit::TestCase
def create_user_company_view
- ActiveRecord::Base.connection.create_view :v_user_company do |v|
+ ActiveRecord::Base.connection.drop_view(:v_user_companies) rescue nil
+ ActiveRecord::Base.connection.create_view :v_user_companies do |v|
v.base_model :user
- v.select :include => :company
+ v.select :select => 'users.email as email, companies.name as company_name', :joins => "LEFT JOIN companies on users.id = companies.owner_id"
end
end
end
View
42 test/view_model_test.rb
@@ -0,0 +1,42 @@
+require 'test_helper'
+
+class ViewModelTest < Test::Unit::TestCase
+
+ def setup
+ create_user_company_view
+
+ @elad = User.create!(:name => 'Elad', :email => 'elad@koko.com')
+ @noob = User.create!(:name => 'Noob', :email => 'noob@microsoft.com')
+
+ @msft = Company.create!(:name => 'Microsoft')
+ @nautilus = Company.create!(:name => 'Nautilus6')
+
+ @msft.user = @noob
+ @nautilus.user = @elad
+ end
+
+ def teardown
+ User.delete_all
+ Company.delete_all
+ end
+
+ def test_view_model_populated
+ assert VUserCompany.count > 0
+ end
+
+ def test_right_company_name_returned
+ @elad_company = VUserCompany.find(:first, :conditions => {:email => 'elad@koko.com'})
+ assert 'Nautilus6', @elad_company.company_name
+ end
+
+ def test_prevent_changes_by_default
+ assert_raises ActiveRecord::ReadOnlyRecord do
+ VUserCompany.create(:email =>'cannot@creatme.com')
+ end
+
+ assert_raises ActiveRecord::StatementInvalid do
+ VUserCompany.destroy_all
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.