Ejemplo n.º 1
0
def test_uri_formation_and_instantiation_with_null_query_value(
        session, caching):
    """Test URI formation and instantiation"""
    cls = Community
    # Register existing for builders since registry is cleared below
    Trackable.register_existing(session)

    builder = Builder(cls, optional=False)
    inst = builder.build(org=None)  # org is a query parameter

    uri = inst.uri
    assert 'org' not in uri, 'org should not be in uri since it is null'
    assert '?' not in uri, 'there should be no query string since org is null'

    assert cls.URI_TYPE is UriType.NATURAL
    instantiated_via_uri = IntertwineModel.instantiate_uri(uri)
    assert instantiated_via_uri is inst

    session.add(inst)
    session.commit()
    uri = inst.uri

    Trackable.clear_all()  # Deregister key to test retrieve from db

    instantiated_from_db_via_uri = IntertwineModel.instantiate_uri(uri)
    assert instantiated_from_db_via_uri is inst
Ejemplo n.º 2
0
def decode(session, json_path, *args, **options):
    """
    Load JSON files within a path and return data structures

    Given a path to a JSON file or a directory containing JSON files,
    returns a dictionary where the keys are classes and the values are
    corresponding sets of objects updated from the JSON file(s).

    Calls another function to actually decode the json_data. This
    other function's name begins with 'decode_' and ends with the last
    directory in the absolute json_path: decode_<dir_name>(json_data)

    Usage:
    >>> json_path = '/data/problems/problems00.json'  # load a JSON file
    >>> u0 = decode(json_path)  # get updates from data load
    >>> json_path = '/data/problems/'  # load all JSON files in a directory
    >>> u1 = decode(json_path)  # get updates from next data load
    >>> u1_problems = u1['Problem']  # get set of updated problems
    >>> u1_connections = u1['ProblemConnection']  # set of updated connections
    >>> u1_ratings = u1['ProblemConnectionRating']  # set of updated ratings
    >>> p0 = Problem('poverty')  # get existing 'Poverty' problem
    >>> p1 = Problem('homelessness')  # get existing 'Homelessness' problem
    >>> p2 = Problem['domestic_violence']  # Problem is subscriptable
    >>> for p in Problem:  # Problem is iterable
    ...    print(p)
    """
    # Gather valid json_paths based on the given file or directory
    json_paths = []
    if os.path.isfile(json_path):
        if (json_path.rsplit('.', 1)[-1].lower() == 'json'
                and 'schema' not in os.path.basename(json_path).lower()):
            json_paths.append(json_path)
    elif os.path.isdir(json_path):
        json_paths = [
            os.path.join(json_path, f) for f in os.listdir(json_path)
            if (os.path.isfile(os.path.join(json_path, f)) and f.rsplit(
                '.', 1)[-1].lower() == 'json' and 'schema' not in f.lower())
        ]
    if len(json_paths) == 0:
        raise InvalidJSONPath(path=json_path)

    # Load raw json_data from each of the json_paths
    json_data = []
    for path in json_paths:
        with io.open(path) as json_file:
            # TODO: May need to change this to load incrementally in the future
            json_data.append(json.load(json_file))

    # Determine the decode function based on directory name and then call it
    if os.path.isfile(json_path):
        dir_name = os.path.abspath(json_path).rsplit('/', 2)[-2]
    else:
        dir_name = os.path.abspath(json_path).rsplit('/', 1)[-1]
    function_name = 'decode_' + dir_name
    module = sys.modules[__name__]
    decode_function = getattr(module, function_name)
    Trackable.register_existing(session)
    return decode_function(json_data)
Ejemplo n.º 3
0
def test_uri_formation_and_instantiation(session, caching):
    """Test URI formation and instantiation"""
    for class_name, cls in Trackable._classes.items():
        # Register existing for builders since registry is cleared below
        Trackable.register_existing(session)

        builder = Builder(cls, optional=False)
        inst = builder.build()
        uri = inst.uri
        if cls.URI_TYPE is UriType.NATURAL:
            instantiated_via_uri = IntertwineModel.instantiate_uri(uri)
            assert instantiated_via_uri is inst

        session.add(inst)
        session.commit()
        uri = inst.uri

        Trackable.clear_all()  # Deregister key to test retrieve from db

        instantiated_from_db_via_uri = IntertwineModel.instantiate_uri(uri)
        assert instantiated_from_db_via_uri is inst
Ejemplo n.º 4
0
def erase_data(session, confirm=None):
    """
    Erase all data from database and clear tracking of all instances

    For Trackable classes, erases all data from the database and clears
    tracking of all instances. Prompts the user to confirm by typing
    'ERASE'. Can alternatively take an optional confirm parameter with
    a value of 'ERASE' to proceed without a user prompt.
    """
    if confirm != 'ERASE':
        prompt = ('This will erase *all* data from the database and '
                  'clear tracking of all instances.\n'
                  'Type "ERASE" (all caps) to proceed. '
                  'Anything else will abort.\n>')
        confirm_again = raw_input(prompt)
        if confirm_again != 'ERASE':
            print('Aborting - leaving data untouched.')
            return

    print('Processing...')
    # limit data to Trackable classes with existing tables
    engine = session.bind
    inspector = Inspector.from_engine(engine)
    table_names = set(inspector.get_table_names())
    classes = [
        x for x in Trackable._classes.values()
        if x.__tablename__ in table_names
    ]
    print('Erase Data classes: ', classes)
    Trackable.register_existing(session, *classes)
    for cls in classes:
        for inst in cls:
            session.delete(inst)
    session.commit()
    Trackable.clear_instances()
    print('Erase data has completed')