Skip to content

Commit b5f223d

Browse files
committed
WIP: change deleteXXX in ResultSet classes.
1 parent 96d631f commit b5f223d

18 files changed

+271
-219
lines changed

lib/DB/Schema/ResultSet/Course.pm

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,13 @@ C<as_result_set>, a boolean if the return is to be a result_set
169169
170170
=head3 output
171171
172-
The deleted course either as a C< DBIx::Class::ResultSet::Course> object or a hashref
173-
of the fields. See above.
172+
Nothing (undef) is returned.
174173
175174
=cut
176175

177176
sub deleteCourse ($self, %args) {
178-
my $course_to_delete = $self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
179-
180-
my $deleted_course = $course_to_delete->delete;
181-
return $deleted_course if $args{as_result_set};
182-
183-
return { $course_to_delete->get_inflated_columns };
177+
$self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1)->delete;
178+
return;
184179
}
185180

186181
=head1 updateCourse

lib/DB/Schema/ResultSet/ProblemPool.pm

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ sub getProblemPool ($self, %args) {
102102
103103
Add a problem pool for a given course
104104
105+
=head3 arguments
106+
107+
=over
108+
109+
=item * C<params> a hashref specifying information on the added problem pool
110+
One must include either the C<course_id> or C<course_name> and the C<pool_name>.
111+
If there is not enough info to get the course and pool, a C<ParametersNeeded> exception is thrown.
112+
113+
114+
=item * C<as_result_set>: boolean
115+
116+
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
117+
See C<DBIx::Class::ResultSet> for more information
118+
119+
=back
120+
121+
=head3 output
122+
123+
Either a hashref of the added problem pool or a C<DBIx::Class::ResultSet::ProblemPool>
124+
if C<as_result_set> is true.
125+
105126
=cut
106127

107128
sub addProblemPool ($self, %args) {
@@ -147,6 +168,25 @@ sub addProblemPool ($self, %args) {
147168
148169
updates the parameters of an existing problem pool
149170
171+
=head3 arguments
172+
173+
=over
174+
175+
=item * C<info> a hashref specifying information on the problem pool
176+
One must include either the C<course_id> or C<course_name> and either the C<pool_name>
177+
or C<problem_pool_id>. If there is not enough info to get the course and pool, a
178+
C<ParametersNeeded> exception is thrown.
179+
180+
=item * C<params>: a hashref containing the information to be updated.
181+
182+
=item * C<as_result_set>: boolean
183+
184+
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
185+
See C<DBIx::Class::ResultSet> for more information
186+
187+
=back
188+
189+
150190
=cut
151191

152192
sub updateProblemPool ($self, %args) {
@@ -173,21 +213,36 @@ sub updateProblemPool ($self, %args) {
173213
return { $updated_pool->get_columns };
174214
}
175215

176-
=head2 updateProblemPool
216+
=head2 deleteProblemPool
177217
178-
updates the parameters of an existing problem pool
218+
delete a Problem Pool
179219
180-
=cut
220+
=head3 arguments
181221
182-
sub deleteProblemPool ($self, %args) {
183-
my $pool = $self->getProblemPool(info => $args{info}, as_result_set => 1);
222+
=over
184223
185-
DB::Exception::PoolNotInCourse->throws(error => 'The problem pool does not exist')
186-
unless defined($pool);
224+
=item * C<info> a hashref specifying information on the problem pool
225+
One must include either the C<course_id> or C<course_name> and either the C<pool_name>
226+
or C<problem_pool_id>. If there is not enough info to get the course and pool, a
227+
C<ParametersNeeded> exception is thrown.
187228
188-
my $deleted_pool = $pool->delete();
189229
190-
return $args{as_result_set} ? $deleted_pool : { $deleted_pool->get_columns };
230+
=item * C<as_result_set>: boolean
231+
232+
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
233+
See C<DBIx::Class::ResultSet> for more information
234+
235+
=back
236+
237+
=head3 output
238+
239+
Nothing (undef) is returned.
240+
241+
=cut
242+
243+
sub deleteProblemPool ($self, %args) {
244+
$self->getProblemPool(info => $args{info}, as_result_set => 1)->delete;
245+
return;
191246
}
192247

193248
#####

lib/DB/Schema/ResultSet/ProblemSet.pm

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,19 +509,13 @@ if false, a hashrefs of ProblemSet.
509509
510510
=head3 output
511511
512-
An hashref of the deleted problem set or an object of type C<DBIx::Class::ResultSet::ProblemSet>
512+
Nothing (undef) is returned.
513513
514514
=cut
515515

516516
sub deleteProblemSet ($self, %args) {
517-
518-
my $set_to_delete = $self->getProblemSet(info => $args{info}, as_result_set => 1);
519-
$set_to_delete->delete;
520-
521-
return $set_to_delete if $args{as_result_set};
522-
my $set = { $set_to_delete->get_inflated_columns, set_type => $set_to_delete->set_type };
523-
delete $set->{type};
524-
return $set;
517+
$self->getProblemSet(info => $args{info}, as_result_set => 1)->delete;
518+
return;
525519
}
526520

527521
# The following are private methods used in this module.

lib/DB/Schema/ResultSet/SetProblem.pm

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ if either the course or set doesn't exist, an exception will be thrown.
157157
158158
=head3 output
159159
160-
An array of Problems (as hashrefs) or an array of C<DBIx::Class::ResultSet::Problem>
160+
A problem (as a hashref) or an array of C<DBIx::Class::ResultSet::Problem>
161161
162162
=cut
163163

@@ -350,26 +350,12 @@ if either the course or set doesn't exist, an exception will be thrown.
350350
351351
=head3 output
352352
353-
A problem (as hashrefs) or an object of class C<DBIx::Class::ResultSet::Problem>
353+
Nothing (undef) is returned.
354354
355355
=cut
356356

357357
sub deleteSetProblem ($self, %args) {
358-
my $set_problem = $self->getSetProblem(info => $args{info}, as_result_set => 1);
359-
my $problem_set = $self->rs('ProblemSet')->getProblemSet(
360-
info => {
361-
course_id => $set_problem->problem_set->course_id,
362-
set_id => $set_problem->set_id
363-
},
364-
as_result_set => 1
365-
);
366-
367-
my $problem = $problem_set->search_related('problems', getProblemInfo($args{info}))->single;
368-
369-
my $deleted_problem = $problem->delete;
370-
371-
return $deleted_problem if $args{as_result_set};
372-
return { $deleted_problem->get_inflated_columns };
358+
$self->getSetProblem(info => $args{info}, as_result_set => 1)->delete;
373359
}
374360

375361
# just a small subroutine to shorten access to the db.

lib/DB/Schema/ResultSet/User.pm

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,7 @@ The deleted user as a C<DBIx::Class::ResultSet::User> object.
149149
# TODO: Delete everything related to the user from all tables.
150150

151151
sub deleteGlobalUser ($self, %args) {
152-
my $user_to_delete = $self->getGlobalUser(info => $args{info}, as_result_set => 1);
153-
154-
my $deleted_user = $user_to_delete->delete;
155-
return $deleted_user if $args{as_result_set};
156-
return removeLoginParams({ $deleted_user->get_inflated_columns });
152+
$self->getGlobalUser(info => $args{info}, as_result_set => 1)->delete;
157153
}
158154

159155
=head1 updateGlobalUser
@@ -533,21 +529,18 @@ from the global user table)
533529
534530
=over
535531
=item - If either information about the user or the course is missing, an exception will be thrown
536-
=item - If the user is already in the course, an exception will be thrown.
532+
=item - If the user is not in the course, an exception will be thrown.
537533
=back
538534
539535
=head3 output
540536
541-
An hashref of the deleted user or merged user or a C<DB::Schema::ResultSet::CourseUser>
537+
Nothing (undef) is returned.
542538
543539
=cut
544540

545541
sub deleteCourseUser ($self, %args) {
546-
my $course_user_to_delete = $self->getCourseUser(info => $args{info}, as_result_set => 1)->delete;
547-
548-
return $course_user_to_delete if $args{as_result_set};
549-
return $args{merged} ? _getMergedUser($course_user_to_delete) : _getCourseUser($course_user_to_delete);
550-
542+
$self->getCourseUser(info => $args{info}, as_result_set => 1)->delete;
543+
return;
551544
}
552545

553546
# This is a small subroutine to shorten access to the db.

lib/DB/Schema/ResultSet/UserProblem.pm

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -490,27 +490,8 @@ or a C<DBIx::Class::ResultSet::UserProblem>
490490
=cut
491491

492492
sub deleteUserProblem ($self, %args) {
493-
my $user_problem =
494-
$args{info}->{user_problem_id}
495-
? $self->find({ user_problem_id => $args{info}->{user_problem_id} })
496-
: $self->getUserProblem(
497-
info => $args{info},
498-
skip_throw => 1,
499-
as_result_set => 1
500-
);
501-
502-
DB::Exception::UserProblemNotFound->throw(message => 'The user '
503-
. getUserInfo($args{info})->{username} // getUserInfo($args{info})->{user_id}
504-
. ' already has problem number '
505-
. getProblemInfo($args{info})->{problem_number}
506-
// ("(set_problem_id): " . getProblemInfo($args{info})->{set_problem_id})
507-
. ' in set with name'
508-
. getSetInfo($args{info})->{set_name} // ("(set_id): " . getSetInfo($args{info})->{set_id}))
509-
unless $user_problem;
510-
511-
my $user_problem_to_delete = $user_problem->delete;
512-
return $user_problem_to_delete if $args{as_result_set};
513-
return $args{merged} ? _mergeUserProblem($user_problem_to_delete) : _getUserProblem($user_problem_to_delete);
493+
$self->getUserProblem(info => $args{info}, as_result_set => 1)->delete;
494+
return;
514495
}
515496

516497
=head2 getUserProblemVersions

lib/DB/Schema/ResultSet/UserSet.pm

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,8 @@ delete a single UserSet for a given course, user, and ProblemSet
408408
=cut
409409

410410
sub deleteUserSet ($self, %args) {
411-
my $user_set = $self->getUserSet(info => $args{info}, as_result_set => 1);
412-
413-
DB::Exception::UserSetNotInCourse->throw(
414-
set_name => $args{info}->{set_name},
415-
course_name => $args{info}->{course_name},
416-
username => $args{info}->{username}
417-
) unless defined($user_set);
418-
419-
$user_set->delete;
420-
# why are we returning anything from this call? success/failure instead?
421-
return $user_set if $args{as_result_set};
422-
return $args{merged} ? _mergeUserSet($user_set) : _getUserSet($user_set);
411+
my $user_set = $self->getUserSet(info => $args{info}, as_result_set => 1)->delete;
412+
return;
423413
}
424414

425415
=head2 getUserSetVersions

t/db/001_courses.t

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ throws_ok {
129129
'DB::Exception::CourseNotFound', 'updateCourse: update a non-existent course_id';
130130

131131
# Delete a course
132-
my $deleted_course = $course_rs->deleteCourse(info => { course_name => 'Geometry II' });
133-
removeIDs($deleted_course);
132+
$course_rs->deleteCourse(info => { course_name => 'Geometry II' });
134133

135-
is_deeply($new_course_params, $deleted_course, 'deleteCourse: delete a course');
134+
# and check that it is no longer in the database.
135+
throws_ok {
136+
$course_rs->getCourse(info => { course_name => 'Geometry II' });
137+
}
138+
'DB::Exception::CourseNotFound', 'deleteCourse: delete a course';
136139

137140
# Try to delete a non-existent course by name
138141
throws_ok {

t/db/003_users.t

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,20 @@ throws_ok {
222222
qr/No such column 'invalid_field'/, 'updateUser: pass in an invalid field';
223223

224224
# Delete users that were created
225-
my $user_to_delete = $users_rs->deleteGlobalUser(info => { username => $user->{username} });
226-
227-
removeIDs($user_to_delete);
228-
cleanUndef($user_to_delete);
229-
is_deeply($updated_user, $user_to_delete, 'deleteUser: delete a user');
230-
231-
my $deleted_selma = $users_rs->deleteGlobalUser(info => { username => 'selma' });
232-
removeIDs($deleted_selma);
233-
cleanUndef($deleted_selma);
234-
is_deeply($deleted_selma, $selma_params, 'deleteUser: deleter another user');
235-
236-
my $deleted_patty = $users_rs->deleteGlobalUser(info => { username => 'patty' });
237-
removeIDs($deleted_patty);
238-
cleanUndef($deleted_patty);
239-
is_deeply($deleted_patty, $patty_params, 'deleteUser: deleter a third user');
240-
241-
my $user_to_delete2 = $users_rs->deleteGlobalUser(info => { username => $added_user2->{username} });
242-
removeIDs($user_to_delete2);
243-
cleanUndef($user_to_delete2);
244-
is_deeply($added_user2, $user_to_delete2, 'deleteUser: delete yet another user.');
225+
$users_rs->deleteGlobalUser(info => { username => $user->{username} });
226+
227+
# and check they are no longer in the database:
228+
throws_ok {
229+
$users_rs->getGlobalUser(info => { username => $user->{username}});
230+
}
231+
'DB::Exception::UserNotFound', 'deleteUser: delete a user';
232+
233+
# Delete others and no need to check because a full check to see that the database
234+
# is restored is done below.
235+
236+
$users_rs->deleteGlobalUser(info => { username => 'selma' });
237+
$users_rs->deleteGlobalUser(info => { username => 'patty' });
238+
$users_rs->deleteGlobalUser(info => { username => $added_user2->{username} });
245239

246240
# Delete a user that doesn't exist.
247241
throws_ok {

t/db/004_course_users.t

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -328,41 +328,39 @@ throws_ok {
328328
}
329329
'DB::Exception::InvalidParameter', 'updateCourseUser: an parameter with invalid value';
330330

331-
# Delete a single user from a course.
332-
my $deleted_user;
333-
my $dont_delete_users; # Switch to not delete added users.
331+
# Delete a course user
332+
$user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'quimby' });
334333

335-
SKIP: {
336-
skip 'delete added users', 5 if $dont_delete_users;
337-
338-
my $deleted_course_user = $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'quimby' });
339-
removeIDs($deleted_course_user);
340-
341-
is_deeply($course_user_params, $deleted_course_user, 'deleteCourseUser: delete a user from a course');
334+
# and check to ensure quimby was deleted.
335+
throws_ok {
336+
$user_rs->getCourseUser(info => { course_name => 'Arithmetic', username => 'quimby' });
337+
}
338+
'DB::Exception::UserNotInCourse', 'deleteCourseUser: delete a user from a course';
342339

343-
$deleted_user = $user_rs->deleteGlobalUser(info => { username => 'quimby' });
344-
removeIDs($deleted_user);
340+
$user_rs->deleteGlobalUser(info => { username => 'quimby' });
345341

346-
is_deeply($user_params, $deleted_user, 'deleteGlobalUser: delete a user');
342+
throws_ok {
343+
$user_rs->getGlobalUser(info => { username => 'quimby' });
344+
}
345+
'DB::Exception::UserNotFound', 'deleteGlobalUser: delete a user';
347346

348-
# deleteUser: Check that if the course doesn't exist, an error is thrown:
349-
throws_ok {
350-
$user_rs->deleteCourseUser(info => { course_name => 'unknown_course', username => 'barney' });
351-
}
352-
'DB::Exception::CourseNotFound', "deleteUser: the course doesn't exist";
347+
# deleteUser: Check that if the course doesn't exist, an error is thrown:
348+
throws_ok {
349+
$user_rs->deleteCourseUser(info => { course_name => 'unknown_course', username => 'barney' });
350+
}
351+
'DB::Exception::CourseNotFound', "deleteUser: the course doesn't exist";
353352

354-
# deleteUser: Check that if the course exists, but the user not a member.
355-
throws_ok {
356-
$user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'marge' });
357-
}
358-
'DB::Exception::UserNotInCourse', 'deleteUser: the user is not a member of the course';
353+
# deleteUser: Check that if the course exists, but the user not a member.
354+
throws_ok {
355+
$user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'marge' });
356+
}
357+
'DB::Exception::UserNotInCourse', 'deleteUser: the user is not a member of the course';
359358

360-
# deleteUser: Send in username_name instead of username
361-
throws_ok {
362-
$user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username_name => 'bart' });
363-
}
364-
'DB::Exception::ParametersNeeded', 'deleteUser: the incorrect information is passed in.';
359+
# deleteUser: Send in username_name instead of username
360+
throws_ok {
361+
$user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username_name => 'bart' });
365362
}
363+
'DB::Exception::ParametersNeeded', 'deleteUser: the incorrect information is passed in.';
366364

367365
# Check that the precalc users have not changed.
368366
@precalc_users_from_db = $user_rs->getCourseUsers(info => { course_name => 'Precalculus' }, merged => 1);

0 commit comments

Comments
 (0)