Files
fixin.me/test/test_helper.rb
barbie-bot 8213ccd9d3 Fix controller tests and SQLite compatibility for defaults_diff
Test infrastructure:
- Allow www.example.com host in test env (ActionDispatch::HostAuthorization
  was blocking all integration test requests)
- Include Devise::Test::IntegrationHelpers in ActionDispatch::IntegrationTest
  so tests can sign in with sign_in(user)

Controller tests:
- Rewrite UsersControllerTest to match actual routes/actions (no new/create/
  edit/destroy); sign in as admin; test update-self rejection via turbo_stream
- Fix Default::UnitsControllerTest to sign in before requesting the index

SQLite compatibility in Unit#defaults_diff:
- Hoist the inner "units" CTE to the outer WITH RECURSIVE level (fixes nested
  WITH syntax error) — this was the existing TODO in the code
- Use Unit.joins(...) for the recursive part instead of a raw Arel::SelectManager
  so the SQLite visitor does not wrap it in parentheses inside UNION ALL
- Drop the named "units" CTE (conflicts with the table name under WITH RECURSIVE
  in SQLite); apply the user/defaults scope directly on the base case
- Qualify GROUP BY columns to avoid ambiguity when bases_units is joined
- Qualify ORDER BY :multiplier/:symbol to avoid ambiguity (Unit.ordering)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-01 04:57:36 +00:00

69 lines
1.9 KiB
Ruby

ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include ActionMailer::TestHelper
include ActionView::Helpers::TranslationHelper
# List of categorized Unicode characters:
# * http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
# File format: http://www.unicode.org/L2/L1999/UnicodeData.html
# Select from graphic ranges: L, M, N, P, S, Zs
UNICODE_CHARS = {
1 => [*"\u0020".."\u007E"],
2 => [*"\u00A0".."\u00AC",
*"\u00AE".."\u05FF",
*"\u0606".."\u061B",
*"\u061D".."\u06DC",
*"\u06DE".."\u070E",
*"\u0710".."\u07FF"]
}
UNICODE_CHARS.default = UNICODE_CHARS[1] + UNICODE_CHARS[2]
def random_string(bytes = 10, except: [])
begin
result = ''
result += UNICODE_CHARS[bytes - result.bytesize].sample while bytes > result.bytesize
end while except.include?(result)
result
end
# Assumes: max >= step and step = 1e[-]N, both as strings
def random_number(max, step)
max.delete!('.')
precision = max.length
start = rand(precision) + 1
d = (rand(max.to_i) + 1) % 10**start
length = rand([0, 1..4, 4..precision].sample)
d = d.truncate(-start + length)
d = 10**(start - length) if d.zero?
BigDecimal(step) * d
end
def randomize_user_password!(user)
random_password.tap { |p| user.update!(password: p) }
end
def random_password
Random.alphanumeric rand(Rails.configuration.devise.password_length)
end
def random_email
"%s@%s.%s" % (1..3).map { Random.alphanumeric(rand(1..20)) }
end
def with_last_email
yield(ActionMailer::Base.deliveries.last)
end
end