server/test/unittest_multisources.py
changeset 519 06390418cd9a
parent 342 6becc066fc00
child 520 29342c0cf55f
equal deleted inserted replaced
518:b6003d74a600 519:06390418cd9a
     8 
     8 
     9 class TwoSourcesConfiguration(TestServerConfiguration):
     9 class TwoSourcesConfiguration(TestServerConfiguration):
    10     sourcefile = 'sources_multi'
    10     sourcefile = 'sources_multi'
    11 
    11 
    12         
    12         
    13 class ExternalSourceConfiguration(TestServerConfiguration):
    13 class ExternalSource1Configuration(TestServerConfiguration):
    14     sourcefile = 'sources_extern'
    14     sourcefile = 'sources_extern'
    15 
    15         
    16 repo2, cnx2 = init_test_database('sqlite', config=ExternalSourceConfiguration('data'))
    16 class ExternalSource2Configuration(TestServerConfiguration):
       
    17     sourcefile = 'sources_multi2'
       
    18 
       
    19 repo2, cnx2 = init_test_database('sqlite', config=ExternalSource1Configuration('data'))
    17 cu = cnx2.cursor()
    20 cu = cnx2.cursor()
    18 ec1 = cu.execute('INSERT Card X: X title "C3: An external card", X wikiid "aaa"')[0][0]
    21 ec1 = cu.execute('INSERT Card X: X title "C3: An external card", X wikiid "aaa"')[0][0]
    19 cu.execute('INSERT Card X: X title "C4: Ze external card", X wikiid "zzz"')
    22 cu.execute('INSERT Card X: X title "C4: Ze external card", X wikiid "zzz"')
    20 aff1 = cu.execute('INSERT Affaire X: X ref "AFFREF", X in_state S WHERE S name "pitetre"')[0][0]
    23 aff1 = cu.execute('INSERT Affaire X: X ref "AFFREF", X in_state S WHERE S name "pitetre"')[0][0]
    21 cnx2.commit()
    24 cnx2.commit()
    22 
    25 
    23 MTIME = now() - 0.1
    26 MTIME = now() - 0.1
    24 
    27 
       
    28 repo3, cnx3 = init_test_database('sqlite', config=ExternalSource2Configuration('data'))
       
    29 
    25 # XXX, access existing connection, no pyro connection
    30 # XXX, access existing connection, no pyro connection
    26 from cubicweb.server.sources.pyrorql import PyroRQLSource
    31 from cubicweb.server.sources.pyrorql import PyroRQLSource
    27 PyroRQLSource.get_connection = lambda x: cnx2
    32 PyroRQLSource.get_connection = lambda x: x.uri == 'extern-multi' and cnx3 or cnx2
    28 # necessary since the repository is closing its initial connections pool though
    33 # necessary since the repository is closing its initial connections pool though
    29 # we want to keep cnx2 valid
    34 # we want to keep cnx2 valid
    30 from cubicweb.dbapi import Connection
    35 from cubicweb.dbapi import Connection
    31 Connection.close = lambda x: None
    36 Connection.close = lambda x: None
    32 
    37 
    45         # add some entities
    50         # add some entities
    46         self.ic1 = self.execute('INSERT Card X: X title "C1: An internal card", X wikiid "aaai"')[0][0]
    51         self.ic1 = self.execute('INSERT Card X: X title "C1: An internal card", X wikiid "aaai"')[0][0]
    47         self.ic2 = self.execute('INSERT Card X: X title "C2: Ze internal card", X wikiid "zzzi"')[0][0]
    52         self.ic2 = self.execute('INSERT Card X: X title "C2: Ze internal card", X wikiid "zzzi"')[0][0]
    48         self.commit()
    53         self.commit()
    49         do_monkey_patch()
    54         do_monkey_patch()
       
    55         print 'main repo', self.repo
       
    56         print 'external simple repo', repo2
       
    57         print 'external multi repo', repo3
       
    58         print 'ic1', self.ic1
       
    59         print 'ic2', self.ic2
    50         
    60         
    51     def tearDown(self):
    61     def tearDown(self):
    52         RepositoryBasedTC.tearDown(self)
    62         RepositoryBasedTC.tearDown(self)
    53         undo_monkey_patch()
    63         undo_monkey_patch()
    54 
    64 
    82         self.assertEquals(sorted(rsetbase.rows), rsetbase.rows)
    92         self.assertEquals(sorted(rsetbase.rows), rsetbase.rows)
    83         rset = self.execute('Any W,X ORDERBY W,X LIMIT 2 OFFSET 2 WHERE X wikiid W')
    93         rset = self.execute('Any W,X ORDERBY W,X LIMIT 2 OFFSET 2 WHERE X wikiid W')
    84         self.assertEquals(rset.rows, rsetbase.rows[2:4])
    94         self.assertEquals(rset.rows, rsetbase.rows[2:4])
    85 
    95 
    86     def test_has_text(self):
    96     def test_has_text(self):
    87         self.repo.sources[-1].synchronize(MTIME) # in case fti_update has been run before
    97         self.repo.sources_by_uri['extern'].synchronize(MTIME) # in case fti_update has been run before
    88         self.failUnless(self.execute('Any X WHERE X has_text "affref"'))
    98         self.failUnless(self.execute('Any X WHERE X has_text "affref"'))
    89         self.failUnless(self.execute('Affaire X WHERE X has_text "affref"'))
    99         self.failUnless(self.execute('Affaire X WHERE X has_text "affref"'))
    90 
   100 
    91     def test_anon_has_text(self):
   101     def test_anon_has_text(self):
    92         self.repo.sources[-1].synchronize(MTIME) # in case fti_update has been run before
   102         self.repo.sources_by_uri['extern'].synchronize(MTIME) # in case fti_update has been run before
    93         self.execute('INSERT Affaire X: X ref "no readable card"')[0][0]
   103         self.execute('INSERT Affaire X: X ref "no readable card"')[0][0]
    94         aff1 = self.execute('INSERT Affaire X: X ref "card"')[0][0]
   104         aff1 = self.execute('INSERT Affaire X: X ref "card"')[0][0]
    95         # grant read access
   105         # grant read access
    96         self.execute('SET X owned_by U WHERE X eid %(x)s, U login "anon"', {'x': aff1}, 'x')
   106         self.execute('SET X owned_by U WHERE X eid %(x)s, U login "anon"', {'x': aff1}, 'x')
    97         self.commit()
   107         self.commit()
    98         cnx = self.login('anon')
   108         cnx = self.login('anon')
    99         cu = cnx.cursor()
   109         cu = cnx.cursor()
       
   110         print '*'*80
   100         rset = cu.execute('Any X WHERE X has_text "card"')
   111         rset = cu.execute('Any X WHERE X has_text "card"')
   101         self.assertEquals(len(rset), 5)
   112         self.assertEquals(len(rset), 5, zip(rset.rows, rset.description))
   102 
   113 
   103     def test_synchronization(self):
   114     def test_synchronization(self):
   104         cu = cnx2.cursor()
   115         cu = cnx2.cursor()
   105         cu.execute('SET X ref "BLAH" WHERE X eid %(x)s', {'x': aff1}, 'x')
   116         cu.execute('SET X ref "BLAH" WHERE X eid %(x)s', {'x': aff1}, 'x')
   106         aff2 = cu.execute('INSERT Affaire X: X ref "AFFREUX", X in_state S WHERE S name "pitetre"')[0][0]
   117         aff2 = cu.execute('INSERT Affaire X: X ref "AFFREUX", X in_state S WHERE S name "pitetre"')[0][0]
   107         cnx2.commit()
   118         cnx2.commit()
   108         try:
   119         try:
   109             # force sync
   120             # force sync
   110             self.repo.sources[-1].synchronize(MTIME)
   121             self.repo.sources_by_uri['extern'].synchronize(MTIME)
   111             self.failUnless(self.execute('Any X WHERE X has_text "blah"'))
   122             self.failUnless(self.execute('Any X WHERE X has_text "blah"'))
   112             self.failUnless(self.execute('Any X WHERE X has_text "affreux"'))
   123             self.failUnless(self.execute('Any X WHERE X has_text "affreux"'))
       
   124             print 'delete', aff2
   113             cu.execute('DELETE Affaire X WHERE X eid %(x)s', {'x': aff2})
   125             cu.execute('DELETE Affaire X WHERE X eid %(x)s', {'x': aff2})
   114             cnx2.commit()
   126             cnx2.commit()
   115             self.repo.sources[-1].synchronize(MTIME)
   127             print 'sync'
   116             self.failIf(self.execute('Any X WHERE X has_text "affreux"'))
   128             self.repo.sources_by_uri['extern'].synchronize(MTIME)
       
   129             print 'query'
       
   130             rset = self.execute('Any X WHERE X has_text "affreux"')
       
   131             print zip(rset.rows, rset.description)
       
   132             self.failIf(rset)
   117         finally:
   133         finally:
   118             # restore state
   134             # restore state
   119             cu.execute('SET X ref "AFFREF" WHERE X eid %(x)s', {'x': aff1}, 'x')
   135             cu.execute('SET X ref "AFFREF" WHERE X eid %(x)s', {'x': aff1}, 'x')
   120             cnx2.commit()
   136             cnx2.commit()
   121 
   137 
   137         
   153         
   138     def test_sort_func_ambigous(self):
   154     def test_sort_func_ambigous(self):
   139         self.execute('Any X ORDERBY DUMB_SORT(RF) WHERE X title RF')
   155         self.execute('Any X ORDERBY DUMB_SORT(RF) WHERE X title RF')
   140 
   156 
   141     def test_in_eid(self):
   157     def test_in_eid(self):
   142         iec1 = self.repo.extid2eid(self.repo.sources[-1], ec1, 'Card', self.session)
   158         iec1 = self.repo.extid2eid(self.repo.sources_by_uri['extern'], ec1,
       
   159                                    'Card', self.session)
   143         rset = self.execute('Any X WHERE X eid IN (%s, %s)' % (iec1, self.ic1))
   160         rset = self.execute('Any X WHERE X eid IN (%s, %s)' % (iec1, self.ic1))
       
   161         print 'hop', iec1, self.ic1
   144         self.assertEquals(sorted(r[0] for r in rset.rows), sorted([iec1, self.ic1]))
   162         self.assertEquals(sorted(r[0] for r in rset.rows), sorted([iec1, self.ic1]))
   145         
   163         
   146     def test_greater_eid(self):
   164     def test_greater_eid(self):
   147         rset = self.execute('Any X WHERE X eid > %s' % self.maxeid)
   165         rset = self.execute('Any X WHERE X eid > %s' % self.maxeid)
   148         self.assertEquals(len(rset.rows), 2) # self.ic1 and self.ic2
   166         self.assertEquals(len(rset.rows), 2) # self.ic1 and self.ic2