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 Aug 29, 2022
1 parent 587dd60 commit d2d5888
Show file tree
Hide file tree
Showing 27 changed files with 1,721 additions and 1,842 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
name: Unit Tests and Coverage

on:
Expand Down Expand Up @@ -50,7 +51,8 @@ jobs:
libmodule-build-tiny-perl \
libnet-ssleay-perl \
libsql-translator-perl \
libtest-exception-perl \
libsub-info-perl \
libterm-table-perl \
libtest-harness-perl \
libtext-csv-perl \
libtry-tiny-perl \
Expand All @@ -61,6 +63,8 @@ jobs:
Mojolicious::Plugin::Authentication \
Mojolicious::Plugin::DBIC \
Mojolicious::Plugin::NotYAMLConfig \
Test2::Suite \
Test2::MojoX \
Devel::Cover::Report::Codecov
- name: Run perl unit tests
Expand Down
5 changes: 4 additions & 1 deletion docker/webwork3.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ RUN apt-get update && \
libnet-ssleay-perl=1.88-2ubuntu1 \
libsql-translator-perl=1.60-1 \
libssl-dev=1.1.1f-1ubuntu2.16 \
libtest-exception-perl=0.43-1 \
libsub-info-perl=0.015-2 \
libterm-table-perl=0.015-2 \
libtest-harness-perl=3.42-2 \
libtext-csv-perl=2.00-1 \
libtry-tiny-perl=0.30-1 \
Expand All @@ -56,6 +57,8 @@ RUN apt-get update && \
Mojolicious::Plugin::Authentication \
Mojolicious::Plugin::DBIC \
Mojolicious::Plugin::NotYAMLConfig \
Test2::Suite \
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 d2d5888

Please sign in to comment.