ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

101 lines
2.5 KiB

  1. require 'rails_helper'
  2. require 'rake'
  3. require 'open3'
  4. RSpec.describe 'nico:export' do
  5. let(:task) { Rake::Task['nico:export'] }
  6. let(:success_status) { instance_double(Process::Status, success?: true) }
  7. let(:failure_status) { instance_double(Process::Status, success?: false) }
  8. def create_post(url)
  9. Post.create!(url:)
  10. end
  11. before(:all) do
  12. Rails.application.load_tasks unless Rake::Task.task_defined?('nico:export')
  13. end
  14. before do
  15. task.reenable
  16. allow(ENV).to receive(:fetch).with('MYSQL_USER').and_return('mysql-user')
  17. allow(ENV).to receive(:fetch).with('MYSQL_PASS').and_return('mysql-pass')
  18. allow(ENV).to receive(:fetch).with('NIZIKA_NICO_PATH').and_return('/srv/nizika-nico')
  19. end
  20. describe 'export' do
  21. it 'exports nicovideo ids to shared nico DB' do
  22. create_post('https://www.nicovideo.jp/watch/sm12345?ref=foo')
  23. create_post('https://www.nicovideo.jp/watch/so67890#comments')
  24. create_post('https://www.nicovideo.jp/watch/nm24680')
  25. create_post('https://example.com/watch/sm99999')
  26. expect(Open3).to receive(:capture3) do |env, *args, **kwargs|
  27. expect(env).to eq(
  28. {
  29. 'MYSQL_USER' => 'mysql-user',
  30. 'MYSQL_PASS' => 'mysql-pass',
  31. },
  32. )
  33. expect(args.take(3)).to eq(
  34. [
  35. 'python3',
  36. '-m',
  37. 'tracked_videos.put_bulk_upsert',
  38. ],
  39. )
  40. expect(args.drop(3)).to contain_exactly(
  41. 'sm12345',
  42. 'so67890',
  43. 'nm24680',
  44. )
  45. expect(kwargs).to eq(chdir: '/srv/nizika-nico')
  46. ['', '', success_status]
  47. end
  48. task.invoke
  49. end
  50. it 'deduplicates video ids' do
  51. create_post('https://www.nicovideo.jp/watch/sm12345')
  52. create_post('https://www.nicovideo.jp/watch/sm12345?from=1')
  53. expect(Open3).to receive(:capture3) do |_env, *args, **_kwargs|
  54. expect(args.drop(3)).to eq(['sm12345'])
  55. ['', '', success_status]
  56. end
  57. task.invoke
  58. end
  59. it 'does not call python when there are no nicovideo posts' do
  60. create_post('https://example.com/watch/sm12345')
  61. expect(Open3).not_to receive(:capture3)
  62. task.invoke
  63. end
  64. it 'raises stderr when python command fails' do
  65. create_post('https://www.nicovideo.jp/watch/sm12345')
  66. allow(Open3).to receive(:capture3).and_return(
  67. [
  68. '',
  69. 'bulk upsert failed',
  70. failure_status,
  71. ],
  72. )
  73. expect {
  74. task.invoke
  75. }.to raise_error(RuntimeError, 'bulk upsert failed')
  76. end
  77. end
  78. end