1- # Standard
2- import unittest
3-
41# Third Party
52from matching .matching import Always , Contains , Matcher , to_match
3+ import pytest
64
75
8- class TestAlways ( unittest . TestCase ) :
6+ class TestAlways :
97 # match on any prompt
108 def test_always (self ):
119 expect_response = "expected response"
1210 prompt = "example prompt"
1311 always = Always (expect_response )
1412 actual_response = always .match (prompt )
15- self . assertEqual ( actual_response , expect_response )
13+ assert actual_response == expect_response
1614
1715 # reject empty prompts
1816 def test_always_empty_prompt (self ):
1917 response = "expected response"
2018 prompt = ""
2119 always = Always (response )
2220 actual_response = always .match (prompt )
23- self . assertIsNone ( actual_response )
21+ assert actual_response is None
2422
2523
26- class TestContains ( unittest . TestCase ) :
24+ class TestContains :
2725 def test_contains (self ):
2826 expect_response = "expected response"
2927 prompt = "example prompt"
3028 match_on = ["example" ]
3129 contains = Contains (match_on , expect_response )
3230 actual_response = contains .match (prompt )
33- self . assertEqual ( actual_response , expect_response )
31+ assert actual_response == expect_response
3432
3533 def test_contains_many (self ):
3634 expect_response = "expected response"
3735 prompt = "a much longer example prompt so we can match on many substring elements of this string"
3836 match_on = ["example" , "many substring elements" , "match on" ]
3937 contains = Contains (match_on , expect_response )
4038 actual_response = contains .match (prompt )
41- self . assertEqual ( actual_response , expect_response )
39+ assert actual_response == expect_response
4240
4341 # if any substrings don't match, return None
4442 def test_contains_mismatch (self ):
@@ -47,7 +45,7 @@ def test_contains_mismatch(self):
4745 match_on = ["example" , "many substring elements" , "match on" , "banana" ]
4846 contains = Contains (match_on , response )
4947 actual_response = contains .match (prompt )
50- self . assertIsNone ( actual_response )
48+ assert actual_response is None
5149
5250 # reject empty prompts
5351 def test_contains_empty (self ):
@@ -56,29 +54,30 @@ def test_contains_empty(self):
5654 match_on = ["example" ]
5755 contains = Contains (match_on , response )
5856 actual_response = contains .match (prompt )
59- self . assertIsNone ( actual_response )
57+ assert actual_response is None
6058
6159
62- class TestMatcher ( unittest . TestCase ) :
60+ class TestMatcher :
6361 def test_to_contains (self ):
6462 response = "I am a response"
6563 substr = ["a" , "b" , "c" ]
6664 pattern = {"contains" : substr , "response" : response }
6765 contains = to_match (pattern )
68- self . assertIsInstance (contains , Contains )
69- self . assertEqual ( contains .response , response )
66+ assert isinstance (contains , Contains )
67+ assert contains .response == response
7068
7169 def test_to_always (self ):
7270 response = "I am a response"
7371 always_pattern = {"response" : response }
7472 always = to_match (always_pattern )
75- self . assertIsInstance (always , Always )
76- self . assertEqual ( always .response , response )
73+ assert isinstance (always , Always )
74+ assert always .response == response
7775
7876 def test_to_invalid (self ):
7977 response = "I am a response"
8078 invalid_pattern = {"banana" : "foo" , "response" : response }
81- self .assertRaises (Exception , to_match , invalid_pattern )
79+ with pytest .raises (TypeError ):
80+ to_match (invalid_pattern )
8281
8382 def test_find_match_contains (self ):
8483 expect_response = "I am a response"
@@ -88,7 +87,7 @@ def test_find_match_contains(self):
8887
8988 prompt = "example prompt"
9089 actual_response = matcher .find_match (prompt )
91- self . assertEqual ( actual_response , expect_response )
90+ assert actual_response == expect_response
9291
9392 def test_find_match_always (self ):
9493 expect_response = "I am a response"
@@ -97,7 +96,7 @@ def test_find_match_always(self):
9796
9897 prompt = "example prompt"
9998 actual_response = matcher .find_match (prompt )
100- self . assertEqual ( actual_response , expect_response )
99+ assert actual_response == expect_response
101100
102101 # test that order is preserved and responses fall back until a match or end of strategies
103102 def test_find_match_fallback (self ):
@@ -110,10 +109,6 @@ def test_find_match_fallback(self):
110109 ]
111110 matcher = Matcher (patterns )
112111 always_response = matcher .find_match (prompt = "example prompt" )
113- self . assertEqual ( always_response , "this is the fallback response" )
112+ assert always_response == "this is the fallback response"
114113 contains_response = matcher .find_match (prompt = "this is the fallback response" )
115- self .assertEqual (contains_response , "a response you will not get" )
116-
117-
118- if __name__ == "__main__" :
119- unittest .main ()
114+ assert contains_response == "a response you will not get"
0 commit comments