22# -*- coding: utf-8 -*-
33
44import unittest
5+ import pytest
56
67from rapidfuzz import process , fuzz , utils
78
1920]
2021
2122class RatioTest (unittest .TestCase ):
22- def setUp (self ):
23- self .s1 = "new york mets"
24- self .s1a = "new york mets"
25- self .s2 = "new YORK mets"
26- self .s3 = "the wonderful new york mets"
27- self .s4 = "new york mets vs atlanta braves"
28- self .s5 = "atlanta braves vs new york mets"
29- self .s6 = "new york mets - atlanta braves"
30-
31- def testEqual (self ):
32- self .assertEqual (fuzz .ratio (self .s1 , self .s1a ),100 )
33-
34- def testCaseInsensitive (self ):
35- self .assertNotEqual (fuzz .ratio (self .s1 , self .s2 ),100 )
36- self .assertEqual (fuzz .ratio (self .s1 , self .s2 , processor = True ),100 )
23+ s1 = "new york mets"
24+ s1a = "new york mets"
25+ s2 = "new YORK mets"
26+ s3 = "the wonderful new york mets"
27+ s4 = "new york mets vs atlanta braves"
28+ s5 = "atlanta braves vs new york mets"
29+ s6 = "new york mets - atlanta braves"
30+
31+ def testNoProcessor (self ):
32+ self .assertEqual (fuzz .ratio (self .s1 , self .s1a ), 100 )
33+ self .assertNotEqual (fuzz .ratio (self .s1 , self .s2 ), 100 )
3734
3835 def testPartialRatio (self ):
39- self .assertEqual (fuzz .partial_ratio (self .s1 , self .s3 ),100 )
36+ self .assertEqual (fuzz .partial_ratio (self .s1 , self .s3 ), 100 )
4037
4138 def testTokenSortRatio (self ):
42- self .assertEqual (fuzz .token_sort_ratio (self .s1 , self .s1a ),100 )
39+ self .assertEqual (fuzz .token_sort_ratio (self .s1 , self .s1a ), 100 )
4340
4441 def testPartialTokenSortRatio (self ):
45- self .assertEqual (fuzz .partial_token_sort_ratio (self .s1 , self .s1a ),100 )
46- self .assertEqual (fuzz .partial_token_sort_ratio (self .s4 , self .s5 ),100 )
42+ self .assertEqual (fuzz .partial_token_sort_ratio (self .s1 , self .s1a ), 100 )
43+ self .assertEqual (fuzz .partial_token_sort_ratio (self .s4 , self .s5 ), 100 )
4744
4845 def testTokenSetRatio (self ):
4946 self .assertEqual (fuzz .token_set_ratio (self .s4 , self .s5 ),100 )
@@ -100,27 +97,34 @@ def testQRatioUnicodeString(self):
10097 score = fuzz .QRatio (s1 , s2 )
10198 self .assertEqual (0 , score )
10299
103- def testWithProcessor (self ):
104- """
105- Any scorer should accept any type as s1 and s2, as long as it is a string
106- after preprocessing.
107- """
108- s1 = ["chicago cubs vs new york mets" , "CitiField" , "2011-05-11" , "8pm" ]
109- s2 = ["chicago cubs vs new york mets" , "CitiFields" , "2012-05-11" , "9pm" ]
110-
111- for scorer in scorers :
112- score = scorer (s1 , s2 , processor = lambda event : event [0 ])
113- self .assertEqual (score , 100 )
114-
115- def testHelp (self ):
116- """
117- test that all help texts can be printed without throwing an exception,
118- since they are implemented in C++ aswell
119- """
120-
121- for scorer in scorers :
122- help (scorer )
123100
101+ @pytest .mark .parametrize ("processor" , [True , utils .default_process , lambda s : utils .default_process (s )])
102+ def testRatioCaseInsensitive (processor ):
103+ assert fuzz .ratio (RatioTest .s1 , RatioTest .s2 , processor = processor ) == 100
104+
105+ @pytest .mark .parametrize ("processor" , [False , None , lambda s : s ])
106+ def testRatioNotCaseInsensitive (processor ):
107+ assert fuzz .ratio (RatioTest .s1 , RatioTest .s2 , processor = processor ) != 100
108+
109+ @pytest .mark .parametrize ("scorer" , scorers )
110+ def testCustomProcessor (scorer ):
111+ """
112+ Any scorer should accept any type as s1 and s2, as long as it is a string
113+ after preprocessing.
114+ """
115+ s1 = ["chicago cubs vs new york mets" , "CitiField" , "2011-05-11" , "8pm" ]
116+ s2 = ["chicago cubs vs new york mets" , "CitiFields" , "2012-05-11" , "9pm" ]
117+ s3 = ["different string" , "CitiFields" , "2012-05-11" , "9pm" ]
118+ assert scorer (s1 , s2 , processor = lambda event : event [0 ]) == 100
119+ assert scorer (s2 , s3 , processor = lambda event : event [0 ]) != 100
120+
121+ @pytest .mark .parametrize ("scorer" , scorers )
122+ def testHelp (scorer ):
123+ """
124+ test that all help texts can be printed without throwing an exception,
125+ since they are implemented in C++ aswell
126+ """
127+ help (scorer )
124128
125129if __name__ == '__main__' :
126130 unittest .main ()
0 commit comments