prefab-cloud-ruby 0.24.3 → 0.24.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +76 -0
- data/Gemfile.lock +4 -4
- data/VERSION +1 -1
- data/bin/console +21 -0
- data/compile_protos.sh +6 -0
- data/lib/prefab/client.rb +25 -4
- data/lib/prefab/config_client.rb +16 -6
- data/lib/prefab/config_loader.rb +1 -1
- data/lib/prefab/config_resolver.rb +2 -4
- data/lib/prefab/config_value_wrapper.rb +18 -0
- data/lib/prefab/context.rb +22 -2
- data/lib/prefab/context_shape.rb +20 -0
- data/lib/prefab/context_shape_aggregator.rb +63 -0
- data/lib/prefab/criteria_evaluator.rb +61 -41
- data/lib/prefab/evaluated_configs_aggregator.rb +60 -0
- data/lib/prefab/evaluated_keys_aggregator.rb +41 -0
- data/lib/prefab/local_config_parser.rb +13 -13
- data/lib/prefab/log_path_aggregator.rb +64 -0
- data/lib/prefab/logger_client.rb +11 -13
- data/lib/prefab/options.rb +37 -1
- data/lib/prefab/periodic_sync.rb +51 -0
- data/lib/prefab/time_helpers.rb +7 -0
- data/lib/prefab-cloud-ruby.rb +8 -1
- data/lib/prefab_pb.rb +33 -220
- data/prefab-cloud-ruby.gemspec +21 -5
- data/test/test_config_loader.rb +15 -15
- data/test/test_config_resolver.rb +102 -102
- data/test/test_config_value_unwrapper.rb +13 -13
- data/test/test_context.rb +42 -0
- data/test/test_context_shape.rb +51 -0
- data/test/test_context_shape_aggregator.rb +137 -0
- data/test/test_criteria_evaluator.rb +253 -150
- data/test/test_evaluated_configs_aggregator.rb +254 -0
- data/test/test_evaluated_keys_aggregator.rb +54 -0
- data/test/test_helper.rb +34 -2
- data/test/test_log_path_aggregator.rb +57 -0
- data/test/test_logger.rb +33 -33
- data/test/test_weighted_value_resolver.rb +2 -2
- metadata +21 -5
- data/lib/prefab/log_path_collector.rb +0 -102
- data/test/test_log_path_collector.rb +0 -58
@@ -14,10 +14,10 @@ class TestConfigResolver < Minitest::Test
|
|
14
14
|
WRONG_ENV_VALUE = 'wrong_env_value'
|
15
15
|
NOT_IN_SEGMENT_VALUE = 'not_in_segment_value'
|
16
16
|
|
17
|
-
DEFAULT_ROW =
|
17
|
+
DEFAULT_ROW = PrefabProto::ConfigRow.new(
|
18
18
|
values: [
|
19
|
-
|
20
|
-
value:
|
19
|
+
PrefabProto::ConditionalValue.new(
|
20
|
+
value: PrefabProto::ConfigValue.new(string: DEFAULT_VALUE)
|
21
21
|
)
|
22
22
|
]
|
23
23
|
)
|
@@ -26,88 +26,88 @@ class TestConfigResolver < Minitest::Test
|
|
26
26
|
@loader = MockConfigLoader.new
|
27
27
|
|
28
28
|
loaded_values = {
|
29
|
-
'key' => { config:
|
29
|
+
'key' => { config: PrefabProto::Config.new(
|
30
30
|
key: 'key',
|
31
31
|
rows: [
|
32
32
|
DEFAULT_ROW,
|
33
|
-
|
33
|
+
PrefabProto::ConfigRow.new(
|
34
34
|
project_env_id: TEST_ENV_ID,
|
35
35
|
values: [
|
36
|
-
|
36
|
+
PrefabProto::ConditionalValue.new(
|
37
37
|
criteria: [
|
38
|
-
|
39
|
-
operator:
|
40
|
-
value_to_match:
|
38
|
+
PrefabProto::Criterion.new(
|
39
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
40
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX'),
|
41
41
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
42
42
|
)
|
43
43
|
],
|
44
|
-
value:
|
44
|
+
value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX')
|
45
45
|
),
|
46
|
-
|
46
|
+
PrefabProto::ConditionalValue.new(
|
47
47
|
criteria: [
|
48
|
-
|
49
|
-
operator:
|
50
|
-
value_to_match:
|
48
|
+
PrefabProto::Criterion.new(
|
49
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
50
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY'),
|
51
51
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
52
52
|
)
|
53
53
|
],
|
54
|
-
value:
|
54
|
+
value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY')
|
55
55
|
),
|
56
|
-
|
56
|
+
PrefabProto::ConditionalValue.new(
|
57
57
|
criteria: [
|
58
|
-
|
59
|
-
operator:
|
60
|
-
value_to_match:
|
58
|
+
PrefabProto::Criterion.new(
|
59
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
60
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectA'),
|
61
61
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
62
62
|
)
|
63
63
|
],
|
64
|
-
value:
|
64
|
+
value: PrefabProto::ConfigValue.new(string: 'valueA')
|
65
65
|
),
|
66
|
-
|
66
|
+
PrefabProto::ConditionalValue.new(
|
67
67
|
criteria: [
|
68
|
-
|
69
|
-
operator:
|
70
|
-
value_to_match:
|
68
|
+
PrefabProto::Criterion.new(
|
69
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
70
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectB'),
|
71
71
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
72
72
|
)
|
73
73
|
],
|
74
|
-
value:
|
74
|
+
value: PrefabProto::ConfigValue.new(string: 'valueB')
|
75
75
|
),
|
76
|
-
|
76
|
+
PrefabProto::ConditionalValue.new(
|
77
77
|
criteria: [
|
78
|
-
|
79
|
-
operator:
|
80
|
-
value_to_match:
|
78
|
+
PrefabProto::Criterion.new(
|
79
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
80
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX'),
|
81
81
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
82
82
|
)
|
83
83
|
],
|
84
|
-
value:
|
84
|
+
value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX')
|
85
85
|
),
|
86
|
-
|
86
|
+
PrefabProto::ConditionalValue.new(
|
87
87
|
criteria: [
|
88
|
-
|
89
|
-
operator:
|
90
|
-
value_to_match:
|
88
|
+
PrefabProto::Criterion.new(
|
89
|
+
operator: PrefabProto::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
|
90
|
+
value_to_match: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY'),
|
91
91
|
property_name: Prefab::CriteriaEvaluator::NAMESPACE_KEY
|
92
92
|
)
|
93
93
|
],
|
94
|
-
value:
|
94
|
+
value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY')
|
95
95
|
),
|
96
|
-
|
97
|
-
value:
|
96
|
+
PrefabProto::ConditionalValue.new(
|
97
|
+
value: PrefabProto::ConfigValue.new(string: 'value_none')
|
98
98
|
)
|
99
99
|
]
|
100
100
|
)
|
101
101
|
|
102
102
|
]
|
103
103
|
) },
|
104
|
-
'key2' => { config:
|
104
|
+
'key2' => { config: PrefabProto::Config.new(
|
105
105
|
key: 'key2',
|
106
106
|
rows: [
|
107
|
-
|
107
|
+
PrefabProto::ConfigRow.new(
|
108
108
|
values: [
|
109
|
-
|
110
|
-
value:
|
109
|
+
PrefabProto::ConditionalValue.new(
|
110
|
+
value: PrefabProto::ConfigValue.new(string: 'valueB2')
|
111
111
|
)
|
112
112
|
]
|
113
113
|
)
|
@@ -164,67 +164,67 @@ class TestConfigResolver < Minitest::Test
|
|
164
164
|
end
|
165
165
|
|
166
166
|
def test_resolving_in_segment
|
167
|
-
segment_config =
|
168
|
-
config_type:
|
167
|
+
segment_config = PrefabProto::Config.new(
|
168
|
+
config_type: PrefabProto::ConfigType::SEGMENT,
|
169
169
|
key: SEGMENT_KEY,
|
170
170
|
rows: [
|
171
|
-
|
171
|
+
PrefabProto::ConfigRow.new(
|
172
172
|
values: [
|
173
|
-
|
174
|
-
value:
|
173
|
+
PrefabProto::ConditionalValue.new(
|
174
|
+
value: PrefabProto::ConfigValue.new(bool: true),
|
175
175
|
criteria: [
|
176
|
-
|
177
|
-
operator:
|
176
|
+
PrefabProto::Criterion.new(
|
177
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
|
178
178
|
value_to_match: string_list(['hotmail.com', 'gmail.com']),
|
179
179
|
property_name: 'user.email'
|
180
180
|
)
|
181
181
|
]
|
182
182
|
),
|
183
|
-
|
183
|
+
PrefabProto::ConditionalValue.new(value: PrefabProto::ConfigValue.new(bool: false))
|
184
184
|
]
|
185
185
|
)
|
186
186
|
]
|
187
187
|
)
|
188
188
|
|
189
|
-
config =
|
189
|
+
config = PrefabProto::Config.new(
|
190
190
|
key: CONFIG_KEY,
|
191
191
|
rows: [
|
192
192
|
# wrong env
|
193
|
-
|
193
|
+
PrefabProto::ConfigRow.new(
|
194
194
|
project_env_id: TEST_ENV_ID,
|
195
195
|
values: [
|
196
|
-
|
196
|
+
PrefabProto::ConditionalValue.new(
|
197
197
|
criteria: [
|
198
|
-
|
199
|
-
operator:
|
200
|
-
value_to_match:
|
198
|
+
PrefabProto::Criterion.new(
|
199
|
+
operator: PrefabProto::Criterion::CriterionOperator::IN_SEG,
|
200
|
+
value_to_match: PrefabProto::ConfigValue.new(string: SEGMENT_KEY)
|
201
201
|
)
|
202
202
|
],
|
203
|
-
value:
|
203
|
+
value: PrefabProto::ConfigValue.new(string: WRONG_ENV_VALUE)
|
204
204
|
),
|
205
|
-
|
205
|
+
PrefabProto::ConditionalValue.new(
|
206
206
|
criteria: [],
|
207
|
-
value:
|
207
|
+
value: PrefabProto::ConfigValue.new(string: DEFAULT_VALUE)
|
208
208
|
)
|
209
209
|
]
|
210
210
|
),
|
211
211
|
|
212
212
|
# correct env
|
213
|
-
|
213
|
+
PrefabProto::ConfigRow.new(
|
214
214
|
project_env_id: PRODUCTION_ENV_ID,
|
215
215
|
values: [
|
216
|
-
|
216
|
+
PrefabProto::ConditionalValue.new(
|
217
217
|
criteria: [
|
218
|
-
|
219
|
-
operator:
|
220
|
-
value_to_match:
|
218
|
+
PrefabProto::Criterion.new(
|
219
|
+
operator: PrefabProto::Criterion::CriterionOperator::IN_SEG,
|
220
|
+
value_to_match: PrefabProto::ConfigValue.new(string: SEGMENT_KEY)
|
221
221
|
)
|
222
222
|
],
|
223
|
-
value:
|
223
|
+
value: PrefabProto::ConfigValue.new(string: IN_SEGMENT_VALUE)
|
224
224
|
),
|
225
|
-
|
225
|
+
PrefabProto::ConditionalValue.new(
|
226
226
|
criteria: [],
|
227
|
-
value:
|
227
|
+
value: PrefabProto::ConfigValue.new(string: DEFAULT_VALUE)
|
228
228
|
)
|
229
229
|
]
|
230
230
|
)
|
@@ -251,50 +251,50 @@ class TestConfigResolver < Minitest::Test
|
|
251
251
|
end
|
252
252
|
|
253
253
|
def test_resolving_not_in_segment
|
254
|
-
segment_config =
|
255
|
-
config_type:
|
254
|
+
segment_config = PrefabProto::Config.new(
|
255
|
+
config_type: PrefabProto::ConfigType::SEGMENT,
|
256
256
|
key: SEGMENT_KEY,
|
257
257
|
rows: [
|
258
|
-
|
258
|
+
PrefabProto::ConfigRow.new(
|
259
259
|
values: [
|
260
|
-
|
261
|
-
value:
|
260
|
+
PrefabProto::ConditionalValue.new(
|
261
|
+
value: PrefabProto::ConfigValue.new(bool: true),
|
262
262
|
criteria: [
|
263
|
-
|
264
|
-
operator:
|
263
|
+
PrefabProto::Criterion.new(
|
264
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
|
265
265
|
value_to_match: string_list(['hotmail.com', 'gmail.com']),
|
266
266
|
property_name: 'user.email'
|
267
267
|
)
|
268
268
|
]
|
269
269
|
),
|
270
|
-
|
270
|
+
PrefabProto::ConditionalValue.new(value: PrefabProto::ConfigValue.new(bool: false))
|
271
271
|
]
|
272
272
|
)
|
273
273
|
]
|
274
274
|
)
|
275
275
|
|
276
|
-
config =
|
276
|
+
config = PrefabProto::Config.new(
|
277
277
|
key: CONFIG_KEY,
|
278
278
|
rows: [
|
279
|
-
|
279
|
+
PrefabProto::ConfigRow.new(
|
280
280
|
values: [
|
281
|
-
|
281
|
+
PrefabProto::ConditionalValue.new(
|
282
282
|
criteria: [
|
283
|
-
|
284
|
-
operator:
|
285
|
-
value_to_match:
|
283
|
+
PrefabProto::Criterion.new(
|
284
|
+
operator: PrefabProto::Criterion::CriterionOperator::IN_SEG,
|
285
|
+
value_to_match: PrefabProto::ConfigValue.new(string: SEGMENT_KEY)
|
286
286
|
)
|
287
287
|
],
|
288
|
-
value:
|
288
|
+
value: PrefabProto::ConfigValue.new(string: IN_SEGMENT_VALUE)
|
289
289
|
),
|
290
|
-
|
290
|
+
PrefabProto::ConditionalValue.new(
|
291
291
|
criteria: [
|
292
|
-
|
293
|
-
operator:
|
294
|
-
value_to_match:
|
292
|
+
PrefabProto::Criterion.new(
|
293
|
+
operator: PrefabProto::Criterion::CriterionOperator::NOT_IN_SEG,
|
294
|
+
value_to_match: PrefabProto::ConfigValue.new(string: SEGMENT_KEY)
|
295
295
|
)
|
296
296
|
],
|
297
|
-
value:
|
297
|
+
value: PrefabProto::ConfigValue.new(string: NOT_IN_SEGMENT_VALUE)
|
298
298
|
)
|
299
299
|
]
|
300
300
|
)
|
@@ -320,28 +320,28 @@ class TestConfigResolver < Minitest::Test
|
|
320
320
|
end
|
321
321
|
|
322
322
|
def test_jit_context_merges_with_existing_context
|
323
|
-
config =
|
323
|
+
config = PrefabProto::Config.new(
|
324
324
|
key: CONFIG_KEY,
|
325
325
|
rows: [
|
326
326
|
DEFAULT_ROW,
|
327
|
-
|
327
|
+
PrefabProto::ConfigRow.new(
|
328
328
|
project_env_id: TEST_ENV_ID,
|
329
329
|
values: [
|
330
|
-
|
330
|
+
PrefabProto::ConditionalValue.new(
|
331
331
|
criteria: [
|
332
|
-
|
333
|
-
operator:
|
332
|
+
PrefabProto::Criterion.new(
|
333
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_IS_ONE_OF,
|
334
334
|
value_to_match: string_list(%w[pro advanced]),
|
335
335
|
property_name: 'team.plan'
|
336
336
|
),
|
337
337
|
|
338
|
-
|
339
|
-
operator:
|
338
|
+
PrefabProto::Criterion.new(
|
339
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
|
340
340
|
value_to_match: string_list(%w[@example.com]),
|
341
341
|
property_name: 'user.email'
|
342
342
|
)
|
343
343
|
],
|
344
|
-
value:
|
344
|
+
value: PrefabProto::ConfigValue.new(string: DESIRED_VALUE)
|
345
345
|
)
|
346
346
|
]
|
347
347
|
)
|
@@ -364,28 +364,28 @@ class TestConfigResolver < Minitest::Test
|
|
364
364
|
end
|
365
365
|
|
366
366
|
def test_jit_can_clobber_existing_context
|
367
|
-
config =
|
367
|
+
config = PrefabProto::Config.new(
|
368
368
|
key: CONFIG_KEY,
|
369
369
|
rows: [
|
370
370
|
DEFAULT_ROW,
|
371
|
-
|
371
|
+
PrefabProto::ConfigRow.new(
|
372
372
|
project_env_id: TEST_ENV_ID,
|
373
373
|
values: [
|
374
|
-
|
374
|
+
PrefabProto::ConditionalValue.new(
|
375
375
|
criteria: [
|
376
|
-
|
377
|
-
operator:
|
376
|
+
PrefabProto::Criterion.new(
|
377
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_IS_ONE_OF,
|
378
378
|
value_to_match: string_list(%w[pro advanced]),
|
379
379
|
property_name: 'team.plan'
|
380
380
|
),
|
381
381
|
|
382
|
-
|
383
|
-
operator:
|
382
|
+
PrefabProto::Criterion.new(
|
383
|
+
operator: PrefabProto::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
|
384
384
|
value_to_match: string_list(%w[@example.com]),
|
385
385
|
property_name: 'user.email'
|
386
386
|
)
|
387
387
|
],
|
388
|
-
value:
|
388
|
+
value: PrefabProto::ConfigValue.new(string: DESIRED_VALUE)
|
389
389
|
)
|
390
390
|
]
|
391
391
|
)
|
@@ -7,53 +7,53 @@ class TestConfigValueUnwrapper < Minitest::Test
|
|
7
7
|
EMPTY_CONTEXT = Prefab::Context.new()
|
8
8
|
|
9
9
|
def test_unwrapping_int
|
10
|
-
config_value =
|
10
|
+
config_value = PrefabProto::ConfigValue.new(int: 123)
|
11
11
|
assert_equal 123, Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_unwrapping_string
|
15
|
-
config_value =
|
15
|
+
config_value = PrefabProto::ConfigValue.new(string: 'abc')
|
16
16
|
assert_equal 'abc', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_unwrapping_double
|
20
|
-
config_value =
|
20
|
+
config_value = PrefabProto::ConfigValue.new(double: 1.23)
|
21
21
|
assert_equal 1.23, Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_unwrapping_bool
|
25
|
-
config_value =
|
25
|
+
config_value = PrefabProto::ConfigValue.new(bool: true)
|
26
26
|
assert_equal true, Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
27
27
|
|
28
|
-
config_value =
|
28
|
+
config_value = PrefabProto::ConfigValue.new(bool: false)
|
29
29
|
assert_equal false, Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_unwrapping_log_level
|
33
|
-
config_value =
|
33
|
+
config_value = PrefabProto::ConfigValue.new(log_level: :INFO)
|
34
34
|
assert_equal :INFO, Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_unwrapping_string_list
|
38
|
-
config_value =
|
38
|
+
config_value = PrefabProto::ConfigValue.new(string_list: PrefabProto::StringList.new(values: %w[a b c]))
|
39
39
|
assert_equal %w[a b c], Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_unwrapping_weighted_values
|
43
43
|
# single value
|
44
|
-
config_value =
|
44
|
+
config_value = PrefabProto::ConfigValue.new(weighted_values: weighted_values([['abc', 1]]))
|
45
45
|
|
46
46
|
assert_equal 'abc', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, EMPTY_CONTEXT)
|
47
47
|
|
48
48
|
# multiple values, evenly distributed
|
49
|
-
config_value =
|
49
|
+
config_value = PrefabProto::ConfigValue.new(weighted_values: weighted_values([['abc', 1], ['def', 1], ['ghi', 1]]))
|
50
50
|
assert_equal 'def', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:000'))
|
51
51
|
assert_equal 'ghi', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:456'))
|
52
52
|
assert_equal 'abc', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:789'))
|
53
53
|
assert_equal 'ghi', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:888'))
|
54
54
|
|
55
55
|
# multiple values, unevenly distributed
|
56
|
-
config_value =
|
56
|
+
config_value = PrefabProto::ConfigValue.new(weighted_values: weighted_values([['abc', 1], ['def', 99], ['ghi', 1]]))
|
57
57
|
assert_equal 'def', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:123'))
|
58
58
|
assert_equal 'def', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:456'))
|
59
59
|
assert_equal 'def', Prefab::ConfigValueUnwrapper.unwrap(config_value, CONFIG_KEY, context_with_key('user:789'))
|
@@ -69,12 +69,12 @@ class TestConfigValueUnwrapper < Minitest::Test
|
|
69
69
|
weighted_value(value, weight)
|
70
70
|
end
|
71
71
|
|
72
|
-
|
72
|
+
PrefabProto::WeightedValues.new(weighted_values: values, hash_by_property_name: hash_by_property_name)
|
73
73
|
end
|
74
74
|
|
75
75
|
def weighted_value(string, weight)
|
76
|
-
|
77
|
-
value:
|
76
|
+
PrefabProto::WeightedValue.new(
|
77
|
+
value: PrefabProto::ConfigValue.new(string: string), weight: weight
|
78
78
|
)
|
79
79
|
end
|
80
80
|
|
data/test/test_context.rb
CHANGED
@@ -132,6 +132,48 @@ class TestContext < Minitest::Test
|
|
132
132
|
assert_empty context.to_h
|
133
133
|
end
|
134
134
|
|
135
|
+
def test_to_proto
|
136
|
+
namespace = "my.namespace"
|
137
|
+
|
138
|
+
contexts = Prefab::Context.new({
|
139
|
+
user: {
|
140
|
+
id: 1,
|
141
|
+
email: 'user-email'
|
142
|
+
},
|
143
|
+
team: {
|
144
|
+
id: 2,
|
145
|
+
name: 'team-name'
|
146
|
+
}
|
147
|
+
})
|
148
|
+
|
149
|
+
assert_equal PrefabProto::ContextSet.new(
|
150
|
+
contexts: [
|
151
|
+
PrefabProto::Context.new(
|
152
|
+
type: "user",
|
153
|
+
values: {
|
154
|
+
"id" => PrefabProto::ConfigValue.new(int: 1),
|
155
|
+
"email" => PrefabProto::ConfigValue.new(string: "user-email")
|
156
|
+
}
|
157
|
+
),
|
158
|
+
PrefabProto::Context.new(
|
159
|
+
type: "team",
|
160
|
+
values: {
|
161
|
+
"id" => PrefabProto::ConfigValue.new(int: 2),
|
162
|
+
"name" => PrefabProto::ConfigValue.new(string: "team-name")
|
163
|
+
}
|
164
|
+
),
|
165
|
+
|
166
|
+
PrefabProto::Context.new(
|
167
|
+
type: "prefab",
|
168
|
+
values: {
|
169
|
+
'current-time' => PrefabProto::ConfigValue.new(int: Prefab::TimeHelpers.now_in_ms),
|
170
|
+
'namespace' => PrefabProto::ConfigValue.new(string: namespace)
|
171
|
+
}
|
172
|
+
)
|
173
|
+
]
|
174
|
+
), contexts.to_proto(namespace)
|
175
|
+
end
|
176
|
+
|
135
177
|
private
|
136
178
|
|
137
179
|
def stringify(hash)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class TestContextShape < Minitest::Test
|
6
|
+
class Email; end
|
7
|
+
|
8
|
+
def test_field_type_number
|
9
|
+
[
|
10
|
+
[1, 1],
|
11
|
+
[99999999999999999999999999999999999999999999, 1],
|
12
|
+
[-99999999999999999999999999999999999999999999, 1],
|
13
|
+
|
14
|
+
['a', 2],
|
15
|
+
['99999999999999999999999999999999999999999999', 2],
|
16
|
+
|
17
|
+
[1.0, 4],
|
18
|
+
[99999999999999999999999999999999999999999999.0, 4],
|
19
|
+
[-99999999999999999999999999999999999999999999.0, 4],
|
20
|
+
|
21
|
+
[true, 5],
|
22
|
+
[false, 5],
|
23
|
+
|
24
|
+
[[], 10],
|
25
|
+
[[1, 2, 3], 10],
|
26
|
+
[['a', 'b', 'c'], 10],
|
27
|
+
|
28
|
+
[Email.new, 2],
|
29
|
+
].each do |value, expected|
|
30
|
+
actual = Prefab::ContextShape.field_type_number(value)
|
31
|
+
|
32
|
+
refute_nil actual, "Expected a value for input: #{value}"
|
33
|
+
assert_equal expected, actual, "Expected #{expected} for #{value}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# If this test fails, it means that we've added a new type to the ConfigValue
|
38
|
+
def test_mapping_is_exhaustive
|
39
|
+
unsupported = [:bytes, :limit_definition, :log_level, :weighted_values, :int_range]
|
40
|
+
|
41
|
+
supported = PrefabProto::ConfigValue.descriptor.entries.reject do |entry|
|
42
|
+
unsupported.include?(entry.name.to_sym)
|
43
|
+
end.map(&:number)
|
44
|
+
|
45
|
+
mapped = Prefab::ContextShape::MAPPING.values.uniq
|
46
|
+
|
47
|
+
unless mapped == supported
|
48
|
+
raise "ContextShape MAPPING needs update: #{mapped} != #{supported}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|