PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/database/mongodb_spec.rb

https://github.com/jeffhos/backup
Ruby | 144 lines | 115 code | 28 blank | 1 comment | 16 complexity | 62ea025343a6c0b2178e25bb2095ad33 MD5 | raw file
  1. # encoding: utf-8
  2. require File.dirname(__FILE__) + '/../spec_helper'
  3. describe Backup::Database::MongoDB do
  4. before do
  5. Backup::Database::MongoDB.any_instance.stubs(:load_defaults!)
  6. end
  7. let(:db) do
  8. Backup::Database::MongoDB.new do |db|
  9. db.name = 'mydatabase'
  10. db.username = 'someuser'
  11. db.password = 'secret'
  12. db.host = 'localhost'
  13. db.port = 123
  14. db.ipv6 = true
  15. db.only_collections = ['users', 'pirates']
  16. db.additional_options = ['--query']
  17. end
  18. end
  19. describe '#new' do
  20. it 'should read the adapter details correctly' do
  21. db.name.should == 'mydatabase'
  22. db.username.should == 'someuser'
  23. db.password.should == 'secret'
  24. db.host.should == 'localhost'
  25. db.port.should == 123
  26. db.only_collections.should == ['users', 'pirates']
  27. db.additional_options.should == '--query'
  28. end
  29. it 'arrays should default to empty arrays when not specified' do
  30. db = Backup::Database::MongoDB.new do |db|
  31. db.name = 'mydatabase'
  32. db.username = 'someuser'
  33. db.password = 'secret'
  34. end
  35. db.only_collections.should == []
  36. db.additional_options.should == ""
  37. end
  38. it 'should ensure the directory is available' do
  39. Backup::Database::MongoDB.any_instance.expects(:mkdir).with("#{Backup::TMP_PATH}/myapp/MongoDB")
  40. Backup::Database::MongoDB.new {}
  41. end
  42. end
  43. describe '#only_collections' do
  44. it 'should return a string for the mongodump selected table to dump option' do
  45. db.collections_to_dump.should == %w[users pirates]
  46. end
  47. end
  48. describe '#credential_options' do
  49. it 'should return the mongo syntax for the credential options' do
  50. db.credential_options.should == "--username='someuser' --password='secret'"
  51. end
  52. it 'should only return the mongo syntax for the user' do
  53. db = Backup::Database::MongoDB.new do |db|
  54. db.username = 'someuser'
  55. end
  56. db.credential_options.should == "--username='someuser'"
  57. end
  58. end
  59. describe '#connectivity_options' do
  60. it 'should return the mongo syntax for the connectivity options' do
  61. db.connectivity_options.should == "--host='localhost' --port='123'"
  62. end
  63. it 'should return only the socket' do
  64. db = Backup::Database::MongoDB.new do |db|
  65. db.host = ''
  66. db.port = 123
  67. end
  68. db.connectivity_options.should == "--port='123'"
  69. end
  70. end
  71. describe '#ipv6' do
  72. it 'should return a mongodb syntax compatible ipv6 flag' do
  73. db.ipv6 = true
  74. db.ipv6.should == '--ipv6'
  75. end
  76. it 'should return an empty string' do
  77. db.ipv6 = nil
  78. db.ipv6.should == ''
  79. end
  80. end
  81. describe '#mongodump_string' do
  82. it 'should return the full mongodump string' do
  83. db.expects(:utility).with(:mongodump).returns('mongodump')
  84. db.mongodump.should ==
  85. "mongodump --db='mydatabase' --username='someuser' --password='secret' " +
  86. "--host='localhost' --port='123' --ipv6 --query --out='#{ File.join(Backup::TMP_PATH, Backup::TRIGGER, 'MongoDB') }'"
  87. end
  88. end
  89. describe '#perform!' do
  90. before do
  91. Backup::Logger.stubs(:message)
  92. db.stubs(:utility).returns('mongodump')
  93. db.stubs(:mkdir)
  94. db.stubs(:run)
  95. end
  96. it 'should run the mongodump command and dump all collections' do
  97. db.only_collections = []
  98. db.expects(:dump!)
  99. db.perform!
  100. end
  101. it 'should run the mongodump command and dump all collections' do
  102. db.only_collections = nil
  103. db.expects(:dump!)
  104. db.perform!
  105. end
  106. it 'should dump only the provided collections' do
  107. db.only_collections = %w[users admins profiles]
  108. db.expects(:specific_collection_dump!)
  109. db.perform!
  110. end
  111. it do
  112. Backup::Logger.expects(:message).with("Backup::Database::MongoDB started dumping and archiving \"mydatabase\".")
  113. db.perform!
  114. end
  115. end
  116. end