def pytest_sessionstart(session): """ Provides initial connection to the database and sets up the rest of the test suite :param session: The session object. Please see <https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart>`_ :type Session object: For more information please see <https://docs.pytest.org/en/latest/reference.html#session>`_ """ warnings.simplefilter('default') config.DATABASE_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://*****:*****@localhost:7687') config.AUTO_INSTALL_LABELS = True try: # Clear the database if required database_is_populated, _ = db.cypher_query("MATCH (a) return count(a)>0 as database_is_populated") if database_is_populated[0][0] and not session.config.getoption("resetdb"): raise SystemError("Please note: The database seems to be populated.\n\tEither delete all nodes and edges manually, or set the --resetdb parameter when calling pytest\n\n\tpytest --resetdb.") else: clear_neo4j_database(db) except CypherError as ce: # Handle instance without password being changed if 'The credentials you provided were valid, but must be changed before you can use this instance' in str(ce): warnings.warn("New database with no password set, setting password to 'test'") change_neo4j_password(db, 'test') db.set_connection('bolt://*****:*****@localhost:7687') warnings.warn("Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs") else: raise ce
def import_from_csv(some_args=None): clear_neo4j_database(db) db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Doctor.csv' as row create (:Doctor{id: toInteger(row._id), name: row.name, surname: row.surname, middle_name: row.middle_name, login: row.login, password: row.password, is_admin: toBoolean(row.is_admin)}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Patient.csv' as row create (:Patient{id: toInteger(row._id), birth_date: date(row.birth_date), name: row.name, surname: row.surname, middle_name: row.middle_name, diagnosis: row.diagnosis, symptoms: row.symptoms, is_male: toBoolean(row.is_male), contract_number: row.contract_number}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Meet.csv' as row create (:Meet{id: toInteger(row._id), meet_datetime: row.meet_datetime, notes: row.notes}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/_DOCTOR.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:DOCTOR]->(end) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/_PATIENT.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:PATIENT]->(end) ''') return True
def custom_init(self, pinit=False, pdestroy=False, abackend=None, **kwargs): """ Note: we ignore args here """ # recover instance with the parent method graph = super().custom_init() # db.init_app(self.app) with self.app.app_context(): if pdestroy: log.critical("Destroy current Neo4j data") from neomodel import clear_neo4j_database clear_neo4j_database(graph.db) if pinit: auto_index = self.variables.get("autoindexing", 'True') == 'True' if auto_index: try: from neomodel import remove_all_labels, install_all_labels remove_all_labels() install_all_labels() except BaseException as e: log.exit(str(e)) return graph
def setUpClass(cls): super().setUpClass() logger.info("Clearing Graph...") neomodel.clear_neo4j_database(graphdb) neomodel.install_all_labels() logger.info("Creating a user...") User.objects.create_user(username=cls.username, password=cls.password)
def destroy(self) -> None: graph = self.get_instance() if self.app: with self.app.app_context(): log.critical("Destroy current Neo4j data") clear_neo4j_database(graph.db)
def main(): import pandas as pd import os from ast import literal_eval from scraper.genius import Genius from scraper.spotify import Spotify from model.graph import connection_url from model.graph.billboard.track import Track from neomodel import db, clear_neo4j_database, config url = connection_url() print(url) config.DATABASE_URL = url db.set_connection(url) print('connected') clear_neo4j_database(db) BILLBOARD_DIR = os.path.join('output', 'billboard') weeks = os.listdir(BILLBOARD_DIR) weeks.sort(reverse=True) for week in weeks: df = pd.read_csv(os.path.join(BILLBOARD_DIR, week, 'main.csv')) for i, row in df.iterrows(): billboard_track = Track.inst(**dict(row)) print(billboard_track) # Sort artists by appearance in the title artists = ', '.join( list( map( lambda x: x['artist_name'], sorted(literal_eval(row['credited_artists']), key=lambda x: x['ordinal'])))) search_str = row['title'] + ' ' + artists genius_resp = Genius.search(search_str) print('\n\nSearching for:', search_str) if genius_resp['meta']['status'] != 200: raise ValueError( 'Probably exceeded Genius limits or invalid search') genius_resp = genius_resp['response']['hits'] for hit in genius_resp: hit = hit['result'] song_data = Genius.get_song(hit['id'])['response']['song'] if 'spotify' in [a['provider'] for a in song_data['media']]: print('Spotify exists!') for i, a in enumerate(song_data['media']): print(a) if a['provider'] == 'spotify': print('Spotify Exists -', song_data['full_title']) spotify_data = Spotify.get_track( song_data['media'][i]['native_uri']) print(spotify_data) break quit() print(weeks)
def setUp(self): clear_neo4j_database(db) profile = Profile().save() project = Project(title='project').save() project.profile.connect(profile) self.profile = profile self.project = project
def reset_db(reset_labels=True, confirmed=False): print('Deleting all nodes and relationships...') if not confirmed: confirm = input('Enter "Y" to confirm: ') confirmed = confirm == 'Y' if not confirmed: print('Reset canceled!') return if reset_labels: remove_all_labels() clear_neo4j_database(db)
def pytest_sessionstart(session): """ Provides initial connection to the database and sets up the rest of the test suite :param session: The session object. Please see <https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart>`_ :type Session object: For more information please see <https://docs.pytest.org/en/latest/reference.html#session>`_ """ warnings.simplefilter("default") config.DATABASE_URL = os.environ.get( "NEO4J_BOLT_URL", "bolt://*****:*****@localhost:7687") config.AUTO_INSTALL_LABELS = True try: # Clear the database if required database_is_populated, _ = db.cypher_query( "MATCH (a) return count(a)>0 as database_is_populated") if database_is_populated[0][0] and not session.config.getoption( "resetdb"): raise SystemError( "Please note: The database seems to be populated.\n\tEither delete all nodes and edges manually, or set the --resetdb parameter when calling pytest\n\n\tpytest --resetdb." ) else: clear_neo4j_database(db, clear_constraints=True, clear_indexes=True) except (Neo4jError, ClientError) as ce: # Handle instance without password being changed if ("The credentials you provided were valid, but must be changed before you can use this instance" in str(ce)): warnings.warn( "New database with no password set, setting password to 'test'" ) try: change_neo4j_password(db, "test") # Ensures that multiprocessing tests can use the new password config.DATABASE_URL = "bolt://*****:*****@localhost:7687" db.set_connection("bolt://*****:*****@localhost:7687") warnings.warn( "Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs" ) except (Neo4jError, ClientError) as e: if ("The credentials you provided were valid, but must be changed before you can use this instance" in str(e)): warnings.warn( "You appear to be running on version 4.0+ of Neo4j, without having changed the password." "Please manually log in, change your password, then update the config.DATABASE_URL call at line 32 in this file" ) else: raise e else: raise ce
def delete_license(request): clear_neo4j_database(db) try: with open(LEVELS_FILE, 'w') as outfile: json.dump([], outfile) except IOError: pass response = HttpResponse( '', content_type='application/json', status=200, ) response['Access-Control-Allow-Origin'] = '*' return response
def setUp(self): clear_neo4j_database(db) profile = Profile().save() project = Project(title='project').save() contribution = Contribution(title='contribution').save() project.profile.connect(profile) contribution.parent_project.connect(project) self.profile = profile self.project = project self.contribution = contribution
def pytest_sessionstart(session): """ Provides initial connection to the database and sets up the rest of the test suite :param session: The session object. Please see <https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart>`_ :type Session object: For more information please see <https://docs.pytest.org/en/latest/reference.html#session>`_ """ warnings.simplefilter('default') config.DATABASE_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://*****:*****@localhost:7687') config.AUTO_INSTALL_LABELS = True reset_db = session.config.getoption("resetdb") try: database_is_populated, _ = db.cypher_query( "MATCH (a) return count(a)>0 as database_is_populated") except CypherError as ce: # Handle instance without password being changed if 'The credentials you provided were valid, but must be changed before you can use this instance' in str( ce): warnings.warn( "New database with no password set, setting password to 'test'" ) change_neo4j_password(db, 'test') db.set_connection('bolt://*****:*****@localhost:7687') warnings.warn( "Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs" ) database_is_populated, _ = db.cypher_query( "MATCH (a) return count(a)>0 as database_is_populated") else: raise ce # If database is populated and user has not requested to clear the db, terminate test run with a SystemError if database_is_populated[0][0] and not reset_db: raise SystemError(DATABASE_POPULATED) # Clear the database if requested if reset_db: print("Clearing Neo4j database.") clear_neo4j_database(db)
def custom_init(self, pinit=False, pdestroy=False, **kwargs): """ Note: we ignore args here """ # recover instance with the parent method graph = super().custom_init() # db.init_app(self.app) with self.app.app_context(): if pdestroy: log.critical("Destroy current Neo4j data") from neomodel import clear_neo4j_database clear_neo4j_database(graph.db) if pinit: pass return graph
def load_db_data(file_name=None): # clear db clear_neo4j_database(db) for constraint in db.cypher_query("CALL db.constraints")[0]: db.cypher_query(f'DROP CONSTRAINT {constraint[0]};') if file_name: csvFromExcelParser(file_name) base_dir = os.path.dirname(os.path.dirname(__file__)) db_file = os.path.join(base_dir, 'db_data.txt') with open(db_file, 'r') as db_data: raw_queries = db_data.readlines() clear_queries = [query.replace('\n', '') for query in raw_queries] queries = list(filter(len, clear_queries)) current_query = '' for query in queries: if query[-1] != ';': current_query += f' {query}' else: current_query = query if not current_query else f'{current_query} {query}' db.cypher_query(current_query) current_query = ''
def test_schema(): clear_neo4j_database(db) # Create the validator and assert that it has no connections validator = Validator(acc_address='cosmos1uwgyzw8mdsaxcmuy28lz79q3xznlnqmph227c7', address='cosmosvaloper1uwgyzw8mdsaxcmuy28lz79q3xznlnqmpj77t5d', cons_address='DD2CA4EF17A292E085C485C25D4B1855CEA9BE69', moniker='loco.block3.community').save() assert validator # assert len(validator.tx) == 0 # assert not validator.tx # Add a block block = Block(height=1, proposer='DD2CA4EF17A292E085C485C25D4B1855CEA9BE69').save() assert block assert len(block.tx) == 0 assert not block.tx # Connect Validator to block validator.tx.connect(block) assert len(validator.tx) == 1 assert validator.tx assert validator.tx.is_connected(block)
def setup_models(): # Initialize the basic database for the application clear_neo4j_database(db) users = Person.get_or_create( ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}), ({'email': '*****@*****.**'}) ) users[0].friends.connect(users[1]) users[0].friends.connect(users[2]) users[0].friends.connect(users[3]) users[0].friends.connect(users[4]) users[4].blocks.connect(users[0]) users[4].blocks.connect(users[5]) users[6].subscribes.connect(users[0]) users[6].subscribes.connect(users[7])
def clear_resources() -> None: """ Clear entire database. :return: None """ clear_neo4j_database(db)
def teardown_models(): clear_neo4j_database(db)
def handle(self, *args, **options): self.stdout.write('Deleting all nodes..\n') clear_neo4j_database(db, True, True) self.stdout.write('Done.\n')
def movie(): yield Movie(title="Space Cop", released=2016).save() clear_neo4j_database(db)
def app(): app = create_app() clear_neo4j_database(db) app.debug = True app.threaded = True return app
def movies(): for i in range(2): Movie(title=i, released=2016).save() clear_neo4j_database(db)
from __future__ import print_function from neomodel import db, change_neo4j_password, clear_neo4j_database from neo4j.v1 import CypherError # Travis default password dance try: clear_neo4j_database(db) except CypherError as ce: # handle instance without password being changed if 'The credentials you provided were valid, but must be changed before you can use this instance' in str( ce): change_neo4j_password(db, 'test') db.set_connection('bolt://*****:*****@localhost:7687') print("New database with no password set, setting password to 'test'") print( "Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs" ) else: raise ce
from __future__ import print_function import warnings import os from neomodel import config, db, clear_neo4j_database, change_neo4j_password from neo4j.v1 import CypherError warnings.simplefilter('default') config.DATABASE_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://*****:*****@localhost:7687') config.AUTO_INSTALL_LABELS = True try: clear_neo4j_database(db) except CypherError as ce: # handle instance without password being changed if 'The credentials you provided were valid, but must be changed before you can use this instance' in str(ce): change_neo4j_password(db, 'test') db.set_connection('bolt://*****:*****@localhost:7687') print("New database with no password set, setting password to 'test'") print("Please 'export NEO4J_BOLT_URL=bolt://neo4j:test@localhost:7687' for subsequent test runs") else: raise ce
def import_from_csv(some_args=None): clear_neo4j_database(db) db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Air_class.csv' as row create (:Air_class{id: toInteger(row._id), class_type:row.class_type, price: toInteger(row.price), seats: toInteger(row.seats)}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Air_flight.csv' as row create (:Air_flight{id: toInteger(row._id)}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Airport.csv' as row create (:Airport{id: toInteger(row._id), name:row.name}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/City.csv' as row create (:City{id: toInteger(row._id), name:row.name}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Person.csv' as row create (:Person{id: toInteger(row._id), email:row.email, is_admin:toBoolean(row.is_admin), name:row.name, password_hash: row.password_hash, phone_number: row.phone_number}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Station.csv' as row create (:Station{id: toInteger(row._id), name:row.name}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Train_class.csv' as row create (:Train_class{id: toInteger(row._id), class_type:row.class_type, price:toInteger(row.price), seats: toInteger(row.seats)}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/Train_ride.csv' as row create (:Train_ride{id: toInteger(row._id)}) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/CLASS.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:CLASS]->(end) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/FROM.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:FROM{time: datetime(row.time)}]->(end) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/LOCATED.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:LOCATED]->(end) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/REGISTERED_ON.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:REGISTERED_ON{buy_date: date(row.buy_date)}]->(end) ''') db.cypher_query(''' load csv with headers from 'http://server:5000/uploads/TO.csv' as row match (st) where st.id = toInteger(row._start) match (end) where end.id = toInteger(row._end) create (st)-[:TO{time: datetime(row.time)}]->(end) ''') return True
def clear_database(self): """Delete database.""" clear_neo4j_database(self.db)
def setUp(self): clear_neo4j_database(db)
def tearDown(self): clear_neo4j_database(db)
def setUp(self): clear_neo4j_database(db) project = Project(title='parent').save() self.project = project
def drop_db(self): clear_neo4j_database(db)
def clear_graph(): init_connection() n_nodes = db.cypher_query("MATCH (n) RETURN count(n)")[0][0][0] n_relations = db.cypher_query("MATCH ()-[r]->() RETURN count(r)")[0][0][0] clear_neo4j_database(db) print("deleted {} nodes and {} relationships".format(n_nodes, n_relations))