File Coverage

blib/lib/JMAP/Tester/Response/Sentence/Set.pm
Criterion Covered Total %
statement 112 113 99.1
branch 13 14 92.8
condition 10 12 83.3
subroutine 23 23 100.0
pod 15 17 88.2
total 173 179 96.6


line stmt bran cond sub pod time code
1 1     1   15 use v5.20.0;
  1         2  
2             package JMAP::Tester::Response::Sentence::Set 0.110;
3             # ABSTRACT: the kind of sentence you get in reply to a setFoos call
4              
5 1     1   5 use Moo;
  1         1  
  1         6  
6             extends 'JMAP::Tester::Response::Sentence';
7              
8 1     1   321 use experimental 'signatures';
  1         2  
  1         6  
9              
10 1     1   675 use Data::Dumper ();
  1         5968  
  1         27  
11 1     1   5 use JMAP::Tester::Abort 'abort';
  1         2  
  1         8  
12              
13 1     1   108 use namespace::clean;
  1         1  
  1         6  
14              
15             #pod =head1 OVERVIEW
16             #pod
17             #pod A "Set" sentence is a kind of L
18             #pod for representing C results. It has convenience methods for getting
19             #pod out the data returned in these kinds of sentences.
20             #pod
21             #pod =method new_state
22             #pod
23             #pod This returns the C in the result.
24             #pod
25             #pod =method old_state
26             #pod
27             #pod This returns the C in the result.
28             #pod
29             #pod =cut
30              
31 1     1 1 3 sub new_state ($self) { $self->arguments->{newState} }
  1         3  
  1         3  
  1         9  
32 1     1 1 30 sub old_state ($self) { $self->arguments->{oldState} }
  1         2  
  1         1  
  1         9  
33              
34             #pod =method created
35             #pod
36             #pod This returns the hashref of data in the C property.
37             #pod
38             #pod =method created_id
39             #pod
40             #pod my $id = $set->created_id( $cr_id );
41             #pod
42             #pod This returns the id given to the object created for the given creation id. If
43             #pod that creation id doesn't correspond to a created object, C is returned.
44             #pod
45             #pod =method created_creation_ids
46             #pod
47             #pod This returns the list of creation ids that were successfully created. Note:
48             #pod this returns I, not object ids.
49             #pod
50             #pod =method created_ids
51             #pod
52             #pod This returns the list of object ids that were successfully created.
53             #pod
54             #pod =method not_created_ids
55             #pod
56             #pod This returns the list of creation ids that were I successfully created.
57             #pod
58             #pod =method create_errors
59             #pod
60             #pod This returns a hashref mapping creation ids to error properties.
61             #pod
62             #pod =method updated_ids
63             #pod
64             #pod This returns a list of object ids that were successfully updated.
65             #pod
66             #pod =method not_updated_ids
67             #pod
68             #pod This returns a list of object ids that were I successfully updated.
69             #pod
70             #pod =method update_errors
71             #pod
72             #pod This returns a hashref mapping object ids to error properties.
73             #pod
74             #pod =method destroyed_ids
75             #pod
76             #pod This returns a list of object ids that were successfully destroyed.
77             #pod
78             #pod =method not_destroyed_ids
79             #pod
80             #pod This returns a list of object ids that were I successfully destroyed.
81             #pod
82             #pod =method destroy_errors
83             #pod
84             #pod This returns a hashref mapping object ids to error properties.
85             #pod
86             #pod =cut
87              
88 1     1 1 4 sub as_set ($self) { $self }
  1         3  
  1         2  
  1         5  
89              
90 10   100 10 1 47 sub created ($self) { $self->arguments->{created} // {} }
  10         20  
  10         14  
  10         106  
91              
92 4     4 1 2749 sub created_id ($self, $creation_id) {
  4         9  
  4         9  
  4         8  
93 4 100       15 return undef unless my $props = $self->created->{$creation_id};
94 2         12 return $props->{id};
95             }
96              
97 2     2 1 8 sub created_creation_ids ($self) {
  2         5  
  2         4  
98 2         6 keys %{ $self->created }
  2         7  
99             }
100              
101 2     2 1 2548 sub created_ids ($self) {
  2         6  
  2         5  
102 2         6 map {; $_->{id} } values %{ $self->created }
  2         11  
  2         8  
103             }
104              
105 3     3 1 2547 sub updated_ids ($self) {
  3         6  
  3         4  
106 3   50     12 my $updated = $self->{arguments}{updated} // {};
107              
108 3 100       10 if (ref $updated eq 'ARRAY') {
109 1         7 return @$updated;
110             }
111              
112 2         32 return keys %$updated;
113             }
114              
115 2     2 0 6 sub updated ($self) {
  2         3  
  2         3  
116 2   50     7 my $updated = $self->{arguments}{updated} // {};
117              
118 2 100       5 if (ref $updated eq 'ARRAY') {
119 1         4 return { map {; $_ => undef } @$updated };
  2         8  
120             }
121              
122 1         5 return $updated;
123             }
124              
125 1     1 1 1023 sub destroyed_ids ($self) { @{ $self->{arguments}{destroyed} } }
  1         3  
  1         2  
  1         3  
  1         10  
126              
127             # Is this the best API to provide? I dunno, maybe. Usage will tell us whether
128             # it's right. -- rjbs, 2016-04-11
129 1     1 1 1009 sub not_created_ids ($self) { keys %{ $self->{arguments}{notCreated} } }
  1         3  
  1         3  
  1         3  
  1         10  
130 1     1 1 761 sub not_updated_ids ($self) { keys %{ $self->{arguments}{notUpdated} } }
  1         5  
  1         2  
  1         2  
  1         9  
131 1     1 1 770 sub not_destroyed_ids ($self) { keys %{ $self->{arguments}{notDestroyed} } }
  1         3  
  1         3  
  1         3  
  1         9  
132              
133 8   100 8 1 773 sub create_errors ($self) { $self->{arguments}{notCreated} // {} }
  8         13  
  8         13  
  8         53  
134 9   100 9 1 2245 sub update_errors ($self) { $self->{arguments}{notUpdated} // {} }
  9         18  
  9         11  
  9         53  
135 9   100 9 1 2191 sub destroy_errors ($self) { $self->{arguments}{notDestroyed} // {} }
  9         12  
  9         26  
  9         48  
136              
137 6     6 0 119 sub assert_no_errors ($self) {
  6         10  
  6         8  
138 6         7 my @errors;
139 6         11 local $Data::Dumper::Terse = 1;
140              
141 6 50       9 if (keys %{ $self->create_errors }) {
  6         14  
142 0         0 push @errors, "notCreated: " . Data::Dumper::Dumper(
143             $self->_strip_json_types( $self->create_errors )
144             );
145             }
146              
147 6 100       10 if (keys %{ $self->update_errors }) {
  6         12  
148 1         4 push @errors, "notUpdated: " . Data::Dumper::Dumper(
149             $self->_strip_json_types( $self->update_errors )
150             );
151             }
152              
153 6 100       121 if (keys %{ $self->destroy_errors }) {
  6         11  
154 1         2 push @errors, "notDestroyed: " . Data::Dumper::Dumper(
155             $self->_strip_json_types( $self->destroy_errors )
156             );
157             }
158              
159 6 100       82 return $self unless @errors;
160              
161 1         10 $self->sentence_broker->response->abort(
162             "errors found in " . $self->name . " sentence",
163             \@errors,
164             );
165             }
166              
167             1;
168              
169             __END__