TDD workshop. Anybody welcome; Data storage abstraction.

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

:D I'm just enjoying the coding.

I'm off to bed, but I'll leave you with this bug to fix ;) Make sure error reporting is at least E_ALL (I added it to tests/configure.php).

Code: Select all

 public function testGetReturnsNullForUndefinedValues() {
    $this->assertNull($this->_dataStore->get('foo'),
      '%s: NULL should be returned because the value is not present'
      );
  }

Code: Select all

AllTests.php
Exception 1!
Unexpected PHP error [Undefined index:  foo] severity [8] in [/Users/d11wtq/data_store/classes/DataStore.php line 20]
    in testGetReturnsNullForUndefinedValues
    in DataStoreTest
    in /Users/d11wtq/data_store/tests/unit/DataStoreTest.php
    in All DataStore tests
FAILURES!!!
Test cases run: 1/1, Passes: 11, Failures: 0, Exceptions: 1
I'm open to alternative to returning NULL too if you guys think it's a bad idea. Will see what's happenin' tomorrow.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

Maybe

Code: Select all

 
    public function get($key) {
        return isset($this->_store[$key]) ? $this->_store[$key] : null;
    }
 
Have a good night ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

matthijs wrote:Maybe

Code: Select all

 
    public function get($key) {
        return isset($this->_store[$key]) ? $this->_store[$key] : null;
    }
 
Have a good night ;)
Not gone to bed yet :P That's a lie. I'm in bed on my Macbook :oops: We have method who's responsibility it is to determine if the value exists. Could we maybe drop the isset() in favour of that? ;)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

of course :)

Code: Select all

 
    public function get($key) {
        return $this->has($key) ? $this->_store[$key] : null;
    }
 
This better?

In bed with your Macbook? Take care you don't make your partner jealous (if you have one) :wink:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by John Cartwright »

Chris Corbyn wrote: Not gone to bed yet :P That's a lie. I'm in bed on my Macbook
Naughty Chris :mrgreen:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Christopher »

Jcart wrote:
Chris Corbyn wrote: Not gone to bed yet :P That's a lie. I'm in bed on my Macbook
Naughty Chris :mrgreen:
Chris + a Sheila + bed == Naughty

Chris + a MacBook + bed == Nice

Chris + a Sheila + a MacBook + bed == Kinky!
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

Moving on... :lol:

When we picked up the initial spec for this project, we were talking about tree-like data structures (i.e. YAML/XML). In that case, we might have values stored at a path like:

foo -> bar -> zip

Should we stop for a moment to think about the interface for accessing that value with the key "zip" on branches "foo" then "bar"? Am I making any sense? :P Basically if it were a PHP array it'd be:

Code: Select all

array (
  'foo '=> array(
    'bar' => array(
      'zip' => 'some value'
    )
  )
);
What might a simple interface for querying that value be? Something like this maybe?

Code: Select all

$dataStore->get('foo/bar/zip');
?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

Good morning again :)

I think your interface looks good. What are alternatives?

Code: Select all

 
$dataStore->get('foo','bar','zip');
 
Your interface wih the slashes looks nicer and more familiar because it's also used for folders and URLs.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

matthijs wrote:What are alternatives?
Well, there are ways to do it using a namespace/hierarchy separator like a slash or a dot. But then we may have other ideas like this?

Code: Select all

$dataStore->get('foo')->get('bar')->get('zip');
I think that just adds unnecessary complexity, but just exploring the ideas :)

I do agree that just using slashes is a lot easier to read (and probably to use). Should we go with the slashes? :)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

At first sight I would say let's go with the slashes. My only concern is: we want to support different kinds of data in the end. How does using slashes cope with that? Thinking about XML, CVS, etc.

Most data will be a bit more verbose then foo, bar and baz.

I'm just waking up so my brains not that fast yet ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

matthijs wrote:At first sight I would say let's go with the slashes. My only concern is: we want to support different kinds of data in the end. How does using slashes cope with that? Thinking about XML, CVS, etc.
We're only interested in storing key-value pairs with some nesting support. We don't have to implement the entire XML API. If at some point down the line we decide to support XPath-like queries then it'll probably degrade nicely to other formats anyway :) For now, let's just concern ourselves with the most simple situations.

I don't think CSV was ever going to work well with this project since it's tabular and not tree-like. I'm sure there'd be a way to force it work but it would be messy :P We'll move onto XmlModel when we finish this wrapper. It doesn't seem we're far away from being able to do that ;)

I should probably discuss our options for writing the test for branching. First and foremost we want to know that values can get has(), set(), get() and remove()'d using the new syntax. But we also will need to ensure that the array we pass to $storageModel->save() is in a format we will stick to.

Let's do the tests for get(), set(), has() and remove() using our new syntax first. Hopefully we'll find that they "just work" :) But we're about to see how the tests support our ability to refactor.

Care to write those get(), set(), has() and remove() tests? ;)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

Ok I'll give it a try!

Might take a moment, because I found out I've got to upgrade 15 wordpress sites (again :evil: If these guys would have learned coding first on this forum I wouldn't have to do that every week)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by matthijs »

Code: Select all

 
public function testGetDeeperNestedKeyReturnsRightValue(){
    $storageModel = $this->_createStorageModel();
    $storageModel->setReturnValue('load', array('foo' => array('bar'=>array('zip'=>'some value'))));
    $dataStore = $this->_createDataStore($storageModel);
    $this->assertEqual('some value',$dataStore->get('foo/bar/zip'));
}
public function testSetDeeperNestedValue(){
    $this->_dataStore->set('foo/bar/zip', 'some value');
    $this->assertEqual('some value', $this->_dataStore->get('foo/bar/zip'));
}
public function testHasDeeperNestedValue(){
    $this->_dataStore->set('foo/bar/zip', 'some value');
    $this->assertTrue($this->_dataStore->has('foo/bar/zip'));
}
 
That first test fails, the other two pass. But that's logical, as I'm not really testing a nested array there.. so the tests could be wrong.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

Interesting that you posted a version using load() rather than a straight set() followed by get() like we did initially :) But that's ok anyway since that was what I was about to move onto.

So I guess we're both in agreement that the data structure which comes out of, and goes into load() and save() will just be a multidimensional array? :) I'm happy with that and it's basically what I was thinking.

So can we get all our tests to pass with this setup if we modify our current design slightly? :)

EDIT | We're missing remove() too actually. Should probably add some test coverage for that one too :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by Chris Corbyn »

I'm working on this now and I'll admit it's not as simple an algorithm as it would first seem. I have it cracked so if you're stuck I'll post my solution :)
Post Reply