File: 064csv.t

package info (click to toggle)
libtm-perl 1.53-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,780 kB
  • ctags: 594
  • sloc: perl: 34,611; sh: 377; makefile: 50
file content (193 lines) | stat: -rw-r--r-- 5,942 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

use Test::More qw(no_plan);

use TM;
use Class::Trait;
use Data::Dumper;
$Data::Dumper::Indent = 1;

sub _chomp {
    my $s = shift;
    chomp $s;
    return $s;
}

my $warn = shift @ARGV;
unless ($warn) {
    close STDERR;
    open (STDERR, ">/dev/null");
    select (STDERR); $| = 1;
}

use constant DONE => 1;
#-------------------------------------------------------

use_ok ('TM::Serializable::CSV');

if (DONE) { # 
    my $content = q|association-type,location,bio-unit
is-born,gold-coast,rumsti
is-born,vienna,ramsti
|;

    my $tm = new TM (baseuri=>"tm:");
    Class::Trait->apply ($tm, "TM::Serializable::CSV");
    $tm->deserialize ($content);
#warn Dumper $tm;
    is ($tm->tids ('rumsti'), 'tm:rumsti', 'topic found');
    is ($tm->tids ('ramsti'), 'tm:ramsti', 'topic found');
    is (scalar $tm->match (TM->FORALL, type => 'tm:is-born', arole => 'tm:location', aplayer => 'tm:gold-coast', 
			                                     brole => 'tm:bio-unit', bplayer => 'tm:rumsti'), 1, 'assoc 1');
    is (scalar $tm->match (TM->FORALL, type => 'tm:is-born', arole => 'tm:location', aplayer => 'tm:vienna',
			                                     brole => 'tm:bio-unit', bplayer => 'tm:ramsti'), 1, 'assoc 2');

     $content = q|bio-unit,association-type,location
remsti,is-born,gold-coast
rimsti,is-born,vienna
|;
    $tm->deserialize ($content);

#warn Dumper $tm;
    is ($tm->tids ('remsti'), 'tm:remsti', 'topic found');
    is ($tm->tids ('rimsti'), 'tm:rimsti', 'topic found');
    is (scalar $tm->match (TM->FORALL, type => 'tm:is-born', arole => 'tm:location', aplayer => 'tm:gold-coast', 
			                                     brole => 'tm:bio-unit', bplayer => 'tm:remsti'), 1, 'assoc 1');
    is (scalar $tm->match (TM->FORALL, type => 'tm:is-born', arole => 'tm:location', aplayer => 'tm:vienna',
			                                     brole => 'tm:bio-unit', bplayer => 'tm:rimsti'), 1, 'assoc 2');
}

if (DONE) {
    my $content = q|name,id,location,homepage
"Rumsti",rumsti,gold-coast,http://rumsti.com
"Ramsti",ramsti,vienna,http://ramsti.com
|;

    my $tm = new TM (baseuri=>"tm:");
    Class::Trait->apply ($tm, "TM::Serializable::CSV");
    $tm->deserialize ($content);
#warn Dumper $tm;
    is ($tm->tids ('rumsti'), 'tm:rumsti', 'topic found');
    is ($tm->tids ('ramsti'), 'tm:ramsti', 'topic found');

    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[0] }
	            $tm->match (TM->FORALL, type => 'name') ],
		  [
		   'tm:ramsti',
		   'tm:rumsti'
		  ]), 'name owners');
    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[1]->[0] }
	            $tm->match (TM->FORALL, type => 'name') ],
		  [
		   'Ramsti',
		   'Rumsti'
		  ]), 'names');

    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[0] }
	            $tm->match (TM->FORALL, type => 'tm:location') ],
		  [
		   'tm:ramsti',
		   'tm:rumsti'
		  ]), 'location owners');
    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[1]->[0] }
	            $tm->match (TM->FORALL, type => 'tm:location') ],
		  [
		   'gold-coast',
		   'vienna'
		  ]), 'locations');

    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[0] }
	            $tm->match (TM->FORALL, type => 'tm:homepage') ],
		  [
		   'tm:ramsti',
		   'tm:rumsti'
		  ]), 'homepage owners');
    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[1]->[0] }
	            $tm->match (TM->FORALL, type => 'tm:homepage') ],
		  [
		   'http://ramsti.com',
		   'http://rumsti.com'
		  ]), 'homepages');
    use TM::Literal;
    ok (eq_array ([
	            sort
	            map {  $_->[TM->PLAYERS]->[1]->[1] }
	            $tm->match (TM->FORALL, type => 'tm:homepage') ],
		  [
		   TM::Literal->URI,
		   TM::Literal->URI
		  ]), 'homepages types');
}

if (DONE) { # round trip
    my $content = q|association-type,location,bio-unit
is-born,gold-coast,rumsti
is-born,vienna,ramsti
|;

    my $tm = new TM (baseuri=>"tm:");
    Class::Trait->apply ($tm, "TM::Serializable::CSV");
    $tm->deserialize ($content);

    my $back = $tm->serialize ('association-type,location,bio-unit', type => 'tm:is-born', baseuri => 0);
    ok (eq_set (_to_list ($content), _to_list ($back)), 'round trip');

       $back = $tm->serialize ('association-type,location,bio-unit', type => 'is-born', baseuri => 0);
    ok (eq_set (_to_list ($content), _to_list ($back)), 'round trip (auto tid)');

       $back = $tm->serialize ('association-type,location,bio-unit', type => 'is-born', baseuri => 1);
    like ($back, qr/tm:gold-coast/, 'round trip (baseuri)');

       $back = $tm->serialize ('association-type,location,bio-unit');
#    warn ">>>>$back<<";
    my @back = split /\n/, $back;

    is (3, (scalar grep { $_ eq 'isa' }              @back), 'back isa');
    is (5, (scalar grep { $_ eq 'is-subclass-of' }   @back), 'back subclass');
    is ($back[0], 'association-type,location,bio-unit', 'back header');
    is (2, (scalar grep { $_ =~ /is-born/ }          @back), 'back is-born');

    my $back2 = $tm->serialize ('association-type,location,bio-unit', specification => '+associations');
    my @back2 = split /\n/, $back2;
#    warn Dumper \@back2;
    ok (eq_set (\@back, \@back2), '+associations is default');

    $back2 = $tm->serialize ('association-type,location,bio-unit', specification => '+all -infrastructure');
    @back2 = split /\n/, $back2;
    is (0, (scalar grep { $_ eq 'isa' }              @back2), 'back no isa');
    is (0, (scalar grep { $_ eq 'is-subclass-of' }   @back2), 'back no subclass');


sub _to_list {
    my $content = shift;

    use Text::CSV;
    my $csv = Text::CSV->new();

    my @content;
    foreach my $line (split /\n/, $content) {
	$csv->parse ($line);
	push @content, [ $csv->fields ];
    }
#    warn Dumper \@content;
    return \@content;
}

}

__END__