|
@@ -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
|