Skip to content

Commit dd8303e

Browse files
committed
Switch to using the Test2::Suite and Test2::MojoX modules for unit tests.
Note that is important for the unit tests to have the `got` argument first and the `expected` argument second in an `is` test. Test2::Tools::Compare converts the second `expected` argument into a Test2::Tools object which is important for strict checks. Also remove the incomplete t/db/test_course_user.pl file.
1 parent 84a22e7 commit dd8303e

28 files changed

+1729
-1842
lines changed

.github/workflows/unit-tests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@ jobs:
5757
libmojolicious-plugin-authentication-perl \
5858
libnet-ssleay-perl \
5959
libsql-translator-perl \
60-
libtest-exception-perl \
60+
libsub-info-perl \
61+
libterm-table-perl \
6162
libtest-harness-perl \
63+
libtest2-suite-perl \
6264
libtext-csv-perl \
6365
libtry-tiny-perl \
6466
libyaml-libyaml-perl
6567
cpanm --sudo --notest \
6668
DBIx::Class::DynamicSubclass \
6769
Mojolicious::Plugin::DBIC \
6870
Mojolicious::Plugin::NotYAMLConfig \
71+
Test2::MojoX \
6972
Devel::Cover::Report::Codecov
7073
7174
- name: Run perl unit tests

.perlcriticrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ severity = 4
55
# Allow no warnings usage with a category restriction (for signatures)
66
[TestingAndDebugging::ProhibitNoWarnings]
77
allow_with_category_restriction = 1
8+
9+
# Allow $a and $b in sort functions. If sort functions are added to the code not in a "sort" call, they must be added
10+
# to this list. Annoyingly both of these policies that do the same thing have to each get the list.
11+
[Perl::Critic::Policy::Community::DollarAB]
12+
extra_pair_functions = user_prob_sort_fxn
13+
[Perl::Critic::Policy::Freenode::DollarAB]
14+
extra_pair_functions = user_prob_sort_fxn

docker/webwork3.dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ RUN apt-get update && \
1111
cpanminus=1.7045-1 \
1212
git=1:2.34.1-1ubuntu1.4 \
1313
libarray-utils-perl=0.5-2 \
14+
libc6-dev=2.35-0ubuntu3.1 \
1415
libcanary-stability-perl=2006-2 \
1516
libcapture-tiny-perl=0.48-1 \
1617
libclass-accessor-lite-perl=0.08-1.1 \
@@ -26,7 +27,6 @@ RUN apt-get update && \
2627
libdbix-class-perl=0.082842-3 \
2728
libdbix-dbschema-perl=0.45-1 \
2829
libdevel-cover-perl=1.36-2build2 \
29-
libc6-dev=2.35-0ubuntu3.1 \
3030
libexception-class-perl=1.45-1 \
3131
libextutils-config-perl=0.008-2 \
3232
libextutils-helpers-perl=0.026-1 \
@@ -43,7 +43,11 @@ RUN apt-get update && \
4343
libnet-ssleay-perl=1.92-1build2 \
4444
libsql-translator-perl=1.62-1 \
4545
libssl-dev=3.0.2-0ubuntu1.6 \
46+
libsub-info-perl=0.015-2 \
47+
libterm-table-perl=0.015-2 \
4648
libtest-exception-perl=0.43-1 \
49+
libtest-harness-perl=3.42-2 \
50+
libtest2-suite-perl=0.000144-1 \
4751
libtext-csv-perl=2.01-1 \
4852
libtry-tiny-perl=0.31-1 \
4953
libtypes-serialiser-perl=1.01-1 \
@@ -56,6 +60,7 @@ RUN apt-get update && \
5660
DBIx::Class::DynamicSubclass \
5761
Mojolicious::Plugin::DBIC \
5862
Mojolicious::Plugin::NotYAMLConfig \
63+
Test2::MojoX \
5964
Devel::Cover::Report::Codecov
6065

6166
ENTRYPOINT ["/bin/bash"]

t/db/001_courses.t

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ BEGIN {
1414
use lib "$main::ww3_dir/lib";
1515
use lib "$main::ww3_dir/t/lib";
1616

17-
use Test::More;
18-
use Test::Exception;
17+
use Test2::V0;
1918
use YAML::XS qw/LoadFile/;
20-
use DateTime::Format::Strptime;
21-
use Mojo::JSON qw/true false/;
19+
use Mojo::JSON qw/true/;
2220

2321
use DB::Schema;
2422

25-
use TestUtils qw/loadCSV removeIDs loadSchema/;
23+
use TestUtils qw/loadCSV removeIDs/;
2624

2725
# Load the database
2826
my $config_file = "$main::ww3_dir/conf/webwork3-test.yml";
@@ -34,7 +32,6 @@ my $schema = DB::Schema->connect(
3432
$config->{database_password},
3533
{ quote_names => 1 }
3634
);
37-
my $strp = DateTime::Format::Strptime->new(pattern => '%F', on_error => 'croak');
3835

3936
my $course_rs = $schema->resultset('Course');
4037

@@ -56,56 +53,56 @@ my @courses_from_db = $course_rs->getCourses;
5653
for my $course (@courses_from_db) { removeIDs($course); }
5754
@courses_from_db = sortByCourseName(\@courses_from_db);
5855

59-
is_deeply(\@courses_from_db, \@courses, 'getCourses: get all courses');
56+
is(\@courses_from_db, \@courses, 'getCourses: get all courses');
6057

61-
## Get a single course by name
58+
# Get a single course by name
6259
my $course = $course_rs->getCourse(info => { course_name => 'Calculus' });
6360

6461
my $calc_id = $course->{course_id};
6562
delete $course->{course_id};
6663
my @calc_courses = grep { $_->{course_name} eq 'Calculus' } @courses;
67-
is_deeply($course, $calc_courses[0], 'getCourse: get a single course by name');
64+
is($course, $calc_courses[0], 'getCourse: get a single course by name');
6865

6966
# Get a single course by course_id
7067
$course = $course_rs->getCourse(info => { course_id => $calc_id });
7168
delete $course->{course_id};
72-
is_deeply($course, $calc_courses[0], 'getCourse: get a single course by id');
69+
is($course, $calc_courses[0], 'getCourse: get a single course by id');
7370

7471
# Try to get a single course by sending proper info:
75-
throws_ok {
76-
$course_rs->getCourse(info => { course_id => $calc_id, course_name => 'Calculus' });
77-
}
78-
'DB::Exception::ParametersNeeded', 'getCourse: sends too much info';
72+
is(
73+
dies { $course_rs->getCourse(info => { course_id => $calc_id, course_name => 'Calculus' }); },
74+
check_isa('DB::Exception::ParametersNeeded'),
75+
'getCourse: sends too much info'
76+
);
7977

80-
throws_ok {
81-
$course_rs->getCourse(info => { name => 'Calculus' });
82-
}
83-
'DB::Exception::ParametersNeeded', 'getCourse: sends wrong info';
78+
is(
79+
dies { $course_rs->getCourse(info => { name => 'Calculus' }); },
80+
check_isa('DB::Exception::ParametersNeeded'),
81+
'getCourse: sends wrong info'
82+
);
8483

8584
# Try to get a single course that doesn't exist
86-
throws_ok {
87-
$course_rs->getCourse(info => { course_name => 'non_existent_course' });
88-
}
89-
'DB::Exception::CourseNotFound', 'getCourse: get a non-existent course';
85+
is(
86+
dies { $course_rs->getCourse(info => { course_name => 'non_existent_course' }); },
87+
check_isa('DB::Exception::CourseNotFound'),
88+
'getCourse: get a non-existent course'
89+
);
9090

9191
# Add a course
92-
my $new_course_params = {
93-
course_name => 'Geometry',
94-
visible => true,
95-
course_dates => {}
96-
};
92+
my $new_course_params = { course_name => 'Geometry', visible => true, course_dates => {} };
9793

9894
my $new_course = $course_rs->addCourse(params => $new_course_params);
9995
my $added_course_id = $new_course->{course_id};
10096
removeIDs($new_course);
10197

102-
is_deeply($new_course_params, $new_course, 'addCourse: add a new course');
98+
is($new_course, $new_course_params, 'addCourse: add a new course');
10399

104100
# Add a course that already exists
105-
throws_ok {
106-
$course_rs->addCourse(params => { course_name => 'Geometry', visible => 1 });
107-
}
108-
'DB::Exception::CourseAlreadyExists', 'addCourse: course already exists';
101+
is(
102+
dies { $course_rs->addCourse(params => { course_name => 'Geometry', visible => true }); },
103+
check_isa('DB::Exception::CourseAlreadyExists'),
104+
'addCourse: course already exists'
105+
);
109106

110107
# Update the course name
111108
my $updated_course = $course_rs->updateCourse(
@@ -116,36 +113,40 @@ my $updated_course = $course_rs->updateCourse(
116113
$new_course_params->{course_name} = 'Geometry II';
117114
delete $updated_course->{course_id};
118115

119-
is_deeply($new_course_params, $updated_course, 'updateCourse: update a course by name');
116+
is($updated_course, $new_course_params, 'updateCourse: update a course by name');
120117

121118
# Try to update an non-existent course
122-
throws_ok {
123-
$course_rs->updateCourse(info => { course_name => 'non_existent_course' });
124-
}
125-
'DB::Exception::CourseNotFound', 'updateCourse: update a non-existent course_name';
119+
is(
120+
dies { $course_rs->updateCourse(info => { course_name => 'non_existent_course' }); },
121+
check_isa('DB::Exception::CourseNotFound'),
122+
'updateCourse: update a non-existent course_name'
123+
);
126124

127-
throws_ok {
128-
$course_rs->updateCourse(info => { course_id => -9 }, params => $new_course_params);
129-
}
130-
'DB::Exception::CourseNotFound', 'updateCourse: update a non-existent course_id';
125+
is(
126+
dies { $course_rs->updateCourse(info => { course_id => -9 }, params => $new_course_params); },
127+
check_isa('DB::Exception::CourseNotFound'),
128+
'updateCourse: update a non-existent course_id'
129+
);
131130

132131
# Delete a course
133132
my $deleted_course = $course_rs->deleteCourse(info => { course_name => 'Geometry II' });
134133
removeIDs($deleted_course);
135134

136-
is_deeply($new_course_params, $deleted_course, 'deleteCourse: delete a course');
135+
is($deleted_course, $new_course_params, 'deleteCourse: delete a course');
137136

138137
# Try to delete a non-existent course by name
139-
throws_ok {
140-
$course_rs->deleteCourse(info => { course_name => 'undefined_name' })
141-
}
142-
'DB::Exception::CourseNotFound', 'deleteCourse: delete a non-existent course_name';
138+
is(
139+
dies { $course_rs->deleteCourse(info => { course_name => 'undefined_name' }) },
140+
check_isa('DB::Exception::CourseNotFound'),
141+
'deleteCourse: delete a non-existent course_name'
142+
);
143143

144144
# Try to delete a non-existent course by id
145-
throws_ok {
146-
$course_rs->deleteCourse(info => { course_id => -9 })
147-
}
148-
'DB::Exception::CourseNotFound', 'deleteCourse: delete a non-existent course_id';
145+
is(
146+
dies { $course_rs->deleteCourse(info => { course_id => -9 }) },
147+
check_isa('DB::Exception::CourseNotFound'),
148+
'deleteCourse: delete a non-existent course_id'
149+
);
149150

150151
sub sortByCourseName {
151152
my $course_rs = shift;
@@ -158,7 +159,6 @@ sub sortByCourseName {
158159
for my $course (@courses_from_db) { removeIDs($course); }
159160
@courses_from_db = sortByCourseName(\@courses_from_db);
160161

161-
is_deeply(\@courses_from_db, \@courses, 'check: courses db table is returned to its original state.');
162+
is(\@courses_from_db, \@courses, 'check: courses db table is returned to its original state.');
162163

163164
done_testing();
164-

0 commit comments

Comments
 (0)