Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split spec/integration_spec.rb #350

Merged
merged 38 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fc7bbac
Remove redundant Ebook model in specs
ellnix Feb 28, 2024
c5990b3
Remove test of ruby's Marshal
ellnix Feb 29, 2024
c4644e5
Remove test of Rails cache functionality
ellnix Feb 29, 2024
4c62567
Remove unused UniqUser class from specs
ellnix Feb 29, 2024
0994f36
Remove unused NullableId class from specs
ellnix Feb 29, 2024
355250e
Remove flawed MongoDocument specs
ellnix Feb 29, 2024
093bfbc
Remove unnecessary Color tests
ellnix Feb 29, 2024
26e607c
Move Post specs
ellnix Feb 29, 2024
3fda1a7
Move Color search specs
ellnix Feb 29, 2024
8504a7a
Move Book specs from integration_spec.rb
ellnix Mar 1, 2024
b0cc102
Move Color specs from integration_spec.rb
ellnix Mar 1, 2024
b9a278c
Move Cat and Doc specs from integration_spec.rb
ellnix Mar 1, 2024
4663f91
Move People specs from integration_spec.rb
ellnix Mar 1, 2024
5c7a6e4
Move Disabled specs from integration_spec.rb
ellnix Mar 1, 2024
55bf813
Move Movie, Restaurant, and pagination specs
ellnix May 25, 2024
91ac1af
`require --spec-helper` in .rspec
ellnix May 25, 2024
4cf7ad7
Move 'an imaginary store' tests
ellnix May 25, 2024
a437c8e
Refactor tests
ellnix May 25, 2024
50ec330
Move SerializedDocument and EncodedString tests
ellnix May 26, 2024
702888b
Move NamespacedDocument specs
ellnix May 26, 2024
862262b
Remove redundant `#search` method test
ellnix May 26, 2024
5deaef9
Move deactivation specs
ellnix May 27, 2024
208c9cb
Move :raise_on_failure specs
ellnix May 27, 2024
88bed6f
Fix a few more race conditions in tests
ellnix May 27, 2024
596a7b4
Move EnqueuedDocument-type tests
ellnix May 27, 2024
b328a69
Move Song specs
ellnix May 27, 2024
4f1663b
Move NestedItem tests
ellnix May 27, 2024
dcc4c08
Move misconfiguration test
ellnix May 27, 2024
de4cf44
Fix searchable attribute warning and its test
ellnix May 27, 2024
121b989
Fix regression in #ms_entries specs
ellnix May 27, 2024
84d355b
Merge test refactoring into main
ellnix May 27, 2024
eefe6be
Refactor proximity_precision test
ellnix May 27, 2024
79fe56a
Remove integration spec file & private method test
ellnix May 27, 2024
300b778
Hide ActiveRecord's create_table messages
ellnix May 27, 2024
f1b4ec3
Make rubocop happy
ellnix May 27, 2024
1d70659
Merge main again due to rubocop and integration_spec
ellnix May 27, 2024
cf8a17f
Fix will_paginate test race condition
ellnix May 27, 2024
487c780
Merge branch 'main' into refactor-integration-spec
brunoocasali Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 0 additions & 185 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,191 +112,6 @@
end
end

describe 'An imaginary store' do
before(:all) do
Product.clear_index!(true)

# Google products
@blackberry = Product.create!(name: 'blackberry', href: 'google', tags: ['decent', 'businessmen love it'])
@nokia = Product.create!(name: 'nokia', href: 'google', tags: ['decent'])

# Amazon products
@android = Product.create!(name: 'android', href: 'amazon', tags: ['awesome'])
@samsung = Product.create!(name: 'samsung', href: 'amazon', tags: ['decent'])
@motorola = Product.create!(name: 'motorola', href: 'amazon', tags: ['decent'],
description: 'Not sure about features since I\'ve never owned one.')

# Ebay products
@palmpre = Product.create!(name: 'palmpre', href: 'ebay', tags: ['discontinued', 'worst phone ever'])
@palm_pixi_plus = Product.create!(name: 'palm pixi plus', href: 'ebay', tags: ['terrible'])
@lg_vortex = Product.create!(name: 'lg vortex', href: 'ebay', tags: ['decent'])
@t_mobile = Product.create!(name: 't mobile', href: 'ebay', tags: ['terrible'])

# Yahoo products
@htc = Product.create!(name: 'htc', href: 'yahoo', tags: ['decent'])
@htc_evo = Product.create!(name: 'htc evo', href: 'yahoo', tags: ['decent'])
@ericson = Product.create!(name: 'ericson', href: 'yahoo', tags: ['decent'])

# Apple products
@iphone = Product.create!(name: 'iphone', href: 'apple', tags: ['awesome', 'poor reception'],
description: 'Puts even more features at your fingertips')
@macbook = Product.create!(name: 'macbookpro', href: 'apple')

# Unindexed products
@sekrit = Product.create!(name: 'super sekrit', href: 'amazon', release_date: Time.now + 1.day)
@no_href = Product.create!(name: 'super sekrit too; missing href')

# Subproducts
@camera = Camera.create!(name: 'canon eos rebel t3', href: 'canon')

100.times { Product.create!(name: 'crapoola', href: 'crappy', tags: ['crappy']) }

@products_in_database = Product.all

Product.reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true)
end

it 'is not synchronous' do
p = Product.new
p.valid?

expect(p).not_to be_ms_synchronous
end

it 'is able to reindex manually' do
results_before_clearing = Product.raw_search('')
expect(results_before_clearing['hits'].size).not_to be(0)
Product.clear_index!(true)
results = Product.raw_search('')
expect(results['hits'].size).to be(0)
Product.reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true)
results_after_reindexing = Product.raw_search('')
expect(results_after_reindexing['hits'].size).not_to be(0)
expect(results_before_clearing['hits'].size).to be(results_after_reindexing['hits'].size)
end

describe 'basic searching' do
it 'finds the iphone' do
results = Product.search('iphone')
expect(results.size).to eq(1)
expect(results).to include(@iphone)
end

it 'searches case insensitively' do
results = Product.search('IPHONE')
expect(results.size).to eq(1)
expect(results).to include(@iphone)
end

it 'finds all amazon products' do
results = Product.search('amazon')
expect(results.size).to eq(3)
expect(results).to include(@android, @samsung, @motorola)
end

it 'finds all "palm" phones with wildcard word search' do
results = Product.search('pal')
expect(results.size).to eq(2)
expect(results).to include(@palmpre, @palm_pixi_plus)
end

it 'searches multiple words from the same field' do
results = Product.search('palm pixi plus')
expect(results.size).to eq(1)
expect(results).to include(@palm_pixi_plus)
end

it 'finds using phrase search' do
results = Product.search('coco "palm"')
expect(results.size).to eq(1)
expect(results).to include(@palm_pixi_plus)
end

it 'narrows the results by searching across multiple fields' do
results = Product.search('apple iphone')
expect(results.size).to eq(2)
expect(results).to include(@iphone)
end

it 'does not search on non-indexed fields' do
results = Product.search('features')
expect(results.size).to eq(0)
end

it 'deletes the associated record' do
ipad = Product.create!(name: 'ipad', href: 'apple', tags: ['awesome', 'great battery'],
description: 'Big screen')

ipad.index!(true)
results = Product.search('ipad')
expect(results.size).to eq(1)

ipad.destroy
results = Product.search('ipad')
expect(results.size).to eq(0)
end

context 'when a document cannot be found in ActiveRecord' do
it 'does not throw an exception' do
Product.index.add_documents!(@palmpre.attributes.merge(id: -1))
expect { Product.search('pal').to_json }.not_to raise_error
Product.index.delete_document!(-1)
end

it 'returns the other results if those are still available locally' do
Product.index.add_documents!(@palmpre.attributes.merge(id: -1))
expect(JSON.parse(Product.search('pal').to_json).size).to eq(2)
Product.index.delete_document!(-1)
end
end

it 'does not duplicate an already indexed record' do
expect(Product.search('nokia').size).to eq(1)
@nokia.index!
expect(Product.search('nokia').size).to eq(1)
@nokia.index!
@nokia.index!
expect(Product.search('nokia').size).to eq(1)
end

it 'does not return products that are not indexable' do
@sekrit.index!
@no_href.index!
results = Product.search('sekrit')
expect(results.size).to eq(0)
end

it 'includes items belong to subclasses' do
@camera.index!
results = Product.search('eos rebel')
expect(results.size).to eq(1)
expect(results).to include(@camera)
end

it 'deletes a not-anymore-indexable product' do
results = Product.search('sekrit')
expect(results.size).to eq(0)

@sekrit.release_date = Time.now - 1.day
@sekrit.save!
@sekrit.index!(true)
results = Product.search('sekrit')
expect(results.size).to eq(1)

@sekrit.release_date = Time.now + 1.day
@sekrit.save!
@sekrit.index!(true)
results = Product.search('sekrit')
expect(results.size).to eq(0)
end

it 'finds using synonyms' do
expect(Product.search('pomme').size).to eq(Product.search('apple').size)
expect(Product.search('m_b_p').size).to eq(Product.search('macbookpro').size)
end
end
end

unless OLD_RAILS
describe 'EnqueuedDocument' do
it 'enqueues a job' do
Expand Down
186 changes: 186 additions & 0 deletions spec/system/tech_shop_spec.rb
Copy link
Collaborator Author

@ellnix ellnix May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention in the commit, but is me adding another layer of tests (system tests).

We would have

  • Unit tests
  • Integration tests (checking that systems fit together given simple scenarios)
  • System tests (do common tasks with a setup of realistic data)
  • Acceptance tests (reserved for full-stack tests, i. e. running a rails app and testing it with selenium verify with 100% certainty that a small subset of setups that we lay out work. Could also be helpful in letting us see what needs to be updated on a user's end when we make changes)

Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
require 'support/models/product'

describe 'Tech shop' do
before(:all) do
Product.clear_index!(true)

# Google products
@blackberry = Product.create!(name: 'blackberry', href: 'google', tags: ['decent', 'businessmen love it'])
@nokia = Product.create!(name: 'nokia', href: 'google', tags: ['decent'])

# Amazon products
@android = Product.create!(name: 'android', href: 'amazon', tags: ['awesome'])
@samsung = Product.create!(name: 'samsung', href: 'amazon', tags: ['decent'])
@motorola = Product.create!(name: 'motorola', href: 'amazon', tags: ['decent'],
description: 'Not sure about features since I\'ve never owned one.')

# Ebay products
@palmpre = Product.create!(name: 'palmpre', href: 'ebay', tags: ['discontinued', 'worst phone ever'])
@palm_pixi_plus = Product.create!(name: 'palm pixi plus', href: 'ebay', tags: ['terrible'])
@lg_vortex = Product.create!(name: 'lg vortex', href: 'ebay', tags: ['decent'])
@t_mobile = Product.create!(name: 't mobile', href: 'ebay', tags: ['terrible'])

# Yahoo products
@htc = Product.create!(name: 'htc', href: 'yahoo', tags: ['decent'])
@htc_evo = Product.create!(name: 'htc evo', href: 'yahoo', tags: ['decent'])
@ericson = Product.create!(name: 'ericson', href: 'yahoo', tags: ['decent'])

# Apple products
@iphone = Product.create!(name: 'iphone', href: 'apple', tags: ['awesome', 'poor reception'],
description: 'Puts even more features at your fingertips')
@macbook = Product.create!(name: 'macbookpro', href: 'apple')

# Unindexed products
@sekrit = Product.create!(name: 'super sekrit', href: 'amazon', release_date: Time.now + 1.day)
@no_href = Product.create!(name: 'super sekrit too; missing href')

# Subproducts
@camera = Camera.create!(name: 'canon eos rebel t3', href: 'canon')

100.times { Product.create!(name: 'crapoola', href: 'crappy', tags: ['crappy']) }

@products_in_database = Product.all

Product.reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true)
end

it 'is not synchronous' do
p = Product.new
p.valid?

expect(p).not_to be_ms_synchronous
end

it 'is able to reindex manually' do
results_before_clearing = Product.raw_search('')
expect(results_before_clearing['hits'].size).not_to be(0)
Product.clear_index!(true)
results = Product.raw_search('')
expect(results['hits'].size).to be(0)
Product.reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true)
results_after_reindexing = Product.raw_search('')
expect(results_after_reindexing['hits'].size).not_to be(0)
expect(results_before_clearing['hits'].size).to be(results_after_reindexing['hits'].size)
end

describe 'basic searching' do
it 'finds the iphone' do
results = Product.search('iphone')
expect(results.size).to eq(1)
expect(results).to include(@iphone)
end

it 'searches case insensitively' do
results = Product.search('IPHONE')
expect(results.size).to eq(1)
expect(results).to include(@iphone)
end

it 'finds all amazon products' do
results = Product.search('amazon')
expect(results.size).to eq(3)
expect(results).to include(@android, @samsung, @motorola)
end

it 'finds all "palm" phones with wildcard word search' do
results = Product.search('pal')
expect(results.size).to eq(2)
expect(results).to include(@palmpre, @palm_pixi_plus)
end

it 'searches multiple words from the same field' do
results = Product.search('palm pixi plus')
expect(results.size).to eq(1)
expect(results).to include(@palm_pixi_plus)
end

it 'finds using phrase search' do
results = Product.search('coco "palm"')
expect(results.size).to eq(1)
expect(results).to include(@palm_pixi_plus)
end

it 'narrows the results by searching across multiple fields' do
results = Product.search('apple iphone')
expect(results.size).to eq(2)
expect(results).to include(@iphone)
end

it 'does not search on non-indexed fields' do
results = Product.search('features')
expect(results.size).to eq(0)
end

it 'deletes the associated record' do
ipad = Product.create!(name: 'ipad', href: 'apple', tags: ['awesome', 'great battery'],
description: 'Big screen')

ipad.index!(true)
results = Product.search('ipad')
expect(results.size).to eq(1)

ipad.destroy
results = Product.search('ipad')
expect(results.size).to eq(0)
end

context 'when a document cannot be found in ActiveRecord' do
it 'does not throw an exception' do
Product.index.add_documents!(@palmpre.attributes.merge(id: -1))
expect { Product.search('pal').to_json }.not_to raise_error
Product.index.delete_document!(-1)
end

it 'returns the other results if those are still available locally' do
Product.index.add_documents!(@palmpre.attributes.merge(id: -1))
expect(JSON.parse(Product.search('pal').to_json).size).to eq(2)
Product.index.delete_document!(-1)
end
end

it 'does not duplicate an already indexed record' do
expect(Product.search('nokia').size).to eq(1)
@nokia.index!
expect(Product.search('nokia').size).to eq(1)
@nokia.index!
@nokia.index!
expect(Product.search('nokia').size).to eq(1)
end

it 'does not return products that are not indexable' do
@sekrit.index!
@no_href.index!
results = Product.search('sekrit')
expect(results.size).to eq(0)
end

it 'includes items belong to subclasses' do
@camera.index!
results = Product.search('eos rebel')
expect(results.size).to eq(1)
expect(results).to include(@camera)
end

it 'deletes a not-anymore-indexable product' do
results = Product.search('sekrit')
expect(results.size).to eq(0)

@sekrit.release_date = Time.now - 1.day
@sekrit.save!
@sekrit.index!(true)
results = Product.search('sekrit')
expect(results.size).to eq(1)

@sekrit.release_date = Time.now + 1.day
@sekrit.save!
@sekrit.index!(true)
results = Product.search('sekrit')
expect(results.size).to eq(0)
end

it 'finds using synonyms' do
expect(Product.search('pomme').size).to eq(Product.search('apple').size)
expect(Product.search('m_b_p').size).to eq(Product.search('macbookpro').size)
end
end
end