manifest_test.rb 2.7 KB

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