Skip to content

Commit

Permalink
Switch to using the Test2::Suite and Test2::MojoX modules for unit te…
Browse files Browse the repository at this point in the history
…sts.

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.
  • Loading branch information
drgrice1 committed Nov 2, 2022
1 parent 23e8c3a commit 5644c16
Show file tree
Hide file tree
Showing 27 changed files with 1,722 additions and 1,842 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ jobs:
libmojolicious-plugin-authentication-perl \
libnet-ssleay-perl \
libsql-translator-perl \
libtest-exception-perl \
libsub-info-perl \
libterm-table-perl \
libtest-harness-perl \
libtest2-suite-perl \
libtext-csv-perl \
libtry-tiny-perl \
libyaml-libyaml-perl
cpanm --sudo --notest \
DBIx::Class::DynamicSubclass \
Mojolicious::Plugin::DBIC \
Mojolicious::Plugin::NotYAMLConfig \
Test2::MojoX \
Devel::Cover::Report::Codecov
- name: Run perl unit tests
Expand Down
7 changes: 6 additions & 1 deletion docker/webwork3.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt-get update && \
cpanminus=1.7045-1 \
git=1:2.34.1-1ubuntu1.4 \
libarray-utils-perl=0.5-2 \
libc6-dev=2.35-0ubuntu3.1 \
libcanary-stability-perl=2006-2 \
libcapture-tiny-perl=0.48-1 \
libclass-accessor-lite-perl=0.08-1.1 \
Expand All @@ -26,7 +27,6 @@ RUN apt-get update && \
libdbix-class-perl=0.082842-3 \
libdbix-dbschema-perl=0.45-1 \
libdevel-cover-perl=1.36-2build2 \
libc6-dev=2.35-0ubuntu3.1 \
libexception-class-perl=1.45-1 \
libextutils-config-perl=0.008-2 \
libextutils-helpers-perl=0.026-1 \
Expand All @@ -43,7 +43,11 @@ RUN apt-get update && \
libnet-ssleay-perl=1.92-1build2 \
libsql-translator-perl=1.62-1 \
libssl-dev=3.0.2-0ubuntu1.6 \
libsub-info-perl=0.015-2 \
libterm-table-perl=0.015-2 \
libtest-exception-perl=0.43-1 \
libtest-harness-perl=3.42-2 \
libtest2-suite-perl=0.000144-1 \
libtext-csv-perl=2.01-1 \
libtry-tiny-perl=0.31-1 \
libtypes-serialiser-perl=1.01-1 \
Expand All @@ -56,6 +60,7 @@ RUN apt-get update && \
DBIx::Class::DynamicSubclass \
Mojolicious::Plugin::DBIC \
Mojolicious::Plugin::NotYAMLConfig \
Test2::MojoX \
Devel::Cover::Report::Codecov

ENTRYPOINT ["/bin/bash"]
104 changes: 52 additions & 52 deletions t/db/001_courses.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ BEGIN {
use lib "$main::ww3_dir/lib";
use lib "$main::ww3_dir/t/lib";

use Test::More;
use Test::Exception;
use Test2::V0;
use YAML::XS qw/LoadFile/;
use DateTime::Format::Strptime;
use Mojo::JSON qw/true false/;
use Mojo::JSON qw/true/;

use DB::Schema;

use TestUtils qw/loadCSV removeIDs loadSchema/;
use TestUtils qw/loadCSV removeIDs/;

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

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

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

is_deeply(\@courses_from_db, \@courses, 'getCourses: get all courses');
is(\@courses_from_db, \@courses, 'getCourses: get all courses');

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

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

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

# Try to get a single course by sending proper info:
throws_ok {
$course_rs->getCourse(info => { course_id => $calc_id, course_name => 'Calculus' });
}
'DB::Exception::ParametersNeeded', 'getCourse: sends too much info';
is(
dies { $course_rs->getCourse(info => { course_id => $calc_id, course_name => 'Calculus' }); },
check_isa('DB::Exception::ParametersNeeded'),
'getCourse: sends too much info'
);

throws_ok {
$course_rs->getCourse(info => { name => 'Calculus' });
}
'DB::Exception::ParametersNeeded', 'getCourse: sends wrong info';
is(
dies { $course_rs->getCourse(info => { name => 'Calculus' }); },
check_isa('DB::Exception::ParametersNeeded'),
'getCourse: sends wrong info'
);

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

# Add a course
my $new_course_params = {
course_name => 'Geometry',
visible => true,
course_dates => {}
};
my $new_course_params = { course_name => 'Geometry', visible => true, course_dates => {} };

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

is_deeply($new_course_params, $new_course, 'addCourse: add a new course');
is($new_course, $new_course_params, 'addCourse: add a new course');

# Add a course that already exists
throws_ok {
$course_rs->addCourse(params => { course_name => 'Geometry', visible => 1 });
}
'DB::Exception::CourseAlreadyExists', 'addCourse: course already exists';
is(
dies { $course_rs->addCourse(params => { course_name => 'Geometry', visible => true }); },
check_isa('DB::Exception::CourseAlreadyExists'),
'addCourse: course already exists'
);

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

is_deeply($new_course_params, $updated_course, 'updateCourse: update a course by name');
is($updated_course, $new_course_params, 'updateCourse: update a course by name');

# Try to update an non-existent course
throws_ok {
$course_rs->updateCourse(info => { course_name => 'non_existent_course' });
}
'DB::Exception::CourseNotFound', 'updateCourse: update a non-existent course_name';
is(
dies { $course_rs->updateCourse(info => { course_name => 'non_existent_course' }); },
check_isa('DB::Exception::CourseNotFound'),
'updateCourse: update a non-existent course_name'
);

throws_ok {
$course_rs->updateCourse(info => { course_id => -9 }, params => $new_course_params);
}
'DB::Exception::CourseNotFound', 'updateCourse: update a non-existent course_id';
is(
dies { $course_rs->updateCourse(info => { course_id => -9 }, params => $new_course_params); },
check_isa('DB::Exception::CourseNotFound'),
'updateCourse: update a non-existent course_id'
);

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

is_deeply($new_course_params, $deleted_course, 'deleteCourse: delete a course');
is($deleted_course, $new_course_params, 'deleteCourse: delete a course');

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

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

sub sortByCourseName {
my $course_rs = shift;
Expand All @@ -158,7 +159,6 @@ sub sortByCourseName {
for my $course (@courses_from_db) { removeIDs($course); }
@courses_from_db = sortByCourseName(\@courses_from_db);

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

done_testing();

Loading

0 comments on commit 5644c16

Please sign in to comment.