test_utils.go
Overview - test_utils.go
This file provides utility functions and structures for integration testing the SigNoz query service. It includes functions for creating mock ClickHouse connections, generating authenticated requests, handling test requests, and creating test users.
Detailed Documentation
NewMockClickhouseReader
- Purpose: Creates a new mock ClickHouse reader for testing purposes.
- Parameters:
t
(*testing.T): Testing object.testDB
(sqlstore.SQLStore): SQL store for testing.
- Returns:
*clickhouseReader.ClickHouseReader
: A pointer to the new ClickHouse reader.mockhouse.ClickConnMockCommon
: The mock ClickHouse connection.
addLogsQueryExpectation
- Purpose: Adds an expectation to the mock ClickHouse connection for a logs query.
- Parameters:
mockClickhouse
(mockhouse.ClickConnMockCommon): The mock ClickHouse connection.logsToReturn
([]model.SignozLog): The logs to return when the query is executed.
- Returns: None
makeTestSignozLog
- Purpose: Creates a test SignozLog object.
- Parameters:
body
(string): The log body.attributes
(map[string]interface{}): The log attributes.
- Returns:
model.SignozLog
: The new SignozLog object.
createTestUser
- Purpose: Creates a test user for authentication purposes.
- Parameters: None
- Returns:
*types.User
: A pointer to the new user object.*model.ApiError
: API error if any.
AuthenticatedRequestForTest
- Purpose: Creates an authenticated HTTP request for testing.
- Parameters:
user
(*types.User): The user to authenticate.path
(string): The request path.postData
(interface{}): The data to post (if any).
- Returns:
*http.Request
: A pointer to the authenticated HTTP request.error
: An error if something went wrong.
HandleTestRequest
- Purpose: Handles a test HTTP request and returns the response.
- Parameters:
handler
(http.Handler): The HTTP handler to use.req
(*http.Request): The HTTP request to handle.expectedStatus
(int): The expected HTTP status code.
- Returns:
*app.ApiResponse
: A pointer to the API response.error
: An error if something went wrong.
Code Examples
// Example of creating a mock ClickHouse reader
const reader, mockClickhouse = NewMockClickhouseReader(t, testDB);
// Example of creating an authenticated request
const req, err = AuthenticatedRequestForTest(user, "/api/v1/logs", { query: "test" });
Include in Getting Started: NO