manifest_test.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require 'test_helper'
  2. require 'docs'
  3. class ManifestTest < MiniTest::Spec
  4. let :doc do
  5. Class.new Docs::Doc
  6. end
  7. let :store do
  8. Docs::NullStore.new
  9. end
  10. let :manifest do
  11. Docs::Manifest.new store, [doc]
  12. end
  13. describe "#store" do
  14. before do
  15. stub(manifest).as_json
  16. end
  17. it "stores a file" do
  18. mock(store).write.with_any_args
  19. manifest.store
  20. end
  21. describe "the file" do
  22. it "is named ::FILENAME" do
  23. mock(store).write Docs::Manifest::FILENAME, anything
  24. manifest.store
  25. end
  26. it "contains the manifest's JSON dump" do
  27. stub(manifest).to_json { 'json' }
  28. mock(store).write anything, 'json'
  29. manifest.store
  30. end
  31. end
  32. end
  33. describe "#as_json" do
  34. let :index_path do
  35. 'index_path'
  36. end
  37. let :db_path do
  38. 'db_path'
  39. end
  40. before do
  41. stub(doc).index_path { index_path }
  42. stub(doc).db_path { db_path }
  43. end
  44. it "returns an array" do
  45. manifest = Docs::Manifest.new store, []
  46. assert_instance_of Array, manifest.as_json
  47. end
  48. context "when the doc has an index and a db" do
  49. before do
  50. stub(store).exist?(index_path) { true }
  51. stub(store).exist?(db_path) { true }
  52. end
  53. it "includes the doc's JSON representation" do
  54. json = manifest.as_json
  55. assert_equal 1, json.length
  56. assert_empty doc.as_json.keys - json.first.keys
  57. end
  58. it "adds an :mtime attribute with the greatest of the index and db files' mtime" do
  59. mtime_index = Time.now - 1
  60. mtime_db = Time.now - 2
  61. stub(store).mtime(index_path) { mtime_index }
  62. stub(store).mtime(db_path) { mtime_db }
  63. assert_equal mtime_index.to_i, manifest.as_json.first[:mtime]
  64. mtime_index, mtime_db = mtime_db, mtime_index
  65. assert_equal mtime_db.to_i, manifest.as_json.first[:mtime]
  66. end
  67. it "adds a :db_size attribute" do
  68. stub(store).size(db_path) { 42 }
  69. assert_equal 42, manifest.as_json.first[:db_size]
  70. end
  71. end
  72. context "when the doc doesn't have an index" do
  73. it "doesn't include the doc" do
  74. stub(store).exist?(db_path) { true }
  75. stub(store).exist?(index_path) { false }
  76. assert_empty manifest.as_json
  77. end
  78. end
  79. context "when the doc doesn't have a db" do
  80. it "doesn't include the doc" do
  81. stub(store).exist?(index_path) { true }
  82. stub(store).exist?(db_path) { false }
  83. assert_empty manifest.as_json
  84. end
  85. end
  86. end
  87. describe "#to_json" do
  88. it "returns the JSON string for #as_json" do
  89. stub(manifest).as_json { { test: 'ok' } }
  90. assert_equal '{"test":"ok"}', manifest.to_json
  91. end
  92. end
  93. end