mysql_storage_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # coding: utf-8
  2. require 'spec_helper'
  3. require 'polipus/storage/mysql_store'
  4. describe Polipus::Storage::MysqlStore do
  5. let(:test_db_name) { 'polipus_mysql_store_spec' }
  6. let(:options) do
  7. {
  8. host: 'localhost',
  9. username: 'root',
  10. password: '',
  11. database: test_db_name,
  12. table_name: 'rspec_pages'
  13. }
  14. end
  15. let(:my)do
  16. o = options.dup
  17. o.delete :database
  18. Mysql2::Client.new o
  19. end
  20. let(:db) { Mysql2::Client.new options }
  21. let(:page)do
  22. Polipus::Page.new 'http://www.google.com/',
  23. body: '<html>
  24. <body>
  25. <a href="/a/1">1</a>
  26. <a href="/a/2">2</a>
  27. </body>
  28. </html>',
  29. code: 201,
  30. depth: 1,
  31. referer: 'http://www.google.com/1',
  32. response_time: 1,
  33. fetched: true,
  34. fetched_at: Time.now,
  35. error: 'an error',
  36. headers: { 'content-type' => ['text/html'] }
  37. end
  38. let(:storage) { Polipus::Storage.mysql_store(options, options[:table_name]) }
  39. before(:each) do
  40. my.query("CREATE DATABASE IF NOT EXISTS #{test_db_name}")
  41. end
  42. after(:each) do
  43. my.query("DROP DATABASE #{test_db_name}")
  44. end
  45. context 'CREATE' do
  46. it 'should store a page' do
  47. page.user_data.a = 1
  48. storage.add(page).should eq Digest::MD5.hexdigest(page.url.to_s)
  49. storage.count.should be 1
  50. storage.exists?(page).should be true
  51. end
  52. end
  53. context 'DELETE' do
  54. let(:filled_storage) do
  55. storage.add page
  56. storage
  57. end
  58. it 'should delete a page' do
  59. filled_storage.remove page
  60. filled_storage.exists?(page).should be false
  61. filled_storage.count.should be 0
  62. end
  63. it 'should empty the storage' do
  64. 2.times do |i|
  65. p = page.to_hash
  66. p['url'] = "#{p['url']}/#{i}"
  67. storage.add Polipus::Page.from_hash(p)
  68. end
  69. filled_storage.count.should be 3
  70. filled_storage.clear
  71. filled_storage.count.should be 0
  72. end
  73. end
  74. context 'UPDATE' do
  75. let(:filled_storage) do
  76. storage.add page
  77. storage
  78. end
  79. it 'should update a page' do
  80. filled_storage.add page
  81. end
  82. end
  83. context 'SELECT' do
  84. let(:filled_storage) do
  85. storage.add page
  86. storage
  87. end
  88. it 'should fetch a page' do
  89. p = filled_storage.get page
  90. expect(p).to_not be nil
  91. expect(p).to be_a Polipus::Page
  92. expect(p.url.to_s).to eq 'http://www.google.com/'
  93. expect(p.links.count).to be 2
  94. expect(p.headers['content-type']).to eq ['text/html']
  95. expect(p.fetched_at).to be > 0
  96. end
  97. end
  98. context 'CURSOR' do
  99. it 'should iterate over pages' do
  100. 10.times do |i|
  101. p = page.to_hash
  102. p['url'] = "#{p['url']}/#{i}"
  103. storage.add Polipus::Page.from_hash(p)
  104. end
  105. storage.count.should be 10
  106. i = 0
  107. storage.each { i += 1 }
  108. expect(i).to be 10
  109. end
  110. end
  111. end