Problem z testami na modelu readonly

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?

Prócz błędu powinien być pełen stacktrace. Wklej go proszę.

Przepraszam, że dopiero teraz ale miałem niezłą jazdę ostatnio :slight_smile:

Wypluło jeszcze coś takiego przy opcji -p

ActiveRecord::StatementInvalid:         ActiveRecord::StatementInvalid: Mysql2::Error: Table 'docs' is read only: DELETE FROM `docs`
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in `_query'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in `block in query'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in `handle_interrupt'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in `query'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `block in execute'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:589:in `block in log'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.2/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `execute'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in `execute'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:225:in `execute_and_free'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/mysql/database_statements.rb:48:in `exec_delete'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:140:in `delete'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:17:in `delete'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:546:in `block (4 levels) in create_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:544:in `each_key'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:544:in `block (3 levels) in create_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:540:in `each'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:540:in `block (2 levels) in create_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:538:in `block in create_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:189:in `disable_referential_integrity'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:523:in `create_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:1015:in `load_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:977:in `setup_fixtures'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.2/lib/active_record/fixtures.rb:852:in `before_setup'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.2/lib/action_dispatch/testing/integration.rb:405:in `before_setup'
        /home/rp/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/test_help.rb:40:in `before_setup'

Wygląda na to, że masz stworzone fixtury dla tego modelu i railsy przed każdym testem próbują czyścić tabelkę i wstawić rekordy do bazy. Usuń plik test/fixtures/docs.yml.

To rzeczywiście dokładnie to. Tyle, że plik docs.yml był cały wykomentowany. Mimo to testy próbowały stworzyć i usunąć jakieś rekordy. Muszę trochę więcej poczytać o testach :slight_smile:

Dzięki za pomoc.