cache_test.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class CacheTest < ActiveSupport::TestCase
  4. test 'cache' do
  5. tests = [
  6. # test 1
  7. {
  8. set: {
  9. key: '123',
  10. data: {
  11. key: 'some value',
  12. }
  13. },
  14. verify: {
  15. key: '123',
  16. data: {
  17. key: 'some value',
  18. }
  19. },
  20. },
  21. # test 2
  22. {
  23. set: {
  24. key: '123',
  25. data: {
  26. key: 'some valueöäüß',
  27. }
  28. },
  29. verify: {
  30. key: '123',
  31. data: {
  32. key: 'some valueöäüß',
  33. }
  34. },
  35. },
  36. # test 3
  37. {
  38. delete: {
  39. key: '123',
  40. },
  41. verify: {
  42. key: '123',
  43. data: nil
  44. },
  45. },
  46. # test 4
  47. {
  48. set: {
  49. key: '123',
  50. data: {
  51. key: 'some valueöäüß2',
  52. }
  53. },
  54. verify: {
  55. key: '123',
  56. data: {
  57. key: 'some valueöäüß2',
  58. }
  59. },
  60. },
  61. # test 5
  62. {
  63. cleanup: true,
  64. verify: {
  65. key: '123',
  66. data: nil
  67. },
  68. },
  69. # test 6
  70. {
  71. set: {
  72. key: '123',
  73. data: {
  74. key: 'some valueöäüß2',
  75. },
  76. param: {
  77. expires_in: 3.seconds,
  78. }
  79. },
  80. sleep: 5,
  81. verify: {
  82. key: '123',
  83. data: nil
  84. },
  85. },
  86. ]
  87. tests.each { |test|
  88. if test[:set]
  89. Cache.write(test[:set], test[:set][:data])
  90. end
  91. if test[:delete]
  92. Cache.delete(test[:delete][:key])
  93. end
  94. if test[:cleanup]
  95. Cache.clear
  96. end
  97. if test[:sleep]
  98. sleep test[:sleep]
  99. end
  100. if test[:verify]
  101. cache = Cache.get(test[:verify])
  102. assert_equal(cache, test[:verify][:data], 'verify')
  103. end
  104. }
  105. end
  106. # verify if second cache write overwrite first one
  107. test 'cache reset' do
  108. Cache.write('some_reset_key', 123)
  109. Cache.write('some_reset_key', 12_356)
  110. cache = Cache.get('some_reset_key')
  111. assert_equal(cache, 12_356, 'verify')
  112. end
  113. end