Ejemplo n.º 1
0
def test_drive_create():
    """Test drive creator by factory type."""
    driver_klass = factory.get("application/zip")

    mappings_str = '{"class_name":"cons_1985","start_date":{"value":"1985-01-01"},"end_date":{"value":"1985-12-31"}}'
    mappings = json.loads(mappings_str)
    driver: Driver = driver_klass(entries=None, mappings=mappings)

    assert driver.__class__.__name__ == "Shapefile"
Ejemplo n.º 2
0
def add_dataset_data_file(dataset_name, dataset_version, user_id, mimetype,
                          dataset_file, **extra_fields):
    """Insert dataset data into database."""
    from sample_db_utils.core.driver import Driver
    from sample_db_utils.factory import factory

    extra_fields.setdefault('create', False)
    extra_fields.setdefault(
        'mappings_json',
        dict(class_id="class_id", start_date="start_date",
             end_date="end_date"))

    affected_rows = 0
    ds = None

    driver_type = factory.get(mimetype)

    with _db.session.begin_nested():
        try:
            ds = _db.session.query(Datasets)\
                .filter(Datasets.name == dataset_name, Datasets.version == dataset_version)\
                .first()
        except ValueError:
            raise RuntimeError(
                f'Dataset {dataset_name}-V{dataset_version} not found!')

        _accessor = DBAccessor(system_id=ds.classification_system_id)

        driver: Driver = driver_type(entries=dataset_file,
                                     mappings=extra_fields['mappings_json'],
                                     storager=_accessor,
                                     user=user_id)

        driver.load_data_sets()
        dataset_table = ds.ds_table

        try:
            driver.store(dataset_table)
            logging.info('Data inserted in table {}'.format(
                driver.__class__.__name__))
            affected_rows = len(driver.get_data_sets())
        except exc.SQLAlchemyError as e:
            _db.session.rollback()
            raise RuntimeError(f'Error while store data: {e}')

    _db.session.commit()

    return ds, affected_rows
Ejemplo n.º 3
0
def insert_dataset_table(verbose, user_full_name, dataset_table_name, mappings,
                         classification_system_name,
                         classification_system_version, dataset_file,
                         create_table):
    """Insert data into a dataset table."""
    print(mappings)
    if verbose:
        click.secho(f'Insert data into dataset_{dataset_table_name}..',
                    bold=True,
                    fg='yellow')

    with open(mappings, "r") as m:
        mappings_json = json.load(m)

    if 'collect_date' not in mappings_json:
        mappings_json['collect_date'] = None

    dataset_drive_type = get_mimetype(dataset_file)

    driver_klass = factory.get(dataset_drive_type)

    args = dict()

    args["mappings_json"] = mappings_json
    args["dataset_file"] = dataset_file
    args["obs_already_exist"] = create_table

    affected_rows = utils.create_dataset_table(
        user_full_name=user_full_name,
        dataset_table_name=dataset_table_name,
        classification_system_name=classification_system_name,
        classification_system_version=classification_system_version,
        driver_type=driver_klass,
        **args)

    click.secho(f'Dataset table dataset_{dataset_table_name} loaded!',
                bold=True,
                fg='green')

    if verbose:
        click.secho(f'\tNumber of data inserted {affected_rows}.',
                    bold=False,
                    fg='black')
Ejemplo n.º 4
0
def test_drive_create_fail():
    """Test drive creator by factory type."""
    driver_klass = factory.get("application/zip")
    driver: Driver = driver_klass(entries=None, mappings=None)

    assert driver.__class__ == "Shapefile"
Ejemplo n.º 5
0
def test_factory():
    """Test factory get type."""
    driver_klass = factory.get("application/zip")

    assert driver_klass.__class__.__name__ == "ABCMeta"
    assert driver_klass.__name__ == "Shapefile"