sample.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.sample()', function() {
  6. it('should generate a type that matches', function() {
  7. // Top-level
  8. assertAllSampledValuesMatchType('string');
  9. assertAllSampledValuesMatchType('number');
  10. assertAllSampledValuesMatchType('boolean');
  11. assertAllSampledValuesMatchType({});
  12. assertAllSampledValuesMatchType([]);
  13. assertAllSampledValuesMatchType('lamda');
  14. assertAllSampledValuesMatchType('json');
  15. assertAllSampledValuesMatchType('ref');
  16. // Facted dictionary
  17. assertAllSampledValuesMatchType({x:'string'});
  18. assertAllSampledValuesMatchType({x:'number'});
  19. assertAllSampledValuesMatchType({x:'boolean'});
  20. assertAllSampledValuesMatchType({x:{}});
  21. assertAllSampledValuesMatchType({x:[]});
  22. assertAllSampledValuesMatchType({x:'lamda'});
  23. assertAllSampledValuesMatchType({x:'json'});
  24. assertAllSampledValuesMatchType({x:'ref'});
  25. // Patterned array
  26. assertAllSampledValuesMatchType(['string']);
  27. assertAllSampledValuesMatchType(['number']);
  28. assertAllSampledValuesMatchType(['boolean']);
  29. assertAllSampledValuesMatchType([{}]);
  30. assertAllSampledValuesMatchType([[]]);
  31. assertAllSampledValuesMatchType(['lamda']);
  32. assertAllSampledValuesMatchType(['json']);
  33. assertAllSampledValuesMatchType(['ref']);
  34. // Patterned array in faceted dictionary
  35. assertAllSampledValuesMatchType({x:['string']});
  36. assertAllSampledValuesMatchType({x:['number']});
  37. assertAllSampledValuesMatchType({x:['boolean']});
  38. assertAllSampledValuesMatchType({x:[{}]});
  39. assertAllSampledValuesMatchType({x:[[]]});
  40. assertAllSampledValuesMatchType({x:['lamda']});
  41. assertAllSampledValuesMatchType({x:['json']});
  42. assertAllSampledValuesMatchType({x:['ref']});
  43. // Faceted dictionary in patterned array
  44. assertAllSampledValuesMatchType([{x:['string']}]);
  45. assertAllSampledValuesMatchType([{x:['number']}]);
  46. assertAllSampledValuesMatchType([{x:['boolean']}]);
  47. assertAllSampledValuesMatchType([{x:[{}]}]);
  48. assertAllSampledValuesMatchType([{x:[[]]}]);
  49. assertAllSampledValuesMatchType([{x:['lamda']}]);
  50. assertAllSampledValuesMatchType([{x:['json']}]);
  51. assertAllSampledValuesMatchType([{x:['ref']}]);
  52. });
  53. });
  54. /**
  55. * Helper method to check that values samples for a specific type indeed match that type.
  56. * @param {[type]} expectedTypeSchema [description]
  57. * @return {[type]} [description]
  58. */
  59. function assertAllSampledValuesMatchType(expectedTypeSchema){
  60. var n = 25;
  61. var samples = rttc.sample(expectedTypeSchema, n);
  62. _.each(samples, function (sample) {
  63. assert.doesNotThrow(function (){
  64. rttc.validateStrict(expectedTypeSchema, sample);
  65. });
  66. });
  67. // Ensure NO MORE THAN `n` (not guaranteed to have exactly `n` samples)
  68. assert(samples.length <= n);
  69. // Ensure uniqueness of generated samples
  70. assert.equal(_.uniq(samples).length, samples.length, 'Expected samples to be unique ('+_.uniq(samples)+') but they weren\'t: '+samples);
  71. }