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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# frozen_string_literal: true
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class SchemaCacheTest < ActiveRecord::TestCase
def setup
@connection = ActiveRecord::Base.connection
@cache = SchemaCache.new @connection
@database_version = @connection.get_database_version
end
def test_primary_key
assert_equal "id", @cache.primary_keys("posts")
end
def test_yaml_dump_and_load
@cache.columns("posts")
@cache.columns_hash("posts")
@cache.data_sources("posts")
@cache.primary_keys("posts")
@cache.indexes("posts")
new_cache = YAML.load(YAML.dump(@cache))
assert_no_queries do
assert_equal 12, new_cache.columns("posts").size
assert_equal 12, new_cache.columns_hash("posts").size
assert new_cache.data_sources("posts")
assert_equal "id", new_cache.primary_keys("posts")
assert_equal 1, new_cache.indexes("posts").size
assert_equal @database_version.to_s, new_cache.database_version.to_s
end
end
def test_yaml_loads_5_1_dump
@cache = YAML.load(File.read(schema_dump_path))
assert_no_queries do
assert_equal 11, @cache.columns("posts").size
assert_equal 11, @cache.columns_hash("posts").size
assert @cache.data_sources("posts")
assert_equal "id", @cache.primary_keys("posts")
end
end
def test_yaml_loads_5_1_dump_without_indexes_still_queries_for_indexes
@cache = YAML.load(File.read(schema_dump_path))
# Simulate assignment in railtie after loading the cache.
old_cache, @connection.schema_cache = @connection.schema_cache, @cache
assert_queries :any, ignore_none: true do
assert_equal 1, @cache.indexes("posts").size
end
ensure
@connection.schema_cache = old_cache
end
def test_yaml_loads_5_1_dump_without_database_version_still_queries_for_database_version
@cache = YAML.load(File.read(schema_dump_path))
# Simulate assignment in railtie after loading the cache.
old_cache, @connection.schema_cache = @connection.schema_cache, @cache
# We can't verify queries get executed because the database version gets
# cached in both MySQL and PostgreSQL outside of the schema cache.
assert_nil @cache.instance_variable_get(:@database_version)
assert_equal @database_version.to_s, @cache.database_version.to_s
ensure
@connection.schema_cache = old_cache
end
def test_primary_key_for_non_existent_table
assert_nil @cache.primary_keys("omgponies")
end
def test_caches_columns
columns = @cache.columns("posts")
assert_equal columns, @cache.columns("posts")
end
def test_caches_columns_hash
columns_hash = @cache.columns_hash("posts")
assert_equal columns_hash, @cache.columns_hash("posts")
end
def test_caches_indexes
indexes = @cache.indexes("posts")
assert_equal indexes, @cache.indexes("posts")
end
def test_caches_database_version
@cache.database_version # cache database_version
assert_no_queries do
assert_equal @database_version.to_s, @cache.database_version.to_s
if current_adapter?(:Mysql2Adapter)
assert_not_nil @cache.database_version.full_version_string
end
end
end
def test_clearing
@cache.columns("posts")
@cache.columns_hash("posts")
@cache.data_sources("posts")
@cache.primary_keys("posts")
@cache.indexes("posts")
@cache.clear!
assert_equal 0, @cache.size
assert_nil @cache.instance_variable_get(:@database_version)
end
def test_dump_and_load
@cache.columns("posts")
@cache.columns_hash("posts")
@cache.data_sources("posts")
@cache.primary_keys("posts")
@cache.indexes("posts")
@cache = Marshal.load(Marshal.dump(@cache))
assert_no_queries do
assert_equal 12, @cache.columns("posts").size
assert_equal 12, @cache.columns_hash("posts").size
assert @cache.data_sources("posts")
assert_equal "id", @cache.primary_keys("posts")
assert_equal 1, @cache.indexes("posts").size
assert_equal @database_version.to_s, @cache.database_version.to_s
end
end
def test_data_source_exist
assert @cache.data_source_exists?("posts")
assert_not @cache.data_source_exists?("foo")
end
def test_clear_data_source_cache
@cache.clear_data_source_cache!("posts")
end
test "#columns_hash? is populated by #columns_hash" do
assert_not @cache.columns_hash?("posts")
@cache.columns_hash("posts")
assert @cache.columns_hash?("posts")
end
test "#columns_hash? is not populated by #data_source_exists?" do
assert_not @cache.columns_hash?("posts")
@cache.data_source_exists?("posts")
assert_not @cache.columns_hash?("posts")
end
private
def schema_dump_path
"test/assets/schema_dump_5_1.yml"
end
end
end
end
|