Python sqlalchemy.sql.sqltypes.Boolean() Examples

The following are 14 code examples of sqlalchemy.sql.sqltypes.Boolean(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.sql.sqltypes , or try the search function .
Example #1
Source File: test_sql.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_dtype(self):
        if self.flavor == 'mysql':
            pytest.skip('Not applicable to MySQL legacy')
        cols = ['A', 'B']
        data = [(0.8, True),
                (0.9, None)]
        df = DataFrame(data, columns=cols)
        df.to_sql('dtype_test', self.conn)
        df.to_sql('dtype_test2', self.conn, dtype={'B': 'STRING'})

        # sqlite stores Boolean values as INTEGER
        assert self._get_sqlite_column_type(
            'dtype_test', 'B') == 'INTEGER'

        assert self._get_sqlite_column_type(
            'dtype_test2', 'B') == 'STRING'
        pytest.raises(ValueError, df.to_sql,
                      'error', self.conn, dtype={'B': bool})

        # single dtype
        df.to_sql('single_dtype_test', self.conn, dtype='STRING')
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'A') == 'STRING'
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'B') == 'STRING' 
Example #2
Source File: test_sql.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_dtype(self):
        if self.flavor == 'mysql':
            pytest.skip('Not applicable to MySQL legacy')
        cols = ['A', 'B']
        data = [(0.8, True),
                (0.9, None)]
        df = DataFrame(data, columns=cols)
        df.to_sql('dtype_test', self.conn)
        df.to_sql('dtype_test2', self.conn, dtype={'B': 'STRING'})

        # sqlite stores Boolean values as INTEGER
        assert self._get_sqlite_column_type(
            'dtype_test', 'B') == 'INTEGER'

        assert self._get_sqlite_column_type(
            'dtype_test2', 'B') == 'STRING'
        pytest.raises(ValueError, df.to_sql,
                      'error', self.conn, dtype={'B': bool})

        # single dtype
        df.to_sql('single_dtype_test', self.conn, dtype='STRING')
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'A') == 'STRING'
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'B') == 'STRING' 
Example #3
Source File: sanity.py    From cum with Apache License 2.0 6 votes vote down vote up
def test_datatype(self, table, column):
        """Tests that database column datatype matches the one defined in the
        models.
        """
        database_column = self.find_database_column(table, column)

        if isinstance(column.type, sqltypes.String):
            expected_type = sqltypes.VARCHAR
        elif isinstance(column.type, sqltypes.Integer):
            expected_type = sqltypes.INTEGER
        elif isinstance(column.type, sqltypes.Boolean):
            expected_type = sqltypes.BOOLEAN
        elif isinstance(column.type, sqltypes.DateTime):
            expected_type = sqltypes.DATETIME

        if not isinstance(database_column['type'], expected_type):
            self.errors.append(
                DatatypeMismatch(table, database_column, expected_type,
                                 parent=self)
            ) 
Example #4
Source File: test_sql.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_notna_dtype(self):
        cols = {'Bool': Series([True, None]),
                'Date': Series([datetime(2012, 5, 1), None]),
                'Int': Series([1, None], dtype='object'),
                'Float': Series([1.1, None])
                }
        df = DataFrame(cols)

        tbl = 'notna_dtype_test'
        df.to_sql(tbl, self.conn)
        returned_df = sql.read_sql_table(tbl, self.conn)  # noqa
        meta = sqlalchemy.schema.MetaData(bind=self.conn)
        meta.reflect()
        if self.flavor == 'mysql':
            my_type = sqltypes.Integer
        else:
            my_type = sqltypes.Boolean

        col_dict = meta.tables[tbl].columns

        assert isinstance(col_dict['Bool'].type, my_type)
        assert isinstance(col_dict['Date'].type, sqltypes.DateTime)
        assert isinstance(col_dict['Int'].type, sqltypes.Integer)
        assert isinstance(col_dict['Float'].type, sqltypes.Float) 
Example #5
Source File: test_sql.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_dtype(self):
        if self.flavor == 'mysql':
            pytest.skip('Not applicable to MySQL legacy')
        cols = ['A', 'B']
        data = [(0.8, True),
                (0.9, None)]
        df = DataFrame(data, columns=cols)
        df.to_sql('dtype_test', self.conn)
        df.to_sql('dtype_test2', self.conn, dtype={'B': 'STRING'})

        # sqlite stores Boolean values as INTEGER
        assert self._get_sqlite_column_type(
            'dtype_test', 'B') == 'INTEGER'

        assert self._get_sqlite_column_type(
            'dtype_test2', 'B') == 'STRING'
        pytest.raises(ValueError, df.to_sql,
                      'error', self.conn, dtype={'B': bool})

        # single dtype
        df.to_sql('single_dtype_test', self.conn, dtype='STRING')
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'A') == 'STRING'
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'B') == 'STRING' 
Example #6
Source File: test_sql.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_notna_dtype(self):
        cols = {'Bool': Series([True, None]),
                'Date': Series([datetime(2012, 5, 1), None]),
                'Int': Series([1, None], dtype='object'),
                'Float': Series([1.1, None])
                }
        df = DataFrame(cols)

        tbl = 'notna_dtype_test'
        df.to_sql(tbl, self.conn)
        returned_df = sql.read_sql_table(tbl, self.conn)  # noqa
        meta = sqlalchemy.schema.MetaData(bind=self.conn)
        meta.reflect()
        if self.flavor == 'mysql':
            my_type = sqltypes.Integer
        else:
            my_type = sqltypes.Boolean

        col_dict = meta.tables[tbl].columns

        assert isinstance(col_dict['Bool'].type, my_type)
        assert isinstance(col_dict['Date'].type, sqltypes.DateTime)
        assert isinstance(col_dict['Int'].type, sqltypes.Integer)
        assert isinstance(col_dict['Float'].type, sqltypes.Float) 
Example #7
Source File: test_sql.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_dtype(self):
        if self.flavor == 'mysql':
            pytest.skip('Not applicable to MySQL legacy')
        cols = ['A', 'B']
        data = [(0.8, True),
                (0.9, None)]
        df = DataFrame(data, columns=cols)
        df.to_sql('dtype_test', self.conn)
        df.to_sql('dtype_test2', self.conn, dtype={'B': 'STRING'})

        # sqlite stores Boolean values as INTEGER
        assert self._get_sqlite_column_type(
            'dtype_test', 'B') == 'INTEGER'

        assert self._get_sqlite_column_type(
            'dtype_test2', 'B') == 'STRING'
        pytest.raises(ValueError, df.to_sql,
                      'error', self.conn, dtype={'B': bool})

        # single dtype
        df.to_sql('single_dtype_test', self.conn, dtype='STRING')
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'A') == 'STRING'
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'B') == 'STRING' 
Example #8
Source File: test_sql.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_notna_dtype(self):
        cols = {'Bool': Series([True, None]),
                'Date': Series([datetime(2012, 5, 1), None]),
                'Int': Series([1, None], dtype='object'),
                'Float': Series([1.1, None])
                }
        df = DataFrame(cols)

        tbl = 'notna_dtype_test'
        df.to_sql(tbl, self.conn)
        returned_df = sql.read_sql_table(tbl, self.conn)  # noqa
        meta = sqlalchemy.schema.MetaData(bind=self.conn)
        meta.reflect()
        if self.flavor == 'mysql':
            my_type = sqltypes.Integer
        else:
            my_type = sqltypes.Boolean

        col_dict = meta.tables[tbl].columns

        assert isinstance(col_dict['Bool'].type, my_type)
        assert isinstance(col_dict['Date'].type, sqltypes.DateTime)
        assert isinstance(col_dict['Int'].type, sqltypes.Integer)
        assert isinstance(col_dict['Float'].type, sqltypes.Float) 
Example #9
Source File: test_sql.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_dtype(self):
        if self.flavor == 'mysql':
            pytest.skip('Not applicable to MySQL legacy')
        cols = ['A', 'B']
        data = [(0.8, True),
                (0.9, None)]
        df = DataFrame(data, columns=cols)
        df.to_sql('dtype_test', self.conn)
        df.to_sql('dtype_test2', self.conn, dtype={'B': 'STRING'})

        # sqlite stores Boolean values as INTEGER
        assert self._get_sqlite_column_type(
            'dtype_test', 'B') == 'INTEGER'

        assert self._get_sqlite_column_type(
            'dtype_test2', 'B') == 'STRING'
        pytest.raises(ValueError, df.to_sql,
                      'error', self.conn, dtype={'B': bool})

        # single dtype
        df.to_sql('single_dtype_test', self.conn, dtype='STRING')
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'A') == 'STRING'
        assert self._get_sqlite_column_type(
            'single_dtype_test', 'B') == 'STRING' 
Example #10
Source File: translate.py    From siuba with MIT License 6 votes vote down vote up
def sql_func_astype(col, _type):
    mappings = {
            str: types.Text,
            'str': types.Text,
            int: types.Integer,
            'int': types.Integer,
            float: types.Numeric,
            'float': types.Numeric,
            bool: types.Boolean,
            'bool': types.Boolean
            }
    try:
        sa_type = mappings[_type]
    except KeyError:
        raise ValueError("sql astype currently only supports type objects: str, int, float, bool")
    return sql.cast(col, sa_type)


# Base translations =========================================================== 
Example #11
Source File: test_sql.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_notna_dtype(self):
        cols = {'Bool': Series([True, None]),
                'Date': Series([datetime(2012, 5, 1), None]),
                'Int': Series([1, None], dtype='object'),
                'Float': Series([1.1, None])
                }
        df = DataFrame(cols)

        tbl = 'notna_dtype_test'
        df.to_sql(tbl, self.conn)
        returned_df = sql.read_sql_table(tbl, self.conn)  # noqa
        meta = sqlalchemy.schema.MetaData(bind=self.conn)
        meta.reflect()
        if self.flavor == 'mysql':
            my_type = sqltypes.Integer
        else:
            my_type = sqltypes.Boolean

        col_dict = meta.tables[tbl].columns

        assert isinstance(col_dict['Bool'].type, my_type)
        assert isinstance(col_dict['Date'].type, sqltypes.DateTime)
        assert isinstance(col_dict['Int'].type, sqltypes.Integer)
        assert isinstance(col_dict['Float'].type, sqltypes.Float) 
Example #12
Source File: test_sql.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_notna_dtype(self):
        cols = {'Bool': Series([True, None]),
                'Date': Series([datetime(2012, 5, 1), None]),
                'Int': Series([1, None], dtype='object'),
                'Float': Series([1.1, None])
                }
        df = DataFrame(cols)

        tbl = 'notna_dtype_test'
        df.to_sql(tbl, self.conn)
        returned_df = sql.read_sql_table(tbl, self.conn)  # noqa
        meta = sqlalchemy.schema.MetaData(bind=self.conn)
        meta.reflect()
        if self.flavor == 'mysql':
            my_type = sqltypes.Integer
        else:
            my_type = sqltypes.Boolean

        col_dict = meta.tables[tbl].columns

        assert isinstance(col_dict['Bool'].type, my_type)
        assert isinstance(col_dict['Date'].type, sqltypes.DateTime)
        assert isinstance(col_dict['Int'].type, sqltypes.Integer)
        assert isinstance(col_dict['Float'].type, sqltypes.Float) 
Example #13
Source File: migration.py    From AnyBlok with Mozilla Public License 2.0 5 votes vote down vote up
def init_modify_type(self, diff):
        if sgdb_in(self.migration.conn.engine, ['MySQL', 'MariaDB']):
            if isinstance(diff[5], TINYINT) and isinstance(diff[6], Boolean):
                # Boolean are TINYINT in MySQL DataBase
                return True
        if sgdb_in(self.migration.conn.engine, ['MsSQL']):
            if isinstance(diff[5], BIT) and isinstance(diff[6], Boolean):
                # Boolean are TINYINT in MySQL DataBase
                return True

        table = "%s.%s" % diff[1:3] if diff[1] else diff[2]
        self.log_names.append("Modify column type %s.%s : %s => %s" % (
            table, diff[3], diff[5], diff[6]))
        return False 
Example #14
Source File: test_dialect.py    From gsheets-db-api with MIT License 4 votes vote down vote up
def test_get_columns(self):
        description = [
            ('datetime', Type.DATETIME, None, None, None, None, True),
            ('number', Type.NUMBER, None, None, None, None, True),
            ('boolean', Type.BOOLEAN, None, None, None, None, True),
            ('date', Type.DATE, None, None, None, None, True),
            ('timeofday', Type.TIMEOFDAY, None, None, None, None, True),
            ('string', Type.STRING, None, None, None, None, True),
        ]
        connection = Mock()
        connection.execute = Mock()
        result = Mock()
        result._cursor_description = Mock()
        result._cursor_description.return_value = description
        connection.execute.return_value = result

        dialect = GSheetsDialect()
        url = make_url('gsheets://docs.google.com/')
        dialect.create_connect_args(url)

        result = dialect.get_columns(connection, 'SOME TABLE')
        expected = [
            {
                'name': 'datetime',
                'type': sqltypes.DATETIME,
                'nullable': True,
                'default': None,
            },
            {
                'name': 'number',
                'type': sqltypes.Numeric,
                'nullable': True,
                'default': None,
            },
            {
                'name': 'boolean',
                'type': sqltypes.Boolean,
                'nullable': True,
                'default': None,
            },
            {
                'name': 'date',
                'type': sqltypes.DATE,
                'nullable': True,
                'default': None,
            },
            {
                'name': 'timeofday',
                'type': sqltypes.TIME,
                'nullable': True,
                'default': None,
            },
            {
                'name': 'string',
                'type': sqltypes.String,
                'nullable': True,
                'default': None,
            },
        ]
        self.assertEqual(result, expected)