diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 0b2e6f24..559abec1 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -56,7 +56,7 @@ jobs: # Run Linter against code base # ################################ - name: Super-Linter - uses: github/super-linter@v4.8.1 + uses: github/super-linter@v4.9.5 env: VALIDATE_ALL_CODEBASE: false diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8f97781f..42ca9be3 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -1,18 +1,76 @@ +--- name: Unit Tests and Coverage on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: unit-tests: - runs-on: ubuntu-latest - # If we are going to use a prebuilt image like this we need a webwork repository on docker hub - container: drgrice1/webwork3 + runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 + - name: Checkout webwork3 source code + uses: actions/checkout@v3 + + # Disabling these things speeds up the setup considerably, and they are not needed for the throw away machine. + - name: Disable man-db and initramfs updates + run: | + sudo sed -i 's/yes/no/g' /etc/initramfs-tools/update-initramfs.conf + sudo rm -f /var/lib/man-db/auto-update + + - name: Install dependencies + env: + DEBIAN_FRONTEND: noninteractive + DEBCONF_NONINTERACTIVE_SEEN: true + DEBCONF_NOWARNINGS: yes + run: | + sudo apt-get update + sudo apt-get install -qy --no-install-recommends --no-install-suggests \ + cpanminus \ + libarray-utils-perl \ + libcanary-stability-perl \ + libcapture-tiny-perl \ + libclass-accessor-lite-perl \ + libclone-perl \ + libcpanel-json-xs-perl \ + libcrypt-ssleay-perl \ + libdata-dump-perl \ + libdatetime-format-strptime-perl \ + libdbd-sqlite3-perl \ + libdbix-class-inflatecolumn-serializer-perl \ + libdbix-class-perl \ + libdbix-dbschema-perl \ + libdevel-cover-perl \ + libexception-class-perl \ + libextutils-config-perl \ + libextutils-helpers-perl \ + libextutils-installpaths-perl \ + libfurl-perl \ + libhttp-parser-xs-perl \ + libimporter-perl \ + libio-socket-ssl-perl \ + liblist-moreutils-perl \ + libmodule-build-tiny-perl \ + libmojolicious-perl \ + libmojolicious-plugin-authentication-perl \ + libnet-ssleay-perl \ + libsql-translator-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 env: HARNESS_PERL_SWITCHES: -MDevel::Cover @@ -20,24 +78,20 @@ jobs: perl t/db/build_db.pl prove -r t - # we probably don'te need to upload the codecov data - # - uses: actions/upload-artifact@v2 - # with: - # name: coverage-report - # path: cover_db/ - - name: Push coverage analysis if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} run: cover -report codecov - # Install node (for npm) and use it to install eslint and stylelint dependencies. - - name: Use Node.js - uses: actions/setup-node@v2 + # Install node (for npm). + - name: Set up node + uses: actions/setup-node@v3 with: node-version: '16' + - name: Install Dependencies run: npm ci + - name: Run typescript (client-side) tests run: npm run test diff --git a/.perlcriticrc b/.perlcriticrc index 5a28ef7f..b3d5fb42 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -5,3 +5,10 @@ severity = 4 # Allow no warnings usage with a category restriction (for signatures) [TestingAndDebugging::ProhibitNoWarnings] allow_with_category_restriction = 1 + +# Allow $a and $b in sort functions. If sort functions are added to the code not in a "sort" call, they must be added +# to this list. Annoyingly both of these policies that do the same thing have to each get the list. +[Community::DollarAB] +extra_pair_functions = user_prob_sort_fxn +[Freenode::DollarAB] +extra_pair_functions = user_prob_sort_fxn diff --git a/docker/webwork3.dockerfile b/docker/webwork3.dockerfile index d5ec402f..334ad370 100644 --- a/docker/webwork3.dockerfile +++ b/docker/webwork3.dockerfile @@ -1,46 +1,66 @@ -FROM ubuntu:20.04 +FROM ubuntu:22.04 -# the following are needed to make sure the timezone packages don't ask for your timezone. ENV DEBIAN_FRONTEND noninteractive ENV DEBCONF_NONINTERACTIVE_SEEN true +ENV DEBCONF_NOWARNINGS yes ENV HARNESS_PERL_SWITCHES -MDevel::Cover RUN apt-get update && \ apt-get install -qy --no-install-recommends --no-install-suggests \ - build-essential=12.8ubuntu1 \ - ca-certificates=20210119~20.04.1 \ - cpanminus=1.7044-1 \ - git=1:2.25.1-1ubuntu3.1 \ - libarray-utils-perl=0.5-1 \ - libclone-perl=0.43-2 \ - libcrypt-ssleay-perl=0.73.06-1build3 \ - libdata-dump-perl=1.23-1 \ - libdatetime-format-strptime-perl=1.7600-1 \ - libdbd-mysql-perl=4.050-3 \ - libdbd-sqlite3-perl=1.64-1build1 \ - libdbix-class-perl=0.082841-1 \ + ca-certificates=20211016 \ + 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 \ + libclone-perl=0.45-1build3 \ + libcommon-sense-perl=3.75-2build1 \ + libcpanel-json-xs-perl=4.27-1build1 \ + libcrypt-ssleay-perl=0.73.06-1build6 \ + libdata-dump-perl=1.25-1 \ + libdatetime-format-strptime-perl=1.7900-1 \ + #libdbd-mysql-perl=4.050-5 \ # if desired to use a full database + libdbd-sqlite3-perl=1.70-3build1 \ libdbix-class-inflatecolumn-serializer-perl=0.09-1 \ + libdbix-class-perl=0.082842-3 \ libdbix-dbschema-perl=0.45-1 \ - libdevel-cover-perl=1.33-1build1 \ - libexception-class-perl=1.44-1 \ - libjson-perl=4.02000-2 \ - libnet-ssleay-perl=1.88-2ubuntu1 \ - libsql-translator-perl=1.60-1 \ - libssl-dev=1.1.1f-1ubuntu2.5 \ + libdevel-cover-perl=1.36-2build2 \ + libexception-class-perl=1.45-1 \ + libextutils-config-perl=0.008-2 \ + libextutils-helpers-perl=0.026-1 \ + libextutils-installpaths-perl=0.012-1.1 \ + libfurl-perl=3.14-2 \ + libhttp-parser-xs-perl=0.17-2build1 \ + libimporter-perl=0.026-1 \ + libio-socket-ssl-perl=2.074-2 \ + libjson-perl=4.04000-1 \ + libjson-xs-perl=4.030-1build3 \ + libmodule-build-tiny-perl=0.039-1.1 \ + libmojolicious-perl=9.22+dfsg-1 \ + libmojolicious-plugin-authentication-perl=1.37-1 \ + 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 \ - libtext-csv-perl=2.00-1 \ - libtry-tiny-perl=0.30-1 \ - libyaml-libyaml-perl=0.81+repack-1 \ - # mariadb-server=1:10.3.31-0ubuntu0.20.04.1 \ # if desired to use a full database - openssl=1.1.1f-1ubuntu2.5 && \ + libtest2-suite-perl=0.000144-1 \ + libtext-csv-perl=2.01-1 \ + libtry-tiny-perl=0.31-1 \ + libtypes-serialiser-perl=1.01-1 \ + libyaml-libyaml-perl=0.83+ds-1build1 \ + make=4.3-4.1build1 \ + #mariadb-server=1:10.6.7-2ubuntu1.1 \ # if desired to use a full database + openssl=3.0.2-0ubuntu1.6 && \ rm -rf /var/lib/apt/lists/* && \ cpanm --notest \ DBIx::Class::DynamicSubclass \ - Mojolicious \ - Mojolicious::Plugin::NotYAMLConfig \ Mojolicious::Plugin::DBIC \ - Mojolicious::Plugin::Authentication \ + Mojolicious::Plugin::NotYAMLConfig \ + Test2::MojoX \ Devel::Cover::Report::Codecov ENTRYPOINT ["/bin/bash"] diff --git a/lib/DB/Schema/Result/Attempt.pm b/lib/DB/Schema/Result/Attempt.pm index ff879e1f..0b6ada18 100644 --- a/lib/DB/Schema/Result/Attempt.pm +++ b/lib/DB/Schema/Result/Attempt.pm @@ -59,7 +59,7 @@ Note: a problem should have only one of a library_id, problem_path or problem_po __PACKAGE__->table('attempt'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( attempt_id => { diff --git a/lib/DB/Schema/Result/CourseSettings.pm b/lib/DB/Schema/Result/CourseSettings.pm index eacdb07e..fbcbc0a7 100644 --- a/lib/DB/Schema/Result/CourseSettings.pm +++ b/lib/DB/Schema/Result/CourseSettings.pm @@ -49,7 +49,7 @@ C: a JSON object that stores email settings __PACKAGE__->table('course_settings'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( course_settings_id => { diff --git a/lib/DB/Schema/Result/CourseUser.pm b/lib/DB/Schema/Result/CourseUser.pm index 123266eb..ceb52c97 100644 --- a/lib/DB/Schema/Result/CourseUser.pm +++ b/lib/DB/Schema/Result/CourseUser.pm @@ -88,7 +88,7 @@ C: whether or not the user shows old answer (boolean) __PACKAGE__->table('course_user'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( course_user_id => { diff --git a/lib/DB/Schema/Result/PoolProblem.pm b/lib/DB/Schema/Result/PoolProblem.pm index 13855576..728e4706 100644 --- a/lib/DB/Schema/Result/PoolProblem.pm +++ b/lib/DB/Schema/Result/PoolProblem.pm @@ -48,7 +48,7 @@ Note: the C can only have one of the two fields __PACKAGE__->table('pool_problem'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( pool_problem_id => { diff --git a/lib/DB/Schema/Result/ProblemSet.pm b/lib/DB/Schema/Result/ProblemSet.pm index e2f620dc..57664aef 100644 --- a/lib/DB/Schema/Result/ProblemSet.pm +++ b/lib/DB/Schema/Result/ProblemSet.pm @@ -71,11 +71,9 @@ L which gives properties common to re =cut -__PACKAGE__->load_components(qw/DynamicSubclass Core/); - __PACKAGE__->table('problem_set'); -__PACKAGE__->load_components(qw/DynamicSubclass Core/, qw/InflateColumn::Serializer Core/); +__PACKAGE__->load_components(qw/DynamicSubclass Core InflateColumn::Serializer InflateColumn::Boolean Core/); __PACKAGE__->add_columns( set_id => { diff --git a/lib/DB/Schema/Result/SetProblem.pm b/lib/DB/Schema/Result/SetProblem.pm index 00c4855c..8b616373 100644 --- a/lib/DB/Schema/Result/SetProblem.pm +++ b/lib/DB/Schema/Result/SetProblem.pm @@ -65,7 +65,7 @@ Note: a problem should have only one of a library_id, problem_path or problem_po __PACKAGE__->table('set_problem'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( set_problem_id => { diff --git a/lib/DB/Schema/Result/UserProblem.pm b/lib/DB/Schema/Result/UserProblem.pm index 5633ee7d..381c3480 100644 --- a/lib/DB/Schema/Result/UserProblem.pm +++ b/lib/DB/Schema/Result/UserProblem.pm @@ -15,7 +15,7 @@ use base qw/DBIx::Class::Core DB::Validation/; __PACKAGE__->table('user_problem'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer Core/); __PACKAGE__->add_columns( user_problem_id => { diff --git a/lib/DB/Schema/Result/UserSet.pm b/lib/DB/Schema/Result/UserSet.pm index 2bed2a10..4df5a9a8 100644 --- a/lib/DB/Schema/Result/UserSet.pm +++ b/lib/DB/Schema/Result/UserSet.pm @@ -58,7 +58,7 @@ types have different params fields. __PACKAGE__->table('user_set'); -__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); +__PACKAGE__->load_components(qw/InflateColumn::Serializer InflateColumn::Boolean Core/); __PACKAGE__->add_columns( user_set_id => { diff --git a/t/db/001_courses.t b/t/db/001_courses.t index 0b2b72d8..c5a5ffdf 100644 --- a/t/db/001_courses.t +++ b/t/db/001_courses.t @@ -14,14 +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/; 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"; @@ -33,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'); @@ -55,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 => 1, - 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( @@ -115,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; @@ -157,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(); - diff --git a/t/db/002_course_settings.t b/t/db/002_course_settings.t index 72334a12..1f5a299e 100644 --- a/t/db/002_course_settings.t +++ b/t/db/002_course_settings.t @@ -14,14 +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 DB::Schema; use WeBWorK3::Utils::Settings qw/getDefaultCourseSettings getDefaultCourseValues - validateSettingsConfFile validateSingleCourseSetting validateSettingConfig + validateSettingsConfFile validateSettingConfig isInteger isTimeString isTimeDuration isDecimal mergeCourseSettings/; use TestUtils qw/removeIDs loadSchema/; @@ -96,94 +95,118 @@ is(validateSettingConfig($valid_setting), 1, 'course setting: valid setting'); # Check various parts of the setting. -throws_ok { - validateSettingConfig({ - var => 'mySetting', - doc => 'this is a setting', - type => 'integer', - category => 'general', - default => 0 - }) -} -'DB::Exception::InvalidCourseField', 'course setting: variable not in kebob case'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc3 => 'this is a setting', - type => 'integer', - category => 'general', - default => 0 - }) -} -'DB::Exception::InvalidCourseField', 'course setting: course setting with illegal field'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - type => 'integer', - category => 'general', - default => 0 - }) -} -'DB::Exception::InvalidCourseField', 'course setting: missing required field'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc => 'this is a setting', - type => 'nonnegint', - category => 'general', - default => 0 - }) -} -'DB::Exception::InvalidCourseFieldType', 'course setting: non valid course parameter type'; +is( + dies { + validateSettingConfig({ + var => 'mySetting', + doc => 'this is a setting', + type => 'integer', + category => 'general', + default => 0 + }) + }, + check_isa('DB::Exception::InvalidCourseField'), + 'course setting: variable not in kebob case' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc3 => 'this is a setting', + type => 'integer', + category => 'general', + default => 0 + }) + }, + check_isa('DB::Exception::InvalidCourseField'), + 'course setting: course setting with illegal field' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + type => 'integer', + category => 'general', + default => 0 + }) + }, + check_isa('DB::Exception::InvalidCourseField'), + 'course setting: missing required field' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc => 'this is a setting', + type => 'nonnegint', + category => 'general', + default => 0 + }) + }, + check_isa('DB::Exception::InvalidCourseFieldType'), + 'course setting: non valid course parameter type' +); # Validate settings -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc => 'this is a setting', - type => 'time', - category => 'general', - default => '12:343' - }) -} -'DB::Exception::InvalidCourseFieldType', 'course setting: bad time string'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc => 'this is a setting', - type => 'integer', - category => 'general', - default => '12.343' - }) -} -'DB::Exception::InvalidCourseFieldType', 'course setting: bad integer format'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc => 'this is a setting', - type => 'time_duration', - category => 'general', - default => '-2 days' - }) -} -'DB::Exception::InvalidCourseFieldType', 'course setting: bad time duration format'; - -throws_ok { - validateSettingConfig({ - var => 'my_setting', - doc => 'this is a setting', - type => 'decimal', - category => 'general', - default => '12:343' - }) -} -'DB::Exception::InvalidCourseFieldType', 'course setting: bad decimal format'; +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc => 'this is a setting', + type => 'time', + category => 'general', + default => '12:343' + }) + }, + check_isa('DB::Exception::InvalidCourseFieldType'), + 'course setting: bad time string' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc => 'this is a setting', + type => 'integer', + category => 'general', + default => '12.343' + }) + }, + check_isa('DB::Exception::InvalidCourseFieldType'), + 'course setting: bad integer format' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc => 'this is a setting', + type => 'time_duration', + category => 'general', + default => '-2 days' + }) + }, + check_isa('DB::Exception::InvalidCourseFieldType'), + 'course setting: bad time duration format' +); + +is( + dies { + validateSettingConfig({ + var => 'my_setting', + doc => 'this is a setting', + type => 'decimal', + category => 'general', + default => '12:343' + }) + }, + check_isa('DB::Exception::InvalidCourseFieldType'), + 'course setting: bad decimal format' +); my $course_rs = $schema->resultset('Course'); @@ -196,7 +219,7 @@ my $default_course_values = getDefaultCourseValues(); my $new_course_info = { course_id => $new_course->{course_id} }; my $course_settings = $course_rs->getCourseSettings(info => $new_course_info); -is_deeply($course_settings, $default_course_values, 'course settings: default course_settings'); +is($course_settings, $default_course_values, 'course settings: default course_settings'); # Set a single course setting in General my $updated_general_setting = { general => { course_description => 'This is my new course description' } }; @@ -206,7 +229,7 @@ my $updated_course_settings = $course_rs->updateCourseSettings( ); my $current_course_values = mergeCourseSettings($default_course_values, $updated_general_setting); -is_deeply($current_course_values, $updated_course_settings, 'course_settings: updated general setting'); +is($current_course_values, $updated_course_settings, 'course_settings: updated general setting'); # Update another general setting $updated_general_setting = { general => { hardcopy_theme => 'One Column' } }; @@ -218,35 +241,36 @@ $updated_course_settings = $course_rs->updateCourseSettings( $current_course_values = mergeCourseSettings($current_course_values, $updated_general_setting); -is_deeply($current_course_values, $updated_course_settings, 'course_settings: updated another general setting'); +is($current_course_values, $updated_course_settings, 'course_settings: updated another general setting'); # Set a single course setting in Optional Modules. my $updated_optional_setting = { optional => { enable_show_me_another => 1 } }; $updated_course_settings = $course_rs->updateCourseSettings(info => $new_course_info, settings => $updated_optional_setting); $current_course_values = mergeCourseSettings($current_course_values, $updated_optional_setting); -is_deeply($current_course_values, $updated_course_settings, 'course_settings: updated optional setting'); +is($current_course_values, $updated_course_settings, 'course_settings: updated optional setting'); # Set a single course setting in problem_set. my $updated_problem_set_setting = { problem_set => { time_assign_due => '11:52' } }; $updated_course_settings = $course_rs->updateCourseSettings(info => $new_course_info, settings => $updated_problem_set_setting); $current_course_values = mergeCourseSettings($current_course_values, $updated_problem_set_setting); -is_deeply($current_course_values, $updated_course_settings, 'course_settings: updated problem set setting'); +is($current_course_values, $updated_course_settings, 'course_settings: updated problem set setting'); # Set a single course setting in problem. my $updated_problem_setting = { problem => { display_mode => 'images' } }; $updated_course_settings = $course_rs->updateCourseSettings(info => $new_course_info, settings => $updated_problem_setting); $current_course_values = mergeCourseSettings($current_course_values, $updated_problem_setting); -is_deeply($current_course_values, $updated_course_settings, 'course_settings: updated problem setting'); +is($current_course_values, $updated_course_settings, 'course_settings: updated problem setting'); # Make sure that an nonexistant setting throws an exception. my $undefined_problem_setting = { general => { non_existent_setting => 1 } }; -throws_ok { - $course_rs->updateCourseSettings(info => $new_course_info, settings => $undefined_problem_setting); -} -'DB::Exception::UndefinedCourseField', 'course settings: undefined course_setting field'; +is( + dies { $course_rs->updateCourseSettings(info => $new_course_info, settings => $undefined_problem_setting); }, + check_isa('DB::Exception::UndefinedCourseField'), + 'course settings: undefined course_setting field' +); # Make sure that an invalid list option setting throws an exception. my $invalid_list_option = { general => { hardcopy_theme => 'default' } }; diff --git a/t/db/003_users.t b/t/db/003_users.t index ace345db..ce646ab2 100644 --- a/t/db/003_users.t +++ b/t/db/003_users.t @@ -14,12 +14,10 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use Test::More; -use Test::Exception; +use Test2::V0; use Clone qw/clone/; use YAML qw/LoadFile/; -use DateTime::Format::Strptime; use Mojo::JSON qw/true false/; use DB::Schema; @@ -35,7 +33,6 @@ my $schema = DB::Schema->connect( $config->{database_password}, { quote_names => 1 } ); -my $strp = DateTime::Format::Strptime->new(pattern => '%F', on_error => 'croak'); my $users_rs = $schema->resultset('User'); my $course_rs = $schema->resultset('Course'); @@ -74,36 +71,39 @@ for my $user (@users_from_db) { cleanUndef($user); } @users_from_db = sort { $a->{username} cmp $b->{username} } @users_from_db; -is_deeply(\@all_students, \@users_from_db, 'getUsers: all users'); +is(\@users_from_db, \@all_students, 'getUsers: all users'); # Get a single user by username my $user = $users_rs->getGlobalUser(info => { username => $all_students[0]->{username} }); removeIDs($user); cleanUndef($user); -is_deeply($all_students[0], $user, 'getUser: by username'); +is($user, $all_students[0], 'getUser: by username'); # Get a single user by user_id $user = $users_rs->getGlobalUser(info => { user_id => 2 }); removeIDs($user); my @stud2 = grep { $_->{username} eq $user->{username} } @all_students; -is_deeply($stud2[0], $user, 'getUser: by user_id'); +is($user, $stud2[0], 'getUser: by user_id'); # Get one user that does not exist -throws_ok { - $user = $users_rs->getGlobalUser(info => { user_id => -9 }); -} -'DB::Exception::UserNotFound', 'getUser: undefined user_id'; +is( + dies { $user = $users_rs->getGlobalUser(info => { user_id => -9 }); }, + check_isa('DB::Exception::UserNotFound'), + 'getUser: undefined user_id' +); -throws_ok { - $user = $users_rs->getGlobalUser(info => { username => 'non_existent_user' }); -} -'DB::Exception::UserNotFound', 'getUser: undefined username'; +is( + dies { $user = $users_rs->getGlobalUser(info => { username => 'non_existent_user' }); }, + check_isa('DB::Exception::UserNotFound'), + 'getUser: undefined username' +); # getUsers: Test that not passing either a course_id or course_name results in an error. -throws_ok { - $users_rs->getCourseUsers(info => { my_course => 'Precalculus' }); -} -'DB::Exception::ParametersNeeded', 'getUsers: course_name or course_id not passed in'; +is( + dies { $users_rs->getCourseUsers(info => { my_course => 'Precalculus' }); }, + check_isa('DB::Exception::ParametersNeeded'), + 'getUsers: course_name or course_id not passed in' +); # Add one user $user = { @@ -117,7 +117,7 @@ $user = { my $new_user = $users_rs->addGlobalUser(params => $user); removeIDs($new_user); -is_deeply($user, $new_user, 'addUser: adding a user'); +is($new_user, $user, 'addUser: adding a user'); # Ensure that the default values are set my $patty_params = { username => 'patty' }; @@ -126,18 +126,14 @@ removeIDs($patty); cleanUndef($patty); # the only default for users is { is_admin: false } $patty_params->{is_admin} = false; -is_deeply($patty, $patty_params, 'addUser: check the default values from db.'); +is($patty, $patty_params, 'addUser: check the default values from db.'); # Try to add a user without passing username info -throws_ok { - $users_rs->addGlobalUser( - params => { - username_name => 'selma', - email => 'selma@google.com' - } - ); -} -'DB::Exception::ParametersNeeded', 'addUser: wrong user_info sent'; +is( + dies { $users_rs->addGlobalUser(params => { username_name => 'selma', email => 'selma@google.com' }); }, + check_isa('DB::Exception::ParametersNeeded'), + 'addUser: wrong user_info sent' +); # Check that adding an invalid field ignores that field. @@ -153,18 +149,14 @@ cleanUndef($selma); # cleanup params for comparison: invalid_field dropped, is_admin matches default delete $selma_params->{invalid_field}; $selma_params->{is_admin} = false; -is_deeply($selma_params, $selma, 'addUser: pass in an invalid field'); +is($selma, $selma_params, 'addUser: pass in an invalid field'); # Add a user with an invalid username -throws_ok { - $users_rs->addGlobalUser( - params => { - username => 'my name is selma', - email => 'selma@google.com' - } - ); -} -'DB::Exception::InvalidParameter', 'addUser: bad username sent'; +is( + dies { $users_rs->addGlobalUser(params => { username => 'my name is selma', email => 'selma@google.com' }); }, + check_isa('DB::Exception::InvalidParameter'), + 'addUser: bad username sent' +); # Check that using an email address for a username is valid: # Add one user @@ -179,82 +171,81 @@ my $user2 = { my $added_user2 = $users_rs->addGlobalUser(params => $user2); removeIDs($added_user2); -is_deeply($user2, $added_user2, 'addUser: check that using an email for a username is valid.'); +is($added_user2, $user2, 'addUser: check that using an email for a username is valid.'); # Update a user my $updated_user = clone $user; $updated_user->{email} = 'spring.cop@gmail.com'; -my $up_user_from_db = $users_rs->updateGlobalUser( +my $updated_user_from_db = $users_rs->updateGlobalUser( info => { username => $updated_user->{username} }, params => $updated_user ); -removeIDs($up_user_from_db); -is_deeply($updated_user, $up_user_from_db, 'updateUser: updating a user'); +removeIDs($updated_user_from_db); +is($updated_user_from_db, $updated_user, 'updateUser: updating a user'); # Try to update a user without passing username info -throws_ok { - $users_rs->updateGlobalUser(info => { username_name => 'wiggam' }, params => $updated_user); -} -'DB::Exception::ParametersNeeded', 'updateUser: wrong user_info sent'; +is( + dies { $users_rs->updateGlobalUser(info => { username_name => 'wiggam' }, params => $updated_user); }, + check_isa('DB::Exception::ParametersNeeded'), + 'updateUser: wrong user_info sent' +); # Try to update a user that doesn't exist -throws_ok { - $users_rs->updateGlobalUser(info => { username => 'non_existent_user' }, params => $updated_user); -} -'DB::Exception::UserNotFound', 'updateUser: update user for a non-existing username'; +is( + dies { $users_rs->updateGlobalUser(info => { username => 'non_existent_user' }, params => $updated_user); }, + check_isa('DB::Exception::UserNotFound'), + 'updateUser: update user for a non-existing username' +); -throws_ok { - $users_rs->updateGlobalUser(info => { user_id => -5 }, params => $updated_user); -} -'DB::Exception::UserNotFound', 'updateUser: update user for a non-existing user_id'; +is( + dies { $users_rs->updateGlobalUser(info => { user_id => -5 }, params => $updated_user); }, + check_isa('DB::Exception::UserNotFound'), + 'updateUser: update user for a non-existing user_id' +); # Check that updated an invalid field throws an error -throws_ok { - $users_rs->updateGlobalUser( - info => { - username => 'wiggam' - }, - params => { - invalid_field => 1 - } - ) -} -qr/No such column 'invalid_field'/, 'updateUser: pass in an invalid field'; +like( + dies { $users_rs->updateGlobalUser(info => { username => 'wiggam' }, params => { invalid_field => 1 }) }, + qr/No such column 'invalid_field'/, + 'updateUser: pass in an invalid field' +); # Delete users that were created my $user_to_delete = $users_rs->deleteGlobalUser(info => { username => $user->{username} }); removeIDs($user_to_delete); cleanUndef($user_to_delete); -is_deeply($updated_user, $user_to_delete, 'deleteUser: delete a user'); +is($user_to_delete, $updated_user, 'deleteUser: delete a user'); my $deleted_selma = $users_rs->deleteGlobalUser(info => { username => 'selma' }); removeIDs($deleted_selma); cleanUndef($deleted_selma); -is_deeply($deleted_selma, $selma_params, 'deleteUser: deleter another user'); +is($deleted_selma, $selma_params, 'deleteUser: deleter another user'); my $deleted_patty = $users_rs->deleteGlobalUser(info => { username => 'patty' }); removeIDs($deleted_patty); cleanUndef($deleted_patty); -is_deeply($deleted_patty, $patty_params, 'deleteUser: deleter a third user'); +is($deleted_patty, $patty_params, 'deleteUser: deleter a third user'); my $user_to_delete2 = $users_rs->deleteGlobalUser(info => { username => $added_user2->{username} }); removeIDs($user_to_delete2); cleanUndef($user_to_delete2); -is_deeply($added_user2, $user_to_delete2, 'deleteUser: delete yet another user.'); +is($user_to_delete2, $added_user2, 'deleteUser: delete yet another user.'); # Delete a user that doesn't exist. -throws_ok { - $user = $users_rs->deleteGlobalUser(info => { username => 'undefined_username' }); -} -'DB::Exception::UserNotFound', 'deleteUser: trying to delete with undefined username'; +is( + dies { $user = $users_rs->deleteGlobalUser(info => { username => 'undefined_username' }); }, + check_isa('DB::Exception::UserNotFound'), + 'deleteUser: trying to delete with undefined username' +); -throws_ok { - $user = $users_rs->deleteGlobalUser(info => { user_id => -3 }); -} -'DB::Exception::UserNotFound', 'deleteUser: trying to delete with undefined user_id'; +is( + dies { $user = $users_rs->deleteGlobalUser(info => { user_id => -3 }); }, + check_isa('DB::Exception::UserNotFound'), + 'deleteUser: trying to delete with undefined user_id' +); -## get a list of courses for a user +# get a list of courses for a user my @user_courses = $course_rs->getUserCourses(info => { username => 'lisa' }); for my $user_course (@user_courses) { @@ -288,14 +279,15 @@ for my $user_course (@user_courses_from_csv) { @user_courses_from_csv = sort { $a->{course_name} cmp $b->{course_name} } @user_courses_from_csv; @user_courses = sort { $a->{course_name} cmp $b->{course_name} } @user_courses; -is_deeply(\@user_courses, \@user_courses_from_csv, 'getUserCourses: get all courses for a given user'); +is(\@user_courses, \@user_courses_from_csv, 'getUserCourses: get all courses for a given user'); -## try to get a list of course from a non-existent user +# try to get a list of course from a non-existent user -throws_ok { - $course_rs->getUserCourses(info => { username => 'non_existent_user' }); -} -'DB::Exception::UserNotFound', 'getUserCourses: try to get a list of courses for a non-existent user'; +is( + dies { $course_rs->getUserCourses(info => { username => 'non_existent_user' }); }, + check_isa('DB::Exception::UserNotFound'), + 'getUserCourses: try to get a list of courses for a non-existent user' +); # Check that the users db table is returned to its original state. @users_from_db = $users_rs->getAllGlobalUsers; @@ -304,7 +296,6 @@ for my $user (@users_from_db) { cleanUndef($user); } @users_from_db = sort { $a->{username} cmp $b->{username} } @users_from_db; -is_deeply(\@all_students, \@users_from_db, - 'check: make sure that the users db table is returned to its original state'); +is(\@users_from_db, \@all_students, 'check: make sure that the users db table is returned to its original state'); done_testing; diff --git a/t/db/004_course_users.t b/t/db/004_course_users.t index b79c4aa9..79e5bdb4 100644 --- a/t/db/004_course_users.t +++ b/t/db/004_course_users.t @@ -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 Clone qw/clone/; -use Mojo::JSON qw/true false/; +use Mojo::JSON qw/false/; use DB::Schema; use TestUtils qw/loadCSV removeIDs/; -use DB::Utils qw/removeLoginParams/; # Load the database my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; @@ -42,7 +40,7 @@ my $user_rs = $schema->resultset('User'); # Get a list of users from the CSV file my @students = loadCSV("$main::ww3_dir/t/db/sample_data/students.csv"); for my $student (@students) { - $student->{is_admin} = 0; + $student->{is_admin} = false; $student->{course_user_params} = $student->{params}; delete $student->{params}; } @@ -60,25 +58,28 @@ my @precalc_users_from_db = $user_rs->getCourseUsers(info => { course_name => 'P @precalc_users_from_db = sort { $a->{username} cmp $b->{username} } @precalc_users_from_db; removeIDs($_) for @precalc_users_from_db; -is_deeply(\@precalc_students, \@precalc_users_from_db, 'getUsers: get users from a course'); +is(\@precalc_users_from_db, \@precalc_students, 'getUsers: get users from a course'); # getUsers: Test that an unknown course results in an error. -throws_ok { - $user_rs->getCourseUsers(info => { course_name => 'unknown_course' }); -} -'DB::Exception::CourseNotFound', 'getUsers: undefined course_name'; +is( + dies { $user_rs->getCourseUsers(info => { course_name => 'unknown_course' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getUsers: undefined course_name' +); # getUsers: Test that an unknown course_id results in an error. -throws_ok { - $user_rs->getCourseUsers(info => { course_id => -3 }); -} -'DB::Exception::CourseNotFound', 'getUsers: undefined course_id'; +is( + dies { $user_rs->getCourseUsers(info => { course_id => -3 }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getUsers: undefined course_id' +); # getUsers: Test that not passing either a course_id or course_name results in an error. -throws_ok { - $user_rs->getCourseUsers(info => { my_course => 'Precalculus' }); -} -'DB::Exception::ParametersNeeded', 'getUsers: course_name or course_id not passed in'; +is( + dies { $user_rs->getCourseUsers(info => { my_course => 'Precalculus' }); }, + check_isa('DB::Exception::ParametersNeeded'), + 'getUsers: course_name or course_id not passed in' +); # Test getUser @@ -91,25 +92,28 @@ my $user = $user_rs->getCourseUser( ); removeIDs($user); -is_deeply($precalc_students[0], $user, 'getCourseUser: get one merged user'); +is($user, $precalc_students[0], 'getCourseUser: get one merged user'); # getUser: Test that an unknown course results in an error -throws_ok { - $user_rs->getCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); -} -'DB::Exception::CourseNotFound', 'getCourseUser: undefined course'; +is( + dies { $user_rs->getCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getCourseUser: undefined course' +); # getUser: Test that an unknown user results in an error -throws_ok { - $user_rs->getCourseUser(info => { course_name => 'Precalculus', username => 'unknown_user' }); -} -'DB::Exception::UserNotFound', 'getCourseUser: undefined user'; +is( + dies { $user_rs->getCourseUser(info => { course_name => 'Precalculus', username => 'unknown_user' }); }, + check_isa('DB::Exception::UserNotFound'), + 'getCourseUser: undefined user' +); # getUser: Test that an existing user who is not in the course returns an error. -throws_ok { - $user_rs->getCourseUser(info => { course_name => 'Arithmetic', username => 'marge' }); -} -'DB::Exception::UserNotInCourse', 'getCourseUser: get a user that is not in the course'; +is( + dies { $user_rs->getCourseUser(info => { course_name => 'Arithmetic', username => 'marge' }); }, + check_isa('DB::Exception::UserNotInCourse'), + 'getCourseUser: get a user that is not in the course' +); # addUser: Add a user to a course # Remove the following user if already defined in the course @@ -124,7 +128,7 @@ my $user_params = { last_name => 'Quimby', email => 'mayor_joe@springfield.gov', student_id => '12345', - is_admin => 0 + is_admin => false }; my $course_user_params = { @@ -147,7 +151,7 @@ for my $key (qw/username course_name/) { removeIDs($user); delete $user_params->{course_name}; -is_deeply($course_user_params, $user, 'addCourseUser: add a user to a course'); +is($user, $course_user_params, 'addCourseUser: add a user to a course'); # Check that adding a user returns a merged user. my $quimby_db = $user_rs->addCourseUser( @@ -161,90 +165,78 @@ my $quimby_params = clone($course_user_params); for my $key (keys %$user_params) { $quimby_params->{$key} = $user_params->{$key}; } -is_deeply($quimby_params, $quimby_db, 'addCourseUser: check that an added user is returned merged'); +is($quimby_db, $quimby_params, 'addCourseUser: check that an added user is returned merged'); # Checking that if the course exists, but the user is already a member an exception is thrown. -throws_ok { - $user_rs->addCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); -} -'DB::Exception::CourseNotFound', "addCourseUser: the course doesn't exist"; +is( + dies { $user_rs->addCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); }, + check_isa('DB::Exception::CourseNotFound'), + "addCourseUser: the course doesn't exist" +); # updateUser:Check that the user updates. my $updated_user = { params => { comment => 'Mayor Joe is the best!!' }, recitation => '2' }; -throws_ok { - $user_rs->addCourseUser(info => { course_name => 'Arithmetic', username => 'moe' }); -} -'DB::Exception::UserAlreadyInCourse', 'addCourseUser: the user is already a member'; +is( + dies { $user_rs->addCourseUser(info => { course_name => 'Arithmetic', username => 'moe' }); }, + check_isa('DB::Exception::UserAlreadyInCourse'), + 'addCourseUser: the user is already a member' +); # try to add a non-existent user from a course: -throws_ok { - $user_rs->addCourseUser(info => { course_name => 'Arithmetic', username => 'non_existent_user' }) -} -'DB::Exception::UserNotFound', 'addCourseUser: try to add a non-existent user to a course'; +is( + dies { $user_rs->addCourseUser(info => { course_name => 'Arithmetic', username => 'non_existent_user' }) }, + check_isa('DB::Exception::UserNotFound'), + 'addCourseUser: try to add a non-existent user to a course' +); # addCourseUser: add a user with undefined parameters -throws_ok { - $user_rs->addCourseUser( - info => { - course_name => 'Topology', - username => 'quimby', - }, - params => { - role => 'student', - undefined_field => 1 - } - ); -} -'DBIx::Class::Exception', 'addCourseUser: an undefined field is passed in'; +is( + dies { + $user_rs->addCourseUser( + info => { course_name => 'Topology', username => 'quimby', }, + params => { role => 'student', undefined_field => 1 } + ); + }, + check_isa('DBIx::Class::Exception'), + 'addCourseUser: an undefined field is passed in' +); # Add a user with undefined course user parameters. -throws_ok { - $user_rs->addCourseUser( - info => { - course_name => 'Topology', - username => 'quimby' - }, - params => { - role => 'student', - course_user_params => { - this_is_not_valid => 1 - } - } - ); -} -'DB::Exception::InvalidField', 'addCourseUser: an undefined parameter is set'; +is( + dies { + $user_rs->addCourseUser( + info => { course_name => 'Topology', username => 'quimby' }, + params => { role => 'student', course_user_params => { this_is_not_valid => 1 } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'addCourseUser: an undefined parameter is set' +); # Add a user with nonvalid fields -throws_ok { - $user_rs->addCourseUser( - info => { - course_name => 'Topology', - username => 'quimby' - }, - params => { - role => 'student', - course_user_params => { - useMathQuill => 0 - } - } - ); -} -'DB::Exception::InvalidParameter', 'addCourseUser: an parameter with invalid value'; +is( + dies { + $user_rs->addCourseUser( + info => { course_name => 'Topology', username => 'quimby' }, + params => { role => 'student', course_user_params => { useMathQuill => 0 } } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addCourseUser: an parameter with invalid value' +); # Add a user with an invalid role -throws_ok { - $user_rs->addCourseUser( - info => { - course_name => 'Topology', - username => 'quimby' - }, - params => { - role => 'cop' - } - ); -} -'DB::Exception::UserRoleUndefined', 'addCourseUser: try to add a user with an undefined user role'; +is( + dies { + $user_rs->addCourseUser( + info => { course_name => 'Topology', username => 'quimby' }, + params => { role => 'cop' } + ); + }, + check_isa('DB::Exception::UserRoleUndefined'), + 'addCourseUser: try to add a user with an undefined user role' +); # updateCourseUser: check that the user updates. $updated_user = { @@ -264,69 +256,79 @@ my $user_from_db = $user_rs->updateCourseUser( ); removeIDs($user_from_db); -is_deeply($course_user_params, $user_from_db, 'updateCourseUser: update a single user in an existing course.'); +is($user_from_db, $course_user_params, 'updateCourseUser: update a single user in an existing course.'); # updateCourseUser: check that if the course doesn't exist, an error is thrown: -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'unknown_course', username => 'barney' }, - params => $updated_user - ); -} -'DB::Exception::CourseNotFound', "updateCourseUser: the course doesn't exist"; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'unknown_course', username => 'barney' }, + params => $updated_user + ); + }, + check_isa('DB::Exception::CourseNotFound'), + "updateCourseUser: the course doesn't exist" +); # updateCourseUser: check that if the course exists, but the user not a member. -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'Arithmetic', username => 'marge' }, - params => $updated_user - ); -} -'DB::Exception::UserNotInCourse', 'updateCourseUser: the user is not a member of the course'; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'Arithmetic', username => 'marge' }, + params => $updated_user + ); + }, + check_isa('DB::Exception::UserNotInCourse'), + 'updateCourseUser: the user is not a member of the course' +); # Try to add a non-existent user from a course. -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'Arithmetic', user_name => 'bart' }, - params => $updated_user - ); -} -'DB::Exception::ParametersNeeded', 'updateCourseUser: the incorrect information is passed in.'; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'Arithmetic', user_name => 'bart' }, + params => $updated_user + ); + }, + check_isa('DB::Exception::ParametersNeeded'), + 'updateCourseUser: the incorrect information is passed in.' +); # Check that a non-existent course throws an error. -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'Arithmetic', username => 'quimby' }, - params => { sleeps_in_class => 1 } - ); -} -'DBIx::Class::Exception', 'updateCourseUser: an invalid field is set'; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'Arithmetic', username => 'quimby' }, + params => { sleeps_in_class => 1 } + ); + }, + check_isa('DBIx::Class::Exception'), + 'updateCourseUser: an invalid field is set' +); # updateCourseUser: update a user with undefined parameters -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'Arithmetic', username => 'quimby' }, - params => { - course_user_params => { - this_is_not_valid => 1 - } - } - ); -} -'DB::Exception::InvalidField', 'updateCourseUser: an undefined parameter is set'; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'Arithmetic', username => 'quimby' }, + params => { course_user_params => { this_is_not_valid => 1 } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'updateCourseUser: an undefined parameter is set' +); # Check that updating a user with nonvalid fields throws an error. -throws_ok { - $user_rs->updateCourseUser( - info => { course_name => 'Arithmetic', username => 'quimby' }, - params => { - course_user_params => { - useMathQuill => 'yes' - } - } - ); -} -'DB::Exception::InvalidParameter', 'updateCourseUser: an parameter with invalid value'; +is( + dies { + $user_rs->updateCourseUser( + info => { course_name => 'Arithmetic', username => 'quimby' }, + params => { course_user_params => { useMathQuill => 'yes' } } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'updateCourseUser: an parameter with invalid value' +); # Delete a single user from a course. my $deleted_user; @@ -338,30 +340,33 @@ SKIP: { my $deleted_course_user = $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'quimby' }); removeIDs($deleted_course_user); - is_deeply($course_user_params, $deleted_course_user, 'deleteCourseUser: delete a user from a course'); + is($deleted_course_user, $course_user_params, 'deleteCourseUser: delete a user from a course'); $deleted_user = $user_rs->deleteGlobalUser(info => { username => 'quimby' }); removeIDs($deleted_user); - is_deeply($user_params, $deleted_user, 'deleteGlobalUser: delete a user'); + is($deleted_user, $user_params, 'deleteGlobalUser: delete a user'); # deleteUser: Check that if the course doesn't exist, an error is thrown: - throws_ok { - $user_rs->deleteCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); - } - 'DB::Exception::CourseNotFound', "deleteUser: the course doesn't exist"; + is( + dies { $user_rs->deleteCourseUser(info => { course_name => 'unknown_course', username => 'barney' }); }, + check_isa('DB::Exception::CourseNotFound'), + "deleteUser: the course doesn't exist" + ); # deleteUser: Check that if the course exists, but the user not a member. - throws_ok { - $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'marge' }); - } - 'DB::Exception::UserNotInCourse', 'deleteUser: the user is not a member of the course'; + is( + dies { $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username => 'marge' }); }, + check_isa('DB::Exception::UserNotInCourse'), + 'deleteUser: the user is not a member of the course' + ); # deleteUser: Send in username_name instead of username - throws_ok { - $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username_name => 'bart' }); - } - 'DB::Exception::ParametersNeeded', 'deleteUser: the incorrect information is passed in.'; + is( + dies { $user_rs->deleteCourseUser(info => { course_name => 'Arithmetic', username_name => 'bart' }); }, + check_isa('DB::Exception::ParametersNeeded'), + 'deleteUser: the incorrect information is passed in.' + ); } # Check that the precalc users have not changed. @@ -370,7 +375,6 @@ SKIP: { @precalc_users_from_db = sort { $a->{username} cmp $b->{username} } @precalc_users_from_db; for (@precalc_users_from_db) { removeIDs($_); } -is_deeply(\@precalc_students, \@precalc_users_from_db, - 'check: ensure that the precalc users in the database is restored.'); +is(\@precalc_users_from_db, \@precalc_students, 'check: ensure that the precalc users in the database is restored.'); done_testing; diff --git a/t/db/005_hwsets.t b/t/db/005_hwsets.t index ef79c073..357960d8 100644 --- a/t/db/005_hwsets.t +++ b/t/db/005_hwsets.t @@ -14,11 +14,9 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use Test::More; -use Test::Exception; +use Test2::V0; use Clone qw/clone/; use YAML::XS qw/LoadFile/; -use DateTime::Format::Strptime; use Mojo::JSON qw/true false/; use DB::Schema; @@ -35,8 +33,6 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); - # $schema->storage->debug(1); # print out the SQL commands. my $problem_set_rs = $schema->resultset('ProblemSet'); @@ -102,7 +98,7 @@ for my $set (@problem_sets_from_db) { delete $set->{course_dates}; } -is_deeply(\@all_problem_sets, \@problem_sets_from_db, 'getProblemSets: get all sets'); +is(\@problem_sets_from_db, \@all_problem_sets, 'getProblemSets: get all sets'); # Filter the precalculus sets: my @precalc_sets = filterBySetType(\@all_problem_sets, undef, 'Precalculus'); @@ -125,7 +121,7 @@ for my $set (@precalc_sets_from_db) { removeIDs($set); } -is_deeply(\@all_precalc_sets, \@precalc_sets_from_db, 'getProblemSets: get sets for one course'); +is(\@precalc_sets_from_db, \@all_precalc_sets, 'getProblemSets: get sets for one course'); # Test all HW sets in one course my @precalc_hw = filterBySetType(\@all_problem_sets, 'HW', 'Precalculus'); @@ -139,26 +135,30 @@ my @precalc_hw_from_db = $problem_set_rs->getHWSets(info => { course_name => 'Pr for my $set (@precalc_hw_from_db) { removeIDs($set); } -is_deeply(\@precalc_hw, \@precalc_hw_from_db, 'getHWSets: get all homework for one course'); +is(\@precalc_hw_from_db, \@precalc_hw, 'getHWSets: get all homework for one course'); # Get one Problem set my $set_one = $precalc_hw[0]; my $set_from_db = $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => $set_one->{set_name} }); removeIDs($set_from_db); -is_deeply($set_one, $set_from_db, 'getProblemSet: get one homework'); +is($set_from_db, $set_one, 'getProblemSet: get one homework'); # Get a problem set that doesn't exist. -throws_ok { - $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => 'nonexistent_set' }); -} -'DB::Exception::SetNotInCourse', 'getProblemSet: non-existent set name'; +is( + dies { + $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => 'nonexistent_set' }); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'getProblemSet: non-existent set name' +); # Try to get a problem set that is not in a given course -throws_ok { - $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_id => 7 }); -} -'DB::Exception::SetNotInCourse', 'getProblemSet: find a set that is not in a course'; +is( + dies { $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_id => 7 }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'getProblemSet: find a set that is not in a course' +); # Add a new problem set my $new_set_params = { @@ -185,7 +185,7 @@ removeIDs($new_set); delete $new_set->{type}; # add the default set_visible $new_set_params->{set_visible} = false; -is_deeply($new_set_params, $new_set, "addProblemSet: add one homework"); +is($new_set, $new_set_params, "addProblemSet: add one homework"); # Try to add a homework without set_name my $new_set2 = { @@ -193,15 +193,11 @@ my $new_set2 = { set_dates => { open => 100, due => 140, answer => 200 }, set_type => 'HW' }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set2 - } - ); -} -'DB::Exception::ParametersNeeded', 'addProblemSet: set_name not passed in.'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set2 }); }, + check_isa('DB::Exception::ParametersNeeded'), + 'addProblemSet: set_name not passed in.' +); # Try to add a homework with bad date fields my $new_set3 = { @@ -209,15 +205,11 @@ my $new_set3 = { set_dates => { open_set => 100, due => 140, answer => 200 }, set_type => 'HW' }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set3 - } - ); -} -'DB::Exception::InvalidField', 'addProblemSet: invalid date field passed in.'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set3 }); }, + check_isa('DB::Exception::InvalidField'), + 'addProblemSet: invalid date field passed in.' +); # Try to add a homework set without all required date fields my $new_set4 = { @@ -225,15 +217,11 @@ my $new_set4 = { set_dates => { open => 100, due => 140 }, set_type => 'HW' }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set4 - } - ); -} -'DB::Exception::FieldsNeeded', 'addProblemSet: missing required date fields'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set4 }); }, + check_isa('DB::Exception::FieldsNeeded'), + 'addProblemSet: missing required date fields' +); # Try to add a homework set without all required date fields my $new_set5 = { @@ -241,15 +229,11 @@ my $new_set5 = { set_dates => { open => 100, due => 140, answer => '1234s' }, set_type => 'HW' }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set5 - } - ); -} -'DB::Exception::InvalidParameter', 'addProblemSet: adding a non-numeric date'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set5 }); }, + check_isa('DB::Exception::InvalidParameter'), + 'addProblemSet: adding a non-numeric date' +); # Try to add a homework set without invalid date order my $new_set6 = { @@ -258,15 +242,11 @@ my $new_set6 = { set_type => 'HW', set_params => {} }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set6 - } - ); -} -'DB::Exception::ImproperDateOrder', 'addProblemSet: adding an illegal date order.'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set6 }); }, + check_isa('DB::Exception::ImproperDateOrder'), + 'addProblemSet: adding an illegal date order.' +); # Check for undefined parameter fields my $new_set7 = { @@ -275,57 +255,62 @@ my $new_set7 = { set_type => 'HW', set_params => { not_a_valid_field => 5 } }; -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - %$new_set7 - } - ); -} -'DB::Exception::InvalidField', 'addProblemSet: adding an undefined parameter field'; +is( + dies { $problem_set_rs->addProblemSet(params => { course_name => 'Precalculus', %$new_set7 }); }, + check_isa('DB::Exception::InvalidField'), + 'addProblemSet: adding an undefined parameter field' +); # Check for invalid parameter fields (the hide_hint param is a boolean) -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_name => 'HW #11', - set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => false }, - set_type => 'HW', - set_params => { hide_hint => 'yes' } - } - ); -} -'DB::Exception::InvalidParameter', 'addProblemSet: adding an non-valid parameter'; +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_name => 'HW #11', + set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => false }, + set_type => 'HW', + set_params => { hide_hint => 'yes' } + } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addProblemSet: adding an non-valid parameter' +); # Check to ensure true/false are passed into the set_params, not 0/1 -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_name => 'HW #11', - set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => false }, - set_type => 'HW', - set_params => { hide_hint => 0 } - } - ); -} -'DB::Exception::InvalidParameter', 'addProblemSet: adding an non-valid boolean parameter'; +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_name => 'HW #11', + set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => false }, + set_type => 'HW', + set_params => { hide_hint => 0 } + } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addProblemSet: adding an non-valid boolean parameter' +); # Check to ensure true/false are passed into the enable_reduced_scoring in set_dates, not 0/1 -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_name => 'HW #11', - set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => 0 }, - set_type => 'HW', - set_params => { hide_hint => 0 } - } - ); -} -'DB::Exception::InvalidParameter', 'addProblemSet: adding an non-valid boolean parameter in set_dates'; +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_name => 'HW #11', + set_dates => { open => 100, due => 140, answer => 200, enable_reduced_scoring => 0 }, + set_type => 'HW', + set_params => { hide_hint => 0 } + } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addProblemSet: adding an non-valid boolean parameter in set_dates' +); # Update a set $new_set_params->{set_name} = "HW #8"; @@ -346,7 +331,7 @@ my $updated_set = $problem_set_rs->updateProblemSet( ); removeIDs($updated_set); delete $new_set_params->{type}; -is_deeply($new_set_params, $updated_set, 'updateSet: change the set parameters'); +is($updated_set, $new_set_params, 'updateSet: change the set parameters'); # Update the set where the set_type is sent, but the type is not: $new_set_params->{set_name} = 'HW #88'; @@ -359,7 +344,7 @@ $updated_set = $problem_set_rs->updateProblemSet( ); removeIDs($updated_set); -is_deeply($new_set_params, $updated_set, "updateSet: update a set with set_type defined."); +is($updated_set, $new_set_params, "updateSet: update a set with set_type defined."); # Change the type of a problem set from a Homework Set to a Quiz. @@ -374,66 +359,62 @@ my $set_with_new_type = $problem_set_rs->updateProblemSet( ); removeIDs($set_with_new_type); -is_deeply($set_with_new_type, $set_with_new_type_params, 'updateSet: change the type of the problem set'); +is($set_with_new_type, $set_with_new_type_params, 'updateSet: change the type of the problem set'); # Try to update a set with an illegal field -throws_ok { - $problem_set_rs->updateProblemSet( - info => { course_name => 'Precalculus', set_id => $new_set_id }, - params => { bad_field => 0 } - ); -} -'DBIx::Class::Exception', 'updateProblemSet: use a non-existing field'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_id => $new_set_id }, + params => { bad_field => 0 } + ); + }, + check_isa('DBIx::Class::Exception'), + 'updateProblemSet: use a non-existing field' +); # Try to update a set with an illegal date field -throws_ok { - $problem_set_rs->updateProblemSet( - info => { course_name => 'Precalculus', set_id => $new_set_id }, - params => { set_dates => { bad_date => 99 } } - ); -} -'DB::Exception::InvalidField', 'updateSet: invalid date field passed in.'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_id => $new_set_id }, + params => { set_dates => { bad_date => 99 } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'updateSet: invalid date field passed in.' +); # Try to update a set with an dates in a bad order -throws_ok { - $problem_set_rs->updateProblemSet( - info => { course_name => 'Precalculus', set_id => $new_set_id }, - params => { - set_dates => { - open => 999, - answer => 100 - } - } - ); -} -'DB::Exception::ImproperDateOrder', 'updateSet: adding an illegal date order.'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_id => $new_set_id }, + params => { set_dates => { open => 999, answer => 100 } } + ); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'updateSet: adding an illegal date order.' +); # Delete a set my $deleted_set = $problem_set_rs->deleteProblemSet(info => { course_name => 'Precalculus', set_name => 'HW #88' }); removeIDs($deleted_set); -is_deeply($set_with_new_type_params, $deleted_set, 'deleteProblemSet: delete a set'); +is($deleted_set, $set_with_new_type_params, 'deleteProblemSet: delete a set'); # Try deleting a set with invalid course_name -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_name => 'Not a course', - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'deleteCourse: try to delete a set from a not existent course.'; +is( + dies { $problem_set_rs->deleteProblemSet(info => { course_name => 'Not a course', set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'deleteCourse: try to delete a set from a not existent course.' +); # Try deleting a set that does not exist -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'HW #99' - } - ); -} -'DB::Exception::SetNotInCourse', 'deleteCourse: try to delete a set that not exist.'; +is( + dies { $problem_set_rs->deleteProblemSet(info => { course_name => 'Precalculus', set_name => 'HW #99' }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'deleteCourse: try to delete a set that not exist.' +); # ensure that the problem_sets table in the database is restored. @all_problem_sets = (@hw_sets, @quizzes, @review_sets); @@ -451,6 +432,6 @@ for my $set (@problem_sets_from_db) { # delete $set->{course_name}; } -is_deeply(\@all_problem_sets, \@problem_sets_from_db, 'check: ensure that the problem_sets table is restored.'); +is(\@problem_sets_from_db, \@all_problem_sets, 'check: ensure that the problem_sets table is restored.'); done_testing; diff --git a/t/db/006_quizzes.t b/t/db/006_quizzes.t index 7ca3abc8..f921eb03 100644 --- a/t/db/006_quizzes.t +++ b/t/db/006_quizzes.t @@ -14,11 +14,9 @@ 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 Clone qw/clone/; -use DateTime::Format::Strptime; use Mojo::JSON qw/true false/; use DB::Schema; @@ -35,8 +33,6 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); - # $schema->storage->debug(1); # print out the SQL commands. my $problem_set_rs = $schema->resultset('ProblemSet'); @@ -45,7 +41,13 @@ my $user_rs = $schema->resultset('User'); my @all_problem_sets; -my @quizzes = loadCSV("$main::ww3_dir/t/db/sample_data/quizzes.csv"); +my @quizzes = loadCSV( + "$main::ww3_dir/t/db/sample_data/quizzes.csv", + { + boolean_fields => ['set_visible'], + param_boolean_fields => ['timed'] + } +); for my $quiz (@quizzes) { $quiz->{set_type} = 'QUIZ'; } @@ -66,26 +68,32 @@ my @precalc_quizzes_from_db = $problem_set_rs->getQuizzes(info => { course_name for my $quiz (@precalc_quizzes_from_db) { removeIDs($quiz); } -is_deeply(\@precalc_quizzes, \@precalc_quizzes_from_db, 'getQuizzes: get all quizzes for one course'); +is(\@precalc_quizzes_from_db, \@precalc_quizzes, 'getQuizzes: get all quizzes for one course'); # Get a single quiz my $quiz_from_db = $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => 'Quiz #1' }); removeIDs($quiz_from_db); my @quiz_from_csv = grep { $_->{set_name} eq 'Quiz #1' } @precalc_quizzes; delete $quiz_from_csv[0]->{type}; -is_deeply($quiz_from_csv[0], $quiz_from_db, 'getQuiz: get one quiz from a single course'); +is($quiz_from_db, $quiz_from_csv[0], 'getQuiz: get one quiz from a single course'); # Try to get a quiz that doesn't exist in a course that does. -throws_ok { - $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => 'nonexisent quiz' }); -} -'DB::Exception::SetNotInCourse', 'getQuiz: non-existent set name'; +is( + dies { + $problem_set_rs->getProblemSet(info => { course_name => 'Precalculus', set_name => 'nonexisent quiz' }); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'getQuiz: non-existent set name' +); # Try to get a quiz from a course that doesn't exist. -throws_ok { - $problem_set_rs->getProblemSet(info => { course_name => 'nonexistent course', set_name => 'Quiz #1' }); -} -'DB::Exception::CourseNotFound', 'getQuiz: try to get a quiz from a non-existent course'; +is( + dies { + $problem_set_rs->getProblemSet(info => { course_name => 'nonexistent course', set_name => 'Quiz #1' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'getQuiz: try to get a quiz from a non-existent course' +); # Add a new quiz my $new_quiz_params = { @@ -103,141 +111,124 @@ my $new_quiz = $problem_set_rs->addProblemSet( ); removeIDs($new_quiz); -## add the default set_visible field +# add the default set_visible field $new_quiz_params->{set_visible} = false; -is_deeply($new_quiz, $new_quiz_params, "addQuiz: add a new quiz"); +is($new_quiz, $new_quiz_params, "addQuiz: add a new quiz"); # Try to add a quiz to a non existent course. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'nonexistent course', - set_name => 'Quiz #1' - } - ); -} -'DB::Exception::CourseNotFound', 'addQuiz: try to add a quiz from a non-existent course'; +is( + dies { + $problem_set_rs->addProblemSet(params => { course_name => 'nonexistent course', set_name => 'Quiz #1' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'addQuiz: try to add a quiz from a non-existent course' +); # Try to add a quiz with non-valid parameters. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - nonexistent_field => 1, - } - ) -} -'DBIx::Class::Exception', 'addQuiz: try to add a quiz with a bad parameter'; +is( + dies { + $problem_set_rs->addProblemSet(params => + { course_name => 'Precalculus', set_type => 'QUIZ', set_name => 'Quiz #99', nonexistent_field => 1, }) + }, + check_isa('DBIx::Class::Exception'), + 'addQuiz: try to add a quiz with a bad parameter' +); # Try to add a quiz without specifying the name. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_visible => true, - } - ); -} -'DB::Exception::ParametersNeeded', 'addQuiz: try to add a quiz with a bad field'; +is( + dies { + $problem_set_rs->addProblemSet( + params => { course_name => 'Precalculus', set_type => 'QUIZ', set_visible => true, }); + }, + check_isa('DB::Exception::ParametersNeeded'), + 'addQuiz: try to add a quiz with a bad field' +); # Try to add a quiz with an undefined parameter. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - set_visible => true, - set_params => { - param1 => 0 - }, - set_dates => { - open => 10, - due => 100, - answer => 200, +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_type => 'QUIZ', + set_name => 'Quiz #99', + set_visible => true, + set_params => { param1 => 0 }, + set_dates => { open => 10, due => 100, answer => 200, } } - } - ); -} -'DB::Exception::InvalidField', 'addQuiz: try to add a quiz with a undefined parameter'; + ); + }, + check_isa('DB::Exception::InvalidField'), + 'addQuiz: try to add a quiz with a undefined parameter' +); # Try to add a quiz with a non-valid parameter. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - set_visible => true, - set_params => { - timed => 'yes' - }, - set_dates => { - open => 10, - due => 100, - answer => 200, +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_type => 'QUIZ', + set_name => 'Quiz #99', + set_visible => true, + set_params => { timed => 'yes' }, + set_dates => { open => 10, due => 100, answer => 200, } } - } - ); -} -'DB::Exception::InvalidParameter', 'addQuiz: try to add a quiz with a non-valid parameter'; + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addQuiz: try to add a quiz with a non-valid parameter' +); # Try to add a quiz with a missing required date fields. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - set_dates => { - open => 10, - due => 100 +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_type => 'QUIZ', + set_name => 'Quiz #99', + set_dates => { open => 10, due => 100 } } - } - ); -} -'DB::Exception::FieldsNeeded', 'addQuiz: try to add a quiz with a missing required date fields'; + ); + }, + check_isa('DB::Exception::FieldsNeeded'), + 'addQuiz: try to add a quiz with a missing required date fields' +); # Try to add a quiz with an undefined date field. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - set_visible => 1, - set_dates => { - open => 10, - due => 100, - answer => 200, - reduced_scoring => 300 +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_type => 'QUIZ', + set_name => 'Quiz #99', + set_visible => 1, + set_dates => { open => 10, due => 100, answer => 200, reduced_scoring => 300 } } - } - ); -} -'DB::Exception::InvalidField', 'addQuiz: try to add a quiz with an undefined date field'; + ); + }, + check_isa('DB::Exception::InvalidField'), + 'addQuiz: try to add a quiz with an undefined date field' +); # Try to add a quiz with dates that are out of order. -throws_ok { - $problem_set_rs->addProblemSet( - params => { - course_name => 'Precalculus', - set_type => 'QUIZ', - set_name => 'Quiz #99', - set_visible => 1, - set_dates => { - open => 10, - due => 300, - answer => 200, +is( + dies { + $problem_set_rs->addProblemSet( + params => { + course_name => 'Precalculus', + set_type => 'QUIZ', + set_name => 'Quiz #99', + set_visible => 1, + set_dates => { open => 10, due => 300, answer => 200, } } - } - ); -} -'DB::Exception::ImproperDateOrder', 'addQuiz: try to add a quiz with dates that are out of order'; - + ); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'addQuiz: try to add a quiz with dates that are out of order' +); # Update the visibility of the quiz my $updated_params = { set_visible => 0 }; my $updated_quiz = $problem_set_rs->updateProblemSet( @@ -248,18 +239,14 @@ my $updated_quiz = $problem_set_rs->updateProblemSet( params => $updated_params ); -$new_quiz->{set_visible} = 0; +$new_quiz->{set_visible} = false; $new_quiz->{set_params} = {}; removeIDs($updated_quiz); -is_deeply($new_quiz, $updated_quiz, 'updateQuiz: successfully update the quiz'); +is($updated_quiz, $new_quiz, 'updateQuiz: successfully update the quiz'); # Update the params of the quiz -$updated_params = { - set_params => { - timed => true - } -}; -$updated_quiz = $problem_set_rs->updateProblemSet( +$updated_params = { set_params => { timed => true } }; +$updated_quiz = $problem_set_rs->updateProblemSet( info => { course_name => 'Precalculus', set_name => 'Quiz #9' @@ -268,7 +255,7 @@ $updated_quiz = $problem_set_rs->updateProblemSet( ); removeIDs($updated_quiz); $new_quiz->{set_params} = { timed => true }; -is_deeply($new_quiz, $updated_quiz, 'updateQuiz: successfully update the params of the quiz'); +is($updated_quiz, $new_quiz, 'updateQuiz: successfully update the params of the quiz'); # Update the dates of the quiz $updated_params = { @@ -287,130 +274,98 @@ $updated_quiz = $problem_set_rs->updateProblemSet( ); removeIDs($updated_quiz); $new_quiz->{set_dates} = clone($updated_params->{set_dates}); -is_deeply($new_quiz, $updated_quiz, 'updateQuiz: successfully update the dates of the quiz'); +is($updated_quiz, $new_quiz, 'updateQuiz: successfully update the dates of the quiz'); # Try to update a non-existent field of the quiz. -throws_ok { - $problem_set_rs->updateProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #9' - }, - params => { - nonexistent_field => 1 - } - ); -} -'DBIx::Class::Exception', 'updateQuiz: try to update a quiz with a non-valid field'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_name => 'Quiz #9' }, + params => { nonexistent_field => 1 } + ); + }, + check_isa('DBIx::Class::Exception'), + 'updateQuiz: try to update a quiz with a non-valid field' +); # Try to update a non-existent param of the quiz. -throws_ok { - $problem_set_rs->updateProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #9' - }, - params => { - set_params => { - show_hint => 1 - } - } - ); -} -'DB::Exception::InvalidField', 'updateQuiz: try to update a quiz with an undefined parameter'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_name => 'Quiz #9' }, + params => { set_params => { show_hint => 1 } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'updateQuiz: try to update a quiz with an undefined parameter' +); # Try to update a parameter with a bad value -throws_ok { - $problem_set_rs->updateProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #9' - }, - params => { - set_params => { - timed => 'yes' - } - } - ); -} -'DB::Exception::InvalidParameter', 'updateQuiz: try to update a quiz with a non-valid field'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_name => 'Quiz #9' }, + params => { set_params => { timed => 'yes' } } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'updateQuiz: try to update a quiz with a non-valid field' +); # Try to update a quiz with an invalid date -throws_ok { - $problem_set_rs->updateProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #9' - }, - params => { - set_dates => { - reduced_scoring => 1000 - } - } - ); -} -'DB::Exception::InvalidField', 'updateQuiz: try to update a quiz with a non-valid date'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_name => 'Quiz #9' }, + params => { set_dates => { reduced_scoring => 1000 } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'updateQuiz: try to update a quiz with a non-valid date' +); # Try to update a quiz with a date out of order. -throws_ok { - $problem_set_rs->updateProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #9' - }, - params => { - set_dates => { - open => 50, - due => 40 - } - } - ); -} -'DB::Exception::ImproperDateOrder', 'updateQuiz: try to update a quiz with out of order dates'; +is( + dies { + $problem_set_rs->updateProblemSet( + info => { course_name => 'Precalculus', set_name => 'Quiz #9' }, + params => { set_dates => { open => 50, due => 40 } } + ); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'updateQuiz: try to update a quiz with out of order dates' +); # Try to delete from a non-existent course. -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_name => 'Course does not exist', - set_name => 'Quiz #9' - } - ); -} -'DB::Exception::CourseNotFound', 'deleteQuiz: try to delete a quiz from a non-existent course'; +is( + dies { + $problem_set_rs->deleteProblemSet( + info => { course_name => 'Course does not exist', set_name => 'Quiz #9' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'deleteQuiz: try to delete a quiz from a non-existent course' +); # Try to delete from a non-existent course. -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_id => 9999, - set_name => 'Quiz #9' - } - ); -} -'DB::Exception::CourseNotFound', 'deleteQuiz: try to delete a quiz from a non-existent course_id'; +is( + dies { $problem_set_rs->deleteProblemSet(info => { course_id => 9999, set_name => 'Quiz #9' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'deleteQuiz: try to delete a quiz from a non-existent course_id' +); # Try to delete from a non-existent set in a course. -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_name => 'Precalculus', - set_name => 'Quiz #999' - } - ); -} -'DB::Exception::SetNotInCourse', 'deleteQuiz: try to delete a non-existent quiz'; +is( + dies { $problem_set_rs->deleteProblemSet(info => { course_name => 'Precalculus', set_name => 'Quiz #999' }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'deleteQuiz: try to delete a non-existent quiz' +); # Try to delete from a non-existent set in a course. -throws_ok { - $problem_set_rs->deleteProblemSet( - info => { - course_name => 'Precalculus', - set_id => 99999 - } - ); -} -'DB::Exception::SetNotInCourse', 'deleteQuiz: try to delete a non-existent quiz as set_id'; +is( + dies { $problem_set_rs->deleteProblemSet(info => { course_name => 'Precalculus', set_id => 99999 }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'deleteQuiz: try to delete a non-existent quiz as set_id' +); # Try to delete from a non-existent set in a course: my $deleted_quiz = $problem_set_rs->deleteProblemSet( @@ -420,7 +375,7 @@ my $deleted_quiz = $problem_set_rs->deleteProblemSet( } ); removeIDs($deleted_quiz); -is_deeply($deleted_quiz, $new_quiz, 'delete Quiz: successfully delete a quiz'); +is($deleted_quiz, $new_quiz, 'delete Quiz: successfully delete a quiz'); # Ensure that the quizzes in the database are restored. @precalc_quizzes_from_db = $problem_set_rs->getQuizzes(info => { course_name => 'Precalculus' }); @@ -429,6 +384,6 @@ is_deeply($deleted_quiz, $new_quiz, 'delete Quiz: successfully delete a quiz'); for my $quiz (@precalc_quizzes_from_db) { removeIDs($quiz); } -is_deeply(\@precalc_quizzes, \@precalc_quizzes_from_db, 'check: ensure that the quizzes have been restored.'); +is(\@precalc_quizzes_from_db, \@precalc_quizzes, 'check: ensure that the quizzes have been restored.'); done_testing; diff --git a/t/db/007_user_set.t b/t/db/007_user_set.t index fb5abff4..ea100fef 100644 --- a/t/db/007_user_set.t +++ b/t/db/007_user_set.t @@ -4,7 +4,6 @@ use warnings; use strict; -use feature 'say'; BEGIN { use File::Basename qw/dirname/; @@ -15,10 +14,8 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use DateTime::Format::Strptime; -use Test::More; +use Test2::V0; use Clone qw/clone/; -use Test::Exception; use YAML::XS qw/LoadFile/; use Mojo::JSON qw/true false/; @@ -39,7 +36,6 @@ my $schema = DB::Schema->connect( ); # $schema->storage->debug(1); # print out the SQL commands. -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); my $user_set_rs = $schema->resultset('UserSet'); my $course_rs = $schema->resultset('Course'); @@ -147,7 +143,7 @@ for my $set (@all_user_sets_from_db) { @all_user_sets_from_db = sort { $a->{course_name} cmp $b->{course_name} || $a->{set_name} cmp $b->{set_name} } @all_user_sets_from_db; -is_deeply(\@all_user_sets_from_db, \@all_user_sets, 'getAllUserSets: get all user sets for all courses'); +is(\@all_user_sets_from_db, \@all_user_sets, 'getAllUserSets: get all user sets for all courses'); my @merged_sets_from_db = $user_set_rs->getAllUserSets(merged => 1); @@ -161,7 +157,7 @@ for my $merged_set (@merged_sets_from_db) { @merged_user_sets = sort { $a->{course_name} cmp $b->{course_name} || $a->{set_name} cmp $b->{set_name} } @merged_user_sets; -is_deeply(\@merged_sets_from_db, \@merged_user_sets, 'getAllUserSets: get all merged sets for all courses'); +is(\@merged_sets_from_db, \@merged_user_sets, 'getAllUserSets: get all merged sets for all courses'); # Get all user set for a given user in a course. @@ -183,7 +179,7 @@ for my $user_set (@user_sets_from_db) { @user_sets_from_db = sort { $a->{set_name} cmp $b->{set_name} } @user_sets_from_db; @user_sets_for_one_user = sort { $a->{set_name} cmp $b->{set_name} } @user_sets_for_one_user; -is_deeply(\@user_sets_from_db, \@user_sets_for_one_user, 'getUserSets: get all user sets for one user'); +is(\@user_sets_from_db, \@user_sets_for_one_user, 'getUserSets: get all user sets for one user'); # Get all merged sets for a given user in a course @@ -202,9 +198,9 @@ for my $merged_set (@merged_sets_from_db) { removeIDs($merged_set); } -is_deeply(\@merged_sets_from_db, \@merged_sets_for_one_user, 'getUserSets: get all merged sets for one user'); +is(\@merged_sets_from_db, \@merged_sets_for_one_user, 'getUserSets: get all merged sets for one user'); -## get all user sets for a given set in a course +# get all user sets for a given set in a course my @user_sets_for_one_set = grep { $_->{course_name} eq 'Precalculus' && $_->{set_name} eq 'HW #1' } @all_user_sets; @@ -215,7 +211,7 @@ for my $user_set (@user_sets_from_db) { delete $user_set->{set_visible} unless defined($user_set->{set_visible}); } -is_deeply(\@user_sets_from_db, \@user_sets_for_one_set, 'getUserSets: get all user sets for a set in a course'); +is(\@user_sets_from_db, \@user_sets_for_one_set, 'getUserSets: get all user sets for a set in a course'); # get all merged user sets for a given set in a course @@ -231,25 +227,33 @@ for my $user_set (@merged_sets_from_db) { removeIDs($user_set); } -is_deeply(\@merged_sets_from_db, \@merged_sets_for_one_set, 'getUserSets: get all merged sets for a set in a course'); +is(\@merged_sets_from_db, \@merged_sets_for_one_set, 'getUserSets: get all merged sets for a set in a course'); # Try to get a user set from a non-existing course. -throws_ok { - $user_set_rs->getUserSetsForUser(info => { course_name => 'non_existent_course', username => 'homer' }); -} -'DB::Exception::CourseNotFound', 'getUserSets: attempt to get user sets from a nonexistent course'; +is( + dies { + $user_set_rs->getUserSetsForUser(info => { course_name => 'non_existent_course', username => 'homer' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'getUserSets: attempt to get user sets from a nonexistent course' +); # Try to get a user set from a non-existing course. -throws_ok { - $user_set_rs->getUserSetsForUser(info => { course_name => 'Precalculus', username => 'non_existent_user' }); -} -'DB::Exception::UserNotFound', 'getUserSets: attempt to get user sets from a nonexistent user'; +is( + dies { + $user_set_rs->getUserSetsForUser(info => { course_name => 'Precalculus', username => 'non_existent_user' }); + }, + check_isa('DB::Exception::UserNotFound'), + 'getUserSets: attempt to get user sets from a nonexistent user' +); # Try to get a user set from a user not in the course. -throws_ok { - $user_set_rs->getUserSetsForUser(info => { course_name => 'non_existent_course', username => 'bart' }); -} -'DB::Exception::CourseNotFound', 'getUserSets: attempt to get user sets from user not in the course'; +is( + dies { $user_set_rs->getUserSetsForUser(info => { course_name => 'non_existent_course', username => 'bart' }); } + , + check_isa('DB::Exception::CourseNotFound'), + 'getUserSets: attempt to get user sets from user not in the course' +); # Get a single UserSet my $info = { @@ -272,7 +276,7 @@ my $user_set_from_csv = clone( removeIDs($user_set); delete $user_set->{set_visible} unless defined($user_set->{set_visible}); -is_deeply($user_set_from_csv, $user_set, 'getUserSet: get a user set from a course'); +is($user_set, $user_set_from_csv, 'getUserSet: get a user set from a course'); # Get a merged UserSet @@ -289,55 +293,47 @@ my $merged_set_from_csv = clone( my $merged_set = $user_set_rs->getUserSet(info => $info, merged => 1); removeIDs($merged_set); -is_deeply($merged_set_from_csv, $merged_set, 'getUserSet: get a merged set from a course'); +is($merged_set, $merged_set_from_csv, 'getUserSet: get a merged set from a course'); # Try to get a user set from a non-existent course. -throws_ok { - $user_set_rs->getUserSet( - info => { - course_name => 'non_existent_course', - username => 'homer', - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'getUserSet: try to get a user set from a non-existent course'; +is( + dies { + $user_set_rs->getUserSet( + info => { course_name => 'non_existent_course', username => 'homer', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'getUserSet: try to get a user set from a non-existent course' +); # Try to get a user set from a non-existent user. -throws_ok { - $user_set_rs->getUserSet( - info => { - course_name => 'Precalculus', - username => 'non_existent_user', - set_name => 'HW #1' - } - ); -} -'DB::Exception::UserNotFound', 'getUserSet: try to get a user set from a non-existent user'; +is( + dies { + $user_set_rs->getUserSet( + info => { course_name => 'Precalculus', username => 'non_existent_user', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::UserNotFound'), + 'getUserSet: try to get a user set from a non-existent user' +); # Try to get a user set from a user not in the course. -throws_ok { - $user_set_rs->getUserSet( - info => { - course_name => 'Precalculus', - username => 'marge', - set_name => 'HW #1' - } - ); -} -'DB::Exception::UserNotInCourse', 'getUserSet: try to get a user set not in the course'; +is( + dies { + $user_set_rs->getUserSet( + info => { course_name => 'Precalculus', username => 'marge', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::UserNotInCourse'), + 'getUserSet: try to get a user set not in the course' +); # Try to get a user set from a non-existent set. -throws_ok { - $user_set_rs->getUserSet( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #999' - } - ); -} -'DB::Exception::SetNotInCourse', 'getUserSet: try to get a user set from a non-existent set'; +is( + dies { + $user_set_rs->getUserSet( + info => { course_name => 'Precalculus', username => 'homer', set_name => 'HW #999' }); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'getUserSet: try to get a user set from a non-existent set' +); # Add a user set my $new_info = { @@ -356,7 +352,7 @@ $new_info->{set_params} = {}; $new_info->{set_version} = 0; $new_info->{set_type} = 'HW'; -is_deeply($new_user_set, $new_info, 'addUserSet: add a new user set'); +is($new_user_set, $new_info, 'addUserSet: add a new user set'); my $hw_set1 = clone((grep { $_->{course_name} eq 'Precalculus' && $_->{set_name} eq 'HW #1' } @hw_sets)[0]); @@ -374,7 +370,7 @@ removeIDs($new_merged_set); # add the default value for set_version $hw_set1->{set_version} = 0; -is_deeply($new_merged_set, $hw_set1, 'addUserSet: add a new user set and check that it is merged correctly'); +is($new_merged_set, $hw_set1, 'addUserSet: add a new user set and check that it is merged correctly'); # Add a user set with a empty set of dates. @@ -394,7 +390,7 @@ $new_user_params2->{set_type} = 'HW'; $new_user_params2->{set_version} = 0; $new_user_params2->{set_params} = {}; -is_deeply($new_user_set2, $new_user_params2, 'addUserSet: add a new user set with empty dates.'); +is($new_user_set2, $new_user_params2, 'addUserSet: add a new user set with empty dates.'); # Test that adding a field that is not in the database is ignored. my $user_set_params3 = { @@ -416,55 +412,47 @@ delete $user_set_params3->{bad_field}; # and need to explicitly set the type $user_set_params3->{set_type} = 'HW'; -is_deeply($user_set3, $user_set_params3, 'addUserSet: add a user set with a bad field'); +is($user_set3, $user_set_params3, 'addUserSet: add a user set with a bad field'); # Try to add a user set to a course that doesn't exist. -throws_ok { - $user_set_rs->addUserSet( - params => { - username => 'otto', - course_name => 'non existent course', - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'addUserSet: try to add a user set to a non-existent course'; +is( + dies { + $user_set_rs->addUserSet( + params => { username => 'otto', course_name => 'non existent course', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'addUserSet: try to add a user set to a non-existent course' +); # Try to add a user set for a set that does not exist in a course. -throws_ok { - $user_set_rs->addUserSet( - params => { - username => 'otto', - course_name => 'Precalculus', - set_name => 'HW #99' - } - ); -} -'DB::Exception::SetNotInCourse', 'addUserSet: try to add a user set to a non-existent set'; +is( + dies { + $user_set_rs->addUserSet( + params => { username => 'otto', course_name => 'Precalculus', set_name => 'HW #99' }); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'addUserSet: try to add a user set to a non-existent set' +); # Try to add a user set for a user that is not in a course. -throws_ok { - $user_set_rs->addUserSet( - params => { - username => 'ralph', - course_name => 'Abstract Algebra', - set_name => 'HW #1' - } - ); -} -'DB::Exception::UserNotInCourse', 'addUserSet: try to add a user set for a user who is not in the course'; +is( + dies { + $user_set_rs->addUserSet( + params => { username => 'ralph', course_name => 'Abstract Algebra', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::UserNotInCourse'), + 'addUserSet: try to add a user set for a user who is not in the course' +); # Try to add a user_set that already exists. -throws_ok { - $user_set_rs->addUserSet( - params => { - username => 'otto', - course_name => 'Precalculus', - set_name => 'HW #1' - } - ); -} -'DB::Exception::UserSetExists', 'addUserSet: try to add a user set that already exists'; +is( + dies { + $user_set_rs->addUserSet( + params => { username => 'otto', course_name => 'Precalculus', set_name => 'HW #1' }); + }, + check_isa('DB::Exception::UserSetExists'), + 'addUserSet: try to add a user set that already exists' +); my $otto_set_info2 = { username => 'otto', @@ -492,26 +480,26 @@ $set_params2->{set_params} = { description => 'This is the description for HW # $set_params2->{set_version} = 1; $set_params2->{set_type} = 'HW'; -is_deeply($user_set2, $set_params2, 'addUserSet: add a new user set with params'); +is($user_set2, $set_params2, 'addUserSet: add a new user set with params'); # When adding a user set with a bad field in the params, it is ignored -throws_ok { - $user_set_rs->addUserSet( - params => { - username => 'otto', - course_name => 'Precalculus', - set_name => 'HW #4', - set_version => 1, - set_params => { - bad_field => 12, - hide_hint => false +is( + dies { + $user_set_rs->addUserSet( + params => { + username => 'otto', + course_name => 'Precalculus', + set_name => 'HW #4', + set_version => 1, + set_params => { bad_field => 12, hide_hint => false } } - } - ) -} -'DB::Exception::InvalidField', 'addUserSet: try to add a new user set with an undefined parameter'; + ) + }, + check_isa('DB::Exception::InvalidField'), + 'addUserSet: try to add a new user set with an undefined parameter' +); -## add a user set with a new date +# add a user set with a new date my $set_dates4 = { open => 1, @@ -544,7 +532,7 @@ $set_params4->{set_type} = 'HW'; $set_params4->{set_params} = {}; $set_params4->{set_version} = 1; -is_deeply($user_set4, $set_params4, 'addUserSet: add a new user set with dates'); +is($user_set4, $set_params4, 'addUserSet: add a new user set with dates'); # add a user with only override dates set. @@ -577,38 +565,30 @@ cleanUndef($ralph_user_set); $ralph_set_info->{set_type} = 'HW'; $ralph_set_info->{set_version} = 0; -is_deeply($ralph_user_set, $ralph_set_info, 'addUserSet: add a new user with dates (some are missing).'); +is($ralph_user_set, $ralph_set_info, 'addUserSet: add a new user with dates (some are missing).'); # Try to add a bad date. -throws_ok { - $user_set_rs->addUserSet( - params => { - %$otto_set_info5, - set_dates => { - open => 100, - due => 9, - answer => 1000, - enable_reduced_scoring => false - } - } - ); -} -'DB::Exception::ImproperDateOrder', 'addUserSet: dates are out of order'; - -throws_ok { - $user_set_rs->addUserSet( - params => { - %$otto_set_info5, - set_dates => { - open => 100, - due => 900, - answer => 800 +is( + dies { + $user_set_rs->addUserSet( + params => { + %$otto_set_info5, + set_dates => { open => 100, due => 9, answer => 1000, enable_reduced_scoring => false } } - } - ); -} -'DB::Exception::ImproperDateOrder', 'addUserSet: dates are out of order'; + ); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'addUserSet: dates are out of order' +); +is( + dies { + $user_set_rs->addUserSet( + params => { %$otto_set_info5, set_dates => { open => 100, due => 900, answer => 800 } }); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'addUserSet: dates are out of order' +); # Add a new user set and test that it is merged correctly. my $otto_quiz_info = { @@ -648,42 +628,29 @@ removeIDs($user_set_to_merge); # otto_quiz has set_version 0. Need to match to compare. $merged_set1->{set_version} = 0; -is_deeply($merged_set1, $user_set_to_merge, 'addUserSet: adding a user set with dates to check merging'); - -## Check that adding a user set that are out of order with the problem sets throws an error. +is($user_set_to_merge, $merged_set1, 'addUserSet: adding a user set with dates to check merging'); -throws_ok { - $user_set_rs->addUserSet( - params => { - %$otto_set_info5, - set_dates => { - due => 1609595640, # this is after the problem set answer date. - } - }, - merged => 1 - ); -} -'DB::Exception::ImproperDateOrder', 'addUserSet: user set is out of order with respect to problem set'; - -## Check that setting a boolean as 0/1 throws an error - -# Currently, this is stripped out if hide_hint is 0. Doe we want to check for this? +# Check that adding a user set that is out of order with the problem sets throws an error. +# This set has a due date after the answer date. +is( + dies { + $user_set_rs->addUserSet(params => { %$otto_set_info5, set_dates => { due => 1609595640 } }, merged => 1); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'addUserSet: user set is out of order with respect to problem set' +); -throws_ok { - $user_set_rs->addUserSet( - params => { - %$otto_set_info5, - set_params => { - hide_hint => 1 - } - }, - merged => 1 - ); -} -'DB::Exception::InvalidParameter', 'addUserSet: boolean valid should be JSON boolean'; +# Check that setting a boolean as 0/1 throws an error +# Currently, this is stripped out if hide_hint is 0. Do we want to check for this? +is( + dies { + $user_set_rs->addUserSet(params => { %$otto_set_info5, set_params => { hide_hint => 1 } }, merged => 1); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addUserSet: boolean valid should be JSON boolean' +); # Update User Set -# Get the user set for $otto_set_info2. my $otto_quiz = $user_set_rs->getUserSet(info => $otto_quiz_info); removeIDs($otto_quiz); @@ -700,7 +667,7 @@ $otto_quiz->{set_dates}->{answer} = $updated_dates->{answer}; my $updated_user_quiz = $user_set_rs->updateUserSet(info => $otto_quiz_info, params => $otto_quiz); removeIDs($updated_user_quiz); -is_deeply($updated_user_quiz, $otto_quiz, 'updateUserSet: update the dates'); +is($updated_user_quiz, $otto_quiz, 'updateUserSet: update the dates'); # Update the params my $updated_user_set2 = $user_set_rs->updateUserSet( @@ -713,7 +680,7 @@ my $updated_user_set2 = $user_set_rs->updateUserSet( ); removeIDs($updated_user_set2); $otto_quiz->{set_params}->{problem_randorder} = true; -is_deeply($updated_user_set2, $otto_quiz, 'updateUserSet: update the params'); +is($updated_user_set2, $otto_quiz, 'updateUserSet: update the params'); # Update a valid field my $updated_user_set3 = $user_set_rs->updateUserSet( @@ -725,68 +692,56 @@ my $updated_user_set3 = $user_set_rs->updateUserSet( removeIDs($updated_user_set3); $otto_quiz->{set_visible} = true; -is_deeply($otto_quiz, $updated_user_set3, 'updateUserSet: update the set visibility'); +is($otto_quiz, $updated_user_set3, 'updateUserSet: update the set visibility'); # Try updating an invalid param. -throws_ok { - $user_set_rs->updateUserSet( - info => $otto_quiz_info, - params => { - set_params => { - not_a_valid_param => 'bad' - } - } - ); -} -'DB::Exception::InvalidField', 'updateUserSet: try setting a parameter that does not exist'; +is( + dies { + $user_set_rs->updateUserSet( + info => $otto_quiz_info, + params => { set_params => { not_a_valid_param => 'bad' } } + ); + }, + check_isa('DB::Exception::InvalidField'), + 'updateUserSet: try setting a parameter that does not exist' +); # Try updating an invalid date. -throws_ok { - $user_set_rs->updateUserSet( - info => $otto_quiz_info, - params => { - set_dates => { - open => 1, - closed => 2 - } - } - ); -} -'DB::Exception::InvalidField', 'updateUserSet: try to update an invalid date field'; +is( + dies { + $user_set_rs->updateUserSet(info => $otto_quiz_info, params => { set_dates => { open => 1, closed => 2 } }); + }, + check_isa('DB::Exception::InvalidField'), + 'updateUserSet: try to update an invalid date field' +); # Test with out of order dates. -throws_ok { - $user_set_rs->updateUserSet( - info => $otto_quiz_info, - params => { - set_dates => { - open => 100, - due => 2, - answer => 200 - } - } - ); -} -'DB::Exception::ImproperDateOrder', 'updateUserSet: try to update with out of order dates'; +is( + dies { + $user_set_rs->updateUserSet( + info => $otto_quiz_info, + params => { set_dates => { open => 100, due => 2, answer => 200 } } + ); + }, + check_isa('DB::Exception::ImproperDateOrder'), + 'updateUserSet: try to update with out of order dates' +); # Try to update a user_set that doesn't exist. -throws_ok { - $user_set_rs->updateUserSet( - info => $otto_set_info5, - params => { - set_params => { - hide_hint => true - } - } - ); -} -'DB::Exception::UserSetNotInCourse', 'updateUserSet: try to update a user set not the in the course'; +is( + dies { + $user_set_rs->updateUserSet(info => $otto_set_info5, params => { set_params => { hide_hint => true } }); + }, + check_isa('DB::Exception::UserSetNotInCourse'), + 'updateUserSet: try to update a user set not the in the course' +); # Try to delete a user_set that doesn't exist. -throws_ok { - $user_set_rs->deleteUserSet(info => $otto_set_info5); -} -'DB::Exception::UserSetNotInCourse', 'deleteUserSet: try to delete a user set not the in the course'; +is( + dies { $user_set_rs->deleteUserSet(info => $otto_set_info5); }, + check_isa('DB::Exception::UserSetNotInCourse'), + 'deleteUserSet: try to delete a user set not the in the course' +); # Delete some user sets that were created. @@ -801,7 +756,7 @@ removeIDs($deleted_user_set); cleanUndef($deleted_user_set); removeIDs($new_user_set2); cleanUndef($new_user_set2); -is_deeply($deleted_user_set, $new_user_set2, "deleteUserSet: successfully delete a user set"); +is($deleted_user_set, $new_user_set2, "deleteUserSet: successfully delete a user set"); my $deleted_user_set2 = $user_set_rs->deleteUserSet( info => { @@ -812,12 +767,12 @@ my $deleted_user_set2 = $user_set_rs->deleteUserSet( ); removeIDs($deleted_user_set2); cleanUndef($deleted_user_set2); -is_deeply($deleted_user_set2, $ralph_user_set, "deleteUserSet: successfully delete another user set"); +is($deleted_user_set2, $ralph_user_set, "deleteUserSet: successfully delete another user set"); my $deleted_user_set3 = $user_set_rs->deleteUserSet(info => $otto_quiz_info); removeIDs($deleted_user_set3); cleanUndef($deleted_user_set3); -is_deeply($deleted_user_set3, $otto_quiz, "deleteUserSet: successfully delete yet another user set"); +is($deleted_user_set3, $otto_quiz, "deleteUserSet: successfully delete yet another user set"); my $deleted_user_set4 = $user_set_rs->deleteUserSet(info => $new_merged_set); @@ -856,6 +811,6 @@ for my $set (@all_user_sets_from_db) { @all_user_sets_from_db = sort { $a->{course_name} cmp $b->{course_name} || $a->{set_name} cmp $b->{set_name} } @all_user_sets_from_db; -is_deeply(\@all_user_sets_from_db, \@all_user_sets, 'check: ensure that the user sets are restored.'); +is(\@all_user_sets_from_db, \@all_user_sets, 'check: ensure that the user sets are restored.'); done_testing; diff --git a/t/db/008_problem_pools.t b/t/db/008_problem_pools.t index dc731a66..a8a72346 100644 --- a/t/db/008_problem_pools.t +++ b/t/db/008_problem_pools.t @@ -14,8 +14,7 @@ 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 Clone qw/clone/; @@ -60,7 +59,7 @@ for my $pool (@problem_pools_from_db) { removeIDs($pool); } -is_deeply(\@problem_pools_from_file, \@problem_pools_from_db, 'getAllProblemPools: find all problem pools'); +is(\@problem_pools_from_db, \@problem_pools_from_file, 'getAllProblemPools: find all problem pools'); my @precalc_pools = $problem_pool_rs->getProblemPools(info => { course_name => 'Precalculus' }); @@ -69,7 +68,7 @@ for my $pool (@precalc_pools) { $pool->{course_name} = 'Precalculus'; } -is_deeply(\@precalc_pools_from_file, \@precalc_pools, 'getProblemPools: get all problem pools from a single course'); +is(\@precalc_pools, \@precalc_pools_from_file, 'getProblemPools: get all problem pools from a single course'); # Get a problem pool my $pool_to_fetch = $problem_pools_from_file[0]; @@ -78,19 +77,26 @@ my $fetched_pool = $problem_pool_rs->getProblemPool(info => $pool_to_fetch); removeIDs($fetched_pool); $fetched_pool->{course_name} = $problem_pools_from_file[0]->{course_name}; -is_deeply($pool_to_fetch, $fetched_pool, 'getProblemPool: get a single pool from a course'); +is($fetched_pool, $pool_to_fetch, 'getProblemPool: get a single pool from a course'); # Try to get a problem pool from a course that doesn't exist. -throws_ok { - $problem_pool_rs->getProblemPool(info => { course_name => 'not existent course', pool_name => 'adding fractions' }); -} -'DB::Exception::CourseNotFound', 'getProblemPool: get a problem pool from a non-existent course'; +is( + dies { + $problem_pool_rs->getProblemPool( + info => { course_name => 'not existent course', pool_name => 'adding fractions' }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'getProblemPool: get a problem pool from a non-existent course' +); # Try to get a problem pool from a course, but the pool doesn't exist. -throws_ok { - $problem_pool_rs->getProblemPool(info => { course_name => 'Arithmetic', pool_name => 'non_existent_pool' }); -} -'DB::Exception::PoolNotInCourse', 'getProblemPool: get a problem pool from a non-existent course'; +is( + dies { + $problem_pool_rs->getProblemPool(info => { course_name => 'Arithmetic', pool_name => 'non_existent_pool' }); + }, + check_isa('DB::Exception::PoolNotInCourse'), + 'getProblemPool: get a problem pool from a non-existent course' +); # Add a problem pool my $course_name = 'Arithmetic'; @@ -104,36 +110,27 @@ my $pool2 = $problem_pool_rs->addProblemPool( ); removeIDs($pool2); -is_deeply( - { - pool_name => $pool_name - }, - $pool2, - 'addProblemPool: add a new problem pool to a course' -); +is($pool2, { pool_name => $pool_name }, 'addProblemPool: add a new problem pool to a course'); # Try to add a pool that already exists. -throws_ok { - $problem_pool_rs->addProblemPool( - params => { - course_name => 'Arithmetic', - pool_name => 'adding fractions' - } - ); -} -'DB::Exception::PoolAlreadyInCourse', 'addProblemPool: pool already exists'; +is( + dies { + $problem_pool_rs->addProblemPool( + params => { course_name => 'Arithmetic', pool_name => 'adding fractions' }); + }, + check_isa('DB::Exception::PoolAlreadyInCourse'), + 'addProblemPool: pool already exists' +); # Try to add a pool with an invalid field. -throws_ok { - $problem_pool_rs->addProblemPool( - params => { - course_name => 'Arithmetic', - pool_name => 'dividing fractions', - other_field => 'XXX' - } - ); -} -'DBIx::Class::Exception', 'addProblemPool: add a pool with non-valid field'; +is( + dies { + $problem_pool_rs->addProblemPool( + params => { course_name => 'Arithmetic', pool_name => 'dividing fractions', other_field => 'XXX' }); + }, + check_isa('DBIx::Class::Exception'), + 'addProblemPool: add a pool with non-valid field' +); # Update an existing problem pool. my $updated_pool = { pool_name => 'subtracting fractions with like denominators', }; @@ -147,27 +144,33 @@ $updated_pool_from_db->{course_name} = 'Arithmetic'; removeIDs($updated_pool_from_db); $updated_pool->{course_name} = 'Arithmetic'; -is_deeply($updated_pool, $updated_pool_from_db, 'updateProblemPool: update the name of a problem pool'); +is($updated_pool_from_db, $updated_pool, 'updateProblemPool: update the name of a problem pool'); # TODO: Try to update a pool that doesn't exist. # Try to get a problem pool from a course that doesn't exist. -throws_ok { - $problem_pool_rs->updateProblemPool( - info => { course_name => 'non_existent_course', pool_name => 'XXXX' }, - parms => $updated_pool - ); -} -'DB::Exception::CourseNotFound', 'udpateProblemPool: update a problem pool from a non-existent course'; +is( + dies { + $problem_pool_rs->updateProblemPool( + info => { course_name => 'non_existent_course', pool_name => 'XXXX' }, + parms => $updated_pool + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'udpateProblemPool: update a problem pool from a non-existent course' +); # Try to get a non-existent problem pool from a course. -throws_ok { - $problem_pool_rs->updateProblemPool( - info => { course_name => 'Arithmetic', pool_name => 'non_existent_pool' }, - params => $updated_pool - ); -} -'DB::Exception::PoolNotInCourse', 'updateProblemPool: update a problem pool from a non-existent course'; +is( + dies { + $problem_pool_rs->updateProblemPool( + info => { course_name => 'Arithmetic', pool_name => 'non_existent_pool' }, + params => $updated_pool + ); + }, + check_isa('DB::Exception::PoolNotInCourse'), + 'updateProblemPool: update a problem pool from a non-existent course' +); # Get all PoolProblems from within a pool my @pool_problems = $problem_pool_rs->getPoolProblems( @@ -211,28 +214,24 @@ is( ); # Check that adding a problem to a non-existence course fails. -throws_ok { - $problem_pool_rs->addProblemToPool( - params => { - course_name => 'non_existing_course', - pool_name => 'adding fractions', - %$prob_to_add - } - ); -} -'DB::Exception::CourseNotFound', 'addProblemToPool: try to add to a nonexisting course'; +is( + dies { + $problem_pool_rs->addProblemToPool( + params => { course_name => 'non_existing_course', pool_name => 'adding fractions', %$prob_to_add }); + }, + check_isa('DB::Exception::CourseNotFound'), + 'addProblemToPool: try to add to a nonexisting course' +); # Check that adding a problem to a non-existence pool fails. -throws_ok { - $problem_pool_rs->addProblemToPool( - params => { - course_name => $updated_pool->{course_name}, - pool_name => 'non_existent_pool_name', - %$prob_to_add - } - ); -} -'DB::Exception::PoolNotInCourse', 'addProblemToPool: try to add to a nonexisting pool'; +is( + dies { + $problem_pool_rs->addProblemToPool(params => + { course_name => $updated_pool->{course_name}, pool_name => 'non_existent_pool_name', %$prob_to_add }); + }, + check_isa('DB::Exception::PoolNotInCourse'), + 'addProblemToPool: try to add to a nonexisting pool' +); # Update a pool problem. my $course_pool_problem_info = {%$updated_pool_from_db}; @@ -252,53 +251,58 @@ is( ); # Check that updating a problem to a non-existence course fails. -throws_ok { - $problem_pool_rs->updatePoolProblem( - info => { - course_name => 'non_existing_course', - pool_name => 'adding fractions', - pool_problem_id => $added_problem->{pool_problem_id} - }, - params => { - params => { - library_id => $updated_library_id - } - } - ); -} -'DB::Exception::CourseNotFound', 'updatePoolProblem: try to update a nonexisting course'; +is( + dies { + $problem_pool_rs->updatePoolProblem( + info => { + course_name => 'non_existing_course', + pool_name => 'adding fractions', + pool_problem_id => $added_problem->{pool_problem_id} + }, + params => { params => { library_id => $updated_library_id } } + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'updatePoolProblem: try to update a nonexisting course' +); # Check that updating a problem to a non-existence course fails. -throws_ok { - $problem_pool_rs->updatePoolProblem( - info => { - course_name => $updated_pool->{course_name}, - pool_name => 'non_existent_pool_name', - pool_problem_id => $added_problem->{pool_problem_id} - }, - params => { library_id => $updated_library_id } - ); -} -'DB::Exception::PoolNotInCourse', 'updatePoolProblem: try to update a nonexisting pool'; +is( + dies { + $problem_pool_rs->updatePoolProblem( + info => { + course_name => $updated_pool->{course_name}, + pool_name => 'non_existent_pool_name', + pool_problem_id => $added_problem->{pool_problem_id} + }, + params => { library_id => $updated_library_id } + ); + }, + check_isa('DB::Exception::PoolNotInCourse'), + 'updatePoolProblem: try to update a nonexisting pool' +); # Check that updating a problem to a non-existing problem fails. -throws_ok { - $problem_pool_rs->updatePoolProblem( - info => { - course_name => $updated_pool->{course_name}, - pool_name => $updated_pool->{pool_name}, - pool_problem_id => -999 - }, - params => { library_id => $updated_library_id } - ); -} -'DB::Exception::PoolProblemNotInPool', 'updatePoolProblem: try to update a nonexisting problem'; +is( + dies { + $problem_pool_rs->updatePoolProblem( + info => { + course_name => $updated_pool->{course_name}, + pool_name => $updated_pool->{pool_name}, + pool_problem_id => -999 + }, + params => { library_id => $updated_library_id } + ); + }, + check_isa('DB::Exception::PoolProblemNotInPool'), + 'updatePoolProblem: try to update a nonexisting problem' +); # Delete a problem pool my $pool_to_delete = $problem_pool_rs->deleteProblemPool(info => $updated_pool); removeIDs($pool_to_delete); $pool_to_delete->{course_name} = 'Arithmetic'; -is_deeply($updated_pool, $pool_to_delete, 'deleteProblemPool: delete an existing problem pool'); +is($pool_to_delete, $updated_pool, 'deleteProblemPool: delete an existing problem pool'); # Ensure that the problem_pool table is restored. @problem_pools_from_db = $problem_pool_rs->getAllProblemPools(); @@ -307,6 +311,6 @@ for my $pool (@problem_pools_from_db) { removeIDs($pool); } -is_deeply(\@problem_pools_from_file, \@problem_pools_from_db, 'check: Ensure that the problem_pool table is restored.'); +is(\@problem_pools_from_db, \@problem_pools_from_file, 'check: Ensure that the problem_pool table is restored.'); done_testing; diff --git a/t/db/009_problems.t b/t/db/009_problems.t index d001c650..86c214b6 100644 --- a/t/db/009_problems.t +++ b/t/db/009_problems.t @@ -14,8 +14,7 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use Test::More; -use Test::Exception; +use Test2::V0; use Clone qw/clone/; use YAML::XS qw/LoadFile/; @@ -57,7 +56,7 @@ for my $problem (@problems_from_db) { removeIDs($problem); } -is_deeply(\@all_problems, \@problems_from_db, 'getGlobalProblems: get all problems'); +is(\@problems_from_db, \@all_problems, 'getGlobalProblems: get all problems'); # Get all problems from one course. my @precalc_problems_from_db = $problem_rs->getProblems(info => { course_name => 'Precalculus' }); @@ -67,31 +66,25 @@ for my $problem (@precalc_problems_from_db) { # Try to get all problems from a non existent course -throws_ok { - $problem_rs->getProblems( - info => { - course_name => 'non existent course' - } - ); -} -'DB::Exception::CourseNotFound', 'getProblems: non-existent course'; +is( + dies { $problem_rs->getProblems(info => { course_name => 'non existent course' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getProblems: non-existent course' +); # Try to get all problems from a non-existent set_id -throws_ok { - $problem_rs->getProblems( - info => { - course_id => 999999 - } - ); -} -'DB::Exception::CourseNotFound', 'getProblems: non-existent course_id'; +is( + dies { $problem_rs->getProblems(info => { course_id => 999999 }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getProblems: non-existent course_id' +); for my $problem (@precalc_problems) { delete $problem->{set_name}; } -is_deeply(\@precalc_problems, \@precalc_problems_from_db, 'getSetProblems: get all problems from one course'); +is(\@precalc_problems_from_db, \@precalc_problems, 'getSetProblems: get all problems from one course'); # Get all problems in one course from one set. my @set_problems1 = $problem_rs->getSetProblems(info => { course_name => 'Precalculus', set_name => 'HW #1' }); @@ -100,19 +93,21 @@ for my $problem (@set_problems1) { removeIDs($problem); } -is_deeply(\@precalc_problems1, \@set_problems1, 'getSetProblems: get all problems from one set'); +is(\@set_problems1, \@precalc_problems1, 'getSetProblems: get all problems from one set'); # Try to get problems from a non-existing course. -throws_ok { - $problem_rs->getSetProblems(info => { course_name => 'non_existing_course', set_name => 'HW #1' }); -} -'DB::Exception::CourseNotFound', 'getSetProblems: get problems from non-existing course'; +is( + dies { $problem_rs->getSetProblems(info => { course_name => 'non_existing_course', set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'getSetProblems: get problems from non-existing course' +); # Try to get problems from a non-existing set. -throws_ok { - $problem_rs->getSetProblems(info => { course_name => 'Precalculus', set_name => 'HW #999' }); -} -'DB::Exception::SetNotInCourse', 'getSetProblems: get problems from non-existing set'; +is( + dies { $problem_rs->getSetProblems(info => { course_name => 'Precalculus', set_name => 'HW #999' }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'getSetProblems: get problems from non-existing set' +); # Get a single problem from a course. my $set_problem = $problem_rs->getSetProblem( @@ -128,7 +123,7 @@ my $expected_problem = { %{ $problems_from_csv[0] } }; # Copy the first probl delete $expected_problem->{set_name}; delete $expected_problem->{course_name}; -is_deeply($expected_problem, $set_problem, 'getSetProblem: get a single problem from a set in a given course'); +is($set_problem, $expected_problem, 'getSetProblem: get a single problem from a set in a given course'); # Add a problem to an existing set. my $new_problem = { @@ -150,7 +145,7 @@ my $prob1 = $problem_rs->addSetProblem( my $prob_id = $prob1->{set_problem_id}; removeIDs($prob1); -is_deeply($new_problem, $prob1, 'addSetProblem: add a valid problem to a set'); +is($prob1, $new_problem, 'addSetProblem: add a valid problem to a set'); # Add a problem and make sure the problem number is working. # Determine the largest current problem number. @@ -183,63 +178,45 @@ my $prob2 = { 'problem_number' => $max + 1, }; -is_deeply($prob2, $prob2_from_db, 'addSetProblem: add a set problem and ensure the problem number is correct.'); +is($prob2_from_db, $prob2, 'addSetProblem: add a set problem and ensure the problem number is correct.'); # Try to add a problem to a non-existent course -throws_ok { - $problem_rs->addSetProblem( - params => { - course_name => 'Non existent course', - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'addSetProblem: try to add a problem to a non-existent course'; +is( + dies { $problem_rs->addSetProblem(params => { course_name => 'Non existent course', set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'addSetProblem: try to add a problem to a non-existent course' +); # Try to add a problem to a non-existent course_id -throws_ok { - $problem_rs->addSetProblem( - params => { - course_id => 999999, - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'addSetProblem: try to add a problem to a non-existent course_id'; +is( + dies { $problem_rs->addSetProblem(params => { course_id => 999999, set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'addSetProblem: try to add a problem to a non-existent course_id' +); # Try to add a problem to a non-existent set -throws_ok { - $problem_rs->addSetProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #99999' - } - ); -} -'DB::Exception::SetNotInCourse', 'addSetProblem: try to add a problem to a non-existent set'; +is( + dies { $problem_rs->addSetProblem(params => { course_name => 'Precalculus', set_name => 'HW #99999' }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'addSetProblem: try to add a problem to a non-existent set' +); # Try to add a problem to a non-existent set -throws_ok { - $problem_rs->addSetProblem( - params => { - course_name => 'Precalculus', - set_id => 999999 - } - ); -} -'DB::Exception::SetNotInCourse', 'addSetProblem: try to add a problem to a non-existent set_id'; +is( + dies { $problem_rs->addSetProblem(params => { course_name => 'Precalculus', set_id => 999999 }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'addSetProblem: try to add a problem to a non-existent set_id' +); # Try to add a problem without information about the file_path, library_id or problem_pool_id -throws_ok { - $problem_rs->addSetProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #1', - problem_params => {} - } - ); -} -'DB::Exception::FieldsNeeded', "addSetProblem: try to add a problem without information about the file_path, etc."; +is( + dies { + $problem_rs->addSetProblem( + params => { course_name => 'Precalculus', set_name => 'HW #1', problem_params => {} }); + }, + check_isa('DB::Exception::FieldsNeeded'), + "addSetProblem: try to add a problem without information about the file_path, etc." +); # Note: we may want to not have the following in the future, but currently its okay. # Try to add a problem with both information about the file_path, and library_id . @@ -263,7 +240,7 @@ delete $set_prob_params2->{set_name}; removeIDs($set_problem2); delete $set_problem2->{problem_number}; -is_deeply($set_problem2, $set_prob_params2, 'addSetProblem: adding a problem with both file_path and library_id'); +is($set_problem2, $set_prob_params2, 'addSetProblem: adding a problem with both file_path and library_id'); # Update a problem my $updated_params = { @@ -284,82 +261,62 @@ my $updated_problem = $problem_rs->updateSetProblem( ); removeIDs($updated_problem); -is_deeply($all_params, $updated_problem, 'updateProblem: update a problem'); +is($updated_problem, $all_params, 'updateProblem: update a problem'); # Try to update a problem in a non-existent course -throws_ok { - $problem_rs->updateSetProblem( - info => { - course_name => 'Non existent course', - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'updateSetProblem: try to update a problem to a non-existent course'; +is( + dies { $problem_rs->updateSetProblem(info => { course_name => 'Non existent course', set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'updateSetProblem: try to update a problem to a non-existent course' +); # Try to update a problem to with a non-existent course_id -throws_ok { - $problem_rs->updateSetProblem( - info => { - course_id => 999999, - set_name => 'HW #1' - } - ); -} -'DB::Exception::CourseNotFound', 'updateSetProblem: try to update a problem to a non-existent course_id'; +is( + dies { $problem_rs->updateSetProblem(info => { course_id => 999999, set_name => 'HW #1' }); }, + check_isa('DB::Exception::CourseNotFound'), + 'updateSetProblem: try to update a problem to a non-existent course_id' +); # Try to update a problem to a non-existent set -throws_ok { - $problem_rs->updateSetProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #99999' - } - ); -} -'DB::Exception::SetNotInCourse', 'updateSetProblem: try to update a problem to a non-existent set'; +is( + dies { $problem_rs->updateSetProblem(info => { course_name => 'Precalculus', set_name => 'HW #99999' }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'updateSetProblem: try to update a problem to a non-existent set' +); # Try to update a problem to a non-existent set_id -throws_ok { - $problem_rs->updateSetProblem( - info => { - course_name => 'Precalculus', - set_id => 999999 - } - ); -} -'DB::Exception::SetNotInCourse', 'updateSetProblem: try to update a problem to a non-existent set_id'; +is( + dies { $problem_rs->updateSetProblem(info => { course_name => 'Precalculus', set_id => 999999 }); }, + check_isa('DB::Exception::SetNotInCourse'), + 'updateSetProblem: try to update a problem to a non-existent set_id' +); # Try to update a problem with a negative problem number. -throws_ok { - $problem_rs->updateSetProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #1', - problem_number => 1, - }, - params => { - problem_number => -9 - } - ); -} -'DB::Exception::InvalidParameter', - 'updateSetProblem: try to update a problem with a non positive integer problem_number'; +is( + dies { + $problem_rs->updateSetProblem( + info => { course_name => 'Precalculus', set_name => 'HW #1', problem_number => 1, }, + params => { problem_number => -9 } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'updateSetProblem: try to update a problem with a non positive integer problem_number' +); # Try to update a problem without information about the file_path, library_id or problem_pool_id -throws_ok { - $problem_rs->updateSetProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #1', - problem_params => { - file_path => 'this_is_a_path', - library_id => 123 +is( + dies { + $problem_rs->updateSetProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #1', + problem_params => { file_path => 'this_is_a_path', library_id => 123 } } - } - ); -} -'DB::Exception::ParametersNeeded', 'updateSetProblem: try to add a problem with too much library info'; + ); + }, + check_isa('DB::Exception::ParametersNeeded'), + 'updateSetProblem: try to add a problem with too much library info' +); # Delete a problem from a set my $deleted_problem = $problem_rs->deleteSetProblem( @@ -371,7 +328,7 @@ my $deleted_problem = $problem_rs->deleteSetProblem( ); removeIDs($deleted_problem); -is_deeply($updated_problem, $deleted_problem, 'deleteSetProblem: delete one problem in an existing set.'); +is($deleted_problem, $updated_problem, 'deleteSetProblem: delete one problem in an existing set.'); my $deleted_problem2 = $problem_rs->deleteSetProblem( info => { @@ -380,7 +337,7 @@ my $deleted_problem2 = $problem_rs->deleteSetProblem( problem_number => $set_problem_to_delete->{problem_number}, } ); -is_deeply($deleted_problem2, $set_problem_to_delete, 'deleteSetProblem: delete another problem.'); +is($deleted_problem2, $set_problem_to_delete, 'deleteSetProblem: delete another problem.'); my $deleted_problem3 = $problem_rs->deleteSetProblem( info => { @@ -390,7 +347,7 @@ my $deleted_problem3 = $problem_rs->deleteSetProblem( } ); removeIDs($deleted_problem3); -is_deeply($deleted_problem3, $prob2, 'deleteSetProblem: delete another problem.'); +is($deleted_problem3, $prob2, 'deleteSetProblem: delete another problem.'); # Make sure the set_problem table is returned to its orginal state. @problems_from_db = $problem_rs->getGlobalProblems; @@ -398,7 +355,7 @@ for my $problem (@problems_from_db) { removeIDs($problem); } -is_deeply(\@all_problems, \@problems_from_db, 'The set_problems table is returned to its original state.'); +is(\@problems_from_db, \@all_problems, 'The set_problems table is returned to its original state.'); # Ensure that the set_problems table is restored. @problems_from_db = $problem_rs->getGlobalProblems; @@ -406,6 +363,6 @@ for my $problem (@problems_from_db) { removeIDs($problem); } -is_deeply(\@all_problems, \@problems_from_db, 'check: Ensure that the set_problems table is restored.'); +is(\@problems_from_db, \@all_problems, 'check: Ensure that the set_problems table is restored.'); done_testing; diff --git a/t/db/010_user_problems.t b/t/db/010_user_problems.t index 83264eb5..4d58fc70 100644 --- a/t/db/010_user_problems.t +++ b/t/db/010_user_problems.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl -# + # This tests the basic database CRUD functions of user problems. -# + use warnings; use strict; @@ -14,16 +14,12 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use Test::More; -use Test::Exception; -use Try::Tiny; -use Carp; +use Test2::V0; use Clone qw/clone/; use YAML::XS qw/LoadFile/; use DB::Schema; -use TestUtils qw/loadCSV removeIDs loadSchema/; -use DB::Utils qw/updateAllFields/; +use TestUtils qw/loadCSV removeIDs/; # Set up the database. my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; @@ -104,8 +100,7 @@ for my $user_problem (@all_user_problems_from_db) { # For comparision, from the database needs to be a number. $_->{status} = 0 + $_->{status} for (@all_user_problems_from_db); -is_deeply(\@user_problems_from_csv, \@all_user_problems_from_db, - 'getAllUserProblems: fetch all user problems from the DB.'); +is(\@all_user_problems_from_db, \@user_problems_from_csv, 'getAllUserProblems: fetch all user problems from the DB.'); # Get merged user problems. @@ -124,7 +119,7 @@ $_->{status} = 0 + $_->{status} for (@merged_problems_from_db); @merged_problems_from_db = sort user_prob_sort_fxn @merged_problems_from_db; -is_deeply(\@merged_problems_from_csv, \@merged_problems_from_db, 'getAllUserProblems: fetch all merged problems'); +is(\@merged_problems_from_db, \@merged_problems_from_csv, 'getAllUserProblems: fetch all merged problems'); # Get user problems from one course. @@ -139,11 +134,8 @@ for my $user_problem (@precalc_user_problems_from_db) { @precalc_user_problems_from_db = sort user_prob_sort_fxn @precalc_user_problems_from_db; -is_deeply( - \@precalc_user_problems, - \@precalc_user_problems_from_db, - 'getUserProblems: get user problems from a single course.' -); +is(\@precalc_user_problems_from_db, \@precalc_user_problems, + 'getUserProblems: get user problems from a single course.'); # Get merged problems from one course. @@ -161,11 +153,8 @@ for my $merged_problem (@precalc_merged_problems_from_db) { @precalc_merged_problems = sort user_prob_sort_fxn @precalc_merged_problems; @precalc_merged_problems_from_db = sort user_prob_sort_fxn @precalc_merged_problems_from_db; -is_deeply( - \@precalc_merged_problems, - \@precalc_merged_problems_from_db, - 'getUserProblems: get merged problems from a single course.' -); +is(\@precalc_merged_problems_from_db, + \@precalc_merged_problems, 'getUserProblems: get merged problems from a single course.'); # Get a single user problem. @@ -192,63 +181,71 @@ my $user_problem_from_csv = clone( )[0] ); -is_deeply($user_problem_from_csv, $user_problem, 'getUserProblem: get a single user problem'); +is($user_problem, $user_problem_from_csv, 'getUserProblem: get a single user problem'); # Get a user problem from a course that doesn't exist. - -throws_ok { - $user_problem_rs->getUserProblem( - info => { - course_name => 'course doesn\'t exist', - username => 'homer', - set_name => 'HW #1', - problem_number => 1 - } - ); -} -'DB::Exception::CourseNotFound', 'getUserProblem: attempt to get a user problem from a nonexistent course'; +is( + dies { + $user_problem_rs->getUserProblem( + info => { + course_name => 'course doesn\'t exist', + username => 'homer', + set_name => 'HW #1', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'getUserProblem: attempt to get a user problem from a nonexistent course' +); # Get a user problem from a user that doesn't exist. - -throws_ok { - $user_problem_rs->getUserProblem( - info => { - course_name => 'Precalculus', - username => 'non_existent_user', - set_name => 'HW #1', - problem_number => 1 - } - ); -} -'DB::Exception::UserNotFound', 'getUserProblem: attempt to get a user problem from a nonexistent user'; +is( + dies { + $user_problem_rs->getUserProblem( + info => { + course_name => 'Precalculus', + username => 'non_existent_user', + set_name => 'HW #1', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::UserNotFound'), + 'getUserProblem: attempt to get a user problem from a nonexistent user' +); # Get a user problem from a set that doesn't exist. - -throws_ok { - $user_problem_rs->getUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #99', - problem_number => 1 - } - ); -} -'DB::Exception::SetNotInCourse', 'getUserProblem: attempt to get a user problem from a nonexistent set'; +is( + dies { + $user_problem_rs->getUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #99', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'getUserProblem: attempt to get a user problem from a nonexistent set' +); # Get a user problem from a problem that doesn't exist. - -throws_ok { - $user_problem_rs->getUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #1', - problem_number => 99 - } - ); -} -'DB::Exception::SetProblemNotFound', 'getUserProblem: attempt to get a user problem from a nonexistent problem'; +is( + dies { + $user_problem_rs->getUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #1', + problem_number => 99 + } + ); + }, + check_isa('DB::Exception::SetProblemNotFound'), + 'getUserProblem: attempt to get a user problem from a nonexistent problem' +); # Add a UserProblem to the database. @@ -270,7 +267,7 @@ $problem_info1->{status} = 0; $problem_info1->{problem_params} = {}; $problem_info1->{problem_version} = 1; -is_deeply($problem_info1, $user_problem1, 'addUserProblem: add a single user problem'); +is($user_problem1, $problem_info1, 'addUserProblem: add a single user problem'); # Add a user problem and get back a merged problem. @@ -310,173 +307,195 @@ for my $key (qw/username seed status problem_version/) { } $problem2->{problem_params}->{weight} = 2; -is_deeply($problem2, $user_problem2, 'addUserProblem: add a user problem and return a merged problem'); +is($user_problem2, $problem2, 'addUserProblem: add a user problem and return a merged problem'); # Attempt to add a UserProblem to a non-existent course. - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'course doesn\'t exist', - username => 'homer', - set_name => 'HW #1', - problem_number => 1 - } - ); -} -'DB::Exception::CourseNotFound', 'addUserProblem: attempt to add a user problem to a nonexistent course'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'course doesn\'t exist', + username => 'homer', + set_name => 'HW #1', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'addUserProblem: attempt to add a user problem to a nonexistent course' +); # Attempt to add a UserProblem for a non-existent problem set. - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #999', - problem_number => 1 - } - ); -} -'DB::Exception::SetNotInCourse', 'addUserProblem: attempt to add a user problem for a non-existent problem set'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #999', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'addUserProblem: attempt to add a user problem for a non-existent problem set' +); # Attempt to add a UserProblem for a non-existent user. - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - username => 'non_existent_user', - set_name => 'HW #1', - problem_number => 1 - } - ); -} -'DB::Exception::UserNotFound', 'addUserProblem: attempt to add a user problem for a non-existent user'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + username => 'non_existent_user', + set_name => 'HW #1', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::UserNotFound'), + 'addUserProblem: attempt to add a user problem for a non-existent user' +); # Attempt to add a UserProblem for a non-existent problem. - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #1', - problem_number => 999 - } - ); -} -'DB::Exception::SetProblemNotFound', 'addUserProblem: attempt to add a user problem for a non-existent problem'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #1', + problem_number => 999 + } + ); + }, + check_isa('DB::Exception::SetProblemNotFound'), + 'addUserProblem: attempt to add a user problem for a non-existent problem' +); # Attempt to add a UserProblem that already exists - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #1', - problem_number => 1 - } - ); -} -'DB::Exception::UserProblemExists', 'addUserProblem: attempt to add a user problem that already exists'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #1', + problem_number => 1 + } + ); + }, + check_isa('DB::Exception::UserProblemExists'), + 'addUserProblem: attempt to add a user problem that already exists' +); # Try to add a UserProblem with a bad seed :) . - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - seed => -1234 - } - ); -} -'DB::Exception::InvalidParameter', 'addUserProblem: attempt to add a user problem with a bad seed'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + seed => -1234 + } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'addUserProblem: attempt to add a user problem with a bad seed' +); # Attempt to add a new User Problem with a non existent field - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - non_existent_field => 1 - } - ); -} -'DBIx::Class::Exception', 'addUserProblem: attempt to add a user problem with a non existent field'; +is( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + non_existent_field => 1 + } + ); + }, + check_isa('DBIx::Class::Exception'), + 'addUserProblem: attempt to add a user problem with a non existent field' +); # Attempt to add a new User Problem with a non existent field - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - non_existent_field => 1 - } - ); -} -qr/No such column 'non_existent_field'/, 'addUserProblem: attempt to add a user problem with a non existent field'; +like( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + non_existent_field => 1 + } + ); + }, + qr/No such column 'non_existent_field'/, + 'addUserProblem: attempt to add a user problem with a non existent field' +); # Attempt to add a new User Problem with invalid library id - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - problem_params => { - library_id => -1234 +like( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + problem_params => { + library_id => -1234 + } } - } - ); -} -qr/library_id is not valid/, 'addUserProblem: attempt to add a user problem with with invalid library id'; + ); + }, + qr/library_id is not valid/, + 'addUserProblem: attempt to add a user problem with with invalid library id' +); # Attempt to add a new User Problem with a bad problem weight - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - problem_params => { - weight => -1 +like( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + problem_params => { + weight => -1 + } } - } - ); -} -qr/weight is not valid/, 'addUserProblem: attempt to add a user problem with a bad problem weight'; + ); + }, + qr/weight is not valid/, + 'addUserProblem: attempt to add a user problem with a bad problem weight' +); # Attempt to add a new User Problem with a invalid problem_pool_id - -throws_ok { - $user_problem_rs->addUserProblem( - params => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 3, - problem_params => { - problem_pool_id => 'fred' +like( + dies { + $user_problem_rs->addUserProblem( + params => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 3, + problem_params => { + problem_pool_id => 'fred' + } } - } - ); -} -qr/problem_pool_id is not valid/, 'addUserProblem: attempt to add a user problem with a bad problem_pool_id'; + ); + }, + qr/problem_pool_id is not valid/, + 'addUserProblem: attempt to add a user problem with a bad problem_pool_id' +); # update a user problem and return as a user problem @@ -493,7 +512,7 @@ $updated_problem1->{status} += 0; delete $updated_problem1->{problem_version} unless defined $updated_problem1->{problem_version}; $user_problem1->{seed} = 4567; -is_deeply($user_problem1, $updated_problem1, 'updateUserProblem: sucessfully update a field'); +is($updated_problem1, $user_problem1, 'updateUserProblem: sucessfully update a field'); # Update a user problem and return as a merged problem. @@ -508,7 +527,7 @@ removeIDs($updated_problem2); $problem2->{seed} = 4567; $updated_problem2->{status} += 0; -is_deeply($problem2, $updated_problem2, 'updateUserProblem: sucessfully update a field and return as a merged problem'); +is($updated_problem2, $problem2, 'updateUserProblem: sucessfully update a field and return as a merged problem'); # Update a user problem in the problem_params @@ -525,176 +544,205 @@ $updated_problem1a->{status} += 0; delete $updated_problem1a->{problem_version} unless defined $updated_problem1a->{problem_version}; $user_problem1->{problem_params}->{library_id} = 1234; -is_deeply($user_problem1, $updated_problem1a, 'updateUserProblem: sucessfully update the problem_params'); +is($updated_problem1a, $user_problem1, 'updateUserProblem: sucessfully update the problem_params'); # Attempt to update a UserProblem to a non-existent course. -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'course doesn\'t exist', - username => 'homer', - set_name => 'HW #1', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::CourseNotFound', 'updateUserProblem: attempt to update a user problem to a nonexistent course'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'course doesn\'t exist', + username => 'homer', + set_name => 'HW #1', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'updateUserProblem: attempt to update a user problem to a nonexistent course' +); # Attempt to update a UserProblem for a non-existent problem set. -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #999', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::SetNotInCourse', 'updateUserProblem: attempt to update a user problem for a non-existent problem set'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #999', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'updateUserProblem: attempt to update a user problem for a non-existent problem set' +); # Attempt to add a UserProblem for a non-existent user. -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - username => 'non_existent_user', - set_name => 'HW #1', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::UserNotFound', 'updateUserProblem: attempt to update a user problem for a non-existent user'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + username => 'non_existent_user', + set_name => 'HW #1', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::UserNotFound'), + 'updateUserProblem: attempt to update a user problem for a non-existent user' +); # Attempt to update a UserProblem for a non-existent problem. -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #1', - problem_number => 999 - }, - params => {} - ); -} -'DB::Exception::SetProblemNotFound', 'updateUserProblem: attempt to update a user problem for a non-existent problem'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #1', + problem_number => 999 + }, + params => {} + ); + }, + check_isa('DB::Exception::SetProblemNotFound'), + 'updateUserProblem: attempt to update a user problem for a non-existent problem' +); # Try to update a UserProblem with a bad seed :) . -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - seed => -1234 - } - ); -} -'DB::Exception::InvalidParameter', 'updateUserProblem: attempt to update a user problem with a bad seed'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + seed => -1234 + } + ); + }, + check_isa('DB::Exception::InvalidParameter'), + 'updateUserProblem: attempt to update a user problem with a bad seed' +); # Attempt to update a new User Problem with a non existent field -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - non_existent_field => 1 - } - ); -} -'DBIx::Class::Exception', 'updateUserProblem: attempt to update a user problem with a non existent field'; +is( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + non_existent_field => 1 + } + ); + }, + check_isa('DBIx::Class::Exception'), + 'updateUserProblem: attempt to update a user problem with a non existent field' +); # Attempt to update a new User Problem with a non existent field -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - non_existent_field => 1 - } - ); -} -qr/No such column 'non_existent_field'/, - 'updateUserProblem: attempt to update a user problem with a non existent field'; +like( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + non_existent_field => 1 + } + ); + }, + qr/No such column 'non_existent_field'/, + 'updateUserProblem: attempt to update a user problem with a non existent field' +); # Attempt to update a new User Problem with invalid library id -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - problem_params => { - library_id => -1234 +like( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + problem_params => { + library_id => -1234 + } } - } - ); -} -qr/library_id is not valid/, 'updateUserProblem: attempt to update a user problem with with invalid library id'; + ); + }, + qr/library_id is not valid/, + 'updateUserProblem: attempt to update a user problem with with invalid library id' +); # Attempt to update a new User Problem with a bad problem weight -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - problem_params => { - weight => -1 +like( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + problem_params => { + weight => -1 + } } - } - ); -} -qr/weight is not valid/, 'updateUserProblem: attempt to update a user problem with a bad problem weight'; + ); + }, + qr/weight is not valid/, + 'updateUserProblem: attempt to update a user problem with a bad problem weight' +); # Attempt to add a new User Problem with a invalid problem_pool_id -throws_ok { - $user_problem_rs->updateUserProblem( - info => { - course_name => 'Precalculus', - set_name => 'HW #4', - username => 'ned', - problem_number => 2 - }, - params => { - problem_params => { - problem_pool_id => 'fred' +like( + dies { + $user_problem_rs->updateUserProblem( + info => { + course_name => 'Precalculus', + set_name => 'HW #4', + username => 'ned', + problem_number => 2 + }, + params => { + problem_params => { + problem_pool_id => 'fred' + } } - } - ); -} -qr/problem_pool_id is not valid/, 'updateUserProblem: attempt to update a user problem with a bad problem_pool_id'; + ); + }, + qr/problem_pool_id is not valid/, + 'updateUserProblem: attempt to update a user problem with a bad problem_pool_id' +); # Get an array of user problems for a single user in a course. @@ -712,8 +760,11 @@ for my $user_problem (@user_problems) { my @course_user_problems_from_csv = grep { $_->{course_name} eq 'Precalculus' && $_->{username} eq 'homer' } @user_problems_from_csv; -is_deeply(\@course_user_problems_from_csv, - \@user_problems, 'getCourseUserProblems: get all user problems for a single user in a course'); +is( + \@user_problems, + \@course_user_problems_from_csv, + 'getCourseUserProblems: get all user problems for a single user in a course' +); # Delete a User Problem @@ -724,7 +775,7 @@ $user_problem_to_delete->{status} += 0; delete $user_problem_to_delete->{problem_version} unless defined $user_problem_to_delete->{problem_version}; -is_deeply($user_problem1, $user_problem_to_delete, 'deleteUserProblem: delete a single user problem'); +is($user_problem_to_delete, $user_problem1, 'deleteUserProblem: delete a single user problem'); # Delete a user problem and return as a merged problem. @@ -733,68 +784,79 @@ removeIDs($user_problem_to_delete2); # the status needs be returned to a numerical value. $user_problem_to_delete2->{status} += 0; -is_deeply($problem2, $user_problem_to_delete2, - 'updateUserProblem: sucessfully update a field and return as a merged problem'); +is($user_problem_to_delete2, $problem2, 'updateUserProblem: sucessfully update a field and return as a merged problem'); # Attempt to delete a UserProblem to a non-existent course. -throws_ok { - $user_problem_rs->deleteUserProblem( - info => { - course_name => 'course doesn\'t exist', - username => 'homer', - set_name => 'HW #1', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::CourseNotFound', 'deleteUserProblem: attempt to delete a user problem to a nonexistent course'; +is( + dies { + $user_problem_rs->deleteUserProblem( + info => { + course_name => 'course doesn\'t exist', + username => 'homer', + set_name => 'HW #1', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::CourseNotFound'), + 'deleteUserProblem: attempt to delete a user problem to a nonexistent course' +); # Attempt to delete a UserProblem for a non-existent problem set. -throws_ok { - $user_problem_rs->deleteUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #999', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::SetNotInCourse', 'deleteUserProblem: attempt to delete a user problem for a non-existent problem set'; +is( + dies { + $user_problem_rs->deleteUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #999', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::SetNotInCourse'), + 'deleteUserProblem: attempt to delete a user problem for a non-existent problem set' +); # Attempt to delete a UserProblem for a non-existent user. -throws_ok { - $user_problem_rs->deleteUserProblem( - info => { - course_name => 'Precalculus', - username => 'non_existent_user', - set_name => 'HW #1', - problem_number => 1 - }, - params => {} - ); -} -'DB::Exception::UserNotFound', 'deleteUserProblem: attempt to delete a user problem for a non-existent user'; +is( + dies { + $user_problem_rs->deleteUserProblem( + info => { + course_name => 'Precalculus', + username => 'non_existent_user', + set_name => 'HW #1', + problem_number => 1 + }, + params => {} + ); + }, + check_isa('DB::Exception::UserNotFound'), + 'deleteUserProblem: attempt to delete a user problem for a non-existent user' +); # Attempt to delete a UserProblem for a non-existent problem. -throws_ok { - $user_problem_rs->deleteUserProblem( - info => { - course_name => 'Precalculus', - username => 'homer', - set_name => 'HW #1', - problem_number => 99 - }, - params => {} - ); -} -'DB::Exception::SetProblemNotFound', 'deleteUserProblem: attempt to delete a user problem for a non-existent problem'; +is( + dies { + $user_problem_rs->deleteUserProblem( + info => { + course_name => 'Precalculus', + username => 'homer', + set_name => 'HW #1', + problem_number => 99 + }, + params => {} + ); + }, + check_isa('DB::Exception::SetProblemNotFound'), + 'deleteUserProblem: attempt to delete a user problem for a non-existent problem' +); # Ensure that the user_problems table is restored. @all_user_problems_from_db = $user_problem_rs->getAllUserProblems(); @@ -806,7 +868,6 @@ for my $user_problem (@all_user_problems_from_db) { @all_user_problems_from_db = sort user_prob_sort_fxn @all_user_problems_from_db; -is_deeply(\@user_problems_from_csv, \@all_user_problems_from_db, - 'check: Ensure that the set_problems table is restored.'); +is(\@all_user_problems_from_db, \@user_problems_from_csv, 'check: Ensure that the set_problems table is restored.'); done_testing; diff --git a/t/db/011_attempts.t b/t/db/011_attempts.t index 6b599915..05152764 100644 --- a/t/db/011_attempts.t +++ b/t/db/011_attempts.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl -# + # This tests the basic database CRUD functions of attempts. -# + use warnings; use strict; @@ -14,14 +14,11 @@ BEGIN { use lib "$main::ww3_dir/lib"; use lib "$main::ww3_dir/t/lib"; -use Test::More; -use Test::Exception; -use Try::Tiny; +use Test2::V0; use YAML::XS qw/LoadFile/; use DB::Schema; -use TestUtils qw/loadCSV removeIDs loadSchema/; -use DB::Utils qw/updateAllFields/; +use TestUtils qw/removeIDs/; # Set up the database. my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; @@ -85,7 +82,7 @@ my $attempt_params1 = { my $attempt1 = $attempt_rs->addAttempt(params => { %$user_problem_info, %$attempt_params1 }); removeIDs($attempt1); -is_deeply($attempt_params1, $attempt1, 'addAttempt: add an attempt'); +is($attempt1, $attempt_params1, 'addAttempt: add an attempt'); my $attempt_params2 = { scores => [ 0, 1, 1 ], @@ -95,7 +92,7 @@ my $attempt_params2 = { my $attempt2 = $attempt_rs->addAttempt(params => { %$user_problem_info, %$attempt_params2 }); removeIDs($attempt2); -is_deeply($attempt_params2, $attempt2, 'addAttempt: add another attempt'); +is($attempt2, $attempt_params2, 'addAttempt: add another attempt'); my $attempt_params3 = { scores => [ 0, 0, 0 ], @@ -105,14 +102,17 @@ my $attempt_params3 = { my $attempt3 = $attempt_rs->addAttempt(params => { %$user_problem_info, %$attempt_params3 }); removeIDs($attempt3); -is_deeply($attempt_params3, $attempt3, 'addAttempt: add yet another attempt'); +is($attempt3, $attempt_params3, 'addAttempt: add yet another attempt'); my @all_attempts = $attempt_rs->getAttempts(info => $user_problem_info); for my $attempt (@all_attempts) { removeIDs($attempt); } -is_deeply([ $attempt_params1, $attempt_params2, $attempt_params3 ], - \@all_attempts, "getAttempts: get attempts for a user problem;"); +is( + \@all_attempts, + [ $attempt_params1, $attempt_params2, $attempt_params3 ], + "getAttempts: get attempts for a user problem;" +); done_testing; diff --git a/t/db/012_set_versions.t b/t/db/012_set_versions.t index 283238f8..e5fac2cb 100644 --- a/t/db/012_set_versions.t +++ b/t/db/012_set_versions.t @@ -14,8 +14,7 @@ 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 Clone qw/clone/; @@ -148,7 +147,7 @@ my $user_set1_from_csv = ( # Make a new user set that has a set_version of 1 -is_deeply($user_set1_from_csv, $user_set1, 'getUserSet: get a single user set from a course.'); +is($user_set1, $user_set1_from_csv, 'getUserSet: get a single user set from a course.'); my $user_set1_v1_params = clone $user_set1; $user_set1_v1_params->{set_version} = 1; @@ -157,7 +156,7 @@ my $user_set1_v1 = $user_set_rs->addUserSet(params => { %$user_set_info1, %$user removeIDs($user_set1_v1); cleanUndef($user_set1_v1); -is_deeply($user_set1_v1, $user_set1_v1_params, "addUserSet: add a user set with version =1 "); +is($user_set1_v1, $user_set1_v1_params, "addUserSet: add a user set with version =1 "); # Make a new user set that has a set_version of 2 @@ -168,7 +167,7 @@ my $user_set1_v2 = $user_set_rs->addUserSet(params => { %$user_set_info1, %$user removeIDs($user_set1_v2); cleanUndef($user_set1_v2); -is_deeply($user_set1_v2, $user_set1_v2_params, "addUserSet: add a user set with version = 2."); +is($user_set1_v2, $user_set1_v2_params, "addUserSet: add a user set with version = 2."); my @all_user_set_versions = $user_set_rs->getUserSetVersions(info => $user_set_info1); for my $user_set (@all_user_set_versions) { @@ -176,7 +175,7 @@ for my $user_set (@all_user_set_versions) { cleanUndef($user_set); } -is_deeply( +is( \@all_user_set_versions, [ $user_set1, $user_set1_v1, $user_set1_v2 ], 'getUserSetVersions: get all versions of a user set.' @@ -195,7 +194,7 @@ my $user_set_v1_to_delete = $user_set_rs->deleteUserSet( removeIDs($user_set_v1_to_delete); cleanUndef($user_set_v1_to_delete); -is_deeply($user_set_v1_to_delete, $user_set1_v1, 'deleteUserSet: delete user set with set_version = 1'); +is($user_set_v1_to_delete, $user_set1_v1, 'deleteUserSet: delete user set with set_version = 1'); my $user_set_v2_to_delete = $user_set_rs->deleteUserSet( info => { @@ -208,7 +207,7 @@ my $user_set_v2_to_delete = $user_set_rs->deleteUserSet( removeIDs($user_set_v2_to_delete); cleanUndef($user_set_v2_to_delete); -is_deeply($user_set_v2_to_delete, $user_set1_v2, 'deleteUserSet: delete a versioned user set'); +is($user_set_v2_to_delete, $user_set1_v2, 'deleteUserSet: delete a versioned user set'); # Ensure that the user_sets table is restored. my @all_user_sets_from_db = $user_set_rs->getAllUserSets(merged => 1); @@ -224,6 +223,6 @@ for my $set (@all_user_sets_from_db) { @all_user_sets_from_db = sort { $a->{course_name} cmp $b->{course_name} || $a->{set_name} cmp $b->{set_name} } @all_user_sets_from_db; -is_deeply(\@all_user_sets_from_db, \@merged_user_sets, 'check: Ensure that the user_sets table is restored.'); +is(\@all_user_sets_from_db, \@merged_user_sets, 'check: Ensure that the user_sets table is restored.'); done_testing; diff --git a/t/db/013_problem_versions.t b/t/db/013_problem_versions.t index 79c06a5c..90101d15 100644 --- a/t/db/013_problem_versions.t +++ b/t/db/013_problem_versions.t @@ -14,8 +14,7 @@ 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 Clone qw/clone/; @@ -102,7 +101,7 @@ my $user_problem1_from_csv = clone( # the status needs be returned to a numerical value. $user_problem1->{status} += 0; -is_deeply($user_problem1_from_csv, $user_problem1, 'getUserProblem: get a single user problem from a course.'); +is($user_problem1, $user_problem1_from_csv, 'getUserProblem: get a single user problem from a course.'); # Make a new user problem that has a problem_version of 2 @@ -112,7 +111,7 @@ $user_problem1_v2_params->{problem_version} = 2; my $user_problem1_v2 = $user_problem_rs->addUserProblem(params => { %$user_problem_info, %$user_problem1_v2_params }); removeIDs($user_problem1_v2); -is_deeply($user_problem1_v2_params, $user_problem1_v2, "addUserProblem: add a user problem with version =2 "); +is($user_problem1_v2, $user_problem1_v2_params, "addUserProblem: add a user problem with version =2 "); # Make a new user set that has a set_version of 3 @@ -122,7 +121,7 @@ $user_problem1_v3_params->{problem_version} = 3; my $user_problem1_v3 = $user_problem_rs->addUserProblem(params => { %$user_problem_info, %$user_problem1_v3_params }); removeIDs($user_problem1_v3); -is_deeply($user_problem1_v3_params, $user_problem1_v3, "addUserProblem: add a user problem with version =3 "); +is($user_problem1_v3, $user_problem1_v3_params, "addUserProblem: add a user problem with version =3 "); my @all_user_problem_versions = $user_problem_rs->getUserProblemVersions(info => $user_problem_info); @@ -131,7 +130,7 @@ for my $user_problem (@all_user_problem_versions) { $user_problem->{status} += 0; } -is_deeply( +is( \@all_user_problem_versions, [ $user_problem1, $user_problem1_v2, $user_problem1_v3 ], 'getUserProblemVersions: get all versions of a user problem' @@ -151,7 +150,7 @@ my $user_problem_v2_to_delete = $user_problem_rs->deleteUserProblem( removeIDs($user_problem_v2_to_delete); $user_problem_v2_to_delete->{status} += 0; -is_deeply($user_problem_v2_to_delete, $user_problem1_v2, 'deleteUserProblem: delete a versioned user problem'); +is($user_problem_v2_to_delete, $user_problem1_v2, 'deleteUserProblem: delete a versioned user problem'); my $user_problem_v3_to_delete = $user_problem_rs->deleteUserProblem( info => { @@ -165,7 +164,7 @@ my $user_problem_v3_to_delete = $user_problem_rs->deleteUserProblem( removeIDs($user_problem_v3_to_delete); $user_problem_v3_to_delete->{status} += 0; -is_deeply($user_problem_v3_to_delete, $user_problem1_v3, 'deleteUserProblem: delete another versioned user problem'); +is($user_problem_v3_to_delete, $user_problem1_v3, 'deleteUserProblem: delete another versioned user problem'); # Ensure that the user_problems table is restored. my @all_user_problems_from_db = $user_problem_rs->getAllUserProblems(); @@ -177,6 +176,6 @@ for my $user_problem (@all_user_problems_from_db) { # For comparision make sure the loaded status are printed to 5 digits. $_->{status} += 0 for (@all_user_problems_from_db); -is_deeply(\@user_problems_from_csv, \@all_user_problems_from_db, 'check: ensure that user_problems table is restored.'); +is(\@all_user_problems_from_db, \@user_problems_from_csv, 'check: ensure that user_problems table is restored.'); done_testing; diff --git a/t/db/README.md b/t/db/README.md index d5b83338..db7a4a41 100644 --- a/t/db/README.md +++ b/t/db/README.md @@ -19,7 +19,7 @@ each test. ## Note -If you get an error, try rerunning steps 2 and 3 above. This rebuids the database +If you get an error, try rerunning steps 2 and 3 above. This rebuilds the database and reruns all of the tests. ## To do diff --git a/t/db/test_course_user.pl b/t/db/test_course_user.pl deleted file mode 100755 index bd9c2fd6..00000000 --- a/t/db/test_course_user.pl +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env perl - -use warnings; -use strict; - -BEGIN { - use File::Basename qw/dirname/; - use Cwd qw/abs_path/; - $main::test_dir = abs_path(dirname(__FILE__)); - $main::lib_dir = dirname(dirname($main::test_dir)) . '/lib'; -} - -use lib "$main::lib_dir"; - -use Text::CSV qw/csv/; -use List::Util qw(uniq); -use Test::More; -use Test::Exception; -use Try::Tiny; - -use DB::WithParams; -use DB::WithDates; -use DB::Schema; -use TestUtils qw/loadCSV removeIDs/; - -# load the database -my $db_file = "$main::test_dir/sample_db.sqlite"; -my $schema = DB::Schema->connect("dbi:SQLite:$db_file"); - -my $course_user_rs = $schema->resultset('CourseUser'); - -my $u = $course_user_rs->find({ course_id => 1, user_id => 1 }); - -my $u2 = $u->get_inflated_columns; - diff --git a/t/mojolicious/001_login.t b/t/mojolicious/001_login.t index 6efa4da1..7d3e3d44 100644 --- a/t/mojolicious/001_login.t +++ b/t/mojolicious/001_login.t @@ -2,11 +2,10 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; -use YAML::XS qw/LoadFile/; - +use Test2::V0; +use Test2::MojoX; use YAML::XS qw/LoadFile/; +use Mojo::JSON qw/true false/; BEGIN { use File::Basename qw/dirname/; @@ -20,7 +19,7 @@ use lib "$main::ww3_dir/lib"; my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; $config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file); -my $t = Test::Mojo->new('WeBWorK3' => LoadFile($config_file)); +my $t = Test2::MojoX->new('WeBWorK3' => LoadFile($config_file)); # Test missing credentials $t->post_ok('/webwork3/api/login')->status_is(500, 'error status')->content_type_is('application/json;charset=UTF-8') @@ -36,11 +35,11 @@ $t->post_ok('/webwork3/api/login')->status_is(500, 'error status')->content_type $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) ->content_type_is('application/json;charset=UTF-8')->json_is( '' => { - logged_in => 1, + logged_in => true, user => { email => 'lisa@google.com', first_name => 'Lisa', - is_admin => 0, + is_admin => false, last_name => 'Simpson', student_id => '23', user_id => 3, @@ -53,7 +52,7 @@ $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => ' # Test logout $t->post_ok('/webwork3/api/logout')->status_is(200)->content_type_is('application/json;charset=UTF-8')->json_is( '' => { - logged_in => 0, + logged_in => false, message => 'Successfully logged out.' }, 'logout' @@ -63,7 +62,7 @@ $t->post_ok('/webwork3/api/logout')->status_is(200)->content_type_is('applicatio $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'wrong_password' })->status_is(200) ->content_type_is('application/json;charset=UTF-8')->json_is( '' => { - logged_in => 0, + logged_in => false, message => 'Incorrect username or password.' }, 'invalid credentials' diff --git a/t/mojolicious/002_courses.t b/t/mojolicious/002_courses.t index bf84f35a..e0f07a74 100644 --- a/t/mojolicious/002_courses.t +++ b/t/mojolicious/002_courses.t @@ -2,8 +2,8 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; BEGIN { @@ -24,18 +24,18 @@ my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; $config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file); my $config = clone(LoadFile($config_file)); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Authenticate with the admin user. $t->post_ok('/webwork3/api/login' => json => { username => 'admin', password => 'admin' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1)->json_is('/user/user_id' => 1) - ->json_is('/user/is_admin' => 1); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true)->json_is('/user/user_id' => 1) + ->json_is('/user/is_admin' => true); $t->get_ok('/webwork3/api/courses')->content_type_is('application/json;charset=UTF-8') - ->json_is('/0/course_name' => 'Precalculus')->json_is('/0/visible' => 1); + ->json_is('/0/course_name' => 'Precalculus')->json_is('/0/visible' => true); $t->get_ok('/webwork3/api/courses/1')->content_type_is('application/json;charset=UTF-8') - ->json_is('/course_name' => 'Precalculus')->json_is('/visible' => 1); + ->json_is('/course_name' => 'Precalculus')->json_is('/visible' => true); # Add a new course my $new_course = { @@ -52,14 +52,14 @@ my $new_course_id = $t->tx->res->json('/course_id'); $new_course->{course_id} = $new_course_id; # The default for visible is true: $new_course->{visible} = true; -is_deeply($new_course, $t->tx->res->json, "addCourse: courses match"); +is($t->tx->res->json, $new_course, "addCourse: courses match"); # Update the course $new_course->{visible} = true; $t->put_ok("/webwork3/api/courses/$new_course_id" => json => $new_course)->status_is(200) ->json_is('/course_name' => $new_course->{course_name}); -is_deeply($new_course, $t->tx->res->json, 'updateCourse: courses match'); +is($t->tx->res->json, $new_course, 'updateCourse: courses match'); # Testing that booleans returned from the server are JSON booleans. # getting the first course @@ -124,8 +124,8 @@ $t->get_ok('/webwork3/api/courses/4/settings')->status_is(200)->content_type_is( $t->post_ok('/webwork3/api/courses' => json => $new_course)->status_is(403)->json_is('/has_permission' => 0); $t->put_ok('/webwork3/api/courses/4' => json => { course_name => 'XXX' })->status_is(403) - ->json_is('/has_permission' => false); + ->json_is('/has_permission' => 0); -$t->delete_ok('/webwork3/api/courses/4')->status_is(403)->json_is('/has_permission' => false); +$t->delete_ok('/webwork3/api/courses/4')->status_is(403)->json_is('/has_permission' => 0); done_testing; diff --git a/t/mojolicious/003_users.t b/t/mojolicious/003_users.t index 1e2ded44..949a2638 100644 --- a/t/mojolicious/003_users.t +++ b/t/mojolicious/003_users.t @@ -2,8 +2,8 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; BEGIN { @@ -33,13 +33,13 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Test all of the user routes with an admin user. $t->post_ok('/webwork3/api/login' => json => { username => 'admin', password => 'admin' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1)->json_is('/user/user_id' => 1) - ->json_is('/user/is_admin' => 1); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true)->json_is('/user/user_id' => 1) + ->json_is('/user/is_admin' => true); my @all_users = $schema->resultset('User')->getAllGlobalUsers(); @@ -56,7 +56,7 @@ my $new_user = { last_name => 'Simpson', username => 'maggie', student_id => '1234123423', - is_admin => 0 + is_admin => false }; $t->post_ok('/webwork3/api/users' => json => $new_user)->status_is(200) @@ -66,12 +66,12 @@ $t->post_ok('/webwork3/api/users' => json => $new_user)->status_is(200) my $new_user_from_db = $t->tx->res->json; $new_user->{user_id} = $new_user_from_db->{user_id}; -is_deeply($new_user, $new_user_from_db, 'addUser: global user added.'); +is($new_user_from_db, $new_user, 'addUser: global user added.'); # Update the user. $new_user->{email} = 'maggie@juno.com'; $t->put_ok("/webwork3/api/users/$new_user->{user_id}" => json => $new_user)->status_is(200); -is_deeply($new_user, $t->tx->res->json, 'updateUser: global user updated'); +is($t->tx->res->json, $new_user, 'updateUser: global user updated'); # Add the user to the course. my $added_user_to_course = { @@ -91,7 +91,7 @@ $t->get_ok('/webwork3/api/courses/1/users/lisa/exists')->status_is(200) $t->get_ok('/webwork3/api/courses/1/users/non_existent_user/exists')->status_is(200) ->content_type_is('application/json;charset=UTF-8'); -is_deeply($t->tx->res->json, {}, 'checkUserExists: check that a non-existent user returns {}'); +is($t->tx->res->json, {}, 'checkUserExists: check that a non-existent user returns {}'); # Testing that booleans returned from the server are JSON booleans. # the first user is the admin @@ -158,10 +158,10 @@ $t->delete_ok("/webwork3/api/users/$another_new_user_id")->status_is(200) # Test that a non-admin user cannot access all of the routes # Logout the admin user and relogin as a non-admin. -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1) - ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => 0); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true) + ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => false); $t->get_ok('/webwork3/api/users')->content_type_is('application/json;charset=UTF-8')->status_is(403) ->json_is('/has_permission' => 0); @@ -182,7 +182,7 @@ $t->delete_ok('/webwork3/api/users/1')->content_type_is('application/json;charse $t->get_ok('/webwork3/api/users/3/courses')->status_is(200)->content_type_is('application/json;charset=UTF-8'); # Relogin as the admin and delete the added users -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'admin', password => 'admin' })->status_is(200); # The following routes test that global users can be handled by an instructor in the course diff --git a/t/mojolicious/004_course_users.t b/t/mojolicious/004_course_users.t index 49f44f17..4551269c 100644 --- a/t/mojolicious/004_course_users.t +++ b/t/mojolicious/004_course_users.t @@ -2,8 +2,8 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; BEGIN { @@ -33,7 +33,7 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Login as an instructor in the Arithmetic course (course_id: 4) $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) diff --git a/t/mojolicious/005_problem_sets.t b/t/mojolicious/005_problem_sets.t index ecb16982..60528298 100644 --- a/t/mojolicious/005_problem_sets.t +++ b/t/mojolicious/005_problem_sets.t @@ -2,12 +2,10 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; -use DateTime::Format::Strptime; - BEGIN { use File::Basename qw/dirname/; use Cwd qw/abs_path/; @@ -29,8 +27,6 @@ my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; $config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file); my $config = clone(LoadFile($config_file)); -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); - # Connect to the database. my $schema = DB::Schema->connect( $config->{database_dsn}, @@ -38,7 +34,7 @@ my $schema = DB::Schema->connect( $config->{database_password}, { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Login as an instructor. Lisa is an instructor in course_id: 4 (Arithmetic) $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) diff --git a/t/mojolicious/006_quizzes.t b/t/mojolicious/006_quizzes.t index be15d571..0a31ff27 100644 --- a/t/mojolicious/006_quizzes.t +++ b/t/mojolicious/006_quizzes.t @@ -2,12 +2,10 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; -use DateTime::Format::Strptime; - BEGIN { use File::Basename qw/dirname/; use Cwd qw/abs_path/; @@ -29,8 +27,6 @@ my $config_file = "$main::ww3_dir/conf/webwork3-test.yml"; $config_file = "$main::ww3_dir/conf/webwork3-test.dist.yml" unless (-e $config_file); my $config = clone(LoadFile($config_file)); -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); - # Connect to the database. my $schema = DB::Schema->connect( $config->{database_dsn}, @@ -39,7 +35,7 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Login as an user with instructor privileges in a course (Arithmetic; course_id: 4) $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) diff --git a/t/mojolicious/007_review_sets.t b/t/mojolicious/007_review_sets.t index 3f1839f4..86b7dc88 100644 --- a/t/mojolicious/007_review_sets.t +++ b/t/mojolicious/007_review_sets.t @@ -2,8 +2,8 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; BEGIN { @@ -18,10 +18,8 @@ use lib "$main::ww3_dir/t/lib"; use DB::Schema; use Clone qw/clone/; use YAML::XS qw/LoadFile/; -use DateTime::Format::Strptime; use TestUtils qw/loadCSV/; -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); # Test the api with common "users" routes. # Load the config file. @@ -37,7 +35,7 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Login as an user with instructor privileges in a course (Arithmetic; course_id: 4) $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) diff --git a/t/mojolicious/008_problems.t b/t/mojolicious/008_problems.t index 72fbb83c..88620c59 100644 --- a/t/mojolicious/008_problems.t +++ b/t/mojolicious/008_problems.t @@ -2,12 +2,10 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; -use DateTime::Format::Strptime; - BEGIN { use File::Basename qw/dirname/; use Cwd qw/abs_path/; @@ -37,12 +35,12 @@ my $schema = DB::Schema->connect( $config->{database_password}, { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # First run tests as logged in as an instructor $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1) - ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => 0); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true) + ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => false); # Load all problems from the CVS files. my @problems_from_csv = loadCSV( @@ -80,7 +78,7 @@ $t->get_ok('/webwork3/api/courses/4/problems')->status_is(200)->content_type_is( my $problems_from_db = $t->tx->res->json; for my $problem (@$problems_from_db) { removeIDs($problem); } -is_deeply(\@arith_problems, $problems_from_db, 'getGlobalProblems: get all problems'); +is($problems_from_db, \@arith_problems, 'getGlobalProblems: get all problems'); # Add a new problem to a set. @@ -111,10 +109,10 @@ $t->put_ok( ->json_is('/problem_number' => $new_problem->{problem_number})->json_is('/problem_params/weight' => 3); # Make sure that a student cannot access the global problem routes -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'ralph', password => 'ralph' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1) - ->json_is('/user/username' => 'ralph')->json_is('/user/is_admin' => 0); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true) + ->json_is('/user/username' => 'ralph')->json_is('/user/is_admin' => false); my $logged_in_user = $t->tx->res->json('/user'); @@ -157,10 +155,10 @@ $t->delete_ok("/webwork3/api/courses/4/sets/$hw1->{set_id}/problems/$new_problem # Make sure that a student not enrolled in the course has access to getting global problems # for that course. -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'ned', password => 'ned' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1)->json_is('/user/username' => 'ned') - ->json_is('/user/is_admin' => 0); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true) + ->json_is('/user/username' => 'ned')->json_is('/user/is_admin' => false); $logged_in_user = $t->tx->res->json('/user'); @@ -174,7 +172,7 @@ is(scalar(grep { $_->{course_name} eq 'Arithmetic' } @$user_courses), 0, 'The us $t->get_ok('/webwork3/api/courses/4/problems')->status_is(403)->content_type_is('application/json;charset=UTF-8'); # Finally, delete the new problem to restore the db to it's pretest state. -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'admin', password => 'admin' })->status_is(200); $t->delete_ok("/webwork3/api/courses/4/sets/$hw1->{set_id}/problems/$new_problem->{set_problem_id}")->status_is(200) diff --git a/t/mojolicious/009_user_problems.t b/t/mojolicious/009_user_problems.t index 7066715a..6af18b9e 100644 --- a/t/mojolicious/009_user_problems.t +++ b/t/mojolicious/009_user_problems.t @@ -2,12 +2,10 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; use Mojo::JSON qw/true false/; -use DateTime::Format::Strptime; - BEGIN { use File::Basename qw/dirname/; use Cwd qw/abs_path/; @@ -37,12 +35,12 @@ my $schema = DB::Schema->connect( $config->{database_password}, { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # First run tests as logged in as an instructor $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) - ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => 1) - ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => 0); + ->content_type_is('application/json;charset=UTF-8')->json_is('/logged_in' => true) + ->json_is('/user/username' => 'lisa')->json_is('/user/is_admin' => false); # Load all problems from the CVS files. my @problems_from_csv = loadCSV( @@ -95,7 +93,7 @@ $t->get_ok('/webwork3/api/courses/4/problems')->status_is(200)->content_type_is( my $problems_from_db = $t->tx->res->json; for my $problem (@$problems_from_db) { removeIDs($problem); } -is_deeply(\@arith_problems, $problems_from_db, 'getGlobalProblems: get all problems'); +is($problems_from_db, \@arith_problems, 'getGlobalProblems: get all problems'); # Get all user problems for a single set. @@ -113,7 +111,7 @@ $_->{status} += 0 for (@$user_problems_from_db); my @user_problems_from_db = sort { $a->{username} cmp $b->{username} || $a->{problem_number} <=> $b->{problem_number} } @$user_problems_from_db; -is_deeply(\@user_problems_from_db, \@arith_user_problems, 'getUserProblems: get all problems for a set in a course.'); +is(\@user_problems_from_db, \@arith_user_problems, 'getUserProblems: get all problems for a set in a course.'); # Get all user problems in a course for a single user. Find user 'ralph' @@ -130,8 +128,7 @@ my @ralph_user_problems_from_file = grep { $_->{username} eq 'ralph' } @arith_us # For comparision make sure the loaded status are printed to 5 digits. $_->{status} += 0 for (@$ralph_user_problems); -is_deeply(\@ralph_user_problems_from_file, - $ralph_user_problems, 'getUserProblems: get all problems for a set in a course.'); +is($ralph_user_problems, \@ralph_user_problems_from_file, 'getUserProblems: get all problems for a set in a course.'); # New to make a new problem first @@ -174,7 +171,7 @@ $t->put_ok( ->json_is('/seed' => 789); # Check that a student has the correct access -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'ralph', password => 'ralph' })->status_is(200); # get all of ralph's problems @@ -208,7 +205,7 @@ $t->delete_ok( ->status_is(403); # Make sure that a user that is in the course, cannot get or update a user problem that is not one's own. -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'moe', password => 'moe' })->status_is(200); # Check that moe is in the course @@ -233,7 +230,7 @@ $t->put_ok( => json => { status => 0.5 })->status_is(403); # Switch back to the instructor and delete the user problem -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200); $t->delete_ok( diff --git a/t/mojolicious/010_problem_pools.t b/t/mojolicious/010_problem_pools.t index 83e3e992..56a8f9a0 100644 --- a/t/mojolicious/010_problem_pools.t +++ b/t/mojolicious/010_problem_pools.t @@ -2,8 +2,8 @@ use Mojo::Base -strict; -use Test::More; -use Test::Mojo; +use Test2::V0; +use Test2::MojoX; BEGIN { use File::Basename qw/dirname/; @@ -18,11 +18,9 @@ use DB::Schema; use Clone qw/clone/; use Mojo::JSON qw/true false/; use YAML::XS qw/LoadFile/; -use DateTime::Format::Strptime; use TestUtils qw/loadCSV removeIDs/; -my $strp = DateTime::Format::Strptime->new(pattern => '%FT%T', on_error => 'croak'); # Test the api with common "users" routes. # Load the config file. @@ -38,7 +36,7 @@ my $schema = DB::Schema->connect( { quote_names => 1 } ); -my $t = Test::Mojo->new(WeBWorK3 => $config); +my $t = Test2::MojoX->new(WeBWorK3 => $config); # Login as an user with instructor privileges in a course (Arithmetic; course_id: 4) $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200) @@ -79,7 +77,7 @@ for my $pool (@$arith_pools) { } # my @sorted_arith_pools = sort { $a->{pool_name} cmp $b->{pool_name} } @$arith_pools; -is_deeply($arith_pools, \@arith_problem_pools, 'getProblemPools: get problem pools from one course'); +is($arith_pools, \@arith_problem_pools, 'getProblemPools: get problem pools from one course'); $t->get_ok("/webwork3/api/courses/4/pools/$arith_pools_from_db[0]->{problem_pool_id}")->status_is(200) ->json_is('/pool_name' => $arith_problem_pools[0]->{pool_name}); @@ -150,7 +148,7 @@ $t->put_ok( )->status_is(200)->content_type_is('application/json;charset=UTF-8')->json_is('/params/library_id' => 8932); # Make sure that students don't have access to Problem Pools -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'ralph', password => 'ralph' })->status_is(200); $t->get_ok('/webwork3/api/courses/4/pools')->status_is(403); @@ -189,7 +187,7 @@ $t->delete_ok( )->status_is(403); # Cleanup. Log back in as the instructor and delete added pool and problem -$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => 0); +$t->post_ok('/webwork3/api/logout')->status_is(200)->json_is('/logged_in' => false); $t->post_ok('/webwork3/api/login' => json => { username => 'lisa', password => 'lisa' })->status_is(200); # Delete the pool problem.