Skip to content

test(mock): Add comprehensive test suite for mock generator #33

@SebastienMelki

Description

@SebastienMelki

Description

Create a comprehensive test suite for the mock generator to ensure all field types, edge cases, and complex scenarios are properly tested.

Test Categories

1. Field Type Coverage Tests

Create proto files testing every protobuf field type:

// test_all_types.proto
message AllFieldTypes {
  // Scalar types
  string string_field = 1;
  int32 int32_field = 2;
  int64 int64_field = 3;
  uint32 uint32_field = 4;
  uint64 uint64_field = 5;
  sint32 sint32_field = 6;
  sint64 sint64_field = 7;
  fixed32 fixed32_field = 8;
  fixed64 fixed64_field = 9;
  sfixed32 sfixed32_field = 10;
  sfixed64 sfixed64_field = 11;
  bool bool_field = 12;
  float float_field = 13;
  double double_field = 14;
  bytes bytes_field = 15;
  
  // Complex types
  NestedMessage nested_message = 16;
  TestEnum enum_field = 17;
  map<string, string> map_field = 18;
  
  // Repeated fields
  repeated string repeated_strings = 19;
  repeated int32 repeated_ints = 20;
  repeated NestedMessage repeated_messages = 21;
  repeated TestEnum repeated_enums = 22;
}

2. Edge Case Tests

Circular References

message CircularA {
  string name = 1;
  CircularB b = 2;
}

message CircularB {
  string name = 1;
  CircularA a = 2;
}

message SelfReferential {
  string name = 1;
  SelfReferential parent = 2;
  repeated SelfReferential children = 3;
}

Deep Nesting

message Level1 {
  string data = 1;
  Level2 level2 = 2;
}

message Level2 {
  string data = 1;
  Level3 level3 = 2;
}

// Continue to Level10...

Empty Messages

message EmptyMessage {}

message ContainsEmpty {
  EmptyMessage empty = 1;
  repeated EmptyMessage empty_list = 2;
}

3. Validation Integration Tests

Test that generated mocks work with validation:

message ValidatedMessage {
  string email = 1 [(buf.validate.field).string.email = true];
  int32 age = 2 [(buf.validate.field).int32 = {gte: 0, lte: 150}];
  string uuid = 3 [(buf.validate.field).string.uuid = true];
}

4. Service Integration Tests

Test complete service mock generation:

service TestService {
  rpc SimpleMethod(SimpleRequest) returns (SimpleResponse);
  rpc ComplexMethod(ComplexRequest) returns (ComplexResponse);
  rpc ListMethod(ListRequest) returns (ListResponse);
}

5. Field Examples Tests

Test that field examples are properly extracted and used:

message ExampleMessage {
  string email = 1; // @example: user@example.com
  string phone = 2; // @example: +1-555-0123
  int32 age = 3;    // @example: 25
}

Test Implementation

Golden File Tests

  1. Generate mock code for all test proto files
  2. Store as golden files
  3. Verify generated code matches golden files
  4. Ensure generated code compiles

Unit Tests

func TestMockGeneration(t *testing.T) {
    tests := []struct {
        name      string
        protoFile string
        validate  func(t *testing.T, generated string)
    }{
        {
            name:      "all_field_types",
            protoFile: "test_all_types.proto",
            validate:  validateAllFieldTypes,
        },
        {
            name:      "circular_references",
            protoFile: "test_circular.proto",
            validate:  validateNoInfiniteLoop,
        },
        // ... more tests
    }
    
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            generated := generateMock(tt.protoFile)
            tt.validate(t, generated)
        })
    }
}

Integration Tests

func TestMockServer(t *testing.T) {
    // Generate mock server
    mockServer := NewMockTestServiceServer()
    
    // Create HTTP handler
    handler := RegisterTestServiceServer(mockServer)
    
    // Test server responds correctly
    req := httptest.NewRequest("POST", "/api/v1/simple", bytes.NewReader(requestJSON))
    rec := httptest.NewRecorder()
    handler.ServeHTTP(rec, req)
    
    assert.Equal(t, http.StatusOK, rec.Code)
    // Verify response structure
}

Test Coverage Goals

  • 100% of protobuf field types tested
  • All edge cases have dedicated tests
  • Generated code compiles in all cases
  • Mock servers pass integration tests
  • Validation works with mock data
  • Performance tests for large messages

Files to Create/Update

  • internal/httpgen/mock_generator_test.go - Main test file
  • internal/httpgen/testdata/proto/test_all_types.proto - Comprehensive type tests
  • internal/httpgen/testdata/proto/test_circular.proto - Circular reference tests
  • internal/httpgen/testdata/proto/test_deep_nesting.proto - Depth limit tests
  • internal/httpgen/testdata/proto/test_validation.proto - Validation integration
  • internal/httpgen/testdata/golden/ - Golden files for all test protos

Success Criteria

  • All test cases pass
  • No panics or infinite loops
  • Generated code compiles without errors
  • Mock data passes validation rules
  • Test coverage > 90%
  • CI/CD pipeline runs all tests

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions