import os

from neo4j import GraphDatabase


def test_cc_users_database_is_populated_from_seed_profiles():
    url = os.getenv('APP_BOLT_URL')
    user = os.getenv('USER_NEO4J')
    password = os.getenv('PASSWORD_NEO4J')
    assert url and user and password
    with GraphDatabase.driver(url, auth=(user, password)) as driver:
        with driver.session(database='cc.users') as session:
            result = session.run('''
                CALL { MATCH (u:User) RETURN count(u) AS users }
                CALL { MATCH (u:User {user_type: 'teacher'}) RETURN count(u) AS teachers }
                CALL { MATCH (u:User {user_type: 'student'}) RETURN count(u) AS students }
                CALL { MATCH (:User)-[r:MEMBER_OF]->(:Institute) RETURN count(r) AS memberships }
                CALL { MATCH (i:Institute) RETURN count(i) AS institutes }
                CALL {
                  MATCH (bad:User)
                  WHERE bad.uuid_string IS NULL OR bad.user_email IS NULL OR bad.cc_username IS NULL
                     OR bad.user_type IS NULL OR bad.user_name IS NULL OR bad.user_db_name IS NULL
                  RETURN count(bad) AS bad_users
                }
                RETURN users, teachers, students, memberships, institutes, bad_users
            ''').single()
    assert dict(result) == {
        'users': 21,
        'teachers': 15,
        'students': 6,
        'memberships': 21,
        'institutes': 2,
        'bad_users': 0,
    }
