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.

search_spec.rb 1.6KB

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