Cześć,
buduję aplikację, która korzysta z danych innego programu który korzysta z MS SQL. Do Rails podpiąłem MariaDB + Connect Engine.
Mam dwa modele “docs” + “items”
Migracja wygląda następująco:
class CreateDocs < ActiveRecord::Migration[5.0]
def up
self.connection.execute %q(CREATE or replace table docs ENGINE=CONNECT CONNECTION='DSN=MsSQL;Database=test_database;UID=user;PWD=password;' TABLE_TYPE=ODBC TABNAME="dbo.docs" READONLY=1;)
end
def down
self.connection.execute %q(DROP TABLE docs)
end
end
Model “docs”
class Doc < ApplicationRecord
def readonly?() true end
def create_or_update() raise ActiveRecord::ReadOnlyRecord end
before_create { raise ActiveRecord::ReadOnlyRecord }
before_destroy { raise ActiveRecord::ReadOnlyRecord }
before_save { raise ActiveRecord::ReadOnlyRecord }
before_update { raise ActiveRecord::ReadOnlyRecord }
after_initialize :readonly!
has_many :items
end
Analogicznie Item, tylko “belongs_to :doc”.
W testach mam:
test/controllers/docs_controller_test.rb
require ‘test_helper’
class DocsControllerTest < ActionDispatch::IntegrationTest
setup do
@doc = docs(:one)
end
test "should get index" do
get docs_url
assert_response :success
end
test "should show doc" do
get doc_url(@doc)
assert_response :success
end
end
Item test wygląda analogicznie.
A potem w testach wygląda to tak:
rp@linux-xcxk:~/test_app> rails test test/controllers/items_controller_test.rb
Started with run options --seed 26335
ERROR[“test_should_show_item”, ItemsControllerTest, 0.09112411504611373]
test_should_show_item#ItemsControllerTest (0.09s)
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: Mysql2::Error: Table ‘docs’ is read only: DELETE FROM docs
ERROR[“test_should_get_index”, ItemsControllerTest, 0.10584185319021344]
test_should_get_index#ItemsControllerTest (0.11s)
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: Mysql2::Error: Table ‘docs’ is read only: DELETE FROM docs
2/2: [=======================================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.10726s
2 tests, 0 assertions, 0 failures, 2 errors, 0 skips
Czy ktoś wie dlaczego testy próbują kasować docs mimo, że nie powinny?