manifest_test.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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" do
  49. before do
  50. stub(store).exist?(index_path) { true }
  51. end
  52. it "includes the doc's JSON representation" do
  53. json = manifest.as_json
  54. assert_equal 1, json.length
  55. assert_empty doc.as_json.keys - json.first.keys
  56. end
  57. it "adds an :mtime attribute" do
  58. mtime = Time.now - 1
  59. stub(store).mtime(index_path) { mtime }
  60. assert_equal mtime.to_i, manifest.as_json.first[:mtime]
  61. end
  62. end
  63. context "when the doc doesn't have an index" do
  64. it "doesn't include the doc" do
  65. stub(store).exist?(index_path) { false }
  66. assert_empty manifest.as_json
  67. end
  68. end
  69. end
  70. describe "#to_json" do
  71. it "returns the JSON string for #as_json" do
  72. stub(manifest).as_json { { test: 'ok' } }
  73. assert_equal '{"test":"ok"}', manifest.to_json
  74. end
  75. end
  76. end