How to reduce logs from database cleaner in ruby application - ruby

Here is my ruby spec_helper for rspec:
As you see I'm using database cleaner because I'm writing tests using the DB.
However, I get all this nonsense in my console output:
Is there a way to supress some of this output? Again remember, I'm not in RAils so I can't simply do:
config.logger.level = Logger::ERROR

Related

How to use RSpec from the IRB console?

I would like to try all the nice oneliners in the documentation of RSpec in the IRB console.
How to execute them in the IRB console?
Please a solution not based in Rails, just plain Ruby.
This might be just what you're looking for:
Standalone
require "rspec/mocks/standalone" to expose the API at the top level (e.g. main) outside
the RSpec environment in a REPL like IRB or in a one-off script.
See https://relishapp.com/rspec/rspec-mocks/v/3-9/docs/outside-rspec/standalone

Ruby Cucumber - Running using Cucumber::Runtime(config).run! - 'Officially supported'?

I have a test farm I'm making that can run Cucumber tests and will take the results and write to a database.
Instead of using backticks to run from the command line I'm considering this, which seemed to work rather well from an irb prompt:
config = {paths: ['.\\scenarios\\my.feature'], filters: [], tag_expressions: ['#open_print_pdf'],tag_limits: [], profiles: ['example'], require: []}
Cucumber::Runtime.new(Cucumber::Configuration.new(config)).run!
I like that I then get some objects that I can interrogate to write things I need to the db like:
steps
feature and scenario name
etc.
Before I considered this I was going to run using the JSON formatter, save the file, and then parse it, make it a hash and send to db.
Is the only supported way to run it through the command line. I find it useful to programmatically run it so I have everything I need without needing to do needless IO.
So, is running using Cucumber::Runtime(config).run! officially supported?

ActiveJob instance freezes RSpec test

I have an after_create_commit callback on my model, Foo, which looks like this:
after_create_commit { LogBroadcastJob.perform_later self }
I've reduced my perform method to return nil to simplify things.
When I create a Foo instance in an RSpec test with factory_girl, the test suite freezes. This only happens when I test models with that callback.
FactoryGirl.create :foo
When I Ctl+C out of my test suite it fails to kill the process. I have to find the process, which is still using my database (Postgresql), and kill it, which means that I don't see any errors on the command line. If I run my test suite again, it creates another process that I have to find and kill.
Does this sound familiar to anyone? How would I find useful errors here?
Maybe relevant: I upgraded from Rails 4.2 to 5.0.0.1 a while back.
This was a concurrency issue. Thanks to the resource provided in #coreyward's comment, I was able to clear this up by setting config/environments/test.rb to
config.eager_load = true
This differs from my config in config/environments/development.rb (and everything works in development), so I can't say I understand yet why it works. But I can now run all my tests with bundle exec guard or bundle exec rake spec.

How to access environment in datamapper migration

I have a padrino server application with datamapper as ORM layer. I have a database migration, say:
migrate 1, :test do
up do
execute 'Some Query'
end
end
This migration is run using the command:
padrino rake dm:migrate -e <env>
Now my problem is that I need access to env in my query (not to choose schema or anything which datamapper does automatically, something very specific to the functionality). I tried debugging the migration to see if there is a variable which stores this value, but no luck. Is there a way?
As it turns out, since I am using Padrino, I can directly use Padrino.env inside up do..end or down do..end blocks:
migrate 1, :test do
up do
env = Padrino.env
execute "Some Query #{env}"
end
end
Although this is Padrino specific, but so is the concept of environment. I am sure something like this would work with other frameworks like Rails as well.

Output SQL from an ActiveRecord migration without executing it (not rails!)

There's a bunch of questions out there similar to this one that talk about rails plugins as a solution - but I'm not using rails, read on for more
I have a Rakefile in a sinatra project which allows me to rake db:migrate. It'll do my migration perfectly, but I'd like to pass that a flag (or write a new rake task) which does the same thing, but outputs the SQL to STDOUT and doesn't commit the changes to the database. Does anyone know how to do this?
My first thought was to try ActiveRecord logging and see if I could get the SQL out at all, but that doesn't work! Any ideas?
namespace :db do
task :migrate_sql do
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
Rake::Task['db:migrate'].invoke
# This does the migration and doesn't output SQL - so no good!
end
end
I think there isn't any easy way to do it, for the following reasons:
up, down, and change are methods which execute other methods; there isn't a global migration query string that gets built and executed
neither the statements methods (add_column, etc) expose their statements as strings; as I understand, they are implemented as connection adapter methods, and for example the mysql adapter has a add_column_sql method, while the postgresql adapter does not, and its sql is a variable inside its add_column method
So, if you really need this functionality, I think your best option is to copy the sql from the log.

Resources