Browse Source

Specs

master
Dylan Baker 2 years ago
parent
commit
ed3e225aba
6 changed files with 66 additions and 2 deletions
  1. 1
    0
      .gitignore
  2. 2
    1
      Gemfile
  3. 14
    0
      Gemfile.lock
  4. 9
    1
      Rakefile
  5. 28
    0
      spec/scraper_spec.rb
  6. 12
    0
      spec/spec_helper.rb

+ 1
- 0
.gitignore View File

@@ -3,3 +3,4 @@ vendor/
3 3
 web/public/*.css
4 4
 web/public/*.js
5 5
 db/log
6
+spec/vcr

+ 2
- 1
Gemfile View File

@@ -13,5 +13,6 @@ gem 'rspec', '~> 3.9'
13 13
 gem 'sassc', '~> 2.2'
14 14
 gem 'sequel', '~> 5.30'
15 15
 gem 'sinatra', '~> 2.0'
16
-
17 16
 gem "truncato", "~> 0.7.11"
17
+gem "vcr", "~> 6.0"
18
+gem "webmock", "~> 3.14"

+ 14
- 0
Gemfile.lock View File

@@ -1,9 +1,14 @@
1 1
 GEM
2 2
   remote: https://rubygems.org/
3 3
   specs:
4
+    addressable (2.8.0)
5
+      public_suffix (>= 2.0.2, < 5.0)
6
+    crack (0.4.5)
7
+      rexml
4 8
     diff-lcs (1.3)
5 9
     dotenv (2.7.5)
6 10
     ffi (1.12.2)
11
+    hashdiff (1.0.1)
7 12
     htmlentities (4.3.4)
8 13
     httparty (0.18.0)
9 14
       mime-types (~> 3.0)
@@ -18,10 +23,12 @@ GEM
18 23
     nokogiri (1.10.9)
19 24
       mini_portile2 (~> 2.4.0)
20 25
     pg (1.2.3)
26
+    public_suffix (4.0.6)
21 27
     rack (2.2.2)
22 28
     rack-protection (2.0.8.1)
23 29
       rack
24 30
     rake (13.0.1)
31
+    rexml (3.2.5)
25 32
     rspec (3.9.0)
26 33
       rspec-core (~> 3.9.0)
27 34
       rspec-expectations (~> 3.9.0)
@@ -48,6 +55,11 @@ GEM
48 55
     truncato (0.7.11)
49 56
       htmlentities (~> 4.3.1)
50 57
       nokogiri (>= 1.7.0, <= 2.0)
58
+    vcr (6.0.0)
59
+    webmock (3.14.0)
60
+      addressable (>= 2.8.0)
61
+      crack (>= 0.3.2)
62
+      hashdiff (>= 0.4.0, < 2.0.0)
51 63
 
52 64
 PLATFORMS
53 65
   ruby
@@ -64,6 +76,8 @@ DEPENDENCIES
64 76
   sequel (~> 5.30)
65 77
   sinatra (~> 2.0)
66 78
   truncato (~> 0.7.11)
79
+  vcr (~> 6.0)
80
+  webmock (~> 3.14)
67 81
 
68 82
 BUNDLED WITH
69 83
    2.1.4

+ 9
- 1
Rakefile View File

@@ -15,11 +15,19 @@ end
15 15
 task 'migrate_test' do
16 16
   require_relative './db/migrate'
17 17
 
18
+  TEST_DB = Sequel.connect(
19
+    adapter: :postgres,
20
+    database: ENV['DB_DATABASE'] + '_test',
21
+    user: ENV['DB_USERNAME'],
22
+    password: ENV['DB_PASSWORD'],
23
+    logger: ENV['APP_ENV'] == 'development' ? Logger.new('db/log') : nil
24
+  )
25
+
18 26
   migrate(db: TEST_DB)
19 27
 end
20 28
 
21 29
 task 'scrape' do
22
-  require_relative './db/scrape'
30
+  require_relative './db/scraper'
23 31
 
24 32
   should_log = ENV['APP_ENV'] == 'development' || ARGV.include?('--log')
25 33
   scraper = Scraper.new(log: should_log)

+ 28
- 0
spec/scraper_spec.rb View File

@@ -0,0 +1,28 @@
1
+require 'dotenv/load'
2
+require 'spec_helper'
3
+
4
+require_relative '../db/scraper'
5
+
6
+RSpec.describe Scraper do
7
+  around(:each) do |example|
8
+    DB.transaction(rollback: :always, auto_savepoint: true) { example.run }
9
+  end
10
+
11
+  subject { described_class.new(log: true) }
12
+
13
+  describe '#initialize' do
14
+    it 'authenticates' do
15
+      expect(subject.send(:cookie)).to_not be_nil
16
+    end
17
+  end
18
+
19
+  describe '#scrape' do
20
+    it 'creates new threads and posts' do
21
+      VCR.use_cassette "create_new_threads_and_posts" do
22
+        expect { subject.scrape }
23
+          .to  change { VLV::Thread.count }.by(109)
24
+          .and change { VLV::Post.count   }.by_at_least(5000)
25
+      end
26
+    end
27
+  end
28
+end

+ 12
- 0
spec/spec_helper.rb View File

@@ -0,0 +1,12 @@
1
+require 'vcr'
2
+require "webmock/rspec"
3
+
4
+ENV["DB_DATABASE"] = ENV["DB_DATABASE"] + '_test'
5
+
6
+WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+VCR.configure do |c|
9
+  c.cassette_library_dir = "spec/vcr"
10
+  c.hook_into :webmock
11
+  c.ignore_localhost = true
12
+end

Loading…
Cancel
Save