Exemplo n.º 1
0
    def test_xml_processing(self):
        self.query(udf.fixindent('''
                CREATE python SCALAR SCRIPT
                process_users(url VARCHAR(200))
                EMITS (firstname VARCHAR(100), lastname VARCHAR(100)) AS

                import urllib
                import lxml.etree as etree
                # import xml.etree.cElementTree as etree


                def run(ctx):
                    data = ''.join(urllib.urlopen(ctx.url).readlines())
                    tree = etree.XML(data)
                    for user in tree.findall('user/[@active="1"]'):
                        fn = user.findtext('first_name')
                        ln = user.findtext('family_name')
                        ctx.emit(fn, ln)
                '''))
            
        with tempdir() as tmp:
            with open(os.path.join(tmp, 'keepers.xml'), 'w') as f:
                f.write(self.xml())
            
            with HTTPServer(tmp) as hs:
                url = 'http://%s:%d/keepers.xml' % hs.address
                rows = self.query('''
                        SELECT process_users('%s')
                        FROM DUAL
                        ORDER BY lastname
                        ''' % url)
            
        expected = [('Joe', 'Hart'), ('Manuel', 'Neuer')]
        self.assertRowsEqual(expected, rows)
Exemplo n.º 2
0
 def test_selftest(self):
     with tempdir() as tmp:
         with open(os.path.join(tmp, 'foo.xml'), 'w') as f:
             f.write('''<foo/>\n''')
         with HTTPServer(tmp) as hs:
             self.assertIn('<foo/>',
                 urllib.urlopen('http://%s:%d/foo.xml' % hs.address).read())
Exemplo n.º 3
0
 def test_1(self):
     with tempdir() as tmp:
         with open(os.path.join(tmp, 'dummy'), 'w') as f:
             f.write('babelfish')
         with HTTPServer(tmp) as httpd:
             url = urllib.urlopen('http://%s:%d/dummy' %
                                  httpd.address)
             data = url.readlines()
     self.assertIn('babelfish', '\n'.join(data))
Exemplo n.º 4
0
 def test_server_is_chdir_safe(self):
     cwd = os.getcwd()
     with tempdir() as tmp:
         self.assertEqual(cwd, os.getcwd())
         with HTTPServer(tmp) as httpd:
             # Current implementation chdir to documentroot;
             # needs subprocesses to avoid this.
             pass
             #self.assertEqual(cwd, os.getcwd())
         self.assertEqual(cwd, os.getcwd())
     self.assertEqual(cwd, os.getcwd())
Exemplo n.º 5
0
    def test_xml_processing(self):
        '''DWA-13842'''
        self.query(
            udf.fixindent('''
                CREATE or REPLACE R SCALAR SCRIPT
                process_users(url VARCHAR(300))
                EMITS (firstname VARCHAR(100), lastname VARCHAR(100)) as
                require('RCurl')
                require('XML')
                run <- function(ctx) {
                    cont <- getURL(ctx$url)
                    tree <- xmlTreeParse(cont)
                    for (i in 1:length(tree$doc$children$users)) {
                        if (tree$doc$children$users[i]$user$attributes['active']==1) {
                                firstname <- tree$doc$children$users[i]$user$children$first_name$children$text$value;
                                familyname <- tree$doc$children$users[i]$user$children$family_name$children$text$value;
                                ctx$emit(firstname, familyname)
                        }
                    }
                }
                '''))

        with tempdir() as tmp:
            with open(os.path.join(tmp, 'keepers.xml'), 'w') as f:
                f.write(self.xml())

            with HTTPServer(tmp) as hs:
                url = 'http://%s:%d/keepers.xml' % hs.address
                query = '''
                        SELECT process_users('%s')
                        FROM DUAL
                        ORDER BY lastname
                        ''' % url
                rows = self.query(query)
                expected = [('Joe', 'Hart'), ('Manuel', 'Neuer')]
                self.assertRowsEqual(expected, rows)