norikra 0.0.13-java → 0.0.14-java

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -46,10 +46,46 @@ end
46
46
  # These instructions should self-destruct in 10 seconds. If they don't, feel
47
47
  # free to delete them.
48
48
 
49
-
50
-
51
-
52
49
  require 'rubygems'
53
50
  require 'rspec'
54
51
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
55
52
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../esper')
53
+
54
+ require 'norikra/logger'
55
+
56
+ $dummylogger = Norikra::DummyLogger.new
57
+ Norikra::Log.init('test', nil, {:logger => $dummylogger})
58
+
59
+ require 'norikra/engine'
60
+
61
+ $running = {}
62
+
63
+ module Norikra::SpecHelper
64
+ def logger ; $dummylogger ; end
65
+ def engine_start
66
+ service = com.espertech.esper.client.EPServiceProviderManager.getDefaultProvider
67
+ administrator = service.getEPAdministrator
68
+ config = administrator.getConfiguration
69
+ runtime = service.getEPRuntime
70
+ $running = {
71
+ service: service,
72
+ administrator: administrator,
73
+ config: config,
74
+ runtime: runtime,
75
+ }
76
+ end
77
+ def engine_stop
78
+ administrator.stopAllStatements
79
+ $running = {}
80
+ end
81
+ def with_engine
82
+ engine_start
83
+ val = yield
84
+ engine_stop
85
+ val
86
+ end
87
+ def service ; $running[:service] ; end
88
+ def config ; $running[:config] ; end
89
+ def runtime ; $running[:runtime] ; end
90
+ def administrator ; $running[:administrator] ; end
91
+ end
data/spec/target_spec.rb CHANGED
@@ -21,4 +21,22 @@ describe Norikra::Target do
21
21
  expect(Norikra::Target.valid?('_Foo')).to be_false
22
22
  end
23
23
  end
24
+
25
+ describe '==' do
26
+ it 'returns true whenever 2 targets have same name' do
27
+ t1 = Norikra::Target.new("target1")
28
+ t2 = Norikra::Target.new("target2")
29
+ t3 = Norikra::Target.new("target3")
30
+ tt = Norikra::Target.new("target1")
31
+
32
+ expect(t1 == tt).to be_true
33
+ expect(t2 == tt).to be_false
34
+ expect(t3 == tt).to be_false
35
+
36
+ expect([t1, t2, t3].include?(tt)).to be_true
37
+ expect([t2, t3].include?(tt)).to be_false
38
+
39
+ expect([t1, t2, t3].include?("target1")).to be_true
40
+ end
41
+ end
24
42
  end
@@ -67,12 +67,6 @@ describe Norikra::TypedefManager do
67
67
  expect(manager.typedefs['sample'].fields['x'].optional?).to be_true
68
68
  end
69
69
  end
70
- describe '#fields_defined?' do
71
- it 'does not fail' do
72
- expect(manager.fields_defined?('sample', ['a','b','x'])).to be_true
73
- expect(manager.fields_defined?('sample', ['a','b','y'])).to be_false
74
- end
75
- end
76
70
 
77
71
  describe '#ready?' do
78
72
  context 'with query with single target' do
@@ -101,6 +95,41 @@ describe Norikra::TypedefManager do
101
95
  end
102
96
  end
103
97
 
98
+ describe '#register_waiting_fields' do
99
+ context 'with query with single target' do
100
+ it 'add no fields into waiting_fields about known fields only' do
101
+ q1 = Norikra::Query.new(:name => 'test', :expression => 'select a,b from sample.win:time(5 sec) where c > 1.0')
102
+ manager.register_waiting_fields(q1)
103
+ q2 = Norikra::Query.new(:name => 'test', :expression => 'select a,b from sample.win:time(5 sec) where c > 1.0 and z')
104
+ manager.register_waiting_fields(q2)
105
+ q3 = Norikra::Query.new(:name => 'test', :expression => 'select a from sample_next.win:time(5 sec) where c > 1.0 and d > 2.0')
106
+ manager.register_waiting_fields(q3)
107
+
108
+ expect(manager.typedefs['sample'].waiting_fields).to eql([])
109
+ expect(manager.typedefs['sample_next'].waiting_fields).to eql([])
110
+ end
111
+ end
112
+
113
+ context 'with query with single target with unknown fields' do
114
+ it 'adds unknown fields into waiting_fields' do
115
+ expect(manager.typedefs['sample'].waiting_fields).to eql([])
116
+ expect(manager.typedefs['sample_next'].waiting_fields).to eql([])
117
+
118
+ q1 = Norikra::Query.new(:name => 'test', :expression => 'select a from sample.win:time(5 sec) where c > 1.0 and p')
119
+ manager.register_waiting_fields(q1)
120
+ q2 = Norikra::Query.new(:name => 'test', :expression => 'select a from sample.win:time(5 sec) where c > 1.0 and p and q')
121
+ manager.register_waiting_fields(q2)
122
+ q3 = Norikra::Query.new(:name => 'test', :expression => 'select a,e from sample_next.win:time(5 sec) where c > 1.0 and d > 2.0')
123
+ manager.register_waiting_fields(q3)
124
+
125
+ expect(q1.fields('sample')).to eql(['a','c','p'])
126
+ expect(q2.fields('sample')).to eql(['a','c','p','q'])
127
+ expect(manager.typedefs['sample'].waiting_fields).to eql(['p', 'q'])
128
+ expect(manager.typedefs['sample_next'].waiting_fields).to eql(['e'])
129
+ end
130
+ end
131
+ end
132
+
104
133
  describe '#generate_fieldset_mapping' do
105
134
  it 'retuns collect mapping for fieldsets' do
106
135
  q1 = Norikra::Query.new(:name => 'test', :expression => 'select a from sample.win:time(5 sec) where c > 1.0 and z')
@@ -151,12 +180,6 @@ describe Norikra::TypedefManager do
151
180
  end
152
181
  end
153
182
 
154
- describe '#format' do
155
- it 'does not fail' do
156
- expect(manager.format('sample', {'a'=>'foo','b'=>'bar','c'=>'0.03'})).to be_instance_of(Hash)
157
- end
158
- end
159
-
160
183
  describe '#subsets' do
161
184
  it 'returns list of query fieldset (and base set), subset of specified fieldset, owned by manager for specified target' do
162
185
  base = {'a'=>'string','b'=>'string','c'=>'double'}
data/spec/typedef_spec.rb CHANGED
@@ -39,6 +39,16 @@ describe Norikra::Typedef do
39
39
  expect(t.fields['l'].type).to eql('long')
40
40
  expect(t.fields['l'].optional?).to be_false
41
41
  end
42
+
43
+ it 'remove waiting field' do
44
+ t = Norikra::Typedef.new
45
+ t.waiting_fields = ['c', 'l', 'k', 'z']
46
+
47
+ t.reserve('k', 'string')
48
+ t.reserve('l', 'long', false)
49
+
50
+ expect(t.waiting_fields).to eql(['c','z'])
51
+ end
42
52
  end
43
53
 
44
54
  describe '#activate' do
@@ -55,6 +65,32 @@ describe Norikra::Typedef do
55
65
  expect(t.baseset.object_id).not_to eql(set.object_id)
56
66
  end
57
67
  end
68
+
69
+ context 'with waiting fields' do
70
+ it 'remove fields in waitings' do
71
+ t = Norikra::Typedef.new
72
+ t.waiting_fields = ['a', 'c']
73
+
74
+ set = Norikra::FieldSet.new({'a'=>'string','b'=>'long','c'=>'double'})
75
+ set.target = 'testing'
76
+ set.level = :base
77
+ t.activate(set)
78
+
79
+ expect(t.waiting_fields).to eql([])
80
+ end
81
+
82
+ it 'does not remove fields in waitings without definition in base fieldset' do
83
+ t = Norikra::Typedef.new
84
+ t.waiting_fields = ['a', 'c', 'x', 'y', 'z']
85
+
86
+ set = Norikra::FieldSet.new({'a'=>'string','b'=>'long','c'=>'double'})
87
+ set.target = 'testing'
88
+ set.level = :base
89
+ t.activate(set)
90
+
91
+ expect(t.waiting_fields).to eql(['x', 'y', 'z'])
92
+ end
93
+ end
58
94
  end
59
95
  end
60
96
 
@@ -75,6 +111,14 @@ describe Norikra::Typedef do
75
111
  expect(t.fields['b'].optional?).to be_false
76
112
  end
77
113
 
114
+ it 'has container fields with chained access fields' do
115
+ t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long', 'c.0' => 'boolean'})
116
+ expect(t.fields.size).to eql(3)
117
+ expect(t.container_fields.size).to eql(1)
118
+ expect(t.container_fields['c'].name).to eql('c')
119
+ expect(t.container_fields['c'].type).to eql('array')
120
+ end
121
+
78
122
  describe '#lazy?' do
79
123
  it 'returns false' do
80
124
  t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
@@ -183,6 +227,38 @@ describe Norikra::Typedef do
183
227
  expect(t.fields['d'].type).to eql('string')
184
228
  expect(t.fields['d'].optional?).to be_true
185
229
  end
230
+
231
+ it 'deletes waiting fields' do
232
+ t = Norikra::Typedef.new({'a'=>'string','b'=>'long'})
233
+ t.waiting_fields = ['c', 'd', 'e']
234
+
235
+ t.push(:query, Norikra::FieldSet.new({'a'=>'string','b'=>'long'}))
236
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long'}))
237
+
238
+ expect(t.waiting_fields.size).to eql(3)
239
+
240
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long','c'=>'double'}))
241
+ expect(t.waiting_fields).to eql(['d','e'])
242
+
243
+ t.push(:query, Norikra::FieldSet.new({'a'=>'string','b'=>'long','d'=>'string'}))
244
+ expect(t.waiting_fields).to eql(['e'])
245
+ end
246
+
247
+ it 'deletes waiting chained access fields' do
248
+ t = Norikra::Typedef.new({'a'=>'string','b'=>'long'})
249
+ t.waiting_fields = ['c', 'd', 'e', 'f.0', 'f.1', 'g.fieldx']
250
+
251
+ t.push(:query, Norikra::FieldSet.new({'a'=>'string','b'=>'long'}))
252
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long'}))
253
+
254
+ expect(t.waiting_fields.size).to eql(6)
255
+
256
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long','c'=>'double','g.fieldy'=>'long','g.fieldz'=>'string'}))
257
+ expect(t.waiting_fields).to eql(['d','e','f.0','f.1','g.fieldx'])
258
+
259
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long','d'=>'string','f.0'=>'long','f.1'=>'long','g.fieldx'=>'string'}))
260
+ expect(t.waiting_fields).to eql(['e'])
261
+ end
186
262
  end
187
263
 
188
264
  describe '#pop' do
@@ -232,9 +308,81 @@ describe Norikra::Typedef do
232
308
  end
233
309
  end
234
310
 
311
+ describe '#simple_guess' do
312
+ it 'can guess field definitions with class of values' do
313
+ typedef = Norikra::Typedef.new({'key1' => 'boolean'})
314
+ t = typedef.simple_guess({'key1' => true, 'key2' => false, 'key3' => 10, 'key4' => 3.1415, 'key5' => 'foobar'})
315
+ r = t.definition
316
+ expect(r['key1']).to eql('boolean')
317
+ expect(r['key2']).to eql('boolean')
318
+ expect(r['key3']).to eql('long')
319
+ expect(r['key4']).to eql('double')
320
+ expect(r['key5']).to eql('string')
321
+ expect(r.keys.size).to eql(5)
322
+ end
323
+
324
+ it 'does not guess with content of string values, nor containers' do
325
+ typedef = Norikra::Typedef.new({})
326
+ t = typedef.simple_guess({'key1' => 'TRUE', 'key2' => 'false', 'key3' => "10", 'key4' => '3.1415', 'key5' => {:a => 1}})
327
+ r = t.definition
328
+ expect(r.size).to eql(4)
329
+ expect(r['key1']).to eql('string')
330
+ expect(r['key2']).to eql('string')
331
+ expect(r['key3']).to eql('string')
332
+ expect(r['key4']).to eql('string')
333
+ end
334
+
335
+ it 'can guess about fields already known with strict mode' do
336
+ typedef = Norikra::Typedef.new({'key1' => 'boolean', 'key2' => 'boolean', 'key3' => 'long'})
337
+ t = typedef.simple_guess({'key1' => true, 'key2' => false, 'key3' => 10, 'key4' => 3.1415, 'key5' => 'foobar'}, true, true)
338
+ r = t.definition
339
+ expect(r['key1']).to eql('boolean')
340
+ expect(r['key2']).to eql('boolean')
341
+ expect(r['key3']).to eql('long')
342
+ expect(r['key4']).to be_nil
343
+ expect(r['key5']).to be_nil
344
+ expect(r.keys.size).to eql(3)
345
+ end
346
+
347
+ it 'can guess about fields already known or waiting fields with strict mode' do
348
+ typedef = Norikra::Typedef.new({'key1' => 'boolean', 'key2' => 'boolean', 'key3' => 'long'})
349
+ typedef.waiting_fields = ['key5']
350
+
351
+ t = typedef.simple_guess({'key1' => true, 'key2' => false, 'key3' => 10, 'key4' => 3.1415, 'key5' => 'foobar'}, true, true)
352
+ r = t.definition
353
+ expect(r['key1']).to eql('boolean')
354
+ expect(r['key2']).to eql('boolean')
355
+ expect(r['key3']).to eql('long')
356
+ expect(r['key4']).to be_nil
357
+ expect(r['key5']).to eql('string')
358
+ expect(r.keys.size).to eql(4)
359
+ end
360
+
361
+ it 'can guess about container chain access fields which pre-defined or be waiting' do
362
+ typedef = Norikra::Typedef.new({'key1' => 'boolean', 'key2' => 'long', 'key3.$0.key4' => 'string'})
363
+ typedef.waiting_fields = ['key4.f1', 'key4.f2.$0', 'key5']
364
+
365
+ t = typedef.simple_guess({
366
+ 'key1' => true,
367
+ 'key2' => 10,
368
+ 'key3' => [{'k2' => 1, 'k3' => 'sssss', 'key4' => 'baz'}],
369
+ 'key4' => {'f1' => 'xxx', 'f2' => [30, true]},
370
+ 'key5' => 'foobar'
371
+ }, true, true)
372
+ r = t.definition
373
+ expect(r.size).to eql(6)
374
+ expect(r['key1']).to eql('boolean')
375
+ expect(r['key2']).to eql('long')
376
+ expect(r['key3$$0$key4']).to eql('string')
377
+ expect(r['key4$f1']).to eql('string')
378
+ expect(r['key4$f2$$0']).to eql('long')
379
+ expect(r['key5']).to eql('string')
380
+ end
381
+ end
382
+
235
383
  describe '#refer' do
236
- context 'for event defined by data-fieldset already known' do
237
- it 'returns fieldset that already known itself' do
384
+ context 'in non-strict mode' do
385
+ it 'returns fieldset that already known itself for event defined by data-fieldset already known' do
238
386
  t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
239
387
 
240
388
  set1 = Norikra::FieldSet.new({'a'=>'string','b'=>'long'})
@@ -244,10 +392,8 @@ describe Norikra::Typedef do
244
392
  expect(r).to be_instance_of(Norikra::FieldSet)
245
393
  expect(r.object_id).to eql(set1.object_id)
246
394
  end
247
- end
248
395
 
249
- context 'for event with known fields only' do
250
- it 'returns fieldset that is overwritten with known field definitions' do
396
+ it 'returns fieldset that is overwritten with known field definitions for event with known fields only' do
251
397
  t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
252
398
  t.reserve('c','boolean',true)
253
399
  t.reserve('d','double',true)
@@ -261,10 +407,8 @@ describe Norikra::Typedef do
261
407
  expect(r.fields['d'].type).to eql('double')
262
408
  expect(r.summary).to eql('a:string,b:long,c:boolean,d:double')
263
409
  end
264
- end
265
410
 
266
- context 'for event with some unknown fields' do
267
- it 'returns fieldset that contains fields as string for unknowns' do
411
+ it 'returns fieldset that contains fields as string for unknowns for event with some unknown fields' do
268
412
  t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
269
413
 
270
414
  r = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14'})
@@ -277,23 +421,72 @@ describe Norikra::Typedef do
277
421
  expect(r.summary).to eql('a:string,b:long,c:string,d:string')
278
422
  end
279
423
  end
280
- end
281
424
 
282
- describe '#format' do
283
- it 'returns hash value with formatted fields as defined' do
284
- t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
285
- t.reserve('c','boolean',true)
286
- t.reserve('d','double',true)
287
-
288
- d = t.format({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14'})
289
- expect(d['a']).to be_instance_of(String)
290
- expect(d['a']).to eql('hoge')
291
- expect(d['b']).to be_instance_of(Fixnum)
292
- expect(d['b']).to eql(2000)
293
- expect(d['c']).to be_instance_of(TrueClass)
294
- expect(d['c']).to eql(true)
295
- expect(d['d']).to be_instance_of(Float)
296
- expect(d['d']).to eql(3.14)
425
+ context 'in strict mode' do
426
+ it 'returns fieldset that already known itself for event defined by data-fieldset already known' do
427
+ t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
428
+
429
+ set1 = Norikra::FieldSet.new({'a'=>'string','b'=>'long'})
430
+ t.push(:data, set1)
431
+
432
+ r = t.refer({'a'=>'foobar','b'=>200000000}, true)
433
+ expect(r).to be_instance_of(Norikra::FieldSet)
434
+ expect(r.object_id).to eql(set1.object_id)
435
+ end
436
+
437
+ it 'returns fieldset that is overwritten with known field definitions for event with known fields only' do
438
+ t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
439
+ t.reserve('c','boolean',true)
440
+ t.reserve('d','double',true)
441
+
442
+ r = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14'}, true)
443
+ expect(t.datafieldsets.include?(r)).to be_false
444
+
445
+ expect(r.fields['a'].type).to eql('string')
446
+ expect(r.fields['b'].type).to eql('long')
447
+ expect(r.fields['c'].type).to eql('boolean')
448
+ expect(r.fields['d'].type).to eql('double')
449
+ expect(r.summary).to eql('a:string,b:long,c:boolean,d:double')
450
+ end
451
+
452
+ it 'returns fieldset that contains fields already known only for event with some unknown fields' do
453
+ t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
454
+
455
+ r1 = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14'}, true)
456
+ expect(t.datafieldsets.include?(r1)).to be_false
457
+
458
+ expect(r1.fields['a'].type).to eql('string')
459
+ expect(r1.fields['b'].type).to eql('long')
460
+ expect(r1.summary).to eql('a:string,b:long')
461
+
462
+ r2 = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14', 'e' => 'yeeeeeees!'}, true)
463
+ expect(t.datafieldsets.include?(r2)).to be_false
464
+
465
+ expect(r2.fields['a'].type).to eql('string')
466
+ expect(r2.fields['b'].type).to eql('long')
467
+ expect(r2.summary).to eql('a:string,b:long')
468
+ end
469
+
470
+ it 'returns fieldset that contains fields already known or waiting fields for event with some unknown fields' do
471
+ t = Norikra::Typedef.new({'a' => 'string', 'b' => 'long'})
472
+ t.waiting_fields = ['d']
473
+
474
+ r1 = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14'}, true)
475
+ expect(t.datafieldsets.include?(r1)).to be_false
476
+
477
+ expect(r1.fields['a'].type).to eql('string')
478
+ expect(r1.fields['b'].type).to eql('long')
479
+ expect(r1.fields['d'].type).to eql('string')
480
+ expect(r1.summary).to eql('a:string,b:long,d:string')
481
+
482
+ r2 = t.refer({'a'=>'hoge','b'=>'2000','c'=>'true','d'=>'3.14', 'e' => 'yeeeeeees!'}, true)
483
+ expect(t.datafieldsets.include?(r2)).to be_false
484
+
485
+ expect(r2.fields['a'].type).to eql('string')
486
+ expect(r2.fields['b'].type).to eql('long')
487
+ expect(r1.fields['d'].type).to eql('string')
488
+ expect(r2.summary).to eql('a:string,b:long,d:string')
489
+ end
297
490
  end
298
491
  end
299
492
 
@@ -317,6 +510,24 @@ describe Norikra::Typedef do
317
510
  expect(t2.fields.keys.sort).to eql(fields.keys.sort)
318
511
  expect(t2.fields.values.map(&:to_hash)).to eql(fields.values.map(&:to_hash))
319
512
  end
513
+
514
+ it 'returns hash instance to show fields which contains hash/array fields' do
515
+ t = Norikra::Typedef.new({'a'=>'string','b'=>'long'})
516
+ t.push(:query, Norikra::FieldSet.new({'a'=>'string','b'=>'long','e.0'=>'string'}))
517
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long','e.0'=>'string','e.1'=>'long'}))
518
+ t.push(:data, Norikra::FieldSet.new({'a'=>'string','b'=>'long','c'=>'double','f.f1'=>'string'}))
519
+ t.push(:query, Norikra::FieldSet.new({'a'=>'string','b'=>'long','d'=>'string'}))
520
+ fields = t.fields
521
+
522
+ r = t.dump
523
+ expect(r.keys.sort).to eql([:a, :b, :c, :d, :e, :f])
524
+ expect(r[:a]).to eql({name: 'a', type: 'string', optional: false})
525
+ expect(r[:b]).to eql({name: 'b', type: 'long', optional: false})
526
+ expect(r[:c]).to eql({name: 'c', type: 'double', optional: true})
527
+ expect(r[:d]).to eql({name: 'd', type: 'string', optional: true})
528
+ expect(r[:e]).to eql({name: 'e', type: 'array', optional: true})
529
+ expect(r[:f]).to eql({name: 'f', type: 'hash', optional: true})
530
+ end
320
531
  end
321
532
  end
322
533
  end