request_validation_test.go

Overview

This file contains unit tests for validating various request types within the artifact package. The primary focus is on testing the correctness of the Validate methods implemented by request structs such as SaveRequest, LoadRequest, DeleteRequest, ListRequest, and VersionsRequest. Each request type implements the Validator interface, which requires a Validate() error method to ensure the request's fields meet predefined constraints before processing.

The tests verify that required fields are present and correctly handled, and that appropriate error messages are returned when validations fail. The file also includes tests for the helper function validateRequiredStrings, which checks for missing required string fields and returns their names.

Interface and Structs

Validator Interface

type Validator interface {
	Validate() error
}

ValidatorTestCase Struct

type ValidatorTestCase struct {
	name       string
	req        Validator
	wantErr    bool
	wantErrMsg string
}

Test Functions

Each test function targets one request type's Validate method, providing multiple scenarios with expected results.

TestSaveRequest_Validate

req := &SaveRequest{
	AppName:   "MyApp",
	UserID:    "user-123",
	SessionID: "sess-abc",
	FileName:  "file.txt",
	Part:      genai.NewPartFromBytes([]byte("data"), "text/plain"),
}
err := req.Validate()

TestLoadRequest_Validate

TestDeleteRequest_Validate

TestListRequest_Validate

TestVersionsRequest_Validate

TestValidateRequiredStrings

fields := []requiredField{
	{Name: "FieldA", Value: "valueA"},
	{Name: "FieldB", Value: ""},
}
missing := validateRequiredStrings(fields)
// missing == []string{"FieldB"}

Helper Function

executeValidatorTestCases

func executeValidatorTestCases(t *testing.T, requestTypeName string, testCases []ValidatorTestCase)

Implementation Details and Algorithms

Interaction with Other System Components

Visual Diagram: Test Structure and Workflow

flowchart TD
ValidatorTestCase -->|holds| Validator
Validator -->|implemented by| SaveRequest
Validator -->|implemented by| LoadRequest
Validator -->|implemented by| DeleteRequest
Validator -->|implemented by| ListRequest
Validator -->|implemented by| VersionsRequest
subgraph TestSuites
TS1[TestSaveRequest_Validate]
TS2[TestLoadRequest_Validate]
TS3[TestDeleteRequest_Validate]
TS4[TestListRequest_Validate]
TS5[TestVersionsRequest_Validate]
end
TS1 --> ValidatorTestCase
TS2 --> ValidatorTestCase
TS3 --> ValidatorTestCase
TS4 --> ValidatorTestCase
TS5 --> ValidatorTestCase
ValidatorTestCase --> executeValidatorTestCases
executeValidatorTestCases -->|calls Validate()| Validator
executeValidatorTestCases -->|checks error| t.Errorf

This diagram summarizes the relationship between the test cases, the Validator interface implementations, and the centralized test executor function.


Note: For detailed validation rules and error message formats, refer to the request type definitions and their Validate() methods in the main artifact package source code. This file focuses exclusively on testing those validations. For related concepts on artifact storage and management, see the Artifact Management topic.