<s> import unittest <EOL> from biicode . test . testing_mem_server_store import TestingMemServerStore <EOL> from biicode . server . model . user import User <EOL> from biicode . common . model . symbolic . block_version import BlockVersion <EOL> from biicode . common . model . brl . brl_block import BRLBlock <EOL> from biicode . common . model . brl . brl_user import BRLUser <EOL> from biicode . server . api . bii_service import BiiService <EOL> from biicode . common . find . finder_request import FinderRequest <EOL> from biicode . common . model . brl . block_cell_name import BlockCellName <EOL> from biicode . common . model . symbolic . reference import ReferencedDependencies <EOL> from biicode . common . model . declare . cpp_declaration import CPPDeclaration <EOL> from biicode . common . find . policy import Policy <EOL> from biicode . server . test . publisher import TestPublisher <EOL> from biicode . common . api . ui import BiiResponse <EOL> import time <EOL> class BaseFinderTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . user = BRLUser ( '<STR_LIT>' ) <EOL> self . store = TestingMemServerStore ( ) <EOL> user = User ( self . user ) <EOL> user . password = '<STR_LIT:password>' <EOL> self . store . create_user ( user ) <EOL> self . service = BiiService ( self . store , self . user ) <EOL> def check_result ( self , result , resolved = None , unresolved = None , updated = None ) : <EOL> unresolved = unresolved or [ ] <EOL> unresolved = { CPPDeclaration ( str ( u ) ) for u in unresolved } <EOL> self . assertEqual ( result . unresolved , unresolved ) <EOL> resolved = resolved or [ ] <EOL> refs = ReferencedDependencies ( ) <EOL> for brl , time , block_cell_names in resolved : <EOL> for name in block_cell_names : <EOL> refs [ BlockVersion ( brl , time ) ] [ CPPDeclaration ( name ) ] = { BlockCellName ( name ) } <EOL> self . assertEqual ( result . resolved , refs ) <EOL> updated = updated or [ ] <EOL> refs = ReferencedDependencies ( ) <EOL> for brl , time , block_cell_names in updated : <EOL> for name in block_cell_names : <EOL> refs [ BlockVersion ( brl , time ) ] [ CPPDeclaration ( name ) ] = { BlockCellName ( name ) } <EOL> self . assertEqual ( result . updated , refs ) <EOL> def build_unresolved_request ( self , unresolved_deps ) : <EOL> if isinstance ( unresolved_deps , basestring ) : <EOL> unresolved_deps = [ unresolved_deps ] <EOL> request = FinderRequest ( ) <EOL> unresolved = set ( ) <EOL> for dep in unresolved_deps : <EOL> unresolved . add ( CPPDeclaration ( dep ) ) <EOL> request . unresolved = unresolved <EOL> request . policy = Policy . default ( ) <EOL> return request <EOL> def build_update_request ( self , block_version , block_cell_name ) : <EOL> request = FinderRequest ( ) <EOL> existing = ReferencedDependencies ( ) <EOL> existing [ block_version ] [ CPPDeclaration ( block_cell_name ) ] . add ( block_cell_name ) <EOL> request . existing = existing <EOL> request . policy = Policy . default ( ) <EOL> request . find = False <EOL> request . update = True <EOL> return request <EOL> class FinderTest ( BaseFinderTest ) : <EOL> """<STR_LIT>""" <EOL> def test_simple_find ( self ) : <EOL> '''<STR_LIT>''' <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> request = self . build_unresolved_request ( name_a ) <EOL> result = self . service . find ( request , BiiResponse ( ) ) <EOL> self . check_result ( result , resolved = [ ( brl_a , i , { name_a } ) ] ) <EOL> def test_simple_update ( self ) : <EOL> '''<STR_LIT>''' <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> request = self . build_update_request ( BlockVersion ( brl_a , i - <NUM_LIT:1> ) , name_a ) <EOL> result = self . service . find ( request , BiiResponse ( ) ) <EOL> ref_updated = [ ( brl_a , i , { name_a } ) ] <EOL> self . check_result ( result , updated = ref_updated ) <EOL> def test_disallow_cycles ( self ) : <EOL> """<STR_LIT>""" <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> request = self . build_unresolved_request ( name_a ) <EOL> request . block_names . add ( name_a . block_name ) <EOL> response = BiiResponse ( ) <EOL> result = self . service . find ( request , response ) <EOL> self . assertIn ( '<STR_LIT>' , str ( response ) ) <EOL> self . check_result ( result , unresolved = request . unresolved ) <EOL> def test_find_transitive ( self ) : <EOL> '''<STR_LIT>''' <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> brl_b = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_b = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher . publish ( brl_b , { '<STR_LIT>' : ( '<STR_LIT:b>' , [ name_a ] ) } , <EOL> dep_versions = BlockVersion ( brl_a , <NUM_LIT:0> ) ) <EOL> request = self . build_unresolved_request ( name_b ) <EOL> result = self . service . find ( request , BiiResponse ( ) ) <EOL> self . check_result ( result , resolved = [ ( brl_b , <NUM_LIT:0> , { name_b } ) ] ) <EOL> def test_diamond_single_solution ( self ) : <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> brl_b = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> brl_c = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> for _ in range ( <NUM_LIT:20> ) : <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> name_b = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> name_c = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:20> ) : <EOL> if i % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> publisher . publish ( brl_b , { '<STR_LIT>' : ( '<STR_LIT:b>' , [ name_a ] ) } , <EOL> dep_versions = BlockVersion ( brl_a , i ) ) <EOL> if ( i % <NUM_LIT:2> == <NUM_LIT:1> or i == <NUM_LIT:10> ) : <EOL> publisher . publish ( brl_c , { '<STR_LIT>' : ( '<STR_LIT:c>' , [ name_a ] ) } , <EOL> dep_versions = BlockVersion ( brl_a , i ) ) <EOL> request = self . build_unresolved_request ( [ name_b , name_c ] ) <EOL> result = self . service . find ( request , BiiResponse ( ) ) <EOL> self . check_result ( result , resolved = [ ( brl_b , <NUM_LIT:5> , { name_b } ) , ( brl_c , <NUM_LIT:5> , { name_c } ) ] ) <EOL> def test_disallow_cycles_transitive ( self ) : <EOL> """<STR_LIT>""" <EOL> brl_a = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_a = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher = TestPublisher ( self . user , self . store ) <EOL> publisher . publish ( brl_a , { '<STR_LIT>' : ( '<STR_LIT:a>' , [ ] ) } ) <EOL> brl_b = BRLBlock ( '<STR_LIT>' % ( self . user , self . user , '<STR_LIT>' ) ) <EOL> name_b = BlockCellName ( self . user + "<STR_LIT>" ) <EOL> publisher . publish ( brl_b , { '<STR_LIT>' : ( '<STR_LIT:b>' , [ name_a ] ) } , dep_versions = BlockVersion ( brl_a , <NUM_LIT:0> ) ) <EOL> request = self . build_unresolved_request ( name_b ) <EOL> request . block_names . add ( name_a . block_name ) <EOL> response = BiiResponse ( ) <EOL> result = self . service . find ( request , response ) <EOL> self . assertIn ( "<STR_LIT>" , str ( response ) ) <EOL> self . assertIn ( '<STR_LIT>' , str ( response ) ) <EOL> self . check_result ( result , unresolved = request . unresolved ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
