-
Notifications
You must be signed in to change notification settings - Fork 16
/
document_blueprint_spec.rb
735 lines (606 loc) · 26.6 KB
/
document_blueprint_spec.rb
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe XapianDb::DocumentBlueprint do
describe ".reset" do
it "clears the blueprint configuration" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
XapianDb::DocumentBlueprint.reset
expect(XapianDb::DocumentBlueprint.configured_classes).to eq([])
end
end
describe ".configured_classes" do
it "returns all configured classes" do
XapianDb::DocumentBlueprint.reset
expect(XapianDb::DocumentBlueprint.configured_classes.size).to eq(0)
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.configured_classes).to eq([IndexedObject])
end
end
describe ".configured?(name)" do
before :each do
XapianDb::DocumentBlueprint.reset
end
it "returns true, if a blueprint with the given name is configured" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.configured?(:IndexedObject)).to be_truthy
end
it "returns false, if no blueprints are configured" do
expect(XapianDb::DocumentBlueprint.configured?(:IndexedObject)).to be_falsey
end
it "returns false, if a blueprint with the given name is not configured" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.configured?(:NotConfigured)).to be_falsey
end
end
describe ".blueprint_for(name)" do
before :each do
XapianDb::DocumentBlueprint.reset
end
it "returns the blueprint for a class" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)).to be_a_kind_of XapianDb::DocumentBlueprint
end
it "returns the blueprint for the super class if no specific blueprint is configured" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
class InheritedIndexedObject < IndexedObject; end
expect(XapianDb::DocumentBlueprint.blueprint_for(:InheritedIndexedObject)).to eq(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject))
end
it "can handle namespaces" do
XapianDb::DocumentBlueprint.setup("Namespace::IndexedObject") do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for("Namespace::IndexedObject")).to be_a_kind_of XapianDb::DocumentBlueprint
end
it "returns nil if there is no blueprint configuration for a class" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:Object)).not_to be
end
it "returns nil if there is no blueprint configuration at all" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:Object)).not_to be
end
it "accepts a string for the class name" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for("IndexedObject")).to be_a_kind_of XapianDb::DocumentBlueprint
end
it "accepts a symbol for the class name" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)).to be_a_kind_of XapianDb::DocumentBlueprint
end
end
describe ".dependencies_for(klass_name, changed_attrs)" do
it "returns dependency objects that match the klass name" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
XapianDb::DocumentBlueprint.setup(:OtherIndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
blueprint.dependency :IndexedObject do |person|
end
end
expect(XapianDb::DocumentBlueprint.dependencies_for('IndexedObject', []).size).to eq(1)
end
it "returns dependency objects that match the klass name and the attributes they're interested in, when specified" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
XapianDb::DocumentBlueprint.setup(:OtherIndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
blueprint.dependency :IndexedObject, when_changed: %i(name) do |person|
end
end
expect(XapianDb::DocumentBlueprint.dependencies_for 'IndexedObject', []).to be_empty # no change for name
expect(XapianDb::DocumentBlueprint.dependencies_for('IndexedObject', ['name']).size).to eq(1)
end
end
describe ".searchable_prefixes" do
it "should return an array of all method names configured to be indexed" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.searchable_prefixes).to include(:id, :name)
end
it "should return %w(indexed_class) if no attributes and no indexes are configured" do
XapianDb::DocumentBlueprint.reset
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
end
expect(XapianDb::DocumentBlueprint.searchable_prefixes).to eq(%w(indexed_class))
end
end
describe ".attributes" do
it "should return an array of all defined attributes" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
blueprint.attribute :name
end
expect(XapianDb::DocumentBlueprint.attributes).to include(:id, :name)
end
it "should return an empty array if no attributes are configured" do
XapianDb::DocumentBlueprint.reset
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.attributes).to eq([])
end
end
describe ".type_info_for(attribute)" do
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :date, :as => :date
blueprint.attribute :untyped
end
end
it "should return the type of an attribute if one is defined" do
expect(XapianDb::DocumentBlueprint.type_info_for(:date)).to eq(:date)
end
it "should return :string if no type is defined" do
expect(XapianDb::DocumentBlueprint.type_info_for(:untyped)).to eq(:string)
end
it "returns nil if the attribute is not defined" do
expect(XapianDb::DocumentBlueprint.type_info_for(:not_defined)).not_to be
end
it "returns nil if no blueprints are defined defined" do
XapianDb::DocumentBlueprint.reset
expect(XapianDb::DocumentBlueprint.type_info_for(:not_defined)).not_to be
end
end
describe ".setup (class)" do
it "stores a blueprint for a given class" do
XapianDb::DocumentBlueprint.setup(:IndexedObject)
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)).to be_a_kind_of(XapianDb::DocumentBlueprint)
end
it "does replace the blueprint for a class if the class is reloaded" do
XapianDb::DocumentBlueprint.setup(:IndexedObject)
expect(XapianDb::DocumentBlueprint.configured_classes.size).to eq(1)
# reload IndexedObject
Object.send(:remove_const, :IndexedObject)
Object.send(:remove_const, :IndexedObjectSubclass)
load File.expand_path('../../basic_mocks.rb', __FILE__)
XapianDb::DocumentBlueprint.setup(:IndexedObject)
expect(XapianDb::DocumentBlueprint.configured_classes.size).to eq(1)
end
it "raises an exception if a method with the same name has different type declarations" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :date, :as => :date
end
expect { XapianDb::DocumentBlueprint.setup(:OtherIndexedObject) do |blueprint|
blueprint.attribute :date, :as => :number
end }.to raise_error ArgumentError
end
it "allows blueprint definitions with symbols" do
XapianDb::DocumentBlueprint.setup(:IndexedObject)
expect(XapianDb::DocumentBlueprint.blueprint_for('IndexedObject')).not_to be_nil
end
it "allows blueprint definitions with strings" do
XapianDb::DocumentBlueprint.setup('IndexedObject')
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)).not_to be_nil
end
it "lazy-loads blueprint classes" do
expect do
XapianDb::DocumentBlueprint.setup(:NotYetLoadedClass)
class NotYetLoadedClass; end
end.not_to raise_error
expect(XapianDb::DocumentBlueprint.blueprint_for(:NotYetLoadedClass)).not_to be_nil
end
end
describe ".value_number_for(:attribute)" do
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
blueprint.attribute :name
end
@position_offset = 2 # slots 0 and 1 are reserved
end
it "returns the value number of an indexed method" do
expect(XapianDb::DocumentBlueprint.value_number_for(:name)).to eq(@position_offset + 1)
end
it "accepts a string as an argument" do
expect(XapianDb::DocumentBlueprint.value_number_for("name")).to eq(@position_offset + 1)
end
it "raises an argument error if the method is not indexed" do
expect { XapianDb::DocumentBlueprint.value_number_for(:not_indexed) }.to raise_error ArgumentError
end
it "raises an argument error if no blueprints are defined" do
XapianDb::DocumentBlueprint.reset
XapianDb::DocumentBlueprint.instance_variable_set(:@attributes, nil)
expect { XapianDb::DocumentBlueprint.value_number_for(:not_indexed) }.to raise_error ArgumentError
end
it "handles multiple blueprints whith the same indexed method at different positions" do
XapianDb::DocumentBlueprint.setup(:OtherIndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :not_in_alphabetical_order
blueprint.index :name
end
expect(XapianDb::DocumentBlueprint.value_number_for(:name)).to eq(@position_offset + 1)
end
it "returns 0 for :indexed_class" do
expect(XapianDb::DocumentBlueprint.value_number_for(:indexed_class)).to eq(0)
end
it "calculates the value number in alphabetical order even if the attributes are not declared in alphabetical order" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :date_of_birth, :as => :date
blueprint.attribute :empty_field
blueprint.attribute :id
blueprint.attribute :name
blueprint.attribute :array
end
expect(XapianDb::DocumentBlueprint.value_number_for(:array)).to eq(@position_offset)
end
end
describe "#adapter (symbol)" do
it "overides the adapter for the configured class" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.adapter :generic
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)._adapter).to be_equal XapianDb::Adapters::GenericAdapter
end
end
describe "#_adapter" do
it "returns the generic adapter if no configration is present" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)._adapter).to be_equal XapianDb::Adapters::GenericAdapter
end
it "returns the globally configured adapter if specified" do
allow(XapianDb::Config).to receive(:adapter).and_return(XapianDb::Adapters::ActiveRecordAdapter)
XapianDb::DocumentBlueprint.setup(:ActiveRecordObject) do |blueprint|
blueprint.attribute :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:ActiveRecordObject)._adapter).to be_equal XapianDb::Adapters::ActiveRecordAdapter
end
it "returns the adapter configured for this blueprint if specified" do
XapianDb::DocumentBlueprint.setup(:DatamapperObject) do |blueprint|
blueprint.adapter :datamapper
blueprint.attribute :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:DatamapperObject)._adapter).to be_equal XapianDb::Adapters::DatamapperAdapter
end
end
describe "#autoindex(boolean)" do
it "turns auto-indexing on or off for this blueprint" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.autoindex false
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).autoindex?).to be_falsey
end
it "is true by default" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).autoindex?).to be_truthy
end
end
describe "#attribute" do
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
end
end
it "adds an attribute to the blueprint" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:id)
end
it "adds the attribute to the indexed methods by default" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).indexed_method_names).to include(:id)
end
it "does not index the attribute if the :index option ist set to false" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id, :index => false
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).indexed_method_names).not_to include(:id)
end
it "allows to specify a weight for the attribute" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id, :weight=> 10
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).options_for_indexed_method(:id).weight).to eq(10)
end
it "accepts a block to specify complex attribute evaluation" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :complex do
if @id == 1
"One"
else
"Not one"
end
end
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:complex)
end
it "allows to specify if the attribute should be prefixed" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id, :prefixed => false
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).options_for_indexed_method(:id).prefixed).to be_falsey
end
it "throws an exception if the attribute name maps to a Xapian::Document method name" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:id)
expect{XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :data
end}.to raise_error ArgumentError
end
end
describe "#attributes" do
it "allows to declare one single attribute" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attributes :id
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:id)
end
it "allows to declare multiple attributes in a single statement (but without options)" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attributes :id, :name, :first_name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:id, :name, :first_name)
end
it "throws an exception if the attribute name maps to a Xapian::Document method name" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attributes :id, :name, :first_name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).attribute_names).to include(:id)
expect{XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attributes :data
end}.to raise_error ArgumentError
end
end
describe "#index" do
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
end
end
it "adds an indexed value to the blueprint" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).options_for_indexed_method(:id)).to be_a_kind_of XapianDb::DocumentBlueprint::IndexOptions
end
it "defaults the weight option to 1" do
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).options_for_indexed_method(:id).weight).to eq(1)
end
it "accepts weight as an option" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id, :weight => 10
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).options_for_indexed_method(:id).weight).to eq(10)
end
it "does not accept a type option" do
expect { XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :date, :as => :date
end }.to raise_error ArgumentError
end
it "allows to declare two methods (can distinguish this from a method with an options hash)" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id, :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).indexed_method_names).to include(:id, :name)
end
it "allows to declare multiple methods (but without options)" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id, :name, :first_name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).indexed_method_names).to include(:id, :name, :first_name)
end
end
describe "#ignore_if" do
it "accepts a block and stores the block as a Proc" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.ignore_if {
active == false
}
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).instance_variable_get(:@ignore_expression)).to be_a_kind_of Proc
end
end
describe "#should_index? obj" do
it "should return true if no ignore expression is given" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
end
blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
obj = IndexedObject.new 1
expect(blueprint.should_index?(obj)).to be_truthy
end
it "should return false if the ignore expression evaluates to true" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
blueprint.ignore_if {id == 1}
end
blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
obj = IndexedObject.new 1
expect(blueprint.should_index?(obj)).to be_falsey
end
it "should return true if the ignore expression evaluates to false" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :id
blueprint.ignore_if {id == 2}
end
blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
obj = IndexedObject.new 1
expect(blueprint.should_index?(obj)).to be_truthy
end
end
describe "base_query" do
it "accepts a base query expression" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :array
blueprint.base_query ActiveRecordObject.includes(:children)
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).lazy_base_query).to be
end
it "converts an explicit base query expression to a proc" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :array
blueprint.base_query ActiveRecordObject.includes(:children)
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).lazy_base_query).to be_a Proc
end
it "accepts a base query expression inside a block" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :array
blueprint.base_query { ActiveRecordObject.includes(:children) }
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject).lazy_base_query).to be_a Proc
end
end
describe "#natural_sort_order" do
it "defaults to id if not specified" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)._natural_sort_order).to eq(:id)
end
it "accepts a method symbol" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.natural_sort_order :name
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)._natural_sort_order).to eq(:name)
end
it "accepts a block" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.natural_sort_order do
@id
end
end
expect(XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)._natural_sort_order).to be_a Proc
end
it "raises an ArgumentError, if a method name AND a block are given" do
expect { XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.natural_sort_order :name do
@id
end
end }.to raise_error ArgumentError
end
end
describe "#dependency(klass_name, when_changed: [], &block)" do
it "adds a dependency to the blueprint" do
dependent_object = OtherIndexedObject.new 1
blueprint = XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.index :id
blueprint.index :name
blueprint.dependency :OtherIndexedObject, when_changed: %i(name) do |person|
[dependent_object]
end
end
expect(blueprint.dependencies.size).to eq(1)
dependency = blueprint.dependencies.first
expect(dependency.dependent_on).to eq 'OtherIndexedObject'
expect(dependency.trigger_attributes).to eq ['name']
expect(dependency.block.call).to eq [dependent_object]
end
end
describe "#accessors_module" do
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :array, as: :json
blueprint.attribute :date_of_birth, :as => :date
blueprint.attribute :empty_field
blueprint.attribute :id, as: :integer
blueprint.attribute :name
end
@blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
@doc = Xapian::Document.new
@doc.add_value(0, "IndexedObject")
@doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:array), [1, "two", Date.new(2011, 1, 1)].to_json)
@doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:date_of_birth), "20110101")
@doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:empty_field), nil.to_s)
@doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:id), Xapian::sortable_serialise(1))
@doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:name), "Kogler")
@doc.extend @blueprint.accessors_module
end
it "builds an accessor module for the blueprint" do
expect(@blueprint.accessors_module).to be_a_kind_of Module
end
it "adds accessor methods for each configured field" do
expect(@blueprint.accessors_module.instance_methods).to include(:id, :name, :date_of_birth)
end
it "adds accessor methods that can handle nil" do
expect(@doc.empty_field).to eq("")
end
it "adds an accessor method for the class of the indexed object" do
expect(@doc.indexed_class).to eq("IndexedObject")
end
it "adds accessor methods that deserialize values to native objects" do
expect(@doc.date_of_birth).to eq(Date.new(2011, 1, 1))
end
it "adds a method to access the document attributes as a hash" do
expect(@doc.attributes).to eq({ "array" => [1, "two", "2011-01-01"],
"date_of_birth" => Date.new(2011, 1, 1),
"empty_field" => "",
"id" => 1,
"name" => "Kogler" })
end
end
describe "#type_map" do
let(:blueprint) { XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject) }
before :each do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :date, :as => :date
blueprint.attribute :untyped
end
end
it "should return a hash table" do
expect(blueprint.type_map).to be_a Hash
end
it "contains the type of an indexed method if a type is defined" do
expect(blueprint.type_map[:date]).to eq(:date)
end
it "contains :string for an indexed method if no type is defined" do
expect(blueprint.type_map[:untyped]).to eq(:string)
end
end
describe ".preprocess_terms" do
before :each do
class Klass
def self.normalize_global(text); end
def self.normalize_local(text); end
end
XapianDb::Config.setup do |config|
config.indexer_preprocess_callback Klass.method(:normalize_global)
end
end
after :each do
XapianDb::Config.setup do |config|
config.indexer_preprocess_callback nil
end
end
it "returns the global indexer preprocess callback if none is configured on the blueprint" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
end
blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
expect(blueprint.preprocess_terms).to eq Klass.method(:normalize_global)
end
it "returns the indexer preprocess callback configured on the blueprint" do
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.indexer_preprocess_callback Klass.method(:normalize_local)
end
blueprint = XapianDb::DocumentBlueprint.blueprint_for(:IndexedObject)
expect(blueprint.preprocess_terms).to eq Klass.method(:normalize_local)
end
end
end