Skip to content

Commit

Permalink
Cast UTCDateTime to a DateTime object for comparison (#119)
Browse files Browse the repository at this point in the history
* Cast UTCDateTime to a timestamp for comparison reasons

* Updated casting

* Updated tests for computed fields with date comparison
  • Loading branch information
rgubby authored Feb 23, 2017
1 parent d3f95f3 commit 52363de
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/mongo/delegates/Tables.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,11 @@ protected function isFunction($value)
*/
protected function castValueType($value, $type=null)
{
// If value is a UTCDateTime, turn into a DateTime object in order to perform comparison
if ($value instanceof \MongoDB\BSON\UTCDateTime) {
$value = $value->toDateTime();
}

switch($type)
{
case 'string':
Expand Down
59 changes: 59 additions & 0 deletions test/unit/mongo/MongoTripodComputedFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,65 @@ public function tearDown()
parent::tearDown();
}

public function testConditionalComputedFieldWithDates() {
$tableSpec = array(
"_id"=> "t_conditional_creators",
"type"=> array("bibo:Document"),
"from"=>"CBD_testing",
"fields"=> array(
array(
"fieldName" => "dateUpdated",
"predicates" => array(array(
"date" => array(
"predicates" => array("dct:updated")
)
))
),
array(
"fieldName" => "datePublished",
"predicates" => array(array(
"date" => array(
"predicates" => array("dct:published")
)
))
)
),
"computed_fields"=>array(
array(
"fieldName" => "status",
"value" => array(
"conditional" => array(
"if" => array('$dateUpdated', '>', '$datePublished'),
"then" => 'Updated',
"else" => 'Published'
)
)
)
)
);

$oldConfig = \Tripod\Mongo\Config::getConfig();
$newConfig = \Tripod\Mongo\Config::getConfig();
$newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec;
\Tripod\Mongo\Config::setConfig($newConfig);
\Tripod\Mongo\Config::getInstance();
$this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing');
$this->loadDatesDataViaTripod();
$this->tripod->generateTableRows('t_conditional_creators');

$collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators');

$tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo1234'));
$this->assertEquals('Updated', $tableDoc['value']['status']);

$tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo12345'));
$this->assertEquals('Published', $tableDoc['value']['status']);

\Tripod\Mongo\Config::setConfig($oldConfig);
\Tripod\Mongo\Config::getInstance();
$collection->drop();
}

public function testConditionalComputedField()
{
$tableSpec = array(
Expand Down
3 changes: 3 additions & 0 deletions test/unit/mongo/MongoTripodTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ protected function loadResourceData()
}
}

protected function loadDatesDataViaTripod() {
$this->loadDataViaTripod('/data/dates.json');
}
protected function loadResourceDataViaTripod()
{
$this->loadDataViaTripod('/data/resources.json');
Expand Down
52 changes: 52 additions & 0 deletions test/unit/mongo/data/dates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"_id":
{
"r":"baseData:foo1234",
"c":"http://talisaspire.com/"
},
"rdf:type":
[
{
"u":"bibo:Document"
}
],
"dct:title" : {
"l" : "A document title"
},
"dct:isVersionOf" : {
"u" : "http://talisaspire.com/works/4d101f63c10a6"
},
"dct:updated" : {
"l" : "2017-02-23T14:49:02+00:00"
},
"dct:published" : {
"l" : "2017-02-22T14:49:02+00:00"
}
},
{
"_id":
{
"r":"baseData:foo12345",
"c":"http://talisaspire.com/"
},
"rdf:type":
[
{
"u":"bibo:Document"
}
],
"dct:title" : {
"l" : "A document title"
},
"dct:isVersionOf" : {
"u" : "http://talisaspire.com/works/4d101f63c10a6"
},
"dct:updated" : {
"l" : "2017-02-23T14:49:02+00:00"
},
"dct:published" : {
"l" : "2017-02-23T14:49:02+00:00"
}
}
]

0 comments on commit 52363de

Please sign in to comment.