Tests: Add `Tests_dbDelta::assertTableHasPrimaryKey()`.
Also fix a typo in the name for the compound key. Props charlestonsw. Fixes #34877. git-svn-id: https://develop.svn.wordpress.org/trunk@36552 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
e1cab8a017
commit
653e0b0827
|
@ -33,7 +33,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
column_1 varchar(255) NOT NULL,
|
column_1 varchar(255) NOT NULL,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1),
|
KEY compound_key (id,column_1),
|
||||||
FULLTEXT KEY fulltext_key (column_1)
|
FULLTEXT KEY fulltext_key (column_1)
|
||||||
) ENGINE=MyISAM
|
) ENGINE=MyISAM
|
||||||
"
|
"
|
||||||
|
@ -105,7 +105,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
column_1 varchar(255) NOT NULL,
|
column_1 varchar(255) NOT NULL,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1)
|
KEY compound_key (id,column_1)
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
|
@ -128,7 +128,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
column_1 varchar(255) NOT NULL,
|
column_1 varchar(255) NOT NULL,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1)
|
KEY compound_key (id,column_1)
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
|
@ -157,7 +157,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
extra_col longtext,
|
extra_col longtext,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1)
|
KEY compound_key (id,column_1)
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
|
@ -171,6 +171,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
|
$this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
|
||||||
|
$this->assertTableHasPrimaryKey( 'id' , $wpdb->prefix . 'dbdelta_test' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -189,7 +190,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
id bigint(20) NOT NULL AUTO_INCREMENT,
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1)
|
KEY compound_key (id,column_1)
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
|
@ -215,7 +216,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
extra_col longtext,
|
extra_col longtext,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1)
|
KEY compound_key (id,column_1)
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
, false // Don't execute.
|
, false // Don't execute.
|
||||||
|
@ -265,7 +266,7 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
column_1 varchar(255) NOT NULL,
|
column_1 varchar(255) NOT NULL,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
KEY key_1 (column_1),
|
KEY key_1 (column_1),
|
||||||
KEY compoud_key (id,column_1),
|
KEY compound_key (id,column_1),
|
||||||
FULLTEXT KEY fulltext_key (column_1)
|
FULLTEXT KEY fulltext_key (column_1)
|
||||||
)
|
)
|
||||||
", false
|
", false
|
||||||
|
@ -286,7 +287,6 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
* @param string $table The database table name.
|
* @param string $table The database table name.
|
||||||
*/
|
*/
|
||||||
protected function assertTableRowHasValue( $column, $value, $table ) {
|
protected function assertTableRowHasValue( $column, $value, $table ) {
|
||||||
|
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$table_row = $wpdb->get_row( "select $column from {$table} where $column = '$value'" );
|
$table_row = $wpdb->get_row( "select $column from {$table} where $column = '$value'" );
|
||||||
|
@ -305,7 +305,6 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
* @param string $table The database table name.
|
* @param string $table The database table name.
|
||||||
*/
|
*/
|
||||||
protected function assertTableHasColumn( $column, $table ) {
|
protected function assertTableHasColumn( $column, $table ) {
|
||||||
|
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
|
$table_fields = $wpdb->get_results( "DESCRIBE {$table}" );
|
||||||
|
@ -313,6 +312,22 @@ class Tests_dbDelta extends WP_UnitTestCase {
|
||||||
$this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
|
$this->assertCount( 1, wp_list_filter( $table_fields, array( 'Field' => $column ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that a table has a primary key.
|
||||||
|
*
|
||||||
|
* Checks for single-column primary keys. May not work for multi-column primary keys.
|
||||||
|
*
|
||||||
|
* @param string $column The column for the primary key.
|
||||||
|
* @param string $table The database table name.
|
||||||
|
*/
|
||||||
|
protected function assertTableHasPrimaryKey( $column , $table ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_indices = $wpdb->get_results( "SHOW INDEX FROM {$table}" );
|
||||||
|
|
||||||
|
$this->assertCount( 1, wp_list_filter( $table_indices, array( 'Key_name' => 'PRIMARY' , 'Column_name' => $column ) , 'AND' ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert that a table doesn't have a column.
|
* Assert that a table doesn't have a column.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue