File Coverage

blib/lib/JMAP/Tester/Response/Sentence/Set.pm
Criterion Covered Total %
statement 41 62 66.1
branch 8 14 57.1
condition 5 12 41.6
subroutine 11 22 50.0
pod 15 17 88.2
total 80 127 62.9


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