15

I have two tables with the almost same structure in my project, one of them was used to calculate user statistics and the another was never being used for that. Now I need to calculate statistics using both of them.

I did that using MySQL Views, everything went fine except for my tests. Seems that Database Cleaner strategy does not save the data on the database and my view never was filled.

I moved the strategy from :transaction to :truncation and the data started to be persisted. But I still with on problem, I continue can't read the data from the view.

# spec/spec_helper.rb
config.before(:each, using_view: true) do
  DatabaseCleaner.strategy = :truncation
end

# spec/models/stats/answer.rb
describe 'POC test', using_view: true do
  it 'works fine without the table view' do
    expect{ create(:answer) }.to change{ Answer.count }
    expect{ create(:knowledge_answer) }.to change{ Knowledge::Answer.count }
  end

  it 'works fine with the table view' do
    expect{ create(:answer) }.to change{ Stats::Answers.count }
    expect{ create(:knowledge_answer) }.to change{ Stats::Answers.count }
  end
end

And when I execute it:

Stats::Answers
  POC test
    works fine with the table view (FAILED - 1)
    works fine without the table view

Failures:

  1) Stats::Answers POC test works fine with the table view
     Failure/Error: expect{ create(:answer) }.to change{ Stats::Answers.count }
       expected result to have changed, but is still 0
     # ./spec/models/stats/answers_spec.rb:18:in `block (3 levels) in <top (required)>'

Finished in 4.75 seconds (files took 30.37 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/models/stats/answers_spec.rb:17 # Stats::Answers POC test works fine with the table view

After a lot of research I found out that the rake db:test:prepare was creating the view as a normal table and not as a view, since it use the db/schema.rb to generate the test database schema. Now I need to discover how to generate the view in the test instead of a normal table.

Any thoughts?

UPDATE:

Still not creating the views into schema even using:

config.active_record.schema_format = :sql
joaofraga
  • 608
  • 7
  • 17
  • I don't think the problem is really related to database_cleaner. Rather it could be that your factory is not working or that some validation is failing. Database cleaner runs after the example so whatever strategy you are using should not effect the result. – max Jun 21 '16 at 20:56
  • What happens when you change your create action to raise if creation fails? IE change save to save! – ruby_newbie Jun 21 '16 at 22:50
  • I've tested the creation and everything goes well. I have a big suite testing it and always saved and worked. This behavior started when I moved to the Table View. Also tested using debugger and etc. – joaofraga Jun 21 '16 at 23:30
  • Updated the example with default behavior working. – joaofraga Jun 22 '16 at 14:43
  • 1
    try to run you migration on test env `RAILS_ENV=test bundle exec rake db:migrate` and then run tests again – itsnikolay Jun 29 '16 at 12:05
  • @itsnikolay it works, but I need to fill schema with that now :/ – joaofraga Jun 29 '16 at 17:36
  • Is the question still actual? Would you provide content of schema.rb relevant to your view? Also, you know that `db:test:prepare` is getting deprecated in Rails, so you should run something with `RAILS_ENV=test` as suggested. – Pavel Bulanov Jul 04 '16 at 09:59
  • Hmm, thank you @PavelBulanov. But is a good practice to maintain schema different from real database? – joaofraga Jul 04 '16 at 13:54
  • It's probably not, but I don't get how it's connected. I understand the issue is that test database created from schema.rb doesn't contain view, and instead contain table, correct? This way you may want to reset database, e.g. see [here](http://stackoverflow.com/questions/25877734/rails-4-resetting-test-database). But I'm a bit lost on what exactly is the problem and your question, honestly. – Pavel Bulanov Jul 04 '16 at 17:32
  • 3
    a) This is a good question, but I suggest editing it down so that the question in "after a lot of research ..." is front and center. b) have you tried any of [the gems that add view support to ActiveRecord](https://www.google.com/#q=activerecord+view+support+gem)? – Dave Schweisguth Jul 20 '16 at 13:08
  • What about "rails db:migrate:reset" ? – ishe_ua Oct 02 '17 at 19:52
  • Did you try recreate schema with bundle exec rake db:schema:dump – StandardNerd Jan 10 '19 at 12:43
  • Can you show the code from the view-based model and the code which creates the view? – Marcus Ilgner Jan 18 '19 at 19:49
  • 1
    A while ago I found this project https://github.com/scenic-views/scenic for versioning and managing database views on Rails, I know it could be a long shot but this could work for you. – Danilo Cabello Feb 17 '19 at 17:28

1 Answers1

2

There is a great gem for views called Scenic which you can use directly to create the views, I used it before and it works fine with the Rspec.

Here is a link for a tutorial how to use it

melsatar
  • 61
  • 7