File: helper.rb

package info (click to toggle)
ruby-friendly-id 5.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 436 kB
  • sloc: ruby: 3,248; makefile: 3
file content (123 lines) | stat: -rw-r--r-- 2,647 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
if ENV["COVERALLS"] || ENV["COVERAGE"]
  require "simplecov"
  if ENV["COVERALLS"]
    require "coveralls"
    SimpleCov.formatter = Coveralls::SimpleCov::Formatter
  end
  SimpleCov.start do
    add_filter "test"
    add_filter "friendly_id/migration"
  end
end

begin
  require "minitest"
rescue LoadError
  require "minitest/unit"
end

begin
  TestCaseClass = Minitest::Test
rescue NameError
  TestCaseClass = Minitest::Unit::TestCase
end

require "mocha/minitest"
require "active_record"
require "active_support/core_ext/time/conversions"
require "erb"

I18n.enforce_available_locales = false

require "friendly_id"

# If you want to see the ActiveRecord log, invoke the tests using `rake test LOG=true`
if ENV["LOG"]
  require "logger"
  ActiveRecord::Base.logger = Logger.new($stdout)
end

if ActiveSupport::VERSION::STRING >= "4.2"
  ActiveSupport.test_order = :random
end

module FriendlyId
  module Test
    def self.included(base)
      if Minitest.respond_to?(:autorun)
        Minitest.autorun
      else
        require "minitest/autorun"
      end
    rescue LoadError
    end

    def transaction
      ActiveRecord::Base.transaction do
        yield

        raise ActiveRecord::Rollback
      end
    end

    def with_instance_of(*args)
      model_class = args.shift
      args[0] ||= {name: "a b c"}
      transaction { yield model_class.create!(*args) }
    end

    module Database
      extend self

      def connect
        version = ActiveRecord::VERSION::STRING
        engine = begin
          RUBY_ENGINE
        rescue
          "ruby"
        end

        ActiveRecord::Base.establish_connection config[driver]
        message = "Using #{engine} #{RUBY_VERSION} AR #{version} with #{driver}"

        puts "-" * 72
        if in_memory?
          ActiveRecord::Migration.verbose = false
          Schema.migrate :up
          puts "#{message} (in-memory)"
        else
          puts message
        end
      end

      def config
        @config ||= YAML.safe_load(
          ERB.new(
            File.read(File.expand_path("../databases.yml", __FILE__))
          ).result
        )
      end

      def driver
        db_driver = ENV.fetch("DB", "sqlite3").downcase
        db_driver = "postgres" if %w[postgresql pg].include?(db_driver)
        db_driver
      end

      def in_memory?
        config[driver]["database"] == ":memory:"
      end
    end
  end
end

class Module
  def test(name, &block)
    define_method("test_#{name.gsub(/[^a-z0-9']/i, "_")}".to_sym, &block)
  end
end

require "schema"
require "shared"
FriendlyId::Test::Database.connect
at_exit { ActiveRecord::Base.connection.disconnect! }