stubs.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # test/support/stubs.rb
  2. #
  3. # Lightweight stubs for DB and Http so that lib/geocode.rb can be loaded in
  4. # unit tests without a real database connection or network access.
  5. #
  6. # Usage — require this file BEFORE requiring any lib that depends on DB or Http:
  7. #
  8. # require_relative "support/stubs"
  9. # require_relative "../../lib/geocode"
  10. # Stub DB module — pure validation logic is implemented here so that
  11. # validate_table_name! can be tested directly.
  12. module DB
  13. SAFE_TABLE_NAME_RE = /\Ada_[a-z0-9_]+\z/
  14. def self.validate_table_name!(name)
  15. unless name.to_s.match?(SAFE_TABLE_NAME_RE)
  16. raise ArgumentError, "Unsafe table name: #{name.inspect} — must match #{SAFE_TABLE_NAME_RE}"
  17. end
  18. name
  19. end
  20. def self.client
  21. raise NotImplementedError, "DB.client is not available in unit tests"
  22. end
  23. end
  24. # Stub Http module — no network calls in unit tests
  25. module Http
  26. def self.get(*)
  27. raise NotImplementedError, "Http.get is not available in unit tests"
  28. end
  29. end
  30. # Prevent require_relative inside lib files from loading the real db.rb / http.rb,
  31. # which would overwrite the stubs above and pull in the mysql2 gem.
  32. # Use File.dirname so expand_path treats the base as a directory, not a file.
  33. _test_support_dir = File.dirname(File.expand_path(__FILE__))
  34. $LOADED_FEATURES << File.expand_path("../../lib/db.rb", _test_support_dir)
  35. $LOADED_FEATURES << File.expand_path("../../lib/http.rb", _test_support_dir)