re-work and fix BSE's tests
[bse.git] / t / 050-local / 030-tags.t
1 #!perl -w
2 use strict;
3 use BSE::Test ();
4 use Test::More tests => 36;
5 use File::Spec;
6 use Carp qw(confess);
7
8 $SIG{__DIE__} = sub { confess @_ };
9
10 BEGIN {
11   unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules");
12 }
13
14 use BSE::API ':all';
15
16 my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin");
17 ok(bse_init($base_cgi),   "initialize api")
18   or print "# failed to bse_init in $base_cgi\n";
19 my $cfg = bse_cfg;
20
21 # set up some test data
22 my $parent = bse_make_catalog(title => "Tags parent",
23                               cfg => $cfg);
24 ok($parent, "make parent article");
25 my @kids;
26 my %kids;
27 for my $title ("A" .. "Z") {
28   my $kid = bse_make_product
29     (
30      cfg => $cfg,
31      title => $title,
32      retailPrice => 0,
33      parentid => $parent->id
34     );
35   push @kids, $kid;
36   $kids{$title} = $kid;
37 }
38
39 {
40   my %tags =
41     (
42      A => "Platform: iPad/iPad: iPad 1",
43      B => "Platform: iPad/iPad: iPad 1",
44      C => "Platform: iPad/iPad: iPad 2",
45      D => "Platform: iPad/iPad: iPad 2",
46      E => "Platform: iPad/iPad: iPad 2",
47      F => "Platform: iPad/iPad: iPad 3",
48      G => "Platform: iPod/iPod: Classic",
49      H => "Platform: iPod/iPod: Classic",
50      I => "Platform: iPod/iPod: Nano",
51      J => "Platform: iPod/iPod: Nano",
52      K => "Platform: iPod/iPod: 6th Gen",
53      L => "Platform: iPod/iPod: 6th Gen",
54      M => "Platform: iPhone/iPhone: 3G",
55      N => "Platform: iPhone/iPhone: 3G",
56      O => "Platform: iPhone/iPhone: 3GS",
57      P => "Platform: iPhone/iPhone: 4",
58      Q => "Platform: iPhone/iPhone: 4",
59     );
60
61   # set the tags
62   my $all_set = 1;
63   for my $title (keys %tags) {
64     my $kid = $kids{$title} or die "No kid $title found";
65     my $error;
66     unless ($kid->set_tags([ split '/', $tags{$title} ], \$error)) {
67       diag "Setting $title tags: $error\n";
68       $all_set = 0;
69     }
70   }
71   ok($all_set, "all kid tags set");
72 }
73
74 is_deeply([ map $_->title, $parent->children ],
75           [ reverse("A" .. "Z") ],
76           "check the childrent are in place");
77
78 {
79   ok(!$parent->can("children_tags"), "non-optimized path");
80   my $tagged = $parent->collection_with_tags
81     (
82      "children",
83      [ "Platform: iPad" ]
84     );
85   ok($tagged, "collection_with_tags returned");
86  SKIP:
87   {
88     ok($tagged->{objects}, "has objects")
89       or skip("No objects", 1);
90     is_deeply([ map $_->title, @{$tagged->{objects}} ],
91               [ reverse("A" .. "F") ],
92               "check we got the expected kids");
93   }
94  SKIP:
95   {
96     ok($tagged->{object_ids}, "has object_ids")
97       or skip("No object_ids", 1);
98     is_deeply($tagged->{object_ids},
99               [ map $_->id, @kids{reverse("A" .. "F")} ],
100               "check object_ids");
101   }
102  SKIP:
103   {
104     ok($tagged->{extratags}, "has extratags")
105       or skip("No extratags", 1);
106     is(join("/", sort map $_->name, @{$tagged->{extratags}}),
107        "iPad: iPad 1/iPad: iPad 2/iPad: iPad 3",
108        "check knowntags");
109   }
110   ok($tagged->{members}, "has members");
111  SKIP:
112   {
113     ok($tagged->{counts}, "has counts")
114       or skip("No counts", 1);
115     is_deeply($tagged->{counts},
116               {
117                "iPad: iPad 1" => 2,
118                "iPad: iPad 2" => 3,
119                "iPad: iPad 3" => 1,
120               }, "check counts");
121   }
122 }
123
124 {
125   ok($parent->can("all_visible_product_tags"), "optimized path");
126   my $tagged = $parent->collection_with_tags
127     (
128      "all_visible_products",
129      [ "Platform: iPad" ]
130     );
131   ok($tagged, "collection_with_tags returned (all_visible_products)");
132  SKIP:
133   {
134     ok($tagged->{objects}, "has objects")
135       or skip("No objects", 1);
136     is_deeply([ map $_->title, @{$tagged->{objects}} ],
137               [ reverse("A" .. "F") ],
138               "check we got the expected kids");
139   }
140  SKIP:
141   {
142     ok($tagged->{object_ids}, "has object_ids")
143       or skip("No object_ids", 1);
144     is_deeply($tagged->{object_ids},
145               [ map $_->id, @kids{reverse("A" .. "F")} ],
146               "check object_ids");
147   }
148  SKIP:
149   {
150     ok($tagged->{extratags}, "has extratags")
151       or skip("No extratags", 1);
152     is(join("/", sort map $_->name, @{$tagged->{extratags}}),
153        "iPad: iPad 1/iPad: iPad 2/iPad: iPad 3",
154        "check knowntags");
155   }
156   ok($tagged->{members}, "has members");
157  SKIP:
158   {
159     ok($tagged->{counts}, "has counts")
160       or skip("No counts", 1);
161     is_deeply($tagged->{counts},
162               {
163                "iPad: iPad 1" => 2,
164                "iPad: iPad 2" => 3,
165                "iPad: iPad 3" => 1,
166               }, "check counts");
167   }
168 }
169
170 {
171   ok($parent->can("all_visible_product_tags"), "optimized path");
172   my $tagged = $parent->collection_with_tags
173     (
174      "all_visible_products",
175      [ "Platform: iPad" ],
176      { noobjects => 1 }
177     );
178   ok($tagged, "collection_with_tags returned (all_visible_products, no objects)");
179   ok(!$tagged->{objects}, "no objects");
180  SKIP:
181   {
182     ok($tagged->{object_ids}, "has object_ids")
183       or skip("No object_ids", 1);
184     # they don't necessarily come back in order
185     is_deeply([ sort @{$tagged->{object_ids}} ],
186               [ sort map $_->id, @kids{reverse("A" .. "F")} ],
187               "check object_ids");
188   }
189  SKIP:
190   {
191     ok($tagged->{extratags}, "has extratags")
192       or skip("No extratags", 1);
193     is(join("/", sort map $_->name, @{$tagged->{extratags}}),
194        "iPad: iPad 1/iPad: iPad 2/iPad: iPad 3",
195        "check knowntags");
196   }
197   ok($tagged->{members}, "has members");
198  SKIP:
199   {
200     ok($tagged->{counts}, "has counts")
201       or skip("No counts", 1);
202     is_deeply($tagged->{counts},
203               {
204                "iPad: iPad 1" => 2,
205                "iPad: iPad 2" => 3,
206                "iPad: iPad 3" => 1,
207               }, "check counts");
208   }
209 }
210
211 END {
212   for my $kid (@kids) {
213     $kid->remove($cfg);
214   }
215   $parent->remove($cfg) if $parent;
216 }