aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb
blob: 86e72c149cf5a52acdd097c2a19f32b583a304a7 (plain) (blame)
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
# frozen_string_literal: true
require "cases/helper"

class Mysql2DatetimePrecisionQuotingTest < ActiveRecord::Mysql2TestCase
  setup do
    @connection = ActiveRecord::Base.connection
  end

  test "microsecond precision for MySQL gte 5.6.4" do
    stub_version "5.6.4" do
      assert_microsecond_precision
    end
  end

  test "no microsecond precision for MySQL lt 5.6.4" do
    stub_version "5.6.3" do
      assert_no_microsecond_precision
    end
  end

  test "microsecond precision for MariaDB gte 5.3.0" do
    stub_version "5.5.5-10.1.8-MariaDB-log" do
      assert_microsecond_precision
    end
  end

  test "no microsecond precision for MariaDB lt 5.3.0" do
    stub_version "5.2.9-MariaDB" do
      assert_no_microsecond_precision
    end
  end

  private
    def assert_microsecond_precision
      assert_match_quoted_microsecond_datetime(/\.000001\z/)
    end

    def assert_no_microsecond_precision
      assert_match_quoted_microsecond_datetime(/\d\z/)
    end

    def assert_match_quoted_microsecond_datetime(match)
      assert_match match, @connection.quoted_date(Time.now.change(usec: 1))
    end

    def stub_version(full_version_string)
      @connection.stubs(:full_version).returns(full_version_string)
      @connection.remove_instance_variable(:@version) if @connection.instance_variable_defined?(:@version)
      yield
    ensure
      @connection.remove_instance_variable(:@version) if @connection.instance_variable_defined?(:@version)
    end
end