Browse Source

Add searching specs

master
Dylan Baker 2 years ago
parent
commit
4df2839f8e
4 changed files with 53 additions and 0 deletions
  1. 1
    0
      db/connect.rb
  2. 2
    0
      lib/models/post.rb
  3. 2
    0
      lib/models/thread.rb
  4. 48
    0
      spec/search_spec.rb

+ 1
- 0
db/connect.rb View File

@@ -1,5 +1,6 @@
1 1
 require 'dotenv'
2 2
 require 'logger'
3
+require 'sequel'
3 4
 
4 5
 Dotenv.load(File.expand_path('../.env'))
5 6
 

+ 2
- 0
lib/models/post.rb View File

@@ -1,3 +1,5 @@
1
+require 'sequel'
2
+
1 3
 module VLV
2 4
   class Post < Sequel::Model
3 5
     many_to_one :thread

+ 2
- 0
lib/models/thread.rb View File

@@ -1,3 +1,5 @@
1
+require 'sequel'
2
+
1 3
 module VLV
2 4
   class Thread < Sequel::Model
3 5
     one_to_many :posts

+ 48
- 0
spec/search_spec.rb View File

@@ -0,0 +1,48 @@
1
+require 'dotenv/load'
2
+require 'spec_helper'
3
+
4
+require_relative '../db/connect'
5
+require_relative '../lib/search'
6
+require_relative '../lib/models/post'
7
+require_relative '../lib/models/thread'
8
+
9
+RSpec.describe 'Search' do
10
+  around(:each) do |example|
11
+    DB.transaction(rollback: :always, auto_savepoint: true) { example.run }
12
+  end
13
+
14
+  before do
15
+    @thread1 = VLV::Thread.create(title: "This is a thread with many words in the title")
16
+    @thread2 = VLV::Thread.create(title: "Thread words")
17
+    @post1   = VLV::Post.create(body: "This is a post with many words in the body", thread_id: @thread1.id)
18
+    @post2   = VLV::Post.create(body: "Post words", thread_id: @thread2.id)
19
+  end
20
+
21
+  describe '#search_threads' do
22
+    it "finds threads with matching words" do
23
+      threads = search_threads('thread words', '', nil, nil, nil, nil, false)
24
+      expect(threads.map(&:title)).to match_array([@thread1.title, @thread2.title])
25
+    end
26
+
27
+    context "with exact_match" do
28
+      it "only finds threads with exactly matching phrases" do
29
+        threads = search_threads('thread words', '', nil, nil, nil, nil, true)
30
+        expect(threads.map(&:title)).to match_array([@thread2.title])
31
+      end
32
+    end
33
+  end
34
+
35
+  describe '#search_posts' do
36
+    it "finds posts with matching words" do
37
+      posts = search_posts('post words', '', nil, nil, nil, false)
38
+      expect(posts.map(&:body)).to match_array([@post1.body, @post2.body])
39
+    end
40
+
41
+    context "with exact_match" do
42
+      it "only finds posts with exactly matching phrases" do
43
+        posts = search_posts('post words', '', nil, nil, nil, true)
44
+        expect(posts.map(&:body)).to match_array([@post2.body])
45
+      end
46
+    end
47
+  end
48
+end

Loading…
Cancel
Save