From 257a5dd97538305804691a5a77a6865ba2059091 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Tue, 9 Apr 2024 20:11:30 +0400 Subject: [PATCH 1/3] write test for Fuzzy Search --- tests/FuzzyTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/FuzzyTest.php diff --git a/tests/FuzzyTest.php b/tests/FuzzyTest.php new file mode 100644 index 0000000..09d4fad --- /dev/null +++ b/tests/FuzzyTest.php @@ -0,0 +1,31 @@ +assertContains('Espresso', $result); + $this->assertNotContains('Mocha', $result); + } + + public function testLevenshteinDistance(): void + { + $str1 = 'Almond'; + $str2 = 'Coconut'; + + $result = Utility::levenshteinDistance($str1, $str2); + + $this->assertEquals(5, $result); + } +} From f5da963915520ed858fba7a059a9c9cc853e30d1 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Thu, 18 Apr 2024 21:52:11 +0400 Subject: [PATCH 2/3] fuzzytest do cover many more cases --- tests/FuzzyTest.php | 48 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/FuzzyTest.php b/tests/FuzzyTest.php index 09d4fad..23d923e 100644 --- a/tests/FuzzyTest.php +++ b/tests/FuzzyTest.php @@ -10,13 +10,57 @@ class FuzzyTest extends TestCase public function testFuzzySearch(): void { $strings = ['Espresso', 'Cappuccino', 'Latte', 'Americano', 'Mocha']; - $searchTerm = 'Espreso'; - $threshold = 1; + // Test with normal data (correct spelling) + $searchTerm = 'Espresso'; + $threshold = 1; $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); $this->assertContains('Espresso', $result); $this->assertNotContains('Mocha', $result); + + // Test with misspelling within threshold + $searchTerm = 'Espreso'; // Missing 's' + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + + $this->assertContains('Espresso', $result); + + // Test with misspelling exceeding threshold + $searchTerm = 'Espressso'; // Extra 's' + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + + $this->assertContains('Espresso', $result); + + // Test with empty search term + $searchTerm = ''; + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + + $this->assertEquals([], $result); // Expect no matches + + // Test with non-string search term (integer) + $searchTerm = (string) 123; + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + + $this->assertEquals([], $result); // Expect no matches (type mismatch) + + // Test with search term containing special characters + $searchTerm = 'Latte!'; + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + + $this->assertContains('Latte', $result); // Should still match 'Latte' + + // Test with case sensitivity (optional, depending on your needs) + $searchTerm = 'eSPRESSO'; // All uppercase + $threshold = 1; + $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); + // Depending on your implementation, this might match (case-insensitive) + // or not match (case-sensitive). Update assertions accordingly. + } public function testLevenshteinDistance(): void From 8fc14d8327a72724bc95db876ca3b6f7dc09dfd9 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 20 Apr 2024 10:34:52 +0400 Subject: [PATCH 3/3] using data providers and add more cases in testLevenshteinDistance() --- tests/FuzzyTest.php | 91 +++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 56 deletions(-) diff --git a/tests/FuzzyTest.php b/tests/FuzzyTest.php index 23d923e..96bb5fe 100644 --- a/tests/FuzzyTest.php +++ b/tests/FuzzyTest.php @@ -7,69 +7,48 @@ class FuzzyTest extends TestCase { - public function testFuzzySearch(): void + /** + * @dataProvider fuzzySearchDataProvider + */ + public function testFuzzySearch(string $searchTerm, array $strings, int $threshold, array $expected): void { - $strings = ['Espresso', 'Cappuccino', 'Latte', 'Americano', 'Mocha']; - - // Test with normal data (correct spelling) - $searchTerm = 'Espresso'; - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertContains('Espresso', $result); - $this->assertNotContains('Mocha', $result); - - // Test with misspelling within threshold - $searchTerm = 'Espreso'; // Missing 's' - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertContains('Espresso', $result); - - // Test with misspelling exceeding threshold - $searchTerm = 'Espressso'; // Extra 's' - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertContains('Espresso', $result); - - // Test with empty search term - $searchTerm = ''; - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertEquals([], $result); // Expect no matches - - // Test with non-string search term (integer) - $searchTerm = (string) 123; - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertEquals([], $result); // Expect no matches (type mismatch) - - // Test with search term containing special characters - $searchTerm = 'Latte!'; - $threshold = 1; - $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - - $this->assertContains('Latte', $result); // Should still match 'Latte' - - // Test with case sensitivity (optional, depending on your needs) - $searchTerm = 'eSPRESSO'; // All uppercase - $threshold = 1; $result = Utility::fuzzySearch($searchTerm, $strings, $threshold); - // Depending on your implementation, this might match (case-insensitive) - // or not match (case-sensitive). Update assertions accordingly. - + $this->assertEquals($expected, $result); } - public function testLevenshteinDistance(): void + public static function fuzzySearchDataProvider(): array { - $str1 = 'Almond'; - $str2 = 'Coconut'; + $strings = ['Espresso', 'Cappuccino', 'Latte', 'Americano', 'Mocha']; + return [ + ['Espresso', $strings, 1, ['Espresso']], + ['Espreso', $strings, 1, ['Espresso']], // Missing 's' + ['Espressso', $strings, 1, ['Espresso']], // Extra 's' + ['', $strings, 1, []], // Empty search term + [(string) 123, $strings, 1, []], // Non-string search term (integer) + ['Latte!', $strings, 1, ['Latte']], // Search term containing special characters + ['eSPRESSO', $strings, 1, ['Espresso']], // Case sensitivity test + ]; + } + /** + * @dataProvider levenshteinDistanceDataProvider + */ + public function testLevenshteinDistance(string $str1, string $str2, int $expected): void + { $result = Utility::levenshteinDistance($str1, $str2); + $this->assertEquals($expected, $result); + } - $this->assertEquals(5, $result); + public static function levenshteinDistanceDataProvider(): array + { + return [ + ['Almond', 'Coconut', 5], + ['Almond', 'Almond', 0], // Same strings + ['Almond', 'Almon', 1], // Missing character + ['Almond', 'Almondd', 1], // Extra character + ['Almond', 'Almend', 1], // Different character + ['Almond', '', 6], // One string is empty + ['', '', 0], // Both strings are empty + ]; } }