Page 1 of 1

Assert clarity

Posted: Fri Jul 29, 2005 8:59 pm
by nielsene
I have a few test cases where I feel the assertions lack clarity.

I'm checking for the existence of certain keys in a dictionary. It is aleady guarenteed that if the key exists its value is non-false; and that the dictionairy will return false if the key doesn't exist.

I've currently coded these assertions as

Code: Select all

$this->assertTrue($this->pb->getQuery("Key Name"));
However, an "assertNotFalse" would probably indicate the intention better. Of course there is no such assertion and I don't think it would really be worth adding it via a local subclass of UnitTestCase. Perhaps

Code: Select all

assertNotEqual(false,$this->pb->getQuery("Key Name"));
It just feels a little funny to use notEquals with false (or true) as one of expected values....


I could write it as

Code: Select all

$this->assertTrue(in_array("Key Name",$this->pb->getKeys()))
but that would be adding the geyKeys() purely to support easier testing -- there isn't a use-case demand for getKeys yet.

I guess the other option would be to use assertEquals with the actual returned value. However the returned values are slightly brittled and will likely have a fair bit of tweaking at the whitespace level that would be hard to properly right a regexp for.


Any other thoughts?

Posted: Fri Jul 29, 2005 9:10 pm
by timvw

Code: Select all

// assertNotFalse
$this->assertTrue($this->pb->getQuery("Key Name") !== false);

Posted: Fri Jul 29, 2005 9:30 pm
by McGruff
Maybe:

Code: Select all

$this->assertTrue(is_string(...));

Posted: Fri Jul 29, 2005 9:37 pm
by McGruff
Actually maybe this expresses the constraint better:

Code: Select all

$this->assertTrue(is_string(...) and str_len(...) > 0);
I'm assuming that all you're really interested in here is that you should get a string back (an sql query, going by your recent posts). On the principle that you don't want to add any unecessary constraints to possible implementations, it doesn't really matter whether the method returns false, null, or an empty string '' if the key doesn't exist.

Posted: Fri Jul 29, 2005 9:38 pm
by nielsene
McGruff wrote:Maybe:

Code: Select all

$this->assertTrue(is_string(...));
I like it. Thanks.


Ooh, new version even better!

Assert changed to:

Code: Select all

$phrase=$this->pb->getQuery($aPhraseLabel);
      $this->assertTrue(is_string($phrase) && strlen($phrase)>0);
I wish I had a good way to compare the actual string to the expected string, but as one of the primary reasons for moving the SQL out to a PhraseBook was to allow a DBA-type person to do tuning at both the DB and query level. The PhraseBook is marked up with sql comments, etc, so its directly edittable/useable in SQL authoring tools and I expect the
linebreaks/indentation of multiline queries to fluctuate freely along with join orders/aliases.

I think they way I'll test the embeded SQL is against a test DB. As long as the named query retreives the right Result Set, its working properly, etc.

Posted: Fri Jul 29, 2005 9:58 pm
by McGruff
nielsene wrote:I think they way I'll test the embeded SQL is against a test DB.
Yes: I'd do the same. Running the query past a database is the real test.