Bugzilla – Attachment 99435 Details for
Bug 19821
Run tests on a separate database
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19821: Run tests on a separate database
Bug-19821-Run-tests-on-a-separate-database.patch (text/plain), 4.23 KB, created by
Bouzid Fergani
on 2020-02-21 20:21:38 UTC
(
hide
)
Description:
Bug 19821: Run tests on a separate database
Filename:
MIME Type:
Creator:
Bouzid Fergani
Created:
2020-02-21 20:21:38 UTC
Size:
4.23 KB
patch
obsolete
>From 20a8909c4f24ffb7e5edbe3259ad3c06c33fa9ff Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >Date: Fri, 15 Dec 2017 15:18:24 +0100 >Subject: [PATCH] Bug 19821: Run tests on a separate database > >Running the tests on the same database as the one used for dev has some >drawbacks: >- Everybody has different data, so we cannot make any assumptions about >data in tests and it can make tests fail for non-obvious reasons. >- Tests have to clean up every change to the database using SQL >transactions, so we cannot write testable code that use transactions >(AFAIK) >- Transactions in tests happen to be committed sometimes, resulting in >garbage data added to the dev database > >This patch provides a .proverc file that will load t/lib/Bootstrap.pm >before the tests are run. >t/lib/Bootstrap.pm is responsible for recreating a fresh database and >telling the test scripts to use it. >To use it, just run prove normally from the root directory of Koha. >By default, the database is 'koha_test' and it's created using MARC21 >SQL files, it can be changed by running: > > prove --norc \ > --Mt::lib::Bootstrap=database,koha_test,marcflavour,UNIMARC > >Test plan: >1. mysql> GRANT ALL PRIVILEGES ON koha_test.* TO > <your-db-user>@localhost >2. cd /path/to/koha >3. prove t/db_dependent/<pick-your-favorite-test-here>.t >4. Verify that it created the database koha_test and that the one you > use usually was untouched (you can edit the test file and comment the > lines related to a transaction (txn_begin, txn_rollback)) > >Signed-off-by: Jon Knight <J.P.Knight@lboro.ac.uk> >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Signed-off-by: Bouzid Fergani <bouzid.fergani@inlibro.com> >--- > .proverc | 1 + > t/lib/Bootstrap.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 73 insertions(+) > create mode 100644 .proverc > create mode 100644 t/lib/Bootstrap.pm > >diff --git a/.proverc b/.proverc >new file mode 100644 >index 0000000..c5b8cf5 >--- /dev/null >+++ b/.proverc >@@ -0,0 +1 @@ >+-Mt::lib::Bootstrap=database,koha_test >diff --git a/t/lib/Bootstrap.pm b/t/lib/Bootstrap.pm >new file mode 100644 >index 0000000..f679d39 >--- /dev/null >+++ b/t/lib/Bootstrap.pm >@@ -0,0 +1,72 @@ >+package t::lib::Bootstrap; >+ >+use Modern::Perl; >+ >+use DBI; >+use File::Temp qw( tempfile ); >+use XML::LibXML; >+ >+sub import { >+ my ($self, %args) = @_; >+ >+ unless (defined $args{database}) { >+ die "Test database is not defined"; >+ } >+ >+ $args{marcflavour} //= 'MARC21'; >+ >+ my $xml = XML::LibXML->load_xml(location => $ENV{KOHA_CONF}); >+ my $root = $xml->documentElement(); >+ my ($databaseElement) = $root->findnodes('//config/database'); >+ my $currentDatabase = $databaseElement->textContent(); >+ >+ if ($currentDatabase eq $args{database}) { >+ die "Test database is the same as database in KOHA_CONF, abort!"; >+ } >+ >+ $databaseElement->firstChild()->setData($args{database}); >+ >+ my ($fh, $filename) = tempfile('koha-conf.XXXXXX', TMPDIR => 1, UNLINK => 1); >+ $xml->toFH($fh); >+ >+ $ENV{KOHA_CONF} = $filename; >+ >+ require C4::Context; >+ C4::Context->import; >+ >+ require C4::Installer; >+ C4::Installer->import; >+ >+ require C4::Languages; >+ >+ my $host = C4::Context->config('hostname'); >+ my $port = C4::Context->config('port'); >+ my $database = C4::Context->config('database'); >+ my $user = C4::Context->config('user'); >+ my $pass = C4::Context->config('pass'); >+ >+ say "Create test database..."; >+ >+ my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, { >+ RaiseError => 1, >+ PrintError => 0, >+ }); >+ >+ $dbh->do("DROP DATABASE IF EXISTS $database"); >+ $dbh->do("CREATE DATABASE $database"); >+ >+ my $installer = C4::Installer->new(); >+ $installer->load_db_schema(); >+ $installer->set_marcflavour_syspref($args{marcflavour}); >+ my (undef, $fwklist) = $installer->marc_framework_sql_list('en', $args{marcflavour}); >+ my @frameworks; >+ foreach my $fwk (@$fwklist) { >+ foreach my $framework (@{ $fwk->{frameworks} }) { >+ push @frameworks, $framework->{fwkfile}; >+ } >+ } >+ my $all_languages = C4::Languages::getAllLanguages(); >+ $installer->load_sql_in_order($all_languages, @frameworks); >+} >+ >+1; >-- >2.7.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19821
:
69825
|
69842
|
69843
|
69844
|
71817
|
75045
|
75091
|
75092
|
81893
|
81894
|
81895
|
81896
|
81897
|
81898
|
81899
|
81900
|
99435
|
99436
|
99437
|
99438
|
99439
|
99440
|
99441
|
103409
|
103410
|
103411
|
103412
|
103413
|
103414
|
103415
|
105876
|
105877
|
105878
|
105879
|
105880
|
105881
|
105882
|
120417
|
120418
|
120419
|
120420
|
120421
|
120422
|
120423