def test_trackable_indexability(session, caching): """Test Trackable indexability (via []s)""" problem_name = 'Test Problem' problem_key = Problem.create_key(name='Test Problem') assert len(problem_key) == 1 with pytest.raises(KeyMissingFromRegistryAndDatabase): # a KeyError Problem[problem_key] with pytest.raises(KeyError): Problem[problem_key] problem = Problem(problem_name) # This registers the key indexed_problem = Problem[problem_key] assert indexed_problem is problem # Unpacked 1-tuples can also be used to index from the registry indexed_problem = Problem[problem_key.human_id] assert indexed_problem is problem session.add(problem) session.commit() Trackable.clear_all() # This deregisters the key # This registers the key since it is found in the database indexed_problem = Problem[problem_key] assert indexed_problem is problem Trackable.clear_all() # This deregisters the key # Unpacked 1-tuples can also be used to index from the database indexed_problem = Problem[problem_key.human_id] assert indexed_problem is problem
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
def test_trackable_tget(session, caching): """Test Trackable get (tget)""" problem_name = 'Test Problem' problem_key = Problem.create_key(name='Test Problem') assert len(problem_key) == 1 nada = 'nada' tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=False) assert tget_problem == nada tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=True) assert tget_problem == nada problem = Problem(problem_name) # This registers the key tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=False) assert tget_problem is problem tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=True) assert tget_problem is problem # Unpacked 1-tuples can also be used to get from the registry tget_problem = Problem.tget(problem_key.human_id, query_on_miss=False) assert tget_problem is problem session.add(problem) session.commit() Trackable.clear_all() # This deregisters the key tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=False) assert tget_problem == nada # This registers the key since it is found in the database tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=True) assert tget_problem is problem tget_problem = Problem.tget(problem_key, default=nada, query_on_miss=False) assert tget_problem is problem Trackable.clear_all() # This deregisters the key # Unpacked 1-tuples can also be used to get from the database tget_problem = Problem.tget(problem_key.human_id, query_on_miss=True) assert tget_problem is problem
def test_decode_same_data(session, caching): """Test decoding incrementally""" create_geo_data(session) json_path = os.path.join(PROBLEM_DATA_DIRECTORY, 'problems02.json') u2 = decode(session, json_path) for updates in u2.values(): session.add_all(updates) session.commit() p2 = Problem.query.filter_by(name='Domestic Violence').one() assert p2 is Problem['domestic_violence'] # Simulate impact of app restart on Trackable by clearing it: Trackable.clear_all() # Try reloading existing data (none should be loaded): u2_repeat = decode(session, json_path) for updates in u2_repeat.values(): assert len(updates) == 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
def test_incremental_decode(session, caching): """Test decoding multiple files incrementally""" create_geo_data(session) # Initial data load: json_path = os.path.join(PROBLEM_DATA_DIRECTORY, 'problems00.json') u0 = decode(session, json_path) for updates in u0.values(): session.add_all(updates) session.commit() p0 = Problem.query.filter_by(name='Poverty').one() assert p0 is Problem['poverty'] # Simulate impact of app restart on Trackable by clearing it: Trackable.clear_all() # Next data load: json_path = os.path.join(PROBLEM_DATA_DIRECTORY, 'problems01.json') u1 = decode(session, json_path) for updates in u1.values(): session.add_all(updates) session.commit() p0 = Problem.query.filter_by(name='Poverty').one() assert p0 is Problem['poverty'] p1 = Problem.query.filter_by(name='Homelessness').one() assert p1 is Problem['homelessness'] # Simulate impact of app restart on Trackable by clearing it: Trackable.clear_all() # Next data load: json_path = os.path.join(PROBLEM_DATA_DIRECTORY, 'problems02.json') u2 = decode(session, json_path) for updates in u2.values(): session.add_all(updates) session.commit() # Make sure they're still the same problems p0 = Problem.query.filter_by(name='Poverty').one() assert p0 is Problem['poverty'] p1 = Problem.query.filter_by(name='Homelessness').one() assert p1 is Problem['homelessness'] p2 = Problem.query.filter_by(name='Domestic Violence').one() assert p2 is Problem['domestic_violence'] c1 = ProblemConnection.query.filter( ProblemConnection.axis == 'scoped', ProblemConnection.broader == p0, ProblemConnection.narrower == p1).one() assert c1.axis == 'scoped' assert c1.broader is p0 assert c1.narrower is p1 c2 = ProblemConnection.query.filter( ProblemConnection.axis == 'causal', ProblemConnection.driver == p2, ProblemConnection.impact == p1).one() assert c2.axis == 'causal' assert c2.driver is p2 assert c2.impact is p1 rs1 = ProblemConnectionRating.query.filter( ProblemConnectionRating.connection == c1) assert len(rs1.all()) > 0 for r in rs1: assert r.connection is c1 geo = Geo['us/tx/austin'] rs2 = ProblemConnectionRating.query.filter( ProblemConnectionRating.problem == p1, ProblemConnectionRating.connection == c2, ProblemConnectionRating.org.is_(None), ProblemConnectionRating.geo == geo) assert len(rs2.all()) > 0 for r in rs2: assert r.problem == p1 assert r.connection == c2 assert r.org is None assert r.geo is geo
def teardown(): Trackable.clear_all() transaction.rollback() connection.close() session.remove()
def teardown(): Trackable.clear_all() intertwine_db.drop_all() if os.path.exists(TESTDB_PATH): os.unlink(TESTDB_PATH)
def teardown(): Trackable.clear_all() ctx.pop()