store_test.rb 3.9 KB

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