Skip to content

Commit

Permalink
test: add tests for part of speech
Browse files Browse the repository at this point in the history
  • Loading branch information
vpukhanov committed Jul 28, 2024
1 parent 7ca4260 commit 753e615
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
7 changes: 6 additions & 1 deletion spec/models/term_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"word" => "example",
"definition" => "This is a [sample] definition.",
"example" => "Here is a [sample] example.",
"author" => "Test Author"
"author" => "Test Author",
"part_of_speech" => "noun"
}
end

Expand All @@ -28,6 +29,10 @@
it "has an author" do
expect(term.author).to eq("Test Author")
end

it "has a part of speech" do
expect(term.part_of_speech).to eq("noun")
end
end

describe "#convert_links" do
Expand Down
68 changes: 68 additions & 0 deletions spec/services/part_of_speech_classifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require "rails_helper"

RSpec.describe PartOfSpeechClassifier do
let(:classifier) { described_class.new }

describe "#classify" do
it "classifies a noun correctly" do
expect(described_class.classify("cat", "A small domesticated carnivorous mammal.")).to eq("noun")
end

it "classifies a verb correctly" do
expect(described_class.classify("run", "To move at a speed faster than a walk.")).to eq("verb")
end

it "classifies an adjective correctly" do
expect(described_class.classify("beautiful", "Pleasing the senses or mind aesthetically.")).to eq("adjective")
end

it "classifies an adverb correctly" do
expect(described_class.classify("quickly", "At a fast speed; rapidly.")).to eq("adverb")
end

it "returns nil for multi-word phrases" do
expect(classifier.classify("ice cream", "A sweet frozen food.")).to be_nil
end

it "uses the definition for context when the word alone is ambiguous" do
expect(classifier.classify("bank", "A financial institution.")).to eq("noun")
expect(classifier.classify("bank", "To rely on or trust in.")).to eq("verb")
end

it "handles capitalized words" do
expect(classifier.classify("Apple", "A technology company.")).to eq("noun")
end

it "classifies less common parts of speech" do
expect(classifier.classify("in", "Used to indicate location.")).to eq("preposition")
expect(classifier.classify("and", "Used to connect words.")).to eq("conjugation")
expect(classifier.classify("the", "Definite article.")).to eq("determiner")
end

it 'returns "misc" for unknown or unclassifiable words' do
expect(classifier.classify("xylophone", "A musical instrument.")).to eq("misc")
end

it "handles words with special characters" do
expect(classifier.classify("co-operate", "To work together.")).to eq("verb")
end
end

describe "edge cases" do
it "handles empty strings" do
expect(classifier.classify("", "")).to be_nil
end

it "handles nil input gracefully" do
expect { classifier.classify(nil, nil) }.not_to raise_error
end

it "handles very long words" do
expect(classifier.classify("supercalifragilisticexpialidocious", "An extraordinarily good word.")).to eq("adjective")
end

it "handles words with numbers" do
expect(classifier.classify("4g", "Fourth generation of mobile networks.")).to eq("noun")
end
end
end
22 changes: 22 additions & 0 deletions spec/services/term_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"author" => "API Author"
} ]
end
let(:part_of_speech) { "noun" }

before do
allow(UrbanDictionaryService).to receive(:define).with(word).and_return(api_response)
allow(PartOfSpeechClassifier).to receive(:classify).with(word, "This is a [sample] definition.").and_return(part_of_speech)
end

it "returns a Term object" do
Expand All @@ -27,6 +29,26 @@
expect(result.definition).to eq('This is a <a href="/terms/sample">sample</a> definition.')
expect(result.example).to eq('Here is a <a href="/terms/sample">sample</a> example.')
expect(result.author).to eq("API Author")
expect(result.part_of_speech).to eq(part_of_speech)
end

it "calls UrbanDictionaryService.define with the correct word" do
described_class.fetch(word)
expect(UrbanDictionaryService).to have_received(:define).with(word)
end

it "calls PartOfSpeechClassifier.classify with the correct word and definition" do
described_class.fetch(word)
expect(PartOfSpeechClassifier).to have_received(:classify).with(word, "This is a [sample] definition.")
end

context "when PartOfSpeechClassifier returns nil" do
let(:part_of_speech) { nil }

it "sets part_of_speech to nil on the Term object" do
result = described_class.fetch(word)
expect(result.part_of_speech).to be_nil
end
end
end
end
31 changes: 31 additions & 0 deletions spec/system/term_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

before do
allow(UrbanDictionaryService).to receive(:define).and_return(sample_definition)
allow(PartOfSpeechClassifier).to receive(:classify).and_return("noun")
end

it "allows a user to look up a term" do
Expand All @@ -26,6 +27,7 @@

expect(page).to have_content("example")
expect(page).to have_content("This is a sample definition.")
expect(page).to have_content("noun")
end

it "shows a not found page for non-existent terms" do
Expand All @@ -47,4 +49,33 @@

expect(current_path).to eq(root_path)
end

it "doesn't show part of speech for multi-word terms" do
multi_word_definition = [
{
"word" => "ice cream",
"definition" => "A frozen dessert.",
"example" => "I love eating ice cream.",
"author" => "Test Author"
}
]
allow(UrbanDictionaryService).to receive(:define).with("ice cream").and_return(multi_word_definition)
allow(PartOfSpeechClassifier).to receive(:classify).and_return(nil)

visit root_path
fill_in "term", with: "ice cream"
click_button "Define"

expect(page).to have_content("ice cream")
expect(page).to have_content("A frozen dessert.")
expect(page).not_to have_content("noun")
end

it "shows a tooltip for the part of speech sparkle" do
visit root_path
fill_in "term", with: "example"
click_button "Define"

expect(page).to have_css("span.tooltip")
end
end

0 comments on commit 753e615

Please sign in to comment.