validate.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as assert from 'power-assert'
  2. import { Validate } from '@cloudbase/database/src/validate'
  3. import { ErrorCode } from '@cloudbase/database/src/constant'
  4. describe('test/unit/validate.test.ts', () => {
  5. it('isFieldOrder - is valid', () => {
  6. try {
  7. Validate.isFieldOrder('desc')
  8. } catch (e) {
  9. assert(e.message === ErrorCode.DirectionError)
  10. }
  11. })
  12. it('isFieldOrder - is invalid', () => {
  13. try {
  14. Validate.isFieldOrder('desd' as any)
  15. } catch (e) {
  16. assert(e.message === ErrorCode.DirectionError)
  17. }
  18. })
  19. it('isOperator - is valid', () => {
  20. assert(Validate.isOperator('<') === true)
  21. })
  22. it('isOperator - is invalid', () => {
  23. try {
  24. Validate.isOperator('>+' as any)
  25. } catch (error) {
  26. assert(error.message === ErrorCode.OpStrError)
  27. }
  28. })
  29. it('isCollName - should right', () => {
  30. assert(Validate.isCollName('coll-1_2') === true)
  31. })
  32. it('isCollName - can not use _ in begin', () => {
  33. try {
  34. Validate.isCollName('_coll-1')
  35. } catch (error) {
  36. assert(error.message === ErrorCode.CollNameError)
  37. }
  38. })
  39. it('isCollName - can not use @#$ and so on', () => {
  40. try {
  41. Validate.isCollName('coll-1_@#$')
  42. } catch (error) {
  43. assert(error.message === ErrorCode.CollNameError)
  44. }
  45. })
  46. it('isCollName - can not empty', () => {
  47. try {
  48. Validate.isCollName('')
  49. } catch (error) {
  50. assert(error.message === ErrorCode.CollNameError)
  51. }
  52. })
  53. it('isCollName - can not more than 32', () => {
  54. let name = 'abcdefgh12abcdefgh12abcdefgh12abcdefgh12'
  55. try {
  56. Validate.isCollName(name)
  57. } catch (error) {
  58. assert(error.message === ErrorCode.CollNameError)
  59. }
  60. })
  61. it('isDocID - should right', () => {
  62. const docId = 'abcdefABCDEF0123456789ab'
  63. try {
  64. Validate.isDocID(docId)
  65. } catch (error) {
  66. assert(error.message === ErrorCode.DocIDError)
  67. }
  68. })
  69. it('isDocID - can not empty', () => {
  70. try {
  71. Validate.isDocID('')
  72. } catch (error) {
  73. assert(error.message === ErrorCode.DocIDError)
  74. }
  75. })
  76. it('isDocID - can not more than 24', () => {
  77. const docId = 'abcdefABCDEF0123456789abcdef'
  78. try {
  79. Validate.isDocID(docId)
  80. } catch (error) {
  81. assert(error.message === ErrorCode.DocIDError)
  82. }
  83. })
  84. it('isDocID - can not use @#$ and so on', () => {
  85. const docId = 'abcdefABCDEF0123456789@#'
  86. try {
  87. Validate.isDocID(docId)
  88. } catch (error) {
  89. assert(error.message === ErrorCode.DocIDError)
  90. }
  91. })
  92. })