Merged PR 274: Add INSERT query functionality into database service.

## Summary
Add support for SQL INSERT query in database_service.py.

## Test process
Updated test_database_on_node.py to test for database INSERTions.

## Checklist
- [X] PR is linked to a **work item**
- [X] **acceptance criteria** of linked ticket are met
- [X] performed **self-review** of the code
- [X] written **tests** for any new functionality added with this PR
- [X] updated the **documentation** if this PR changes or adds functionality
- [ ] written/updated **design docs** if this PR implements new functionality
- [X] updated the **change log**
- [X] ran **pre-commit** checks for code style
- [ ] attended to any **TO-DOs** left in the code

Related work items: #2306
This commit is contained in:
Nick Todd
2024-02-14 16:51:25 +00:00
4 changed files with 26 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where the red agent acted to early
- Fixed the order of service health state
- Fixed an issue where starting a node didn't start the services on it
- Added support for SQL INSERT command.

View File

@@ -17,7 +17,7 @@ Key capabilities
- Creates a database file in the ``Node`` 's ``FileSystem`` upon creation.
- Handles connecting clients by maintaining a dictionary of connections mapped to session IDs.
- Authenticates connections using a configurable password.
- Simulates ``SELECT`` and ``DELETE`` SQL queries.
- Simulates ``SELECT``, ``DELETE`` and ``INSERT`` SQL queries.
- Returns query results and status codes back to clients.
- Leverages the Service base class for install/uninstall, status tracking, etc.

View File

@@ -189,7 +189,7 @@ class DatabaseService(Service):
}
def _process_sql(
self, query: Literal["SELECT", "DELETE"], query_id: str, connection_id: Optional[str] = None
self, query: Literal["SELECT", "DELETE", "INSERT"], query_id: str, connection_id: Optional[str] = None
) -> Dict[str, Union[int, List[Any]]]:
"""
Executes the given SQL query and returns the result.
@@ -197,6 +197,7 @@ class DatabaseService(Service):
Possible queries:
- SELECT : returns the data
- DELETE : deletes the data
- INSERT : inserts the data
:param query: The SQL query to be executed.
:return: Dictionary containing status code and data fetched.
@@ -220,9 +221,27 @@ class DatabaseService(Service):
return {"status_code": 404, "data": False}
elif query == "DELETE":
self.db_file.health_status = FileSystemItemHealthStatus.COMPROMISED
return {"status_code": 200, "type": "sql", "data": False, "uuid": query_id, "connection_id": connection_id}
return {
"status_code": 200,
"type": "sql",
"data": False,
"uuid": query_id,
"connection_id": connection_id,
}
elif query == "INSERT":
if self.health_state_actual == SoftwareHealthState.GOOD:
return {
"status_code": 200,
"type": "sql",
"data": False,
"uuid": query_id,
"connection_id": connection_id,
}
else:
return {"status_code": 404, "data": False}
else:
# Invalid query
self.sys_log.info(f"{self.name}: Invalid {query}")
return {"status_code": 500, "data": False}
def describe_state(self) -> Dict:

View File

@@ -101,6 +101,7 @@ def test_database_client_query(uc2_network):
db_client.connect()
assert db_client.query("SELECT")
assert db_client.query("INSERT")
def test_create_database_backup(uc2_network):
@@ -150,7 +151,7 @@ def test_database_client_cannot_query_offline_database_server(uc2_network):
assert len(db_client.connections)
assert db_client.query("SELECT") is True
assert db_client.query("INSERT") is True
db_server.power_off()
for i in range(db_server.shut_down_duration + 1):
@@ -160,3 +161,4 @@ def test_database_client_cannot_query_offline_database_server(uc2_network):
assert db_service.operating_state is ServiceOperatingState.STOPPED
assert db_client.query("SELECT") is False
assert db_client.query("INSERT") is False