manifest_test.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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" do
  59. mtime = Time.now - 1
  60. stub(store).mtime(index_path) { mtime }
  61. assert_equal mtime.to_i, manifest.as_json.first[:mtime]
  62. end
  63. end
  64. context "when the doc doesn't have an index" do
  65. it "doesn't include the doc" do
  66. stub(store).exist?(db_path) { true }
  67. stub(store).exist?(index_path) { false }
  68. assert_empty manifest.as_json
  69. end
  70. end
  71. context "when the doc doesn't have a db" do
  72. it "doesn't include the doc" do
  73. stub(store).exist?(index_path) { true }
  74. stub(store).exist?(db_path) { false }
  75. assert_empty manifest.as_json
  76. end
  77. end
  78. end
  79. describe "#to_json" do
  80. it "returns the JSON string for #as_json" do
  81. stub(manifest).as_json { { test: 'ok' } }
  82. assert_equal '{"test":"ok"}', manifest.to_json
  83. end
  84. end
  85. end