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
22 class DbTestPreviewer
extends TestRecorder
{
23 protected $lb; // /< Database load balancer
24 protected $db; // /< Database connection to the main DB
25 protected $curRun; // /< run ID number for the current run
26 protected $prevRun; // /< run ID number for the previous run, if any
27 protected $results; // /< Result array
30 * This should be called before the table prefix is changed
31 * @param TestRecorder $parent
33 function __construct( $parent ) {
34 parent
::__construct( $parent );
36 $this->lb
= wfGetLBFactory()->newMainLB();
37 // This connection will have the wiki's table prefix, not parsertest_
38 $this->db
= $this->lb
->getConnection( DB_MASTER
);
42 * Set up result recording; insert a record for the run with the date
43 * and all that fun stuff
48 if ( !$this->db
->tableExists( 'testrun', __METHOD__
)
49 ||
!$this->db
->tableExists( 'testitem', __METHOD__
)
51 print "WARNING> `testrun` table not found in database.\n";
52 $this->prevRun
= false;
54 // We'll make comparisons against the previous run later...
55 $this->prevRun
= $this->db
->selectField( 'testrun', 'MAX(tr_id)' );
61 function getName( $test, $subtest ) {
63 return "$test subtest #$subtest";
69 function record( $test, $subtest, $result ) {
70 parent
::record( $test, $subtest, $result );
71 $this->results
[ $this->getName( $test, $subtest ) ] = $result;
75 if ( $this->prevRun
) {
76 // f = fail, p = pass, n = nonexistent
77 // codes show before then after
79 'fp' => 'previously failing test(s) now PASSING! :)',
80 'pn' => 'previously PASSING test(s) removed o_O',
81 'np' => 'new PASSING test(s) :)',
83 'pf' => 'previously passing test(s) now FAILING! :(',
84 'fn' => 'previously FAILING test(s) removed O_o',
85 'nf' => 'new FAILING test(s) :(',
86 'ff' => 'still FAILING test(s) :(',
91 $res = $this->db
->select( 'testitem', [ 'ti_name', 'ti_success' ],
92 [ 'ti_run' => $this->prevRun
], __METHOD__
);
94 foreach ( $res as $row ) {
95 if ( !$this->parent
->regex
96 ||
preg_match( "/{$this->parent->regex}/i", $row->ti_name
)
98 $prevResults[$row->ti_name
] = $row->ti_success
;
102 $combined = array_keys( $this->results +
$prevResults );
104 # Determine breakdown by change type
106 foreach ( $combined as $test ) {
107 if ( !isset( $prevResults[$test] ) ) {
109 } elseif ( $prevResults[$test] == 1 ) {
111 } else /* if ( $prevResults[$test] == 0 )*/ {
115 if ( !isset( $this->results
[$test] ) ) {
117 } elseif ( $this->results
[$test] == 1 ) {
119 } else /*if ( $this->results[$test] == 0 ) */ {
123 $code = $before . $after;
125 if ( isset( $table[$code] ) ) {
126 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
131 foreach ( $table as $code => $label ) {
132 if ( !empty( $breakdown[$code] ) ) {
133 $count = count( $breakdown[$code] );
134 printf( "\n%4d %s\n", $count, $label );
136 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
137 print " * $differing_test_name [$statusInfo]\n";
142 print "No previous test runs to compare against.\n";
150 * Returns a string giving information about when a test last had a status change.
151 * Could help to track down when regressions were introduced, as distinct from tests
152 * which have never passed (which are more change requests than regressions).
153 * @param string $testname
154 * @param string $after
157 private function getTestStatusInfo( $testname, $after ) {
158 // If we're looking at a test that has just been removed, then say when it first appeared.
159 if ( $after == 'n' ) {
160 $changedRun = $this->db
->selectField( 'testitem',
162 [ 'ti_name' => $testname ],
164 $appear = $this->db
->selectRow( 'testrun',
165 [ 'tr_date', 'tr_mw_version' ],
166 [ 'tr_id' => $changedRun ],
169 return "First recorded appearance: "
170 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date
) )
171 . ", " . $appear->tr_mw_version
;
174 // Otherwise, this test has previous recorded results.
175 // See when this test last had a different result to what we're seeing now.
177 'ti_name' => $testname,
178 'ti_success' => ( $after == 'f' ?
"1" : "0" ) ];
180 if ( $this->curRun
) {
181 $conds[] = "ti_run != " . $this->db
->addQuotes( $this->curRun
);
184 $changedRun = $this->db
->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__
);
186 // If no record of ever having had a different result.
187 if ( is_null( $changedRun ) ) {
188 if ( $after == "f" ) {
189 return "Has never passed";
191 return "Has never failed";
195 // Otherwise, we're looking at a test whose status has changed.
196 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
197 // In this situation, give as much info as we can as to when it changed status.
198 $pre = $this->db
->selectRow( 'testrun',
199 [ 'tr_date', 'tr_mw_version' ],
200 [ 'tr_id' => $changedRun ],
202 $post = $this->db
->selectRow( 'testrun',
203 [ 'tr_date', 'tr_mw_version' ],
204 [ "tr_id > " . $this->db
->addQuotes( $changedRun ) ],
206 [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
210 $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date
) ) . ", {$post->tr_mw_version}";
215 return ( $after == "f" ?
"Introduced" : "Fixed" ) . " between "
216 . date( "d-M-Y H:i:s", strtotime( $pre->tr_date
) ) . ", " . $pre->tr_mw_version
221 * Close the DB connection
224 $this->lb
->closeAll();