forked from petdance/webservice-solr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.t
143 lines (123 loc) · 3.88 KB
/
document.t
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
use Test::More tests => 23;
use strict;
use warnings;
use WebService::Solr::Document;
use WebService::Solr::Field;
use JSON::XS;
my @fields = (
[ id => 1, { boost => 1.6 } ],
[ sku => 'A6B9A', { boost => '1.0' } ],
[ manu => 'The Bird Book', { boost => '7.1' } ],
[ weight => '4.0', { boost => 3.2 } ],
[ name => 'Sally Jesse Raphael' ],
);
my @field_objs = map { WebService::Solr::Field->new( @$_ ) } @fields;
{
my $expect = join( '',
'<doc boost="3.0">',
'<field boost="1.6" name="id">1</field>',
'<field boost="1.0" name="sku">A6B9A</field>',
'<field boost="7.1" name="manu">The Bird Book</field>',
'<field boost="3.2" name="weight">4.0</field>',
'<field name="name">Sally Jesse Raphael</field>',
'</doc>' );
{
my $doc = WebService::Solr::Document->new( @fields[ 0 .. 4 ] );
isa_ok( $doc, 'WebService::Solr::Document' );
$doc->boost( '3.0' );
is( $doc->to_xml, $expect, 'to_xml(), array refs' );
}
{
my $doc = WebService::Solr::Document->new( @field_objs[ 0 .. 4 ] );
isa_ok( $doc, 'WebService::Solr::Document' );
$doc->boost( '3.0' );
is( $doc->to_xml, $expect, 'to_xml(), objs' );
}
{
my $doc = WebService::Solr::Document->new();
isa_ok( $doc, 'WebService::Solr::Document' );
$doc->boost( '3.0' );
$doc->add_fields( @field_objs[ 0 .. 4 ] );
is( $doc->to_xml, $expect, 'to_xml(), add_fields()' );
}
{
my $doc = WebService::Solr::Document->new(
$field_objs[ 0 ],
@fields[ 1 .. 3 ],
$field_objs[ 4 ]
);
isa_ok( $doc, 'WebService::Solr::Document' );
$doc->boost( '3.0' );
is( $doc->to_xml, $expect, 'to_xml(), mixed' );
}
}
{
my $doc = WebService::Solr::Document->new( key => 'value' );
isa_ok( $doc, 'WebService::Solr::Document' );
is( $doc->to_xml,
'<doc><field name="key">value</field></doc>',
'to_xml(), key=>val'
);
}
{
my $doc
= WebService::Solr::Document->new( key => 'value', $field_objs[ 0 ] );
isa_ok( $doc, 'WebService::Solr::Document' );
is( $doc->to_xml,
'<doc><field name="key">value</field><field boost="1.6" name="id">1</field></doc>',
'to_xml(), key=>val + obj'
);
}
{
my $doc
= WebService::Solr::Document->new( $field_objs[ 0 ], key => 'value' );
isa_ok( $doc, 'WebService::Solr::Document' );
is( $doc->to_xml,
'<doc><field boost="1.6" name="id">1</field><field name="key">value</field></doc>',
'to_xml(), obj + key=>val'
);
}
{
my $doc = WebService::Solr::Document->new( @field_objs[ 0 .. 4 ],
$field_objs[ 1 ] );
isa_ok( $doc, 'WebService::Solr::Document' );
{
my @values = $doc->values_for( 'id' );
is_deeply( \@values, [ 1 ], 'values_for() -- single value' );
}
{
my @values = $doc->values_for( 'sku' );
is_deeply(
\@values,
[ 'A6B9A', 'A6B9A' ],
'values_for() -- multi-value'
);
}
{
my @values = $doc->values_for( 'dne' );
is_deeply( \@values, [], 'values_for() -- no values' );
}
}
{
my $doc = WebService::Solr::Document->new( x => [ 1, 2, 3 ] );
isa_ok( $doc, 'WebService::Solr::Document' );
is( scalar @{ $doc->fields }, 3, 'arrayref of values to fields' );
}
{
my $doc = WebService::Solr::Document->new( @fields[ 0 .. 4 ] );
is_deeply(
[ sort( $doc->field_names ) ],
[ qw(id manu name sku weight) ],
'field_names'
);
}
{
my $doc = WebService::Solr::Document->new(
bools => decode_json( q/{"arr": [true,false,true]}/ )->{ arr } );
isa_ok( $doc, 'WebService::Solr::Document' );
is_deeply(
[ 1, 0, 1 ],
[ $doc->values_for( 'bools' ) ],
'boolean arrays'
);
}