4
4
5
5
module ActiveRecord
6
6
module Type
7
- class TypeMapTest < ActiveRecord :: TestCase
7
+ module TypeMapSharedTests
8
8
def test_default_type
9
- mapping = TypeMap . new
9
+ mapping = klass . new
10
10
11
11
assert_kind_of Value , mapping . lookup ( :undefined )
12
12
end
13
13
14
+ def test_requires_value_or_block
15
+ mapping = klass . new
16
+
17
+ assert_raises ( ArgumentError ) do
18
+ mapping . register_type ( /only key/i )
19
+ end
20
+ end
21
+
22
+ def test_fetch
23
+ mapping = klass . new
24
+ mapping . register_type ( 1 , "string" )
25
+
26
+ assert_equal "string" , mapping . fetch ( 1 ) { "int" }
27
+ assert_equal "int" , mapping . fetch ( 2 ) { "int" }
28
+ end
29
+
30
+ def test_fetch_memoizes
31
+ mapping = klass . new
32
+
33
+ looked_up = false
34
+ mapping . register_type ( 1 ) do
35
+ fail if looked_up
36
+ looked_up = true
37
+ "string"
38
+ end
39
+
40
+ assert_equal "string" , mapping . fetch ( 1 )
41
+ assert_equal "string" , mapping . fetch ( 1 )
42
+ end
43
+
44
+ def test_register_clears_cache
45
+ mapping = klass . new
46
+
47
+ mapping . register_type ( 1 , "string" )
48
+ mapping . lookup ( 1 )
49
+ mapping . register_type ( 1 , "int" )
50
+
51
+ assert_equal "int" , mapping . lookup ( 1 )
52
+ end
53
+ end
54
+
55
+ class TypeMapTest < ActiveRecord ::TestCase
56
+ include TypeMapSharedTests
57
+
14
58
def test_registering_types
15
59
boolean = Boolean . new
16
- mapping = TypeMap . new
60
+ mapping = klass . new
17
61
18
62
mapping . register_type ( /boolean/i , boolean )
19
63
@@ -23,26 +67,17 @@ def test_registering_types
23
67
def test_overriding_registered_types
24
68
time = Time . new
25
69
timestamp = DateTime . new
26
- mapping = TypeMap . new
70
+ mapping = klass . new
27
71
28
72
mapping . register_type ( /time/i , time )
29
73
mapping . register_type ( /time/i , timestamp )
30
74
31
75
assert_equal mapping . lookup ( "time" ) , timestamp
32
76
end
33
77
34
- def test_fuzzy_lookup
35
- string = +""
36
- mapping = TypeMap . new
37
-
38
- mapping . register_type ( /varchar/i , string )
39
-
40
- assert_equal mapping . lookup ( "varchar(20)" ) , string
41
- end
42
-
43
78
def test_aliasing_types
44
79
string = +""
45
- mapping = TypeMap . new
80
+ mapping = klass . new
46
81
47
82
mapping . register_type ( /string/i , string )
48
83
mapping . alias_type ( /varchar/i , "string" )
@@ -53,17 +88,17 @@ def test_aliasing_types
53
88
def test_changing_type_changes_aliases
54
89
time = Time . new
55
90
timestamp = DateTime . new
56
- mapping = TypeMap . new
91
+ mapping = klass . new
57
92
58
93
mapping . register_type ( /timestamp/i , time )
59
94
mapping . alias_type ( /datetime/i , "timestamp" )
60
95
mapping . register_type ( /timestamp/i , timestamp )
61
96
62
- assert_equal mapping . lookup ( "datetime" ) , timestamp
97
+ assert_equal timestamp , mapping . lookup ( "datetime" )
63
98
end
64
99
65
100
def test_aliases_keep_metadata
66
- mapping = TypeMap . new
101
+ mapping = klass . new
67
102
68
103
mapping . register_type ( /decimal/i ) { |sql_type | sql_type }
69
104
mapping . alias_type ( /number/i , "decimal" )
@@ -72,10 +107,19 @@ def test_aliases_keep_metadata
72
107
assert_equal mapping . lookup ( "number" ) , "decimal"
73
108
end
74
109
110
+ def test_fuzzy_lookup
111
+ string = +""
112
+ mapping = klass . new
113
+
114
+ mapping . register_type ( /varchar/i , string )
115
+
116
+ assert_equal mapping . lookup ( "varchar(20)" ) , string
117
+ end
118
+
75
119
def test_register_proc
76
120
string = +""
77
121
binary = Binary . new
78
- mapping = TypeMap . new
122
+ mapping = klass . new
79
123
80
124
mapping . register_type ( /varchar/i ) do |type |
81
125
if type . include? ( "(" )
@@ -89,29 +133,40 @@ def test_register_proc
89
133
assert_equal mapping . lookup ( "varchar" ) , binary
90
134
end
91
135
136
+ def test_parent_fallback
137
+ boolean = Boolean . new
138
+
139
+ parent = klass . new
140
+ parent . register_type ( /boolean/i , boolean )
141
+
142
+ mapping = klass . new ( parent )
143
+ assert_equal boolean , mapping . lookup ( "boolean" )
144
+ end
145
+
146
+ private
147
+ def klass
148
+ TypeMap
149
+ end
150
+ end
151
+
152
+ class HashLookupTypeMapTest < ActiveRecord ::TestCase
153
+ include TypeMapSharedTests
154
+
92
155
def test_additional_lookup_args
93
- mapping = TypeMap . new
156
+ mapping = HashLookupTypeMap . new
94
157
95
- mapping . register_type ( / varchar/i ) do |type , limit |
158
+ mapping . register_type ( " varchar" ) do |type , limit |
96
159
if limit > 255
97
160
"text"
98
161
else
99
162
"string"
100
163
end
101
164
end
102
- mapping . alias_type ( / string/i , "varchar" )
165
+ mapping . alias_type ( " string" , "varchar" )
103
166
104
- assert_equal mapping . lookup ( "varchar" , 200 ) , "string"
105
- assert_equal mapping . lookup ( "varchar" , 400 ) , "text"
106
- assert_equal mapping . lookup ( "string" , 400 ) , "text"
107
- end
108
-
109
- def test_requires_value_or_block
110
- mapping = TypeMap . new
111
-
112
- assert_raises ( ArgumentError ) do
113
- mapping . register_type ( /only key/i )
114
- end
167
+ assert_equal "string" , mapping . lookup ( "varchar" , 200 )
168
+ assert_equal "text" , mapping . lookup ( "varchar" , 400 )
169
+ assert_equal "text" , mapping . lookup ( "string" , 400 )
115
170
end
116
171
117
172
def test_lookup_non_strings
@@ -121,68 +176,31 @@ def test_lookup_non_strings
121
176
mapping . register_type ( 2 , "int" )
122
177
mapping . alias_type ( 3 , 1 )
123
178
124
- assert_equal mapping . lookup ( 1 ) , "string"
125
- assert_equal mapping . lookup ( 2 ) , "int"
126
- assert_equal mapping . lookup ( 3 ) , "string"
179
+ assert_equal "string" , mapping . lookup ( 1 )
180
+ assert_equal "int" , mapping . lookup ( 2 )
181
+ assert_equal "string" , mapping . lookup ( 3 )
127
182
assert_kind_of Type ::Value , mapping . lookup ( 4 )
128
183
end
129
184
130
- def test_fetch
131
- mapping = TypeMap . new
132
- mapping . register_type ( 1 , "string" )
133
-
134
- assert_equal "string" , mapping . fetch ( 1 ) { "int" }
135
- assert_equal "int" , mapping . fetch ( 2 ) { "int" }
136
- end
137
-
138
- def test_fetch_yields_args
139
- mapping = TypeMap . new
140
-
141
- assert_equal "foo-1-2-3" , mapping . fetch ( "foo" , 1 , 2 , 3 ) { |*args | args . join ( "-" ) }
142
- assert_equal "bar-1-2-3" , mapping . fetch ( "bar" , 1 , 2 , 3 ) { |*args | args . join ( "-" ) }
143
- end
144
-
145
- def test_fetch_memoizes
146
- mapping = TypeMap . new
147
-
148
- looked_up = false
149
- mapping . register_type ( 1 ) do
150
- fail if looked_up
151
- looked_up = true
152
- "string"
153
- end
154
-
155
- assert_equal "string" , mapping . fetch ( 1 )
156
- assert_equal "string" , mapping . fetch ( 1 )
157
- end
158
-
159
185
def test_fetch_memoizes_on_args
160
- mapping = TypeMap . new
186
+ mapping = HashLookupTypeMap . new
161
187
mapping . register_type ( "foo" ) { |*args | args . join ( "-" ) }
162
188
163
189
assert_equal "foo-1-2-3" , mapping . fetch ( "foo" , 1 , 2 , 3 ) { |*args | args . join ( "-" ) }
164
190
assert_equal "foo-2-3-4" , mapping . fetch ( "foo" , 2 , 3 , 4 ) { |*args | args . join ( "-" ) }
165
191
end
166
192
167
- def test_register_clears_cache
168
- mapping = TypeMap . new
169
-
170
- mapping . register_type ( 1 , "string" )
171
- mapping . lookup ( 1 )
172
- mapping . register_type ( 1 , "int" )
193
+ def test_fetch_yields_args
194
+ mapping = klass . new
173
195
174
- assert_equal "int" , mapping . lookup ( 1 )
196
+ assert_equal "foo-1-2-3" , mapping . fetch ( "foo" , 1 , 2 , 3 ) { |*args | args . join ( "-" ) }
197
+ assert_equal "bar-1-2-3" , mapping . fetch ( "bar" , 1 , 2 , 3 ) { |*args | args . join ( "-" ) }
175
198
end
176
199
177
- def test_parent_fallback
178
- boolean = Boolean . new
179
-
180
- parent = TypeMap . new
181
- parent . register_type ( /boolean/i , boolean )
182
-
183
- mapping = TypeMap . new ( parent )
184
- assert_equal mapping . lookup ( "boolean" ) , boolean
185
- end
200
+ private
201
+ def klass
202
+ HashLookupTypeMap
203
+ end
186
204
end
187
205
end
188
206
end
0 commit comments