github.com/zchee/zap-cloudlogging@v0.0.0-20220819025602-19b026d3900e/operation_test.go (about)

     1  // Copyright 2022 The zap-cloudlogging Authors
     2  // SPDX-License-Identifier: BSD-3-Clause
     3  
     4  package zapcloudlogging
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"go.uber.org/zap"
    11  	logpb "google.golang.org/genproto/googleapis/logging/v2"
    12  	"google.golang.org/protobuf/testing/protocmp"
    13  )
    14  
    15  func TestOperation(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	op := &operation{
    19  		LogEntryOperation: &logpb.LogEntryOperation{
    20  			Id:       "id",
    21  			Producer: "producer",
    22  			First:    true,
    23  			Last:     false,
    24  		},
    25  	}
    26  	field := Operation("id", "producer", true, false)
    27  
    28  	if diff := cmp.Diff(field, zap.Object(OperationKey, op),
    29  		protocmp.Transform(),
    30  	); diff != "" {
    31  		t.Fatalf("(-want, +got)\n%s\n", diff)
    32  	}
    33  }
    34  
    35  func TestOperationStart(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	op := &operation{
    39  		LogEntryOperation: &logpb.LogEntryOperation{
    40  			Id:       "id",
    41  			Producer: "producer",
    42  			First:    true,
    43  			Last:     false,
    44  		},
    45  	}
    46  	field := OperationStart("id", "producer")
    47  
    48  	if diff := cmp.Diff(field, zap.Object(OperationKey, op),
    49  		protocmp.Transform(),
    50  	); diff != "" {
    51  		t.Fatalf("(-want, +got)\n%s\n", diff)
    52  	}
    53  }
    54  
    55  func TestOperationCont(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	op := &operation{
    59  		LogEntryOperation: &logpb.LogEntryOperation{
    60  			Id:       "id",
    61  			Producer: "producer",
    62  			First:    false,
    63  			Last:     false,
    64  		},
    65  	}
    66  	field := OperationCont("id", "producer")
    67  
    68  	if diff := cmp.Diff(field, zap.Object(OperationKey, op),
    69  		protocmp.Transform(),
    70  	); diff != "" {
    71  		t.Fatalf("(-want, +got)\n%s\n", diff)
    72  	}
    73  }
    74  
    75  func TestOperationEnd(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	op := &operation{
    79  		LogEntryOperation: &logpb.LogEntryOperation{
    80  			Id:       "id",
    81  			Producer: "producer",
    82  			First:    false,
    83  			Last:     true,
    84  		},
    85  	}
    86  	field := OperationEnd("id", "producer")
    87  
    88  	if diff := cmp.Diff(field, zap.Object(OperationKey, op),
    89  		protocmp.Transform(),
    90  	); diff != "" {
    91  		t.Fatalf("(-want, +got)\n%s\n", diff)
    92  	}
    93  }