Skip to content

Commit 77aa6a9

Browse files
committed
Merge pull request strongloop-archive#50 from zero5100/tdb_length_fix
Ensure that a generated string value is not longer than the property's specified length
2 parents 5e0528a + e813380 commit 77aa6a9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/test-data-builder.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ TestDataBuilder.prototype._gatherDefaultPropertyValues = function(Model) {
120120

121121
switch (prop.type) {
122122
case String:
123-
result[name] = 'a test ' + name + ' #' + (++valueCounter);
123+
var generatedString = 'a test ' + name + ' #' + (++valueCounter);
124+
125+
// If this property has a maximum length, ensure that the generated
126+
// string is not longer than the property's max length
127+
if (prop.length) {
128+
// Chop off the front part of the string so it is equal to the length
129+
generatedString = generatedString.substring(
130+
generatedString.length - prop.length);
131+
}
132+
result[name] = generatedString;
124133
break;
125134
case Number:
126135
result[name] = 1230000 + (++valueCounter);

test/test-data-builder.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ describe('TestDataBuilder', function() {
4848
itAutoFillsRequiredPropertiesWithUniqueValuesFor(Number);
4949
itAutoFillsRequiredPropertiesWithUniqueValuesFor(Date);
5050

51+
it('limits the length of the generated string value to the property length', function(done) {
52+
var testMaxStringLength = 10;
53+
givenTestModel({
54+
required1: { type: String, required: true },
55+
required2: { type: String, required: true, length: testMaxStringLength }
56+
});
57+
58+
new TestDataBuilder()
59+
.define('model', TestModel, {})
60+
.buildTo(this, function(err) {
61+
if (err) return done(err);
62+
expect(this.model.required1).to.not.equal(this.model.required2);
63+
expect(this.model.required2).to.have.length.of.at.most(testMaxStringLength);
64+
done();
65+
}.bind(this));
66+
});
67+
5168
it('auto-fills required Boolean properties with false', function(done) {
5269
givenTestModel({
5370
required: { type: Boolean, required: true }

0 commit comments

Comments
 (0)