Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

called syncOriginal() after setting attributes getAll() and find() - … #58

Merged
merged 9 commits into from
Jun 23, 2017

Conversation

zoul0813
Copy link
Contributor

@zoul0813 zoul0813 commented Jun 22, 2017

added support for timestamps, and fixed "isDirty" logic (isDirty is required for timestamps to work properly).


This change is Reviewable

…this fixes the 'dirty' issue, support timestamps (dirty needs to work for timestamps to function properly for updated_at)
This was referenced Jun 22, 2017
@baopham
Copy link
Owner

baopham commented Jun 22, 2017

Nice. This change would need some unit tests to make sure it is working and won't break in the future.

If you need help with the tests, let me know. It'd be nice if we can support timestamps.

@zoul0813
Copy link
Contributor Author

Having trouble getting DynamoDbLocal setup for the test.

I already have DDb Local setup for my laravel project, and use the following to start it ...

java -Djava.library.path=..path/DynamoDBLocal_lib -jar ..path/DynamoDBLocal.jar -sharedDb --port 8081 -dbPath ..path/webroot/database/local/dynamo

I adjusted this to the following for the tests

java -Djava.library.path=..path/DynamoDBLocal_lib -jar ..path/DynamoDBLocal.jar - -port 3000 -dbPath ..path/tmp/laravel-dynamodb/ (my fork is in the tmp folder)

Doing this, I managed to go from having every test generate errors (and take forever trying to connect to what was not there), and now am getting 6 errors ... they all appear to be the same error though.

5) BaoPham\DynamoDb\Tests\DynamoDbModelTest::testUpdateRecord
Error: Call to a member function connection() on null

...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Model.php:1042
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Model.php:1008
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php:777
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php:732
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php:525
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Model.php:1285
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Concerns/HasTimestamps.php:71
...path/tmp/laravel-dynamodb/vendor/illuminate/database/Eloquent/Concerns/HasTimestamps.php:42
...path/tmp/laravel-dynamodb/src/DynamoDbModel.php:127
...path/tmp/laravel-dynamodb/src/DynamoDbModel.php:147
...path/tmp/laravel-dynamodb/tests/DynamoDbModelTest.php:67

Do I need to do something other than start DDb Local pointing at the project directory? Looks like the tests take the dynamodb_local_init.db and move it to dynamodb_local_test.db which I assume creates all of the necessary tables, with expected default items in it?

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

the tests take the dynamodb_local_init.db and move it to dynamodb_local_test.db which I assume creates all of the necessary tables

Yes

Can you make sure you cd into the project root and run the exact command:

java -Djava.library.path=./DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar --port 3000

Then in a new tab, run:

./vendor/bin/phpunit

…CONTRIBUTING.md with instructions to start DynamoDB Local and PHPUnit
@zoul0813
Copy link
Contributor Author

Ahh, looks like I actually did have it all setup correctly. Following the errors, the timestamp logic does apparently try to connect to the database at some point to retrieve a date/time format in the event that one is not already set on the model. Should be an easy fix to explicitly set the date/time format on DynamoDbModel - just a matter of picking a default format.

I'm thinking we should use the format generated by JavaScript's Date toJSON method, as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

Since Laravel is using Carbon so why not use its toIso8601String http://carbon.nesbot.com/docs/#api-commonformats?

@zoul0813
Copy link
Contributor Author

I figured using the JavaScript json format would make the json document more compatible with other application code that may be using/accessing it (lambda triggers, client side code, mobile apps, etc) either directly or indirectly.

@zoul0813
Copy link
Contributor Author

Looks like those two formats are the same actually. Javascripts date object calls toIsoString behind the scenes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

ISO 8601 format it is then 👍

@zoul0813
Copy link
Contributor Author

Adding protected $dateFormat = 'o'; to DynamoDbModel seems to have solved the problem.

> phpunit
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

........................................................          56 / 56 (100%)

Time: 1.84 seconds, Memory: 32.00MB

OK (56 tests, 122 assertions)

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

Ah yeah totally forgot about that!

Still need to have 2 test cases though, one for created_at and one for updated_at.

# The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information and emits a result string that complies with ISO 8601.
# https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip
@zoul0813
Copy link
Contributor Author

zoul0813 commented Jun 23, 2017

Not quite sure why, but all the timestamps are being set to 1970-01-01 ... in my app, they are being set to the correct current time. I checked the HasTimestamps trait and verified that it's creating a new Carbon instance, with the current time ... but for some reason, after it's being set to $this->testModel->created_at it gets set to 1970-01-01 ... not quite sure where this is happening - any ideas? I'm thinking it may have something to do with the tests themselves?

-- Nevermind, I think it has to do with the format I'm using ... working on it now.

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

Let me try and see

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

For a more deterministic test, maybe we should try Carbon::setTestNow

return $item;
}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closing brace for the class must go on the next line after the body


}

class TimestampModel extends \BaoPham\DynamoDb\DynamoDbModel

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each class must be in a file by itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick is really nit-picky... can I just ignore some of these?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yeah, this one can be ignored.

'hash' => 'count',
],
];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 blank line at end of file; 2 found

@zoul0813
Copy link
Contributor Author

I created a new test class, so we can setup as many tests as needed - this also allows the other test classes to set public $timestamps = false;, as timestamps are on by default and may effect the other test results.

I did use Carbon::setTestNow()

The problem was using protected $dateFormat = 'o';, I wasn't paying attention and got that shorthand format off some Microsoft site ... I switched it over to DateTime::ISO8601 from the PHP Docs...

"scripts": {
"test": "phpunit",
"dynamodb_local": "java -Djava.library.path=./DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar --port 3000"
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! just like package.json 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

CONTRIBUTING.md Outdated

or

```composer run-script tests```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean test here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did ... fixing now

CONTRIBUTING.md Outdated
@@ -1,3 +1,19 @@
# Contribution Guide

Thank you for considering contributing to this library. Please make sure your code follows the PSR-2 coding standard and the PSR-4 autoloading standard before sending a pull request.

## Running Tests
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well remove the test section in README.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, didn't realize there was a test section there - did you add it recently?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's been there for a while :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated CONTRIBUTING.md and README.md


public function testCreateRecord()
{
\Carbon\Carbon::setTestNow(\Carbon\Carbon::create(2017, 06, 24, 5, 30, 0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put a use Carbon\Carbon on top?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, ... that would make sense :)

}
}

class TimestampModel extends \BaoPham\DynamoDb\DynamoDbModel

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each class must be in a file by itself

…ch README.md style (- and = instead of #), added DynamoDb Local details to CONTRIBUTING.md
@baopham baopham merged commit bd940d3 into baopham:master Jun 23, 2017
@baopham
Copy link
Owner

baopham commented Jun 23, 2017

Many thanks @zoul0813 for pushing this through.

@zoul0813
Copy link
Contributor Author

zoul0813 commented Jun 23, 2017

Your welcome, and don't forget to check off the TODO item at the bottom of README.md ... just saw that.

@baopham
Copy link
Owner

baopham commented Jun 23, 2017

Yep one down!

@zoul0813 zoul0813 deleted the feature/dirty-timestamps branch June 23, 2017 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants