Exemplo n.º 1
0
def s3_bucket(extension):
    with conn():
        b = 's3://%s/%s%s' % (test_bucket_name, next(_tmps), extension)
        try:
            yield b
        finally:
            drop(resource(b))
Exemplo n.º 2
0
def fkey(url, pkey):
    try:
        fkey = odo([(i,
                     int(np.random.randint(pkey.count().scalar())),
                     int(np.random.randint(10000)))
                    for i in range(10)],
                   url % 'fkey',
                   dshape=dshape('''var * {id: int64,
                                           sym_id: map[int64,
                                                       {id: int64,
                                                        sym: string,
                                                        price: float64,
                                                        main: map[int64,
                                                                  {id: int64,
                                                                  data: int64}]}],
                                           size: int64}'''),
                   foreign_keys=dict(sym_id=pkey.c.id),
                   primary_key=['id'])
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield fkey
        finally:
            drop(fkey)
Exemplo n.º 3
0
def sql_with_null(url):
    ds = dshape(""" var * {name: ?string,
                           sex: ?string,
                           amount: int,
                           id: int,
                           comment: ?string}
              """)
    rows = [('Alice', 'F', 100, 1, 'Alice comment'),
            (None, 'M', 300, 2, None),
            ('Drew', 'F', 100, 4, 'Drew comment'),
            ('Bob', 'M', 100, 5, 'Bob comment 2'),
            ('Drew', 'M', 200, 5, None),
            ('first', None, 300, 4, 'Missing info'),
            (None, None, 300, 6, None)]
    try:
        x = url % next(names)
        t = data(x, dshape=ds)
        print(x)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        assert t.dshape == ds
        t = data(odo(rows, t))
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 4
0
def fkey(url, pkey):
    try:
        fkey = odo([(i,
                     int(np.random.randint(pkey.count().scalar())),
                     int(np.random.randint(10000)))
                    for i in range(10)],
                   url % 'fkey',
                   dshape=dshape('''var * {id: int64,
                                           sym_id: map[int64,
                                                       {id: int64,
                                                        sym: string,
                                                        price: float64,
                                                        main: map[int64,
                                                                  {id: int64,
                                                                  data: int64}]}],
                                           size: int64}'''),
                   foreign_keys=dict(sym_id=pkey.c.id),
                   primary_key=['id'])
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield fkey
        finally:
            drop(fkey)
Exemplo n.º 5
0
def pkey(url, main):
    choices = [
        u'AAPL', u'HPQ', u'ORCL', u'IBM', u'DOW', u'SBUX', u'AMD', u'INTC',
        u'GOOG', u'PRU', u'MSFT', u'AIG', u'TXN', u'DELL', u'PEP'
    ]
    n = 100
    data = list(
        zip(range(n),
            np.random.choice(choices, size=n).tolist(),
            np.random.uniform(10000, 20000, size=n).tolist(),
            np.random.randint(main.count().scalar(), size=n).tolist()))
    try:
        pkey = odo(
            data,
            url % 'pkey',
            dshape=dshape(
                'var * {id: int64, sym: string, price: float64, main: map[int64, T]}'
            ),
            foreign_keys=dict(main=main.c.id),
            primary_key=['id'])
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield pkey
        finally:
            drop(pkey)
Exemplo n.º 6
0
def pkey(url, main):
    choices = [u'AAPL', u'HPQ', u'ORCL', u'IBM', u'DOW', u'SBUX', u'AMD',
               u'INTC', u'GOOG', u'PRU', u'MSFT', u'AIG', u'TXN', u'DELL',
               u'PEP']
    n = 100
    data = list(zip(range(n),
                    np.random.choice(choices, size=n).tolist(),
                    np.random.uniform(10000, 20000, size=n).tolist(),
                    np.random.randint(main.count().scalar(), size=n).tolist()))
    try:
        pkey = odo(data, url % 'pkey',
                dshape=dshape('''var * {id: int64,
                                        sym: string,
                                        price: float64,
                                        main: map[int64,
                                                  {id: int64,
                                                   data: int64}]}'''),
                   foreign_keys=dict(main=main.c.id),
                   primary_key=['id'])
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield pkey
        finally:
            drop(pkey)
Exemplo n.º 7
0
def sql_with_null(url):
    ds = dshape(""" var * {name: ?string,
                           sex: ?string,
                           amount: int,
                           id: int,
                           comment: ?string}
              """)
    rows = [('Alice', 'F', 100, 1, 'Alice comment'), (None, 'M', 300, 2, None),
            ('Drew', 'F', 100, 4, 'Drew comment'),
            ('Bob', 'M', 100, 5, 'Bob comment 2'), ('Drew', 'M', 200, 5, None),
            ('first', None, 300, 4, 'Missing info'),
            (None, None, 300, 6, None)]
    try:
        x = url % next(names)
        t = data(x, dshape=ds)
        print(x)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        assert t.dshape == ds
        t = data(odo(rows, t))
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 8
0
def s3_bucket(extension):
    with conn():
        b = 's3://%s/%s%s' % (test_bucket_name, next(_tmps), extension)
        try:
            yield b
        finally:
            drop(resource(b))
Exemplo n.º 9
0
def test_drop():
    with tmpfile('json') as fn:
        js = JSON(fn)
        append(js, [1, 2, 3])

        assert os.path.exists(fn)
        drop(js)
        assert not os.path.exists(fn)
Exemplo n.º 10
0
def test_drop():
    with tmpfile('json') as fn:
        js = JSON(fn)
        append(js, [1, 2, 3])

        assert os.path.exists(fn)
        drop(js)
        assert not os.path.exists(fn)
Exemplo n.º 11
0
def test_drop():
    with tmpfile('.bson') as fn:
        bs = BSON(fn)
        append(bs, dat)

        assert os.path.exists(fn)
        drop(bs)
        assert not os.path.exists(fn)
Exemplo n.º 12
0
def sql(url):
    try:
        t = resource(url, dshape='var * {a: int32, b: int32}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 13
0
def fsql(engine, fcsv, name):
    try:
        t = resource('%s::%s' % (url, name), dshape=discover(fcsv))
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 14
0
def sql(url):
    try:
        t = resource(url, dshape='var * {a: int32, b: int32}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 15
0
def hive_table(host):
    name = ('temp' + str(uuid.uuid1()).replace('-', ''))[:30]
    uri = 'hive://hdfs@%s:10000/default::%s' % (host, name)

    try:
        yield uri
    finally:
        with ignoring(Exception):
            drop(uri)
Exemplo n.º 16
0
def hive_table(host):
    name = ('temp' + str(uuid.uuid1()).replace('-', ''))[:30]
    uri = 'hive://hdfs@%s:10000/default::%s' % (host, name)

    try:
        yield uri
    finally:
        with ignoring(Exception):
            drop(uri)
Exemplo n.º 17
0
def decimal_sql(engine, name):
    try:
        t = resource("%s::%s" % (url, name), dshape="var * {a: ?decimal[10, 3], b: decimal[11, 2]}")
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 18
0
def dsql(engine, dcsv, name):
    try:
        t = resource("%s::%s" % (url, name), dshape=discover(dcsv))
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 19
0
def sql_with_float(url):
    try:
        t = data(url % next(names), dshape='var * {c: float64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 20
0
def test_drop_reflects_database_state(url):
    data = list(zip(range(5), range(1, 6)))

    t = odo(data, url, dshape='var * {A: int64, B: int64}')
    assert t.exists()
    assert resource(url).exists()

    drop(url)
    with pytest.raises(ValueError):
        resource(url)  # Table doesn't exist and no dshape
Exemplo n.º 21
0
def sql_with_float(url):
    try:
        t = resource(url % next(names), dshape='var * {c: float64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 22
0
def sql_with_float(url):
    try:
        t = resource(url, dshape='var * {c: float64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 23
0
def sql_with_schema(url):
    try:
        t = resource(url, dshape='var * {a: int32, b: ?int32}',
                     schema=next(names))
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 24
0
def quoted_sql(pg_ip, csv):
    url = 'postgresql://postgres@{}/test::foo bar'.format(pg_ip)
    try:
        t = resource(url, dshape=discover(csv))
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 25
0
def quoted_sql(engine, fcsv):
    dshape = var * Record(list(zip('ab', discover(fcsv).measure.types)))
    try:
        t = resource('%s::foo bar' % url, dshape=dshape)
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 26
0
def nyc():
    try:
        t = odo(example('nyc.csv'),
                'postgresql://postgres@localhost/test::nyc')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 27
0
def decimal_sql(engine, name):
    try:
        t = resource('%s::%s' % (url, name),
                     dshape="var * {a: ?decimal[10, 3], b: decimal[11, 2]}")
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 28
0
def sql(url):
    try:
        t = resource(url, dshape='var * {A: string, B: int64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([('a', 1), ('b', 2)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 29
0
def quoted_sql(engine, fcsv):
    dshape = var * Record(list(zip('ab', discover(fcsv).measure.types)))
    try:
        t = resource('%s::foo bar' % url, dshape=dshape)
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 30
0
def complex_sql(url):
    ds = """var * {
        Name: string, RegistrationDate: date, ZipCode: int32, Consts: float64
    }"""
    try:
        t = resource(url, dshape=ds)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 31
0
def test_drop_group():
    with tmpfile('.hdf5') as fn:
        f = h5py.File(fn)
        try:
            f.create_dataset('/group/data', data=x, chunks=True,
                             maxshape=(None,) + x.shape[1:])
            drop(f['/group'])
            assert '/group' not in f.keys()
        finally:
            with ignoring(Exception):
                f.close()
Exemplo n.º 32
0
def data():
    try:
        t = odo(example('nyc.csv'),
                'mysql+pymysql://%s@localhost/test::nyc' % getuser())
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t.bind
        finally:
            drop(t)
Exemplo n.º 33
0
def quoted_sql(csv):
    url = "postgresql://postgres@localhost/test::foo bar"
    try:
        t = resource(url, dshape=discover(csv))
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 34
0
def sql(engine, csv, name):
    dshape = var * Record(list(zip('ab', discover(csv).measure.types)))
    try:
        t = resource('%s::%s' % (url, name), dshape=dshape)
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 35
0
def big_sql(url):
    try:
        t = data(url % next(names), dshape='var * {A: string, B: int64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo(zip(list('a' * 100), list(range(100))), t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 36
0
def fsql(engine, fcsv, name):
    dshape = discover(fcsv)
    dshape = DataShape(
        var, Record([(n, typ) for n, typ in zip('ab', dshape.measure.types)]))
    try:
        t = resource('%s::%s' % (url, name), dshape=dshape)
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 37
0
def sql_with_timedeltas(url):
    try:
        t = data(url % next(names), dshape='var * {N: timedelta}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([(timedelta(seconds=n), ) for n in range(10)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 38
0
def complex_sql(url):
    ds = """var * {
        Name: string, RegistrationDate: date, ZipCode: int32, Consts: float64
    }"""
    try:
        t = resource(url, dshape=ds)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        yield t
        drop(t)
Exemplo n.º 39
0
def sql_with_dts(url):
    try:
        t = resource(url, dshape="var * {A: datetime}")
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([(d,) for d in pd.date_range("2014-01-01", "2014-02-01")], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 40
0
def sql_with_dts(url):
    try:
        t = resource(url % next(names), dshape='var * {A: datetime}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([(d,) for d in pd.date_range('2014-01-01', '2014-02-01')], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 41
0
def sql(engine, csv, name):
    dshape = var * Record(list(zip('ab', discover(csv).measure.types)))
    try:
        t = resource('%s::%s' % (url, name), dshape=dshape)
    except sqlalchemy.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 42
0
def sql_with_timedeltas(url):
    try:
        t = data(url % next(names), dshape='var * {N: timedelta}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([(timedelta(seconds=n),) for n in range(10)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 43
0
def sqlb(url):
    try:
        t = data(url % next(names), dshape='var * {A: string, B: int64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([('a', 1), ('b', 2)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 44
0
def sql_with_dts(url):
    try:
        t = data(url % next(names), dshape='var * {A: datetime}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([(d,) for d in pd.date_range('2014-01-01', '2014-02-01')], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 45
0
def big_sql(url):
    try:
        t = data(url % next(names), dshape='var * {A: string, B: int64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo(zip(list('a'*100), list(range(100))), t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 46
0
def sqla(url):
    try:
        t = data(url % next(names), dshape='var * {A: ?string, B: ?int32}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([('a', 1), (None, 1), ('c', None)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 47
0
def sqla(url):
    try:
        t = resource(url % next(names), dshape='var * {A: ?string, B: ?int32}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([('a', 1), (None, 1), ('c', None)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 48
0
def sqlb(url):
    try:
        t = resource(url % next(names), dshape='var * {A: string, B: int64}')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        t = odo([('a', 1), ('b', 2)], t)
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 49
0
def sql_with_ugly_schema(url):
    try:
        t = resource(url, dshape='var * {a: int32, b: ?int32}',
                     schema='foo.b.ar')
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
            t.bind.execute(sa.sql.ddl.DropSchema(t.schema))
Exemplo n.º 50
0
def sql_with_schema():
    url = 'postgresql://postgres@localhost/test::%s' % next(names)
    try:
        t = resource(url, dshape='var * {a: int32, b: ?int32}',
                     schema=next(names))
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 51
0
def nyc_csv(pg_ip):
    try:
        t = odo(
            example('nyc.csv'),
            'postgresql://postgres@{}/test::nyc'.format(pg_ip),
        )
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 52
0
def test_drop_group():
    with tmpfile('.hdf5') as fn:
        f = h5py.File(fn)
        try:
            f.create_dataset('/group/data',
                             data=x,
                             chunks=True,
                             maxshape=(None, ) + x.shape[1:])
            drop(f['/group'])
            assert '/group' not in f.keys()
        finally:
            with ignoring(Exception):
                f.close()
Exemplo n.º 53
0
def sql(url):
    ds = dshape('var * {A: string, B: int64}')
    try:
        t = data(url % next(names), dshape=ds)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        assert t.dshape == ds
        t = data(odo([('a', 1), ('b', 2)], t))
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 54
0
def main(url):
    try:
        main = odo([(i, int(np.random.randint(10))) for i in range(13)],
                   url % 'main',
                   dshape=dshape('var * {id: int64, data: int64}'),
                   primary_key=['id'])
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield main
        finally:
            drop(main)
Exemplo n.º 55
0
def sql_two_tables(url):
    dshape = 'var * {a: int32}'
    try:
        t = resource(url % next(names), dshape=dshape)
        u = resource(url % next(names), dshape=dshape)
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield u, t
        finally:
            drop(t)
            drop(u)
Exemplo n.º 56
0
def nyc(pg_ip):
    # odoing csv -> pandas -> postgres is more robust, as it doesn't require
    # the postgres server to be on the same filesystem as the csv file.
    nyc_pd = odo(example('nyc.csv'), pd.DataFrame)
    try:
        t = odo(nyc_pd, 'postgresql://postgres@{}/test::nyc'.format(pg_ip))
    except sa.exc.OperationalError as e:
        pytest.skip(str(e))
    else:
        try:
            yield t
        finally:
            drop(t)
Exemplo n.º 57
0
def import_asn():
    try:
        logging.info("Dropping old asn-lists if exists")
        drop("mysql+pymysql://{}:{}@{}:{}/{}::asn".format(
            os.environ.get("DB_USER", "root"),
            os.environ.get("DB_PASS", "abc123"),
            os.environ.get("DB_HOST", "localhost"),
            os.environ.get("DB_PORT", "3306"),
            os.environ.get("DB_NAME", "censorship"),
        ))
    except ValueError:
        pass

    with open(
            os.environ.get("IMPORT_FILE",
                           "geoip-list/GeoLite2-ASN-Blocks-IPv4.csv")) as f:
        logging.info("Importing asn-lists")
        odo(
            [
                dict(row,
                     autonomous_system_number=
                     f'AS{row["autonomous_system_number"]}')
                for row in csv.DictReader(f, skipinitialspace=True)
            ],
            "mysql+pymysql://{}:{}@{}:{}/{}::asn".format(
                os.environ.get("DB_USER", "root"),
                os.environ.get("DB_PASS", "abc123"),
                os.environ.get("DB_HOST", "localhost"),
                os.environ.get("DB_PORT", "3306"),
                os.environ.get("DB_NAME", "censorship"),
            ),
            dshape="""
                var * {
                    network: string,
                    autonomous_system_number: string[8],
                    autonomous_system_organization: string
                }
            """,
            local="LOCAL",
        )

    logging.info("Building index for asn-lists")
    _conn = pymysql.connect(host=os.environ.get("DB_HOST", "localhost"),
                            port=int(os.environ.get("DB_PORT", 3306)),
                            user=os.environ.get("DB_USER", "root"),
                            password=os.environ.get("DB_PASS", "abc123"),
                            db=os.environ.get("DB_NAME", "censorship"))
    with _conn.cursor() as cur:
        cur.execute('''
                    CREATE INDEX asn_idx_autonomous_system_number ON asn(autonomous_system_number);
                    ''')