Skip to content

Commit

Permalink
Incuded some tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
darthapple committed May 15, 2016
1 parent 8ff04ff commit 297ff76
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 25 deletions.
28 changes: 3 additions & 25 deletions lib/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,7 @@ function Schema(obj, options) {
}

/*
* Added support for timestamps attribute
*
* 1) In this case we can set the name of timestamp fields (<fieldname>):
*
* var userSchema = new Schema({
* id: {
* type: String,
* hashKey: true,
* default: shortId.generate
* },
* timestamps: {
* createdAt: '<fieldname>',
* updatedAt: '<fieldname>'
* });
* 2) In this case the field will use default names: createdAt and updatedAt
*
* var userSchema = new Schema({
* id: {
* type: String,
* hashKey: true,
* default: shortId.generate
* },
* timestamps: true
* });
* Added support for timestamps attribute
*/
if (this.options.timestamps) {
var createdAt = null;
Expand All @@ -79,7 +56,8 @@ function Schema(obj, options) {
}

obj[createdAt] = { type: Date, default: Date.now };
obj[updatedAt] = { type: Date, default: Date.now, set: function() { return Date.now(); } };
obj[updatedAt] = { type: Date, default: Date.now, set: function() { return Date.now(); } };
this.timestamps = { createdAt: createdAt, updatedAt: updatedAt };
}

this.useDocumentTypes = !!this.options.useDocumentTypes;
Expand Down
86 changes: 86 additions & 0 deletions test/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,92 @@ describe('Schema tests', function (){
done();
});

it('Schema with timestamps options', function (done) {
var schema1 = new Schema({
id: {
type: Number,
validate: function(v) { return v > 0; },
rangeKey: true
},
name: {
type: String,
required: true
},
},
{
throughput: {read: 10, write: 2},
timestamps: true
});

var schema2 = new Schema({
id: {
type: Number,
validate: function(v) { return v > 0; },
rangeKey: true
},
name: {
type: String,
required: true
},
},
{
throughput: {read: 10, write: 2},
timestamps: { createdAt: 'createDate', updatedAt: 'lastUpdate'}
});


schema1.attributes.id.type.name.should.eql('number');
should(schema1.attributes.id.isSet).not.be.ok;
should.not.exist(schema1.attributes.id.default);
var validator = schema1.attributes.id.validator;
should.exist(validator);
validator(-1).should.not.be.ok;
validator(1).should.be.ok;
should(schema1.attributes.id.required).not.be.ok;

schema1.attributes.name.type.name.should.eql('string');
schema1.attributes.name.isSet.should.not.be.ok;
should.not.exist(schema1.attributes.name.default);
should.not.exist(schema1.attributes.name.validator);
schema1.attributes.name.required.should.be.ok;

schema1.rangeKey.should.equal(schema1.attributes.id);

schema1.throughput.read.should.equal(10);
schema1.throughput.write.should.equal(2);
//
// Schema1 timestamps validation
//
schema1.timestamps.should.exists;
schema1.timestamps.createdAt.should.exists;
schema1.timestamps.createdAt.should.be.equal('createdAt');
schema1.timestamps.updatedAt.should.exists;
schema1.timestamps.updatedAt.should.be.equal('updatedAt');

schema1.attributes.createdAt.type.name.should.eql('date');
should.exist(schema1.attributes.createdAt.default);

schema1.attributes.updatedAt.type.name.should.eql('date');
should.exist(schema1.attributes.updatedAt.default);
//
// Schema2 timestamps validation
//
schema2.timestamps.should.exists;
schema2.timestamps.createdAt.should.exists;
schema2.timestamps.createdAt.should.be.equal('createDate');
schema2.timestamps.updatedAt.should.exists;
schema2.timestamps.updatedAt.should.be.equal('lastUpdate');

schema2.attributes.createDate.type.name.should.eql('date');
should.exist(schema2.attributes.createDate.default);

schema2.attributes.lastUpdate.type.name.should.eql('date');
should.exist(schema2.attributes.lastUpdate.default);

done();
});


it('Schema with use Document Types', function (done) {
var schema = new Schema({
id: {
Expand Down

0 comments on commit 297ff76

Please sign in to comment.