Skip to content

Commit fd6613a

Browse files
committed
WIP: deleting from db--update store tests
WIP: cleanup the tests WIP: deleting from db--update store tests
1 parent 8ba540e commit fd6613a

File tree

14 files changed

+221
-102
lines changed

14 files changed

+221
-102
lines changed

lib/DB/Schema/ResultSet/ProblemPool.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ sub getPoolProblem ($self, %args) {
333333
. ' is not in the pool named \''
334334
. $problem_pool->pool_name
335335
. "'");
336+
return;
336337
} else {
337338
# Pick a random problem.
338339
my $prob = $pool_problems[ rand @pool_problems ];

lib/WeBWorK3.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ sub problemSetRoutes ($app, $course_routes) {
172172
# CRUD for User Sets
173173
$course_routes->get('/user-sets')->to('ProblemSet#getAllUserSets');
174174
$course_routes->get('/sets/:set_id/users')->to('ProblemSet#getUserSets');
175+
$course_routes->get('/sets/:set_id/users/:course_user_id')->to('ProblemSet#getUserSet');
175176
$course_routes->post('/sets/:set_id/users')->to('ProblemSet#addUserSet');
176177
$course_routes->put('/sets/:set_id/users/:course_user_id')->to('ProblemSet#updateUserSet');
177178
$course_routes->delete('/sets/:set_id/users/:course_user_id')->to('ProblemSet#deleteUserSet');

lib/WeBWorK3/Controller/ProblemSet.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ sub getUserSets ($self) {
106106
return;
107107
}
108108

109+
sub getUserSet ($self) {
110+
my $user_set = $self->schema->resultset('UserSet')->getUserSet(
111+
info => {
112+
course_id => int($self->param('course_id')),
113+
set_id => int($self->param('set_id')),
114+
course_user_id => int($self->param('course_user_id'))
115+
}
116+
);
117+
$self->render(json => $user_set);
118+
return;
119+
}
120+
109121
sub addUserSet ($self) {
110122
my $new_user_set = $self->schema->resultset('UserSet')->addUserSet(
111123
params => {

src/stores/courses.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ export const useCourseStore = defineStore('courses', {
7777
/**
7878
* This deletes the course in the database and the store.
7979
*/
80-
async deleteCourse(course: Course): Promise<Course | undefined> {
80+
async deleteCourse(course: Course): Promise<void> {
8181
const response = await api.delete(`courses/${course.course_id}`);
8282
if (response.status === 200) {
83-
const deleted_course = new Course(response.data as ParseableCourse);
84-
const index = this.courses.findIndex(course => course.course_id === deleted_course.course_id);
83+
const index = this.courses.findIndex(c => c.course_id === course.course_id);
8584
this.courses.splice(index, 1);
86-
return deleted_course;
8785
} else {
8886
throw response.data as ResponseError;
8987
}

src/stores/problem_sets.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,19 @@ export const useProblemSetStore = defineStore('problem_sets', {
180180
/**
181181
* Delete the given ProblemSet from the database and the store.
182182
*/
183-
async deleteProblemSet(set: ProblemSet) {
183+
async deleteProblemSet(set: ProblemSet): Promise<void> {
184184
const response = await api.delete(`courses/${set.course_id}/sets/${set.set_id}`);
185-
const set_to_delete = parseProblemSet(response.data as ParseableProblemSet);
186-
const index = this.problem_sets.findIndex(set => set.set_id === set_to_delete.set_id);
187-
if (index >= 0) {
188-
this.problem_sets.splice(index, 1);
185+
if (response.status === 200) {
186+
const index = this.problem_sets.findIndex((s) => s.set_id === set.set_id);
187+
if (index < 0) {
188+
logger.error('[problem_set store/deleteProblemSet]: the problem set was not found in the store');
189+
} else {
190+
// splice is used so vue3 reacts to changes.
191+
this.problem_sets.splice(index, 1);
192+
}
193+
} else {
194+
logger.error(JSON.stringify(response));
189195
}
190-
// TODO: what if this fails
191-
return set_to_delete;
192196
},
193197
// UserSet actions
194198

@@ -268,16 +272,21 @@ export const useProblemSetStore = defineStore('problem_sets', {
268272
/**
269273
* Deletes the given UserSet from the store and the database.
270274
*/
271-
async deleteUserSet(user_set: UserSet) {
275+
async deleteUserSet(user_set: UserSet): Promise<void> {
272276
const course_id = useSessionStore().course.course_id;
273277
const response = await
274278
api.delete(`courses/${course_id}/sets/${user_set.set_id}/users/${user_set.course_user_id ?? 0}`);
275-
// TODO: check for errors
276-
const deleted_user_set = parseDBUserSet(response.data as ParseableDBUserSet);
277-
const index = this.db_user_sets.findIndex(s => s.user_set_id === deleted_user_set.user_set_id);
278-
this.db_user_sets.splice(index, 1);
279-
user_set.set(deleted_user_set.toObject());
280-
return user_set;
279+
if (response.status === 200) {
280+
const index = this.db_user_sets.findIndex((set) => set.user_set_id === user_set.user_set_id);
281+
if (index < 0) {
282+
logger.error('[user store/deleteUserSet]: the user set was not found in the store');
283+
} else {
284+
// splice is used so vue3 reacts to changes.
285+
this.db_user_sets.splice(index, 1);
286+
}
287+
} else {
288+
logger.error(JSON.stringify(response));
289+
}
281290
},
282291

283292
/**

src/stores/set_problems.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ export const useSetProblemStore = defineStore('set_problems', {
166166
/**
167167
* Delete the given SetProblem in both the store and the database.
168168
*/
169-
async deleteSetProblem(problem: SetProblem): Promise<SetProblem> {
169+
async deleteSetProblem(problem: SetProblem): Promise<void> {
170170
const course_id = useSessionStore().course.course_id;
171171

172-
const response = await api.delete(`courses/${course_id}/sets/${
172+
await api.delete(`courses/${course_id}/sets/${
173173
problem.set_id}/problems/${problem.set_problem_id}`);
174-
const deleted_problem = new SetProblem(response.data as ParseableSetProblem);
175-
// TODO: check for errors
176-
const index = this.set_problems.findIndex(prob => prob.set_problem_id === deleted_problem.set_problem_id);
177-
this.set_problems.splice(index, 1);
178-
return deleted_problem;
174+
const index = this.set_problems.findIndex(prob => prob.set_problem_id === problem.set_problem_id);
175+
if (index < 0) {
176+
logger.error('[stores/set_problems/deleteSetProblem]: the set problem was not found in the store');
177+
} else {
178+
// splice is used so vue3 reacts to changes.
179+
this.set_problems.splice(index, 1);
180+
}
179181
},
180182
// UserProblem actions
181183

@@ -250,26 +252,25 @@ export const useSetProblemStore = defineStore('set_problems', {
250252
/**
251253
* Delete the given UserProblem from the database and the store.
252254
*/
253-
async deleteUserProblem(user_problem: UserProblem): Promise<UserProblem> {
255+
async deleteUserProblem(user_problem: UserProblem): Promise<void> {
254256
const course_id = useSessionStore().course.course_id;
255257
const set_problem = this.set_problems.find(prob => prob.set_problem_id === user_problem.set_problem_id);
256258
const problem_set_store = useProblemSetStore();
257259
const user_set = problem_set_store.findUserSet({ user_set_id: user_problem.user_set_id, });
258260
if (user_set == undefined) {
259261
throw 'deleteUserProblem: returned undefined user set';
260262
}
261-
const response = await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0
263+
await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0
262264
}/users/${user_set.user_id}/problems/${user_problem.user_problem_id}`);
263-
const db_deleted_problem = new DBUserProblem(response.data as ParseableDBUserProblem);
265+
264266
const index = this.db_user_problems
265-
.findIndex(user_problem => user_problem.user_problem_id === db_deleted_problem.user_problem_id);
266-
if (index >= 0) {
267-
this.db_user_problems.splice(index, 1);
267+
.findIndex(user_problem => user_problem.user_problem_id === user_problem.user_problem_id);
268+
if (index < 0) {
269+
logger.error('[stores/set_problems/deleteUserProblem]: the set problem was not found in the store');
270+
} else {
271+
// splice is used so vue3 reacts to changes.
272+
this.set_problems.splice(index, 1);
268273
}
269-
// Create a new UserProblem to return as
270-
const deleted_user_problem = new UserProblem(Object.assign(db_deleted_problem.toObject(),
271-
user_problem.toObject()));
272-
return deleted_user_problem;
273274
}
274275
}
275276
});

src/stores/users.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { api } from 'boot/axios';
22
import { defineStore } from 'pinia';
33

44
import { logger } from 'boot/logger';
5-
import { DBCourseUser, ParseableCourseUser, ParseableDBCourseUser, ParseableUser } from 'src/common/models/users';
5+
import { DBCourseUser, ParseableDBCourseUser, ParseableUser } from 'src/common/models/users';
66
import { User, CourseUser } from 'src/common/models/users';
77
import { invalidError, ResponseError } from 'src/common/api-requests/errors';
88
import { UserRole } from 'src/stores/permissions';
@@ -155,15 +155,20 @@ export const useUserStore = defineStore('user', {
155155
/**
156156
* Deletes the given User in the database and in the store.
157157
*/
158-
async deleteUser(user: User): Promise<User | undefined> {
158+
async deleteUser(user: User): Promise<void> {
159159
const session_store = useSessionStore();
160160
const course_id = session_store.course.course_id;
161161
const response = await api.delete(`courses/${course_id}/global-users/${user.user_id ?? 0}`);
162162
if (response.status === 200) {
163163
const index = this.users.findIndex((u) => u.user_id === user.user_id);
164-
// splice is used so vue3 reacts to changes.
165-
this.users.splice(index, 1);
166-
return new User(response.data as ParseableUser);
164+
if (index < 0) {
165+
logger.error('[user store/deleteUser]: the user was not found in the store');
166+
} else {
167+
// splice is used so vue3 reacts to changes.
168+
this.users.splice(index, 1);
169+
}
170+
} else {
171+
logger.error(JSON.stringify(response));
167172
}
168173
},
169174

@@ -261,16 +266,16 @@ export const useUserStore = defineStore('user', {
261266
/**
262267
* Deletes a Course User from the store and the database.
263268
*/
264-
async deleteCourseUser(course_user: CourseUser): Promise<CourseUser | undefined> {
269+
async deleteCourseUser(course_user: CourseUser): Promise<void> {
265270
const response = await api.delete(`courses/${course_user.course_id}/users/${course_user.user_id}`);
266271
if (response.status === 200) {
267272
const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id);
268-
269-
// splice is used so vue3 reacts to changes.
270-
this.db_course_users.splice(index, 1);
271-
const deleted_course_user = new DBCourseUser(response.data as ParseableCourseUser);
272-
const user = this.users.find(u => u.user_id === deleted_course_user.user_id);
273-
return new CourseUser(Object.assign({}, user?.toObject(), deleted_course_user.toObject()));
273+
if (index < 0) {
274+
logger.error('[user store/deleteCourseUser]: the user was not found in the store');
275+
} else {
276+
// splice is used so vue3 reacts to changes.
277+
this.db_course_users.splice(index, 1);
278+
}
274279
} else if (response.status === 250) {
275280
logger.error(response.data);
276281
throw response.data as ResponseError;

t/db/007_user_set.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ for my $user_set (@merged_user_sets) {
132132
$user_set->{set_visible} = $set->{set_visible} unless defined($user_set->{set_visible});
133133
}
134134

135-
# Get all user sets for a given user in a course.
135+
# Get all user sets
136136
my @all_user_sets_from_db = $user_set_rs->getAllUserSets();
137137

138138
for my $set (@all_user_sets_from_db) {

t/mojolicious/005_problem_sets.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ my $another_new_set = { name => 'this is the wrong field' };
147147
$t->post_ok('/webwork3/api/courses/4/sets' => json => $another_new_set)
148148
->content_type_is('application/json;charset=UTF-8')->json_is('/exception' => 'DB::Exception::ParametersNeeded');
149149

150-
# Some cleanup to restore the databse
150+
# Some cleanup to restore the database
151151
# Delete an existing set.
152152
$t->delete_ok("/webwork3/api/courses/4/sets/$new_set_id")->content_type_is('application/json;charset=UTF-8')
153153
->status_is(200);

tests/stores/courses.spec.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { api } from 'boot/axios';
1616
import { useCourseStore } from 'src/stores/courses';
1717
import { Course } from 'src/common/models/courses';
1818
import { cleanIDs, loadCSV } from '../utils';
19+
import { AxiosError } from 'axios';
1920

2021
describe('Test the course store', () => {
2122

@@ -78,8 +79,18 @@ describe('Test the course store', () => {
7879

7980
test('Delete a course', async () => {
8081
const course_store = useCourseStore();
81-
const deleted_course = await course_store.deleteCourse(added_course) ?? new Course();
82-
expect(cleanIDs(deleted_course)).toStrictEqual(cleanIDs(updated_course));
82+
await course_store.deleteCourse(added_course);
83+
84+
// Check that the course is no longer in the database by getting an exception.
85+
await api.get(`/courses/${added_course.course_id}`)
86+
.then(() => {
87+
fail('Expected failure response');
88+
})
89+
.catch((e: AxiosError) => {
90+
expect(e.response?.status).toBe(500);
91+
expect((e.response?.data as {exception: string}).exception)
92+
.toBe('DB::Exception::CourseNotFound');
93+
});
8394
});
8495
});
8596

@@ -92,8 +103,9 @@ describe('Test the course store', () => {
92103
course_name: ''
93104
});
94105
expect(course.isValid()).toBe(false);
95-
await expect(async () => { await course_store.addCourse(course); })
96-
.rejects.toThrow('The added course is invalid');
106+
await expect(async () => {
107+
await course_store.addCourse(course);
108+
}).rejects.toThrow('The added course is invalid');
97109
});
98110

99111
test('Try to update an invalid course', async () => {

0 commit comments

Comments
 (0)