def _reset_databases(self):
     for key, engine in self.engines.items():
         conn_string = self.test_databases[key]
         conn_pieces = urlutils.urlparse(conn_string)
         engine.dispose()
         if conn_string.startswith('sqlite'):
             # We can just delete the SQLite database, which is
             # the easiest and cleanest solution
             db_path = conn_pieces.path.strip('/')
             if os.path.exists(db_path):
                 os.unlink(db_path)
             # No need to recreate the SQLite DB. SQLite will
             # create it for us if it's not there...
         elif conn_string.startswith('mysql'):
             # We can execute the MySQL client to destroy and re-create
             # the MYSQL database, which is easier and less error-prone
             # than using SQLAlchemy to do this via MetaData...trust me.
             (user, password, database, host) = \
                 get_db_connection_info(conn_pieces)
             sql = ("drop database if exists %(db)s; "
                    "create database %(db)s;") % {'db': database}
             cmd = ("mysql -u \"%(user)s\" -p\"%(password)s\" -h %(host)s "
                    "-e \"%(sql)s\"") % {'user': user, 'password': password,
                                         'host': host, 'sql': sql}
             self.execute_cmd(cmd)
         elif conn_string.startswith('postgresql'):
             self._reset_pg(conn_pieces)
示例#2
0
    def client_request(self, client, method, url, **kwargs):
        # Check that certain things are called correctly
        if method in ["GET", "DELETE"]:
            assert "json" not in kwargs

        # Note the call
        self.callstack.append(
            (method,
             url,
             kwargs.get("headers") or {},
             kwargs.get("json") or kwargs.get("data")))
        try:
            fixture = self.fixtures[url][method]
        except KeyError:
            pass
        else:
            return TestResponse({"headers": fixture[0],
                                 "text": fixture[1]})

        # Call the method
        args = urlutils.parse_qsl(urlutils.urlparse(url)[4])
        kwargs.update(args)
        munged_url = url.rsplit('?', 1)[0]
        munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_')
        munged_url = munged_url.replace('-', '_')

        callback = "%s_%s" % (method.lower(), munged_url)

        if not hasattr(self, callback):
            raise AssertionError('Called unknown API method: %s %s, '
                                 'expected fakes method name: %s' %
                                 (method, url, callback))

        resp = getattr(self, callback)(**kwargs)
        if len(resp) == 3:
            status, headers, body = resp
        else:
            status, body = resp
            headers = {}
        return TestResponse({
            "status_code": status,
            "text": body,
            "headers": headers,
        })
示例#3
0
 def test_urlparse(self):
     url = 'http://www.yahoo.com'
     result = urlutils.urlparse(url)
     self.assertEquals(result.scheme, 'http')
示例#4
0
 def test_urlparse(self):
     url = 'http://www.yahoo.com'
     result = urlutils.urlparse(url)
     self.assertEqual(result.scheme, 'http')