def test_create_query(db: Session, superuser: User) -> None: name = random_lower_string() refresh_interval = timedelta(days=1) query_template = test_query_template() query_in = QueryCreate(name=name, template=query_template, refresh_interval=refresh_interval) query = crud.query.create(db=db, obj_in=query_in, created_by_id=superuser.id) assert query assert query.name == name assert query.template == query_template assert query.interface_type == QUERY_INTERFACE_TYPE assert query.refresh_interval == refresh_interval.total_seconds() assert query.created_by_id == superuser.id
def test_delete_query(db: Session, superuser: User) -> None: name = random_lower_string() refresh_interval = timedelta(days=1) query_template = test_query_template() query_in = QueryCreate(name=name, template=query_template, refresh_interval=refresh_interval) query = crud.query.create(db=db, obj_in=query_in, created_by_id=superuser.id) query2 = crud.query.remove(db, id=query.id) query3 = crud.query.get(db=db, id=query.id) assert query3 is None assert query2.id == query.id assert query2.name == name assert query2.created_by_id == superuser.id
def test_create_form_input_interface_fail_not_superuser( client: TestClient, normal_user_token_headers: dict, db: Session) -> None: """If the user attempting to access the endpoint is not a superuser""" data = { "name": random_lower_string(), "template": test_query_template().dict(), "refresh_interval": randint(36000, 576000), } response = client.post( f"{settings.API_V1_STR}/interfaces/queries/", headers=normal_user_token_headers, json=data, ) content = response.json() assert response.status_code == 400 assert content["detail"] == "The user is not a superuser"
def test_update_query(db: Session, superuser: User) -> None: name = random_lower_string() refresh_interval = timedelta(days=1) query_template = test_query_template() query_in = QueryCreate(name=name, template=query_template, refresh_interval=refresh_interval) query = crud.query.create(db=db, obj_in=query_in, created_by_id=superuser.id) name2 = random_lower_string() query_update = QueryUpdate(name=name2) updated_query = crud.query.update(db=db, db_obj=query, obj_in=query_update, updated_by_id=superuser.id) assert query.id == updated_query.id assert query.name == updated_query.name assert query.name == name2 assert query.updated_by_id == superuser.id
def test_create_form_input_interface(client: TestClient, superuser_token_headers: dict, db: Session) -> None: """Successful query creation""" data = { "name": random_lower_string(), "template": test_query_template().dict(), "refresh_interval": randint(36000, 576000), } response = client.post( f"{settings.API_V1_STR}/interfaces/queries/", headers=superuser_token_headers, json=data, ) content = response.json() assert response.status_code == 200 assert content["name"] == data["name"] assert content["interface_type"] == QUERY_INTERFACE_TYPE assert content["template"] == data["template"] assert content["refresh_interval"] == data["refresh_interval"]
def test_get_multi_query(db: Session, superuser: User) -> None: names = [random_lower_string() for i in range(10)] refresh_interval = timedelta(hours=1) query_template = test_query_template() new_queries_in = [ QueryCreate(name=n, template=query_template, refresh_interval=refresh_interval) for n in names ] [ crud.query.create(db=db, obj_in=query_in, created_by_id=superuser.id) for query_in in new_queries_in ] stored_queries = crud.query.get_multi(db=db) for nqi in new_queries_in: found_match = False for sq in stored_queries.records: name_match = nqi.name == sq.name table_template_match = nqi.template == sq.template if name_match and table_template_match: found_match = True break assert found_match