Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Test case for bug in casting array of embedded documents #467

Closed
wants to merge 1 commit into from

2 participants

@nealerickson

From the conversion I had with nate the other day on IRC this appears to be a bug.

I modified the testTypeCasting method and $_schema so that I could test the casting of arrays of embedded documents. The documents are being saved, however the values being saved to the db are not cast properly.

Ideally the following schema would allow me to properly cast an array of embedded documents at the accounts path.

protected $_schema = array(
    '_id' => array('type' => 'id'),
    ...
    'accounts' => array('type' => 'object', 'array' => true),
    'accounts._id' => array('type' => 'id'),
    'accounts.name' => array('type' => 'string'),
    'accounts.created' => array('type' => 'date'),
    ...
    'notifications.baz' => array('type' => 'boolean')
);

However, when saving the following data the values in account arrays are not being properly cast. The _id fields remain strings while the created fields are numeric timestamps and not instances of MongoDate.

$data = array(
      '_id' => '4c8f86167675abfabd970300',
      ...
      'accounts' => array(array(
              '_id' => "4fb6e2dd3e91581fe6e75736",
              'name' => 'Foo',
              'created' => time()
          ),array(
              '_id' => "4fb6e2df3e91581fe6e75737",
              'name' => 'Bar',
              'created' => time()
          )                
      ),
     'empty_array' => array(),
     'authors' => '4c8f86167675abfabdb00300',
...
'rank' => '3.45688'
);
@nateabele nateabele was assigned
@nateabele
Owner

I'd actually suggest breaking this out into a separate test, specifically for subdocuments. Also, the formatting looks a bit wonky. Maybe try running the patch through li3_qa? Thanks much.

@nealerickson

Thanks, I'll take a look at li3_qa. I can clean it up and re-submit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 1 deletion.
  1. +28 −1 tests/cases/data/source/mongo_db/ExporterTest.php
View
29 tests/cases/data/source/mongo_db/ExporterTest.php
@@ -26,6 +26,10 @@ class ExporterTest extends \lithium\test\Unit {
'title' => array('type' => 'string'),
'tags' => array('type' => 'string', 'array' => true),
'comments' => array('type' => 'MongoId'),
+ 'accounts' => array('type' => 'object', 'array' => true),
+ 'accounts._id' => array('type' => 'id'),
+ 'accounts.name' => array('type' => 'string'),
+ 'accounts.created' => array('type' => 'date'),
'authors' => array('type' => 'MongoId', 'array' => true),
'created' => array('type' => 'MongoDate'),
'modified' => array('type' => 'datetime'),
@@ -295,6 +299,16 @@ public function testTypeCasting() {
'comments' => array(
"4c8f86167675abfabdbe0300", "4c8f86167675abfabdbf0300", "4c8f86167675abfabdc00300"
),
+ 'accounts' => array(array(
+ '_id' => "4fb6e2dd3e91581fe6e75736",
+ 'name' => 'Foo',
+ 'created' => time()
+ ),array(
+ '_id' => "4fb6e2df3e91581fe6e75737",
+ 'name' => 'Bar',
+ 'created' => time()
+ )
+ ),
'empty_array' => array(),
'authors' => '4c8f86167675abfabdb00300',
'created' => time(),
@@ -321,7 +335,20 @@ public function testTypeCasting() {
$this->assertTrue($result['comments'][2] instanceof MongoId);
$this->assertEqual('4c8f86167675abfabdbe0300', (string) $result['comments'][0]);
$this->assertEqual('4c8f86167675abfabdbf0300', (string) $result['comments'][1]);
- $this->assertEqual('4c8f86167675abfabdc00300', (string) $result['comments'][2]);
+ $this->assertEqual('4c8f86167675abfabdc00300', (string) $result['comments'][2]);
+
+ $this->assertTrue($result['accounts'] instanceof DocumentArray);
+ $this->assertEqual(2, count($result['accounts']));
+
+ $this->assertTrue($result['accounts'][0]['_id'] instanceof MongoId);
+ $this->assertEqual('4fb6e2dd3e91581fe6e75736', (string) $result['accounts'][0]['_id']);
+ $this->assertTrue($result['accounts'][1]['_id'] instanceof MongoId);
+ $this->assertEqual('4fb6e2df3e91581fe6e75737', (string) $result['accounts'][1]['_id']);
+
+ $this->assertTrue($result['accounts'][0]['created'] instanceof MongoDate);
+ $this->assertTrue($result['accounts'][0]['created']->sec > 0);
+ $this->assertTrue($result['accounts'][1]['created'] instanceof MongoDate);
+ $this->assertTrue($result['accounts'][1]['created']->sec > 0);
$this->assertEqual($data['comments'], $result['comments']->data());
$this->assertEqual(array('test'), $result['tags']->data());
Something went wrong with that request. Please try again.