<s> '''<STR_LIT>''' <EOL> import unittest <EOL> import random <EOL> import tempfile <EOL> from certfuzz . scoring . scorable_set import ScorableSet2 <EOL> from certfuzz . scoring . errors import ScorableSetError , EmptySetError <EOL> from certfuzz . helpers import random_str <EOL> import os <EOL> import shutil <EOL> import csv <EOL> class MockScorableThing ( object ) : <EOL> def __init__ ( self ) : <EOL> self . key = random_str ( <NUM_LIT:8> ) <EOL> self . probability = random . uniform ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> class Test ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . ss = ScorableSet2 ( ) <EOL> self . things = [ ] <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> fd , f = tempfile . mkstemp ( dir = self . tmpdir ) <EOL> os . close ( fd ) <EOL> os . remove ( f ) <EOL> self . tmpfile = f <EOL> for _x in xrange ( <NUM_LIT:5> ) : <EOL> thing = MockScorableThing ( ) <EOL> self . ss . add_item ( thing . key , thing ) <EOL> self . things . append ( thing ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def test_scaled_score ( self ) : <EOL> self . ss . _update_probabilities ( ) <EOL> score_sum = sum ( [ x for x in self . ss . scaled_score . itervalues ( ) ] ) <EOL> self . assertAlmostEqual ( score_sum , <NUM_LIT:1.0> ) <EOL> def test_del_item ( self ) : <EOL> while self . things : <EOL> n_before = len ( self . things ) <EOL> self . assertEqual ( len ( self . ss . things ) , n_before ) <EOL> thing_to_del = self . things . pop ( ) <EOL> self . ss . del_item ( thing_to_del . key ) <EOL> n_after = n_before - <NUM_LIT:1> <EOL> self . assertEqual ( len ( self . things ) , n_after ) <EOL> self . assertEqual ( len ( self . ss . things ) , n_after ) <EOL> def test_add_item ( self ) : <EOL> ss = ScorableSet2 ( ) <EOL> for _x in xrange ( <NUM_LIT:100> ) : <EOL> thing = MockScorableThing ( ) <EOL> self . assertEqual ( len ( ss . things ) , _x ) <EOL> ss . add_item ( thing . key , thing ) <EOL> self . assertEqual ( len ( ss . things ) , _x + <NUM_LIT:1> ) <EOL> self . assertTrue ( thing . key in ss . things ) <EOL> self . assertTrue ( thing in ss . things . values ( ) ) <EOL> def test_empty_set ( self ) : <EOL> ss = ScorableSet2 ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( ss . things ) ) <EOL> self . assertRaises ( EmptySetError , ss . next_key ) <EOL> self . assertRaises ( EmptySetError , ss . next_item ) <EOL> def test_read_csv ( self ) : <EOL> self . assertRaises ( ScorableSetError , self . ss . _read_csv ) <EOL> self . ss . datafile = self . tmpfile <EOL> d = { '<STR_LIT:x>' : <NUM_LIT:1> , '<STR_LIT:y>' : <NUM_LIT:2> , '<STR_LIT:z>' : <NUM_LIT:3> } <EOL> keys = list ( d . keys ( ) ) <EOL> with open ( self . ss . datafile , '<STR_LIT:wb>' ) as datafile : <EOL> writer = csv . writer ( datafile ) <EOL> writer . writerow ( keys ) <EOL> row = [ d [ k ] for k in keys ] <EOL> writer . writerow ( row ) <EOL> read_csv = self . ss . _read_csv ( ) <EOL> d_out = read_csv . pop ( <NUM_LIT:0> ) <EOL> for k in keys : <EOL> self . assertTrue ( k in d_out ) <EOL> self . assertEqual ( d [ k ] , int ( d_out [ k ] ) ) <EOL> def test_update_csv ( self ) : <EOL> self . ss . _update_probabilities ( ) <EOL> self . assertRaises ( ScorableSetError , self . ss . update_csv ) <EOL> self . ss . datafile = self . tmpfile <EOL> self . assertFalse ( os . path . exists ( self . tmpfile ) ) <EOL> self . ss . update_csv ( ) <EOL> self . assertTrue ( os . path . exists ( self . tmpfile ) ) <EOL> with open ( self . ss . datafile , '<STR_LIT:rb>' ) as f : <EOL> data = list ( csv . DictReader ( f ) ) <EOL> self . assertEqual ( len ( data ) , <NUM_LIT:1> ) <EOL> for row in data : <EOL> for k in self . ss . things . keys ( ) : <EOL> self . assertTrue ( k in row ) <EOL> self . ss . update_csv ( ) <EOL> with open ( self . ss . datafile , '<STR_LIT:rb>' ) as f : <EOL> data = list ( csv . DictReader ( f ) ) <EOL> self . assertEqual ( len ( data ) , <NUM_LIT:2> ) <EOL> for row in data : <EOL> for k in self . ss . things . keys ( ) : <EOL> self . assertTrue ( k in row ) <EOL> self . ss . update_csv ( ) <EOL> with open ( self . ss . datafile , '<STR_LIT:rb>' ) as f : <EOL> self . assertEqual ( len ( f . readlines ( ) ) , <NUM_LIT:4> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
