Exemple #1
0
    def from_postgres(cls,
                      sql,
                      username=None,
                      password=None,
                      host=None,
                      db=None,
                      port=None):
        """
        Args:
            sql: str
                A valid SQL statement
            username: str
                Required if env variable ``PGUSER`` not populated
            password: str
                Required if env variable ``PGPASSWORD`` not populated
            host: str
                Required if env variable ``PGHOST`` not populated
            db: str
                Required if env variable ``PGDATABASE`` not populated
            port: int
                Required if env variable ``PGPORT`` not populated.
        """

        from parsons.databases.postgres import Postgres
        pg = Postgres(username=username,
                      password=password,
                      host=host,
                      db=db,
                      port=port)
        return pg.query(sql)
Exemple #2
0
    def to_postgres(self, table_name, username=None, password=None, host=None,
                    db=None, port=None, **copy_args):
        """
        Write a table to a Postgres database.

        Args:
            table_name: str
                The table name and schema (``my_schema.my_table``) to point the file.
            username: str
                Required if env variable ``PGUSER`` not populated
            password: str
                Required if env variable ``PGPASSWORD`` not populated
            host: str
                Required if env variable ``PGHOST`` not populated
            db: str
                Required if env variable ``PGDATABASE`` not populated
            port: int
                Required if env variable ``PGPORT`` not populated.
            \**copy_args: kwargs
                See :func:`~parsons.databases.Postgres.copy`` for options.

        Returns:
            ``None``
        """  # noqa: W605

        from parsons.databases.postgres import Postgres
        pg = Postgres(username=username, password=password, host=host, db=db, port=port)
        pg.copy(self, table_name, **copy_args)
Exemple #3
0
    def test_connection(self):

        # Test connection with kwargs passed
        Postgres(username='******', password='******', host='test', db='test')

        # Test connection with env variables
        os.environ['PGUSER'] = '******'
        os.environ['PGPASSWORD'] = '******'
        os.environ['PGHOST'] = 'host_env'
        os.environ['PGDATABASE'] = 'db_env'
        os.environ['PGPORT'] = '5432'
        pg_env = Postgres()

        self.assertEqual(pg_env.username, 'user_env')
        self.assertEqual(pg_env.password, 'pass_env')
        self.assertEqual(pg_env.host, 'host_env')
        self.assertEqual(pg_env.db, 'db_env')
        self.assertEqual(pg_env.port, 5432)
Exemple #4
0
    def setUp(self):

        self.pg = Postgres(username='******',
                           password='******',
                           host='test',
                           db='test',
                           port=123)

        self.tbl = Table([['ID', 'Name'], [1, 'Jim'], [2, 'John'],
                          [3, 'Sarah']])

        self.mapping = self.pg.generate_data_types(self.tbl)
Exemple #5
0
    def setUp(self):

        self.temp_schema = TEMP_SCHEMA
        self.pg = Postgres()

        self.tbl = Table([['ID', 'Name'], [1, 'Jim'], [2, 'John'],
                          [3, 'Sarah']])

        # Create a schema, create a table, create a view
        setup_sql = f"""
                    drop schema if exists {self.temp_schema} cascade;
                    create schema {self.temp_schema};
                    """

        other_sql = f"""
                    create table {self.temp_schema}.test (id smallint,name varchar(5));
                    create view {self.temp_schema}.test_view as (select * from {self.temp_schema}.test);
                    """ # noqa: E501

        self.pg.query(setup_sql)

        self.pg.query(other_sql)
Exemple #6
0
    def setUp(self):

        self.pg = Postgres(username='******', password='******', host='test', db='test', port=123)

        self.tbl = Table([['ID', 'Name'],
                          [1, 'Jim'],
                          [2, 'John'],
                          [3, 'Sarah']])

        self.tbl2 = Table([
            ["c1", "c2", "c3", "c4", "c5", "c6", "c7"],
            ["a", "", 1, "NA", 1.4, 1, 2],
            ["b", "", 2, "NA", 1.4, 1, 2],
            ["c", "", 3.4, "NA", "", "", "a"],
            ["d", "", 5, "NA", 1.4, 1, 2],
            ["e", "", 6, "NA", 1.4, 1, 2],
            ["f", "", 7.8, "NA", 1.4, 1, 2],
            ["g", "", 9, "NA", 1.4, 1, 2],
        ])

        self.mapping = self.pg.generate_data_types(self.tbl)
        self.mapping2 = self.pg.generate_data_types(self.tbl2)
        self.pg.DO_PARSE_BOOLS = True
        self.mapping3 = self.pg.generate_data_types(self.tbl2)