How to update fixture column in Rails Minitest - ruby-on-rails-4

How can i update column of a fixture for temporary use only with update_column command.
Right now i have following command that is running fine:
name = names(:one)
role = roles(:one)
name.role_id = role.id
assert name.save
And it is running fine, but is there any efficient way to do it in one line something like name.update_column(---, ----) ?

Thanks #richfisher for your answer, later on i figure out another way to do it. update_attributes is not a good idea to be used in test, because the problem with update_attributes is
It runs callbacks and validations
and usually we do not want to run these things in test cases
Instead of update_attributes we can use update_column like this
name.update_column(:role_id, roles(:one).id)
The advantage of using update_column is
It did not run callbacks and validations

name = names(:one)
name.update_attributes(role_id: roles(:one).id)

Related

skip unit test but run it manually from PyCharm

I would like to use #unittest.skip to have my automated testrun pass by some long-running tests. I want to still be able to run them from PyCharm manually. However, when I add the attribute PyCharm is no longer able to execute the test. It fails immediately if the attribute is on the class level. It runs the class initialization and then skips the method if I attribute the method instead. I'm using the "Unittests" runner. Any workarounds for this? Identifying something as in PyCharm or not is insufficient; I really want something like NUnit's Ignore behavior.

Rails - Keep a table out of structure.sql during migrations

It is straightforward to ignore tables when your schema format is :ruby, but is there a way to do it when your schema format is :sql?
Ideally something like this in environment.rb:
ActiveRecord::SQLDumper.ignore_tables = ['table_name']
After a quick perusal through the AR source code it looks unpromising.
There is currently no way to do this, when the schema format is set to :sql, Rails doesn't go through the regular SchemaDumper but instead uses the tasks in ActiveRecord::Tasks::PostgreSQLDatabaseTasks to do the dump, check it out here.
The code is quite straightforward. I came up with a simple patch for ActiveRecord that should work as expected. It relies on setting the tables to ignore in your database.yml file. It basically adds the following code:
ignore_tables = configuration['ignore_tables']
unless ignore_tables.blank?
args += ignore_tables.split(',').map do |table|
"-T #{table}"
end
end
I just submitted a pull request to rails with those changes. In case you'd want to test it.
For the sake of people landing here after Googling this problem in 2022: As of this PR the structure.sql dump should respect the ignore_tables configuration of ActiveRecord::SchemaDumper. If you're using a regular schema.rb, this option can be a mix of strings and regexes; However, if you're using a structure.sql, it will only take strings according to the docs.
In any case, you can add an initializer to modify the list of ignored tables however you need. In my case, I have a number of backup tables that are created when certain risky operations are performed, and I'd like to exclude those from the structure.sql. Adding this initializer solved it for me:
# config/initializers/ignore_tables.rb
backup_tables = ActiveRecord::Base.connection.tables.select do |t|
t.match?(/_backup_.*$/)
end
ActiveRecord::SchemaDumper.ignore_tables += backup_tables

ActsAsTenant current_tenant + Capybara + RSpec testing

I am trying to create an integration test with Capybara/RSpec that has a valid current_user (using the Warden.test_mode! trick) and I have also created a valid current_tenant by doing this in the test:
ActsAsTenant.current_tenant = Account.first
When the test is ran it comes up with a "undefined method for nil:NilClass" error that is characteristic of the current_tenant not being set. I have verified that Account.first does in fact have what I expect in it.
What might be the problem and how can I fix it?
You can check the gem 'act_as_tent' doc over here
https://github.com/ErwinM/acts_as_tenant#testing
You might need to create the Account. If you're using FactoryBot, that might look like:
ActsAsTenant.current_tenant = FactoryBot.create :account
You might also consider using:
ActsAsTenant.without_tenant do
# more testy things
end

Spree and unit testing

I've been trying to run some unit tests in my spree application, which involves creating a new Order. The first hurdle I ran into had to do with countries not loading, due to seed data not being entered in the test database. A question was posted about it here, if you want extra credit work: https://github.com/spree/spree/issues/5308
However, I was able to bypass that issue by inventing a country inside the test, for the sake of testing the rest of my code. I've tried doing the same for a variant, but I keep running into this error:
Error:
VariantTest#test_variant_test:
RuntimeError: No master variant found to infer price
test/models/variant_test.rb:10:in `block in <class:VariantTest>'
I created a second test to see if Variants were getting made at all, and I got the same error message. This is the test I've run:
require 'test_helper'
class VariantTest < ActiveSupport::TestCase
test "variant test" do
f = Spree::Variant.new
f.cost_price = 20
f.sku = "test"
f.is_master = true
f.track_inventory = false
f.save!
test1 = Spree::Variant.find_by sku: "test"
assert_not_nil(test1, "Variant wasn't created")
end
end
I've tried creating two Variants, one of which is master and one of which is not, and testing the sku for the non-master variant, but I keep getting the exact same error message about the master variant not being found. Am I missing something?
Just to answer your queestion: You need to set price for your variant. You will get then new error because you're missing product for that variant and so on.
Belive me, you really want to use default factories with FactoryGirl, you won't have to lose time reinventing the wheel. Just look at them here or directly at variant factory, if you'll have any questions about them just ask.

Can a fixture be changed dynamically between test methods in CakePHP?

Is it possible to have a fixture change between test methods? If so, how can I do this?
My syntax for this problem :
In the cakephp framework i am building tests for a behavior that is configured by adding fields to the table. This is intended to work in the same way that adding the "created"
and "modified" fields will auto-populate these fields on save.
To test this I could create dozens of fixtures/model combos to test the different setups, but it would be a hundred times better, faster and easier to just have the fixture change "shape" between test methods.
If you are not familiar with the CakePHP framework, you can maybe still help me as it uses SimpleTest
Edit: rephrased question to be more general
I'm not familiar specifically with CakePHP, but this kind of thing seems to happen anywhere with fixtures.
There is no built in way in rails at least for this to happen, and I imagine not in cakePHP or anywhere else either because the whole idea of a fixture, is that it is fixed
There are 2 'decent' workarounds I'm aware of
Write a changefixture method, and just before you do your asserts/etc, run it with the parameters of what to change. It should go and update the database or whatever needs to be done.
Don't use fixtures at all, and use some kind of object factory or object generator to create your objects each time
This is not an answer to my quetion, but a solution to my issue example.
Instead of using multiple fixtures or changing the fixtures, I edit the Model::_schema arrays by removing the fields that I wanted to test without. This has the effect that the model acts as if the fields was not there, but I am unsure if this is a 100% test. I do not think it is for all cases, but it works for my example.