store_test.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. require 'test_helper'
  2. class StoreTest < ActiveSupport::TestCase
  3. test 'store fs - get_location' do
  4. sha = 'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
  5. location = Store::Provider::File.get_location(sha)
  6. assert_equal(Rails.root.join('storage', 'fs', 'ed70', '02b4', '39e9a', 'c845f', '22357d8', '22bac14', '44730fbdb6016d3ec9432297b9ec9f73').to_s, location)
  7. end
  8. test 'store fs - empty dir remove' do
  9. sha = 'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
  10. content = 'content'
  11. location = Store::Provider::File.get_location(sha)
  12. result = Store::Provider::File.add(content, sha)
  13. assert(result)
  14. exists = File.exist?(location)
  15. assert(exists)
  16. result = Store::Provider::File.delete(sha)
  17. exists = File.exist?(location)
  18. assert_not(exists)
  19. exists = File.exist?(Rails.root.join('storage', 'fs', 'ed70', '02b4'))
  20. assert_not(exists)
  21. exists = File.exist?(Rails.root.join('storage', 'fs', 'ed70'))
  22. assert_not(exists)
  23. exists = File.exist?(Rails.root.join('storage', 'fs'))
  24. assert(exists)
  25. exists = File.exist?(Rails.root.join('storage'))
  26. assert(exists)
  27. end
  28. test 'store attachment and move it between backends' do
  29. files = [
  30. {
  31. data: 'hello world',
  32. filename: 'test.txt',
  33. o_id: 1,
  34. },
  35. {
  36. data: 'hello world äöüß',
  37. filename: 'testäöüß.txt',
  38. o_id: 2,
  39. },
  40. {
  41. data: File.binread(Rails.root.join('test', 'data', 'pdf', 'test1.pdf')),
  42. filename: 'test.pdf',
  43. o_id: 3,
  44. },
  45. {
  46. data: File.binread(Rails.root.join('test', 'data', 'pdf', 'test1.pdf')),
  47. filename: 'test-again.pdf',
  48. o_id: 4,
  49. },
  50. ]
  51. files.each do |file|
  52. sha = Digest::SHA256.hexdigest(file[:data])
  53. # add attachments
  54. store = Store.add(
  55. object: 'Test',
  56. o_id: file[:o_id],
  57. data: file[:data],
  58. filename: file[:filename],
  59. preferences: {},
  60. created_by_id: 1,
  61. )
  62. assert store
  63. # get list of attachments
  64. attachments = Store.list(
  65. object: 'Test',
  66. o_id: file[:o_id],
  67. )
  68. assert attachments
  69. # sha check
  70. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  71. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  72. # filename check
  73. assert_equal(file[:filename], attachments[0].filename)
  74. # provider check
  75. assert_equal('DB', attachments[0].provider)
  76. end
  77. success = Store::File.verify
  78. assert success, 'verify ok'
  79. Store::File.move('DB', 'File')
  80. files.each do |file|
  81. sha = Digest::SHA256.hexdigest(file[:data])
  82. # get list of attachments
  83. attachments = Store.list(
  84. object: 'Test',
  85. o_id: file[:o_id],
  86. )
  87. assert attachments
  88. # sha check
  89. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  90. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  91. # filename check
  92. assert_equal(file[:filename], attachments[0].filename)
  93. # provider check
  94. assert_equal('File', attachments[0].provider)
  95. end
  96. success = Store::File.verify
  97. assert success, 'verify ok'
  98. Store::File.move('File', 'DB')
  99. files.each do |file|
  100. sha = Digest::SHA256.hexdigest(file[:data])
  101. # get list of attachments
  102. attachments = Store.list(
  103. object: 'Test',
  104. o_id: file[:o_id],
  105. )
  106. assert(attachments)
  107. assert_equal(attachments.count, 1)
  108. # sha check
  109. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  110. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  111. # filename check
  112. assert_equal(file[:filename], attachments[0].filename)
  113. # provider check
  114. assert_equal('DB', attachments[0].provider)
  115. # delete attachments
  116. success = Store.remove(
  117. object: 'Test',
  118. o_id: file[:o_id],
  119. )
  120. assert(success)
  121. # check attachments again
  122. attachments = Store.list(
  123. object: 'Test',
  124. o_id: file[:o_id],
  125. )
  126. assert_not(attachments[0])
  127. end
  128. end
  129. end