1
0

manifest_test.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require 'test_helper'
  2. require 'docs'
  3. class ManifestTest < MiniTest::Spec
  4. let :doc do
  5. doc = Class.new Docs::Scraper
  6. doc.name = 'TestDoc'
  7. doc.options[:attribution] = 'foo'
  8. doc
  9. end
  10. let :store do
  11. Docs::NullStore.new
  12. end
  13. let :manifest do
  14. Docs::Manifest.new store, [doc]
  15. end
  16. describe "#store" do
  17. before do
  18. stub(manifest).as_json
  19. end
  20. it "stores a file" do
  21. mock(store).write.with_any_args
  22. manifest.store
  23. end
  24. describe "the file" do
  25. it "is named ::FILENAME" do
  26. mock(store).write Docs::Manifest::FILENAME, anything
  27. manifest.store
  28. end
  29. it "contains the manifest's JSON dump" do
  30. stub(manifest).to_json { 'json' }
  31. mock(store).write anything, 'json'
  32. manifest.store
  33. end
  34. end
  35. end
  36. describe "#as_json" do
  37. let :meta_path do
  38. 'meta_path'
  39. end
  40. before do
  41. stub(doc).meta_path { meta_path }
  42. end
  43. it "returns an array" do
  44. manifest = Docs::Manifest.new store, []
  45. assert_instance_of Array, manifest.as_json
  46. end
  47. context "when the doc has a meta file" do
  48. before do
  49. stub(store).exist?(meta_path) { true }
  50. stub(store).read(meta_path) { '{"name":"Test", "db_size": 776533}' }
  51. end
  52. it "includes the doc's meta representation" do
  53. json = manifest.as_json
  54. assert_equal 1, json.length
  55. assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\"}", json[0].to_s
  56. end
  57. end
  58. context "when the doc doesn't have a meta file" do
  59. it "doesn't include the doc" do
  60. stub(store).exist?(meta_path) { false }
  61. assert_empty manifest.as_json
  62. end
  63. end
  64. end
  65. describe "#to_json" do
  66. it "returns the JSON string for #as_json" do
  67. stub(manifest).as_json { { test: 'ok' } }
  68. assert_equal "{\n \"test\": \"ok\"\n}", manifest.to_json
  69. end
  70. end
  71. end