<s> """<STR_LIT>""" <EOL> import redis <EOL> import unittest <EOL> import markov <EOL> from markov import Markov , add_line_to_index , make_key , max_for_key , min_for_key , score_for_completion , score_for_line , get_key_and_completion , generate , get_relevant_key_and_seed , get_random_key_and_seed , get_completion , STOP <EOL> class TestMarkovFunctions ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = redis . Redis ( db = <NUM_LIT:11> ) <EOL> self . prefix = '<STR_LIT:test>' <EOL> def test_make_key ( self ) : <EOL> """<STR_LIT>""" <EOL> key = make_key ( ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , self . prefix ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> key = make_key ( ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> def test_score_for_completion ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> self . assertEqual ( score_for_completion ( '<STR_LIT>' , '<STR_LIT:a>' , self . client ) , <NUM_LIT:100> ) <EOL> self . assertEqual ( score_for_completion ( '<STR_LIT>' , '<STR_LIT>' , self . client ) , <NUM_LIT:50> ) <EOL> def test_max_for_key ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> self . assertEqual ( max_for_key ( '<STR_LIT>' , self . client ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( max_for_key ( '<STR_LIT>' , self . client ) , <NUM_LIT:0> ) <EOL> def test_min_for_key ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> self . assertEqual ( min_for_key ( '<STR_LIT>' , self . client ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( min_for_key ( '<STR_LIT>' , self . client ) , <NUM_LIT:0> ) <EOL> def test_add_line_to_index ( self ) : <EOL> """<STR_LIT>""" <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> line1 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> line2 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> add_line_to_index ( line , self . client , prefix = "<STR_LIT:test>" ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT:a>" ) , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> add_line_to_index ( line1 , self . client , prefix = "<STR_LIT:test>" ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT:a>" ) , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> add_line_to_index ( line2 , self . client , prefix = "<STR_LIT:test>" ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT:a>" ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( self . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> def test_score_for_line ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> line2 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> line3 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( score_for_line ( line , self . client , prefix = self . prefix ) , <NUM_LIT:100> ) <EOL> self . assertEqual ( score_for_line ( line2 , self . client , prefix = self . prefix ) , <NUM_LIT> / <NUM_LIT:3> ) <EOL> self . assertEqual ( score_for_line ( line3 , self . client , prefix = self . prefix ) , <NUM_LIT> / <NUM_LIT:3> ) <EOL> def test_get_key_and_completion ( self ) : <EOL> """<STR_LIT>""" <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> key , completion = get_key_and_completion ( line , <NUM_LIT:2> , <NUM_LIT:1> , self . prefix ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> self . assertEqual ( completion , '<STR_LIT:a>' ) <EOL> key , completion = get_key_and_completion ( line , <NUM_LIT:2> , <NUM_LIT:2> , self . prefix ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> self . assertEqual ( completion , '<STR_LIT>' ) <EOL> key , completion = get_key_and_completion ( line , <NUM_LIT:3> , <NUM_LIT:2> , self . prefix ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> self . assertEqual ( completion , '<STR_LIT>' ) <EOL> key , completion = get_key_and_completion ( line , <NUM_LIT:4> , <NUM_LIT:1> , self . prefix ) <EOL> self . assertEqual ( key , '<STR_LIT>' ) <EOL> self . assertEqual ( completion , markov . STOP ) <EOL> def test_generate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> generated = generate ( self . client , prefix = self . prefix , max_words = <NUM_LIT:3> ) <EOL> assert len ( generated ) >= <NUM_LIT:2> <EOL> assert len ( generated ) <= <NUM_LIT:3> <EOL> generated = generate ( self . client , seed = [ '<STR_LIT>' , '<STR_LIT>' ] , prefix = self . prefix , max_words = <NUM_LIT:3> ) <EOL> assert generated [ <NUM_LIT:2> ] == '<STR_LIT>' <EOL> assert '<STR_LIT>' not in generated <EOL> generated = generate ( self . client , relevant_terms = [ "<STR_LIT>" , ] , prefix = self . prefix ) <EOL> assert '<STR_LIT>' in generated <EOL> generated = generate ( self . client , relevant_terms = [ "<STR_LIT>" , ] , prefix = self . prefix ) <EOL> assert '<STR_LIT>' in generated <EOL> generated = generate ( self . client , relevant_terms = [ "<STR_LIT>" , ] , prefix = self . prefix ) <EOL> assert len ( generated ) == <NUM_LIT:0> <EOL> def test_get_relevant_key_and_seed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> key , seed = get_relevant_key_and_seed ( self . client , relevant_terms = [ "<STR_LIT>" , ] , prefix = self . prefix ) <EOL> assert "<STR_LIT>" in seed <EOL> key , seed = get_relevant_key_and_seed ( self . client , relevant_terms = [ "<STR_LIT>" , ] , prefix = self . prefix ) <EOL> assert seed == [ ] <EOL> assert key is None <EOL> def test_get_random_key_and_seed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> key , seed = get_random_key_and_seed ( self . client , prefix = self . prefix ) <EOL> assert len ( seed ) == <NUM_LIT:2> <EOL> assert self . prefix not in seed <EOL> assert self . prefix in key <EOL> def test_get_completion ( self ) : <EOL> """<STR_LIT>""" <EOL> self . test_add_line_to_index ( ) <EOL> key , seed = get_random_key_and_seed ( self . client , prefix = self . prefix ) <EOL> if STOP not in seed : <EOL> assert get_completion ( self . client , key ) is not None <EOL> else : <EOL> assert get_completion ( self . client , key ) is None <EOL> key = "<STR_LIT>" <EOL> assert get_completion ( self . client , key ) in [ "<STR_LIT:a>" , "<STR_LIT>" ] <EOL> assert get_completion ( self . client , key , exclude = [ "<STR_LIT:a>" , ] ) == "<STR_LIT>" <EOL> assert get_completion ( self . client , key , exclude = [ "<STR_LIT:a>" , "<STR_LIT>" ] ) is None <EOL> assert get_completion ( self . client , key , relevant_terms = [ "<STR_LIT>" , ] ) == "<STR_LIT>" <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> keys = self . client . keys ( self . prefix + "<STR_LIT:*>" ) <EOL> for key in keys : <EOL> self . client . delete ( key ) <EOL> class TestMarkovClass ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . markov = Markov ( prefix = "<STR_LIT>" , db = <NUM_LIT:11> ) <EOL> def test_add_line_to_index ( self ) : <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> line1 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> line2 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> self . markov . add_line_to_index ( line ) <EOL> self . markov . add_line_to_index ( line1 ) <EOL> self . markov . add_line_to_index ( line2 ) <EOL> self . assertEqual ( self . markov . client . zscore ( "<STR_LIT>" , "<STR_LIT:a>" ) , <NUM_LIT> ) <EOL> self . assertEqual ( self . markov . client . zscore ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:1.0> ) <EOL> def test_score_for_line ( self ) : <EOL> self . test_add_line_to_index ( ) <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> self . assertEqual ( self . markov . score_for_line ( line ) , <NUM_LIT:100> ) <EOL> def test_generate ( self ) : <EOL> self . test_add_line_to_index ( ) <EOL> generated = self . markov . generate ( max_words = <NUM_LIT:3> ) <EOL> assert len ( generated ) >= <NUM_LIT:2> <EOL> assert len ( generated ) <= <NUM_LIT:3> <EOL> generated = self . markov . generate ( seed = [ '<STR_LIT>' , '<STR_LIT>' ] , max_words = <NUM_LIT:3> ) <EOL> assert '<STR_LIT>' in generated <EOL> assert '<STR_LIT>' not in generated <EOL> def test_flush ( self ) : <EOL> m1 = Markov ( prefix = "<STR_LIT>" , db = <NUM_LIT:5> ) <EOL> m2 = Markov ( prefix = "<STR_LIT>" , db = <NUM_LIT:5> ) <EOL> line = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> line1 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> line2 = [ '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' ] <EOL> m1 . add_line_to_index ( line ) <EOL> m1 . add_line_to_index ( line1 ) <EOL> m1 . add_line_to_index ( line2 ) <EOL> important_line = [ '<STR_LIT>' , '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> m2 . add_line_to_index ( important_line ) <EOL> r = redis . Redis ( db = <NUM_LIT:5> ) <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:6> <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:3> <EOL> m1 . flush ( prefix = "<STR_LIT>" ) <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:3> <EOL> m2 . flush ( prefix = "<STR_LIT>" ) <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert len ( r . keys ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> keys = self . markov . client . keys ( self . markov . prefix + "<STR_LIT:*>" ) <EOL> for key in keys : <EOL> self . markov . client . delete ( key ) </s>
