rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / parser / DbTestPreviewer.php
blob784c29cf4bbac916bcd43f71875a6c20b11d412e
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Testing
22 class DbTestPreviewer extends TestRecorder {
23 protected $filter; // /< Test name filter callback
24 protected $lb; // /< Database load balancer
25 protected $db; // /< Database connection to the main DB
26 protected $curRun; // /< run ID number for the current run
27 protected $prevRun; // /< run ID number for the previous run, if any
28 protected $results; // /< Result array
30 /**
31 * This should be called before the table prefix is changed
33 function __construct( $db, $filter = false ) {
34 $this->db = $db;
35 $this->filter = $filter;
38 /**
39 * Set up result recording; insert a record for the run with the date
40 * and all that fun stuff
42 function start() {
43 if ( !$this->db->tableExists( 'testrun', __METHOD__ )
44 || !$this->db->tableExists( 'testitem', __METHOD__ )
45 ) {
46 print "WARNING> `testrun` table not found in database.\n";
47 $this->prevRun = false;
48 } else {
49 // We'll make comparisons against the previous run later...
50 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
53 $this->results = [];
56 function record( $test, ParserTestResult $result ) {
57 $this->results[$test['desc']] = $result->isSuccess() ? 1 : 0;
60 function report() {
61 if ( $this->prevRun ) {
62 // f = fail, p = pass, n = nonexistent
63 // codes show before then after
64 $table = [
65 'fp' => 'previously failing test(s) now PASSING! :)',
66 'pn' => 'previously PASSING test(s) removed o_O',
67 'np' => 'new PASSING test(s) :)',
69 'pf' => 'previously passing test(s) now FAILING! :(',
70 'fn' => 'previously FAILING test(s) removed O_o',
71 'nf' => 'new FAILING test(s) :(',
72 'ff' => 'still FAILING test(s) :(',
75 $prevResults = [];
77 $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ],
78 [ 'ti_run' => $this->prevRun ], __METHOD__ );
79 $filter = $this->filter;
81 foreach ( $res as $row ) {
82 if ( !$filter || $filter( $row->ti_name ) ) {
83 $prevResults[$row->ti_name] = $row->ti_success;
87 $combined = array_keys( $this->results + $prevResults );
89 # Determine breakdown by change type
90 $breakdown = [];
91 foreach ( $combined as $test ) {
92 if ( !isset( $prevResults[$test] ) ) {
93 $before = 'n';
94 } elseif ( $prevResults[$test] == 1 ) {
95 $before = 'p';
96 } else /* if ( $prevResults[$test] == 0 ) */ {
97 $before = 'f';
100 if ( !isset( $this->results[$test] ) ) {
101 $after = 'n';
102 } elseif ( $this->results[$test] == 1 ) {
103 $after = 'p';
104 } else /* if ( $this->results[$test] == 0 ) */ {
105 $after = 'f';
108 $code = $before . $after;
110 if ( isset( $table[$code] ) ) {
111 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
115 # Write out results
116 foreach ( $table as $code => $label ) {
117 if ( !empty( $breakdown[$code] ) ) {
118 $count = count( $breakdown[$code] );
119 printf( "\n%4d %s\n", $count, $label );
121 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
122 print " * $differing_test_name [$statusInfo]\n";
126 } else {
127 print "No previous test runs to compare against.\n";
130 print "\n";
134 * Returns a string giving information about when a test last had a status change.
135 * Could help to track down when regressions were introduced, as distinct from tests
136 * which have never passed (which are more change requests than regressions).
137 * @param string $testname
138 * @param string $after
139 * @return string
141 private function getTestStatusInfo( $testname, $after ) {
142 // If we're looking at a test that has just been removed, then say when it first appeared.
143 if ( $after == 'n' ) {
144 $changedRun = $this->db->selectField( 'testitem',
145 'MIN(ti_run)',
146 [ 'ti_name' => $testname ],
147 __METHOD__ );
148 $appear = $this->db->selectRow( 'testrun',
149 [ 'tr_date', 'tr_mw_version' ],
150 [ 'tr_id' => $changedRun ],
151 __METHOD__ );
153 return "First recorded appearance: "
154 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
155 . ", " . $appear->tr_mw_version;
158 // Otherwise, this test has previous recorded results.
159 // See when this test last had a different result to what we're seeing now.
160 $conds = [
161 'ti_name' => $testname,
162 'ti_success' => ( $after == 'f' ? "1" : "0" ) ];
164 if ( $this->curRun ) {
165 $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
168 $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
170 // If no record of ever having had a different result.
171 if ( is_null( $changedRun ) ) {
172 if ( $after == "f" ) {
173 return "Has never passed";
174 } else {
175 return "Has never failed";
179 // Otherwise, we're looking at a test whose status has changed.
180 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
181 // In this situation, give as much info as we can as to when it changed status.
182 $pre = $this->db->selectRow( 'testrun',
183 [ 'tr_date', 'tr_mw_version' ],
184 [ 'tr_id' => $changedRun ],
185 __METHOD__ );
186 $post = $this->db->selectRow( 'testrun',
187 [ 'tr_date', 'tr_mw_version' ],
188 [ "tr_id > " . $this->db->addQuotes( $changedRun ) ],
189 __METHOD__,
190 [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
193 if ( $post ) {
194 $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
195 } else {
196 $postDate = 'now';
199 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
200 . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
201 . " and $postDate";