store_test.rb 3.1 KB

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