All, Is there a way to figure out the table prefix that Simpletest generates at runtime? I am trying to run a test that does a bunch of inserts, then I need to figure out how many rows were created in the table. However, I can't query the table because simpletest changes the name of it every time. Also, I tried {table_name} instead of table_name in the query but it didn't work (line numbers inserted below to help with reading the error message later): 21 public function test_sync () { 22 require_once(drupal_get_path('module','compoundmanagement').'/compoundmanagement.inc'); 23 $result = db_query('select count(objdid) as count from {cpd_compounds}'); 24 $row = $result->db_fetch_object($result); 25 $this->pass('There are '.$row->count.' compounds in cpd_compounds'); 26 $this->drupalGet('sync_cpds',array('absolute' => TRUE)); 27 $result = db_query('select count(objdid) as count from {cpd_compounds}'); 28 $row = $result->db_fetch_object($result); 29 $this->pass('There are now '.$row->count.' compounds in cpd_compounds'); 30 $this->assertEqual($row->count,1313,"CPD_COMPOUNDS should have 1313 rows."); 31 } The test above threw this error: An error occurred. /batch?id=18&op=do <br /> <b>Fatal error</b>: Call to a member function db_fetch_object() on a non-object in <b>C:\wamp\www\drupal\sites\all\modules\compoundmanagement\compoundmanagement.test</b> on line <b>24</b><br /> Any ideas what to do here? I have been developing in Drupal for a while now but am just now discovering the unit testing available in SimpleTest. Best, Dave
The table aliasing with {} should definitely work just fine in SimpleTest, or else nothing in Drupal would work. You probably have some other error. Do your queries work in PHPMyAdmin or in the MySQL command line, in the regular database where you know the table names?? That said, there's a global $db_prefix that gives you the database prefix (see settings.php and the db_prefix_tables function in includes/database.inc). --Jennifer David Thibault wrote:
Is there a way to figure out the table prefix that Simpletest generates at runtime? I am trying to run a test that does a bunch of inserts, then I need to figure out how many rows were created in the table. However, I can't query the table because simpletest changes the name of it every time. Also, I tried {table_name} instead of table_name in the query but it didn't work (line numbers inserted below to help with reading the error message later):
21 public function test_sync () { 22 require_once(drupal_get_path('module','compoundmanagement').'/compoundmanagement.inc'); 23 $result = db_query('select count(objdid) as count from {cpd_compounds}'); 24 $row = $result->db_fetch_object($result); 25 $this->pass('There are '.$row->count.' compounds in cpd_compounds'); 26 $this->drupalGet('sync_cpds',array('absolute' => TRUE)); 27 $result = db_query('select count(objdid) as count from {cpd_compounds}'); 28 $row = $result->db_fetch_object($result); 29 $this->pass('There are now '.$row->count.' compounds in cpd_compounds'); 30 $this->assertEqual($row->count,1313,"CPD_COMPOUNDS should have 1313 rows."); 31 }
The test above threw this error:
An error occurred. /batch?id=18&op=do <br /> <b>Fatal error</b>: Call to a member function db_fetch_object() on a non-object in <b>C:\wamp\www\drupal\sites\all\modules\compoundmanagement\compoundmanagement.test</b> on line <b>24</b><br />
-- Jennifer Hodgdon * Poplar ProductivityWare www.poplarware.com Drupal, WordPress, and custom Web programming
21 public function test_sync () { Should be testSync(). 22 require_once(drupal_get_path('module','compoundmanagement').'/compoundmanage ment.inc'); Should be module_load_include(). 23 $result = db_query('select count(objdid) as count from {cpd_compounds}'); "cpd_compounds" does not match your module name. 24 $row = $result->db_fetch_object($result); If this is for D7: db_fetch_object() no longer exists. You can iterate over $result directly. Read more on http://drupal.org/node/310069 If this is for D6, then $result simply is no class and db_fetch_object() simply is no method. 25 $this->pass('There are '.$row->count.' compounds in cpd_compounds'); Should use t() as always, with appropriate placeholders (@count in this case). 26 $this->drupalGet('sync_cpds',array('absolute' => TRUE)); Unless you have a very good reason to do so, the options array (and absolute URL) is not required. 27 $result = db_query('select count(objdid) as count from {cpd_compounds}'); SimpleTest always queries the table in the database of the currently running test, as along as it is escaped (which it is here). 28 $row = $result->db_fetch_object($result); Your real error is likely here (again). 29 $this->pass('There are now '.$row->count.' compounds in cpd_compounds'); See above. 30 $this->assertEqual($row->count,1313,"CPD_COMPOUNDS should have 1313 rows."); Only use double-quotes where technically required. The test above threw this error: <b>Fatal error</b>: Call to a member function db_fetch_object() on a non-object In the end, the actual error message is pretty concise. ;) sun
On Tue, 2010-01-26 at 17:10 +0100, Daniel F. Kudwien wrote:
25 $this->pass('There are '.$row->count.' compounds in cpd_compounds');
Should use t() as always, with appropriate placeholders (@count in this case).
I don't see the use for t() here. This is pure technical information, why should they be translated? Even if they can be, if I use potx (for example) to extract all my custom module strings, wouldn't it extract those in tests also, if so, an external, non technician, translator should not have these strings among the pure end user UI strings. See http://drupal.org/update/modules/6/7#schema_translation and why do they stopped translating schema description! I think there is absolutely no point to translate tests, and my argument is the same as theirs. Pierre.
I don't see the use for t() here. This is pure technical information, why should they be translated?
As of now, we use t() in tests. To eventually change this, see http://drupal.org/node/500866 However, even when changing this, it's likely that we will still use t() in situations like:
25 $this->pass('There are '.$row->count.' compounds in cpd_compounds');
Otherwise, $row->count or whatever other variable would be displayed unfiltered to the browser. sun
On Tue, 2010-01-26 at 17:45 +0100, Daniel F. Kudwien wrote:
I don't see the use for t() here. This is pure technical information, why should they be translated?
As of now, we use t() in tests.
To eventually change this, see http://drupal.org/node/500866
However, even when changing this, it's likely that we will still use t() in situations like:
25 $this->pass('There are '.$row->count.' compounds in cpd_compounds');
Otherwise, $row->count or whatever other variable would be displayed unfiltered to the browser.
sun
Ok, thanks for the url, I read the thread. For escaping, a simple check_plain() is enough. Pierre.
Yep, that was it. <stupid look>I put $result->db_fetch_object($result) instead of just db_fetch_object($result).</stupid look> Best, Dave -----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Daniel F. Kudwien Sent: Tuesday, January 26, 2010 11:11 AM To: development@drupal.org Subject: Re: [development] Simpletest question 21 public function test_sync () { Should be testSync(). 22 require_once(drupal_get_path('module','compoundmanagement').'/compoundmanage ment.inc'); Should be module_load_include(). 23 $result = db_query('select count(objdid) as count from {cpd_compounds}'); "cpd_compounds" does not match your module name. 24 $row = $result->db_fetch_object($result); If this is for D7: db_fetch_object() no longer exists. You can iterate over $result directly. Read more on http://drupal.org/node/310069 If this is for D6, then $result simply is no class and db_fetch_object() simply is no method. 25 $this->pass('There are '.$row->count.' compounds in cpd_compounds'); Should use t() as always, with appropriate placeholders (@count in this case). 26 $this->drupalGet('sync_cpds',array('absolute' => TRUE)); Unless you have a very good reason to do so, the options array (and absolute URL) is not required. 27 $result = db_query('select count(objdid) as count from {cpd_compounds}'); SimpleTest always queries the table in the database of the currently running test, as along as it is escaped (which it is here). 28 $row = $result->db_fetch_object($result); Your real error is likely here (again). 29 $this->pass('There are now '.$row->count.' compounds in cpd_compounds'); See above. 30 $this->assertEqual($row->count,1313,"CPD_COMPOUNDS should have 1313 rows."); Only use double-quotes where technically required. The test above threw this error: <b>Fatal error</b>: Call to a member function db_fetch_object() on a non-object In the end, the actual error message is pretty concise. ;) sun
participants (4)
-
Daniel F. Kudwien -
David Thibault -
Jennifer Hodgdon -
Pierre R.