Changed tests to import minimal constants.
[LibTracker-Client-Perl.git] / t / 05-tags.t
blob0e80ceb150da03c345260f669a634c9f872deed4
1 use Test::More;
2 use LibTracker::Client qw(:services);
4 if( defined $ENV{LTC_TRACKER_RUNNING} ) {
5         plan tests => 20;
7 else {
8         plan skip_all => "LTC_TRACKER_RUNNING is not set";
11 SKIP: {
12         skip "LTC_TEST_PATH is not set", 20 unless defined $ENV{LTC_TEST_PATH};
14         my $file = $ENV{LTC_TEST_PATH};
15         my $tag1 = "XXX_LTCTAG1_XXX";
16         my $tag2 = "XXX_LTCTAG2_XXX";
18         my $tracker = LibTracker::Client->get_instance();
20         # add a tag
21         my $num = $tracker->add_keywords(SERVICE_FILES, $file, [ $tag1, $tag2 ]);
22         is( $num, 2, "number of tags added" );
24         # get tags
25         my $tags = $tracker->get_keywords(SERVICE_FILES, $file);
26         ok( $tags, "get_keywords return value" );
27         is( ref $tags, "ARRAY", "get_keywords return value type" );
28         ok( contains( $tags, $tag1 ), "recently added tag1 for file" );
29         ok( contains( $tags, $tag2 ), "recently added tag2 for file" );
31         # get all tags
32         $tags = $tracker->get_all_keywords(SERVICE_FILES);
33         ok( $tags, "all_keywords return value" );
34         is( ref $tags, "ARRAY", "all_keywords return value type" );
35         ok( contains( $tags, $tag1 ), "recently added tag1 in all tags" );
36         ok( contains( $tags, $tag2 ), "recently added tag2 in all tags" );
38         # remove tags
39         $num = $tracker->remove_keywords(SERVICE_FILES, $file, [ $tag2 ]);
40         is( $num, 1, "number of tags removed" );
42         # check if tags were removed
43         $tags = $tracker->get_keywords(SERVICE_FILES, $file);
44         is( ref $tags, "ARRAY", "get keywords return type" );
45         ok( contains( $tags, $tag1 ), "still contains tag1" );
46         ok( !contains( $tags, $tag2 ), "does not contain tag2" );
48         # search tags
49         my $results = $tracker->search_keywords(0, SERVICE_FILES, [ $tag1 ], 0, 100);
50         ok( $results, "results returned" );
51         is( ref $results, "ARRAY", "results type check" );
52         ok( contains( $results, $file ), "results include test file" );
54         # remove all tags
55         my $success = $tracker->remove_all_keywords(SERVICE_FILES, $file);
56         ok( $success, "remove all tags" );
58         # verify
59         $results = $tracker->get_keywords(SERVICE_FILES, $file);
60         ok( $results, "results resturned after removing all tags" );
61         is( ref $results, "ARRAY", "no results type check" );
62         cmp_ok( scalar @{$results}, '==', 0, "zero results" );
65 sub contains
67         my $aref = shift;
68         my $value = shift;
70         foreach my $elem ( @{$aref} ) {
71                 return 1 if ($elem eq $value);
72         }
74         return 0;