Skip to content

Commit bde43c9

Browse files
authored
Merge pull request #61 from pstaabp/tests-on-separate-db
Tests on separate db
2 parents 8b760f7 + 835ba26 commit bde43c9

22 files changed

+24124
-132
lines changed

.github/workflows/unit-tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ jobs:
1717
env:
1818
HARNESS_PERL_SWITCHES: -MDevel::Cover
1919
run: |
20-
sed 's/\/opt\/webwork\//\/__w\/webwork3\//g' conf/webwork3.dist.yml > conf/webwork3.yml
20+
cp conf/webwork3.dist.yml conf/webwork3.yml
21+
cp conf/ww3-dev.dist.yml conf/ww3-dev.yml
2122
perl t/db/build_db.pl
2223
prove -r t
2324

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pnpm-debug.log*
2727
# Project files
2828
sample_db.sqlite
2929
conf/webwork3.yml
30+
conf/ww3-dev.yml
3031
conf/apache2/webwork3-apache2.conf
3132
conf/apache2/webwork3.service
3233
conf/apache2/renderer.service

conf/webwork3.dist.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
---
22
secrets:
33
- 3cdf63327fcf77deaed1d200df4b9fee66af2326
4-
webwork3_home: /opt/webwork/webwork3
5-
6-
# If ignore_permissions is set to true, all routes can be executed.
7-
# This should only be used in development.
8-
ignore_permissions: true
4+
webwork3_home: .
95

106
# Database settings
117

128
# For the sqlite database
13-
database_dsn: dbi:SQLite:/opt/webwork/webwork3/t/db/sample_db.sqlite
9+
database_dsn: dbi:SQLite:./t/db/sample_db.sqlite
1410
# For mysql or mariadb
1511
#database_dsn: dbi:mysql:dbname=webwork3
1612

conf/ww3-dev.dist.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
secrets:
3+
- 3cdf63327fcf77deaed1d200df4b9fee66af2326
4+
webwork3_home: .
5+
6+
# Database settings
7+
8+
# These settings are used for running unit_tests. You should choose a database
9+
# that is separate from any either production or other testing database. The
10+
# sqlite one is recommended. If you choose another full-featured DB, select a
11+
# database that is different than others.
12+
13+
# For the sqlite database
14+
database_dsn: dbi:SQLite:./t/db/sample_db.sqlite
15+
# For mysql or mariadb
16+
# note: choose a database
17+
#database_dsn: dbi:mysql:dbname=webwork3_test
18+
19+
# Database credentials for mysql or mariadb.
20+
# These are ignored if the 'sqlite' database is used.
21+
database_user: webworkWrite
22+
database_password: password

lib/DB/TestUtils.pm

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,4 @@ sub filterBySetType {
7979
return @filtered_sets;
8080
}
8181

82-
sub loadSchema {
83-
# load some configuration for the database:
84-
85-
my $config = LoadFile("$main::lib_dir/../conf/webwork3.yml");
86-
87-
# Load the database
88-
return DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
89-
}
90-
9182
1;

super-linter.log

Lines changed: 23890 additions & 0 deletions
Large diffs are not rendered by default.

t/db/001_courses.t

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use strict;
88
BEGIN {
99
use File::Basename qw/dirname/;
1010
use Cwd qw/abs_path/;
11-
$main::test_dir = abs_path(dirname(__FILE__));
12-
$main::lib_dir = dirname(dirname($main::test_dir)) . '/lib';
11+
$main::ww3_dir = abs_path(dirname(__FILE__)) . '/../..';
1312
}
1413

15-
use lib "$main::lib_dir";
14+
use lib "$main::ww3_dir/lib";
1615

1716
use List::MoreUtils qw(uniq);
1817

1918
use Test::More;
2019
use Test::Exception;
20+
use YAML::XS qw/LoadFile/;
2121

2222
use DB::WithParams;
2323
use DB::WithDates;
@@ -27,16 +27,22 @@ use DB::TestUtils qw/loadCSV removeIDs loadSchema/;
2727

2828
# load some configuration for the database:
2929

30-
my $schema = loadSchema();
30+
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
31+
die "The file $config_file does not exist. Did you make a copy of it from ww3-dev.dist.yml ?"
32+
unless (-e $config_file);
33+
34+
my $config = LoadFile($config_file);
35+
36+
my $schema =
37+
DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3138

3239
# $schema->storage->debug(1); # print out the SQL commands.
3340

3441
my $course_rs = $schema->resultset("Course");
3542

3643
## get a list of courses from the CSV file
3744

38-
my @courses = loadCSV("$main::test_dir/sample_data/courses.csv");
39-
45+
my @courses = loadCSV("$main::ww3_dir/t/db/sample_data/courses.csv");
4046
for my $course (@courses) {
4147
delete $course->{course_params};
4248
}
@@ -147,7 +153,7 @@ for my $user_course (@user_courses) {
147153
removeIDs($user_course);
148154
}
149155

150-
my @students = loadCSV("$main::test_dir/sample_data/students.csv");
156+
my @students = loadCSV("$main::ww3_dir/t/db/sample_data/students.csv");
151157

152158
my @user_courses_from_csv = grep { $_->{username} eq "lisa" } @students;
153159

t/db/002_course_settings.t

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ use strict;
88
BEGIN {
99
use File::Basename qw/dirname/;
1010
use Cwd qw/abs_path/;
11-
$main::test_dir = abs_path(dirname(__FILE__));
12-
$main::webwork_home = dirname(dirname($main::test_dir));
13-
$main::lib_dir = "$main::webwork_home/lib";
11+
$main::ww3_dir = abs_path(dirname(__FILE__)) . '/../..';
1412
}
1513

16-
use lib "$main::lib_dir";
14+
use lib "$main::ww3_dir/lib";
1715

18-
use Data::Dump qw/dd/;
16+
use Data::Dumper;
1917
use List::MoreUtils qw(uniq);
2018

2119
use Test::More;
@@ -27,14 +25,20 @@ use DB::WithParams;
2725
use DB::WithDates;
2826
use DB::Schema;
2927

30-
# use WeBWorK3::Utils::Settings qw/checkSettings/;
3128
use WeBWorK3::Utils::Settings qw/getDefaultCourseSettings getDefaultCourseValues
3229
validateSettingsConfFile validateSingleCourseSetting validateSettingConfig
3330
isInteger isTimeString isTimeDuration isDecimal mergeCourseSettings/;
3431

3532
use DB::TestUtils qw/loadCSV removeIDs loadSchema/;
3633

37-
my $schema = loadSchema();
34+
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
35+
die "The file $config_file does not exist. Did you make a copy of it from ww3-dev.dist.yml ?"
36+
unless (-e $config_file);
37+
38+
my $config = LoadFile($config_file);
39+
40+
my $schema =
41+
DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3842

3943
# $schema->storage->debug(1); # print out the SQL commands.
4044

t/db/003_users.t

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use strict;
88
BEGIN {
99
use File::Basename qw/dirname/;
1010
use Cwd qw/abs_path/;
11-
$main::test_dir = abs_path(dirname(__FILE__));
12-
$main::lib_dir = dirname(dirname($main::test_dir)) . '/lib';
11+
$main::ww3_dir = abs_path(dirname(__FILE__)) . '/../..';
1312
}
1413

15-
use lib "$main::lib_dir";
14+
use lib "$main::ww3_dir/lib";
1615

1716
use Text::CSV qw/csv/;
18-
use Data::Dump qw/dd/;
17+
use Data::Dumper;
1918
use Test::More;
2019
use Test::Exception;
2120
use Try::Tiny;
@@ -25,8 +24,18 @@ use DB::WithParams;
2524
use DB::WithDates;
2625
use DB::Schema;
2726
use DB::TestUtils qw/loadCSV removeIDs loadSchema/;
27+
use YAML qw/LoadFile/;
28+
29+
my $config;
30+
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
31+
if (-e $config_file) {
32+
$config = LoadFile($config_file);
33+
} else {
34+
die "The file $config_file does not exist. Did you make a copy of it from ww3-dev.dist.yml ?";
35+
}
2836

29-
my $schema = loadSchema();
37+
my $schema =
38+
DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
3039

3140
# $schema->storage->debug(1); # print out the SQL commands.
3241

@@ -37,7 +46,7 @@ my $maggie = $users_rs->find({ username => "maggie" });
3746
$maggie->delete if defined($maggie);
3847

3948
## get a list of users from the CSV file
40-
my @students = loadCSV("$main::test_dir/sample_data/students.csv");
49+
my @students = loadCSV("$main::ww3_dir/t/db/sample_data/students.csv");
4150

4251
# remove duplicates
4352
my %seen = ();

t/db/004_course_users.t

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ use strict;
88
BEGIN {
99
use File::Basename qw/dirname/;
1010
use Cwd qw/abs_path/;
11-
$main::test_dir = abs_path(dirname(__FILE__));
12-
$main::lib_dir = dirname(dirname($main::test_dir)) . '/lib';
11+
$main::ww3_dir = abs_path(dirname(__FILE__)) . '/../..';
1312
}
1413

15-
use lib "$main::lib_dir";
14+
use lib "$main::ww3_dir/lib";
1615

1716
use Text::CSV qw/csv/;
1817
use List::Util qw(uniq);
@@ -27,15 +26,23 @@ use DB::Schema;
2726
use DB::TestUtils qw/loadCSV removeIDs loadSchema/;
2827
use DB::Utils qw/removeLoginParams/;
2928

30-
my $schema = loadSchema();
29+
my $config_file = "$main::ww3_dir/conf/ww3-dev.yml";
30+
die "The file $config_file does not exist. Did you make a copy of it from ww3-dev.dist.yml ?"
31+
unless (-e $config_file);
32+
33+
my $config = LoadFile($config_file);
34+
35+
my $schema =
36+
DB::Schema->connect($config->{database_dsn}, $config->{database_user}, $config->{database_password});
37+
3138
# $schema->storage->debug(1); # print out the SQL commands.
3239

3340
my $course_rs = $schema->resultset("Course");
3441
my $user_rs = $schema->resultset("User");
3542
my $cu_rs = $schema->resultset("CourseUser");
3643

3744
## get a list of users from the CSV file
38-
my @students = loadCSV("$main::test_dir/sample_data/students.csv");
45+
my @students = loadCSV("$main::ww3_dir/t/db/sample_data/students.csv");
3946
for my $student (@students) {
4047
$student->{is_admin} = 0;
4148
}

0 commit comments

Comments
 (0)