prefab-cloud-ruby 0.24.3 → 0.24.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.rubocop.yml +13 -0
  4. data/CHANGELOG.md +78 -0
  5. data/Gemfile.lock +4 -4
  6. data/VERSION +1 -1
  7. data/bin/console +21 -0
  8. data/compile_protos.sh +6 -0
  9. data/lib/prefab/client.rb +25 -4
  10. data/lib/prefab/config_client.rb +17 -6
  11. data/lib/prefab/config_loader.rb +1 -1
  12. data/lib/prefab/config_resolver.rb +2 -4
  13. data/lib/prefab/config_value_wrapper.rb +18 -0
  14. data/lib/prefab/context.rb +22 -2
  15. data/lib/prefab/context_shape.rb +20 -0
  16. data/lib/prefab/context_shape_aggregator.rb +63 -0
  17. data/lib/prefab/criteria_evaluator.rb +61 -41
  18. data/lib/prefab/evaluated_configs_aggregator.rb +60 -0
  19. data/lib/prefab/evaluated_keys_aggregator.rb +41 -0
  20. data/lib/prefab/http_connection.rb +5 -1
  21. data/lib/prefab/local_config_parser.rb +13 -13
  22. data/lib/prefab/log_path_aggregator.rb +64 -0
  23. data/lib/prefab/logger_client.rb +11 -13
  24. data/lib/prefab/options.rb +37 -1
  25. data/lib/prefab/periodic_sync.rb +51 -0
  26. data/lib/prefab/time_helpers.rb +7 -0
  27. data/lib/prefab-cloud-ruby.rb +9 -2
  28. data/lib/prefab_pb.rb +33 -220
  29. data/prefab-cloud-ruby.gemspec +21 -5
  30. data/test/test_config_loader.rb +15 -15
  31. data/test/test_config_resolver.rb +102 -102
  32. data/test/test_config_value_unwrapper.rb +13 -13
  33. data/test/test_context.rb +42 -0
  34. data/test/test_context_shape.rb +51 -0
  35. data/test/test_context_shape_aggregator.rb +137 -0
  36. data/test/test_criteria_evaluator.rb +253 -150
  37. data/test/test_evaluated_configs_aggregator.rb +254 -0
  38. data/test/test_evaluated_keys_aggregator.rb +54 -0
  39. data/test/test_helper.rb +34 -2
  40. data/test/test_log_path_aggregator.rb +57 -0
  41. data/test/test_logger.rb +33 -33
  42. data/test/test_weighted_value_resolver.rb +2 -2
  43. metadata +21 -5
  44. data/lib/prefab/log_path_collector.rb +0 -102
  45. 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 = Prefab::ConfigRow.new(
17
+ DEFAULT_ROW = PrefabProto::ConfigRow.new(
18
18
  values: [
19
- Prefab::ConditionalValue.new(
20
- value: Prefab::ConfigValue.new(string: DEFAULT_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: Prefab::Config.new(
29
+ 'key' => { config: PrefabProto::Config.new(
30
30
  key: 'key',
31
31
  rows: [
32
32
  DEFAULT_ROW,
33
- Prefab::ConfigRow.new(
33
+ PrefabProto::ConfigRow.new(
34
34
  project_env_id: TEST_ENV_ID,
35
35
  values: [
36
- Prefab::ConditionalValue.new(
36
+ PrefabProto::ConditionalValue.new(
37
37
  criteria: [
38
- Prefab::Criterion.new(
39
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
40
- value_to_match: Prefab::ConfigValue.new(string: 'projectB.subprojectX'),
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: Prefab::ConfigValue.new(string: 'projectB.subprojectX')
44
+ value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX')
45
45
  ),
46
- Prefab::ConditionalValue.new(
46
+ PrefabProto::ConditionalValue.new(
47
47
  criteria: [
48
- Prefab::Criterion.new(
49
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
50
- value_to_match: Prefab::ConfigValue.new(string: 'projectB.subprojectY'),
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: Prefab::ConfigValue.new(string: 'projectB.subprojectY')
54
+ value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY')
55
55
  ),
56
- Prefab::ConditionalValue.new(
56
+ PrefabProto::ConditionalValue.new(
57
57
  criteria: [
58
- Prefab::Criterion.new(
59
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
60
- value_to_match: Prefab::ConfigValue.new(string: 'projectA'),
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: Prefab::ConfigValue.new(string: 'valueA')
64
+ value: PrefabProto::ConfigValue.new(string: 'valueA')
65
65
  ),
66
- Prefab::ConditionalValue.new(
66
+ PrefabProto::ConditionalValue.new(
67
67
  criteria: [
68
- Prefab::Criterion.new(
69
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
70
- value_to_match: Prefab::ConfigValue.new(string: 'projectB'),
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: Prefab::ConfigValue.new(string: 'valueB')
74
+ value: PrefabProto::ConfigValue.new(string: 'valueB')
75
75
  ),
76
- Prefab::ConditionalValue.new(
76
+ PrefabProto::ConditionalValue.new(
77
77
  criteria: [
78
- Prefab::Criterion.new(
79
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
80
- value_to_match: Prefab::ConfigValue.new(string: 'projectB.subprojectX'),
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: Prefab::ConfigValue.new(string: 'projectB.subprojectX')
84
+ value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectX')
85
85
  ),
86
- Prefab::ConditionalValue.new(
86
+ PrefabProto::ConditionalValue.new(
87
87
  criteria: [
88
- Prefab::Criterion.new(
89
- operator: Prefab::Criterion::CriterionOperator::HIERARCHICAL_MATCH,
90
- value_to_match: Prefab::ConfigValue.new(string: 'projectB.subprojectY'),
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: Prefab::ConfigValue.new(string: 'projectB.subprojectY')
94
+ value: PrefabProto::ConfigValue.new(string: 'projectB.subprojectY')
95
95
  ),
96
- Prefab::ConditionalValue.new(
97
- value: Prefab::ConfigValue.new(string: 'value_none')
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: Prefab::Config.new(
104
+ 'key2' => { config: PrefabProto::Config.new(
105
105
  key: 'key2',
106
106
  rows: [
107
- Prefab::ConfigRow.new(
107
+ PrefabProto::ConfigRow.new(
108
108
  values: [
109
- Prefab::ConditionalValue.new(
110
- value: Prefab::ConfigValue.new(string: 'valueB2')
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 = Prefab::Config.new(
168
- config_type: Prefab::ConfigType::SEGMENT,
167
+ segment_config = PrefabProto::Config.new(
168
+ config_type: PrefabProto::ConfigType::SEGMENT,
169
169
  key: SEGMENT_KEY,
170
170
  rows: [
171
- Prefab::ConfigRow.new(
171
+ PrefabProto::ConfigRow.new(
172
172
  values: [
173
- Prefab::ConditionalValue.new(
174
- value: Prefab::ConfigValue.new(bool: true),
173
+ PrefabProto::ConditionalValue.new(
174
+ value: PrefabProto::ConfigValue.new(bool: true),
175
175
  criteria: [
176
- Prefab::Criterion.new(
177
- operator: Prefab::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
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
- Prefab::ConditionalValue.new(value: Prefab::ConfigValue.new(bool: false))
183
+ PrefabProto::ConditionalValue.new(value: PrefabProto::ConfigValue.new(bool: false))
184
184
  ]
185
185
  )
186
186
  ]
187
187
  )
188
188
 
189
- config = Prefab::Config.new(
189
+ config = PrefabProto::Config.new(
190
190
  key: CONFIG_KEY,
191
191
  rows: [
192
192
  # wrong env
193
- Prefab::ConfigRow.new(
193
+ PrefabProto::ConfigRow.new(
194
194
  project_env_id: TEST_ENV_ID,
195
195
  values: [
196
- Prefab::ConditionalValue.new(
196
+ PrefabProto::ConditionalValue.new(
197
197
  criteria: [
198
- Prefab::Criterion.new(
199
- operator: Prefab::Criterion::CriterionOperator::IN_SEG,
200
- value_to_match: Prefab::ConfigValue.new(string: SEGMENT_KEY)
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: Prefab::ConfigValue.new(string: WRONG_ENV_VALUE)
203
+ value: PrefabProto::ConfigValue.new(string: WRONG_ENV_VALUE)
204
204
  ),
205
- Prefab::ConditionalValue.new(
205
+ PrefabProto::ConditionalValue.new(
206
206
  criteria: [],
207
- value: Prefab::ConfigValue.new(string: DEFAULT_VALUE)
207
+ value: PrefabProto::ConfigValue.new(string: DEFAULT_VALUE)
208
208
  )
209
209
  ]
210
210
  ),
211
211
 
212
212
  # correct env
213
- Prefab::ConfigRow.new(
213
+ PrefabProto::ConfigRow.new(
214
214
  project_env_id: PRODUCTION_ENV_ID,
215
215
  values: [
216
- Prefab::ConditionalValue.new(
216
+ PrefabProto::ConditionalValue.new(
217
217
  criteria: [
218
- Prefab::Criterion.new(
219
- operator: Prefab::Criterion::CriterionOperator::IN_SEG,
220
- value_to_match: Prefab::ConfigValue.new(string: SEGMENT_KEY)
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: Prefab::ConfigValue.new(string: IN_SEGMENT_VALUE)
223
+ value: PrefabProto::ConfigValue.new(string: IN_SEGMENT_VALUE)
224
224
  ),
225
- Prefab::ConditionalValue.new(
225
+ PrefabProto::ConditionalValue.new(
226
226
  criteria: [],
227
- value: Prefab::ConfigValue.new(string: DEFAULT_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 = Prefab::Config.new(
255
- config_type: Prefab::ConfigType::SEGMENT,
254
+ segment_config = PrefabProto::Config.new(
255
+ config_type: PrefabProto::ConfigType::SEGMENT,
256
256
  key: SEGMENT_KEY,
257
257
  rows: [
258
- Prefab::ConfigRow.new(
258
+ PrefabProto::ConfigRow.new(
259
259
  values: [
260
- Prefab::ConditionalValue.new(
261
- value: Prefab::ConfigValue.new(bool: true),
260
+ PrefabProto::ConditionalValue.new(
261
+ value: PrefabProto::ConfigValue.new(bool: true),
262
262
  criteria: [
263
- Prefab::Criterion.new(
264
- operator: Prefab::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
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
- Prefab::ConditionalValue.new(value: Prefab::ConfigValue.new(bool: false))
270
+ PrefabProto::ConditionalValue.new(value: PrefabProto::ConfigValue.new(bool: false))
271
271
  ]
272
272
  )
273
273
  ]
274
274
  )
275
275
 
276
- config = Prefab::Config.new(
276
+ config = PrefabProto::Config.new(
277
277
  key: CONFIG_KEY,
278
278
  rows: [
279
- Prefab::ConfigRow.new(
279
+ PrefabProto::ConfigRow.new(
280
280
  values: [
281
- Prefab::ConditionalValue.new(
281
+ PrefabProto::ConditionalValue.new(
282
282
  criteria: [
283
- Prefab::Criterion.new(
284
- operator: Prefab::Criterion::CriterionOperator::IN_SEG,
285
- value_to_match: Prefab::ConfigValue.new(string: SEGMENT_KEY)
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: Prefab::ConfigValue.new(string: IN_SEGMENT_VALUE)
288
+ value: PrefabProto::ConfigValue.new(string: IN_SEGMENT_VALUE)
289
289
  ),
290
- Prefab::ConditionalValue.new(
290
+ PrefabProto::ConditionalValue.new(
291
291
  criteria: [
292
- Prefab::Criterion.new(
293
- operator: Prefab::Criterion::CriterionOperator::NOT_IN_SEG,
294
- value_to_match: Prefab::ConfigValue.new(string: SEGMENT_KEY)
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: Prefab::ConfigValue.new(string: NOT_IN_SEGMENT_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 = Prefab::Config.new(
323
+ config = PrefabProto::Config.new(
324
324
  key: CONFIG_KEY,
325
325
  rows: [
326
326
  DEFAULT_ROW,
327
- Prefab::ConfigRow.new(
327
+ PrefabProto::ConfigRow.new(
328
328
  project_env_id: TEST_ENV_ID,
329
329
  values: [
330
- Prefab::ConditionalValue.new(
330
+ PrefabProto::ConditionalValue.new(
331
331
  criteria: [
332
- Prefab::Criterion.new(
333
- operator: Prefab::Criterion::CriterionOperator::PROP_IS_ONE_OF,
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
- Prefab::Criterion.new(
339
- operator: Prefab::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
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: Prefab::ConfigValue.new(string: DESIRED_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 = Prefab::Config.new(
367
+ config = PrefabProto::Config.new(
368
368
  key: CONFIG_KEY,
369
369
  rows: [
370
370
  DEFAULT_ROW,
371
- Prefab::ConfigRow.new(
371
+ PrefabProto::ConfigRow.new(
372
372
  project_env_id: TEST_ENV_ID,
373
373
  values: [
374
- Prefab::ConditionalValue.new(
374
+ PrefabProto::ConditionalValue.new(
375
375
  criteria: [
376
- Prefab::Criterion.new(
377
- operator: Prefab::Criterion::CriterionOperator::PROP_IS_ONE_OF,
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
- Prefab::Criterion.new(
383
- operator: Prefab::Criterion::CriterionOperator::PROP_ENDS_WITH_ONE_OF,
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: Prefab::ConfigValue.new(string: DESIRED_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 = Prefab::ConfigValue.new(int: 123)
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 = Prefab::ConfigValue.new(string: 'abc')
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 = Prefab::ConfigValue.new(double: 1.23)
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 = Prefab::ConfigValue.new(bool: true)
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 = Prefab::ConfigValue.new(bool: false)
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 = Prefab::ConfigValue.new(log_level: :INFO)
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 = Prefab::ConfigValue.new(string_list: Prefab::StringList.new(values: %w[a b c]))
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 = Prefab::ConfigValue.new(weighted_values: weighted_values([['abc', 1]]))
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 = Prefab::ConfigValue.new(weighted_values: weighted_values([['abc', 1], ['def', 1], ['ghi', 1]]))
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 = Prefab::ConfigValue.new(weighted_values: weighted_values([['abc', 1], ['def', 99], ['ghi', 1]]))
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
- Prefab::WeightedValues.new(weighted_values: values, hash_by_property_name: hash_by_property_name)
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
- Prefab::WeightedValue.new(
77
- value: Prefab::ConfigValue.new(string: string), weight: weight
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