github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/event/writer_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package event
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/go-git/go-billy/v5/memfs"
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func Test_write(t *testing.T) {
    28  
    29  	type named struct {
    30  		Field string
    31  	}
    32  
    33  	example := "test"
    34  
    35  	for _, test := range []struct {
    36  		Name  string
    37  		Event any
    38  	}{
    39  		{
    40  			Name:  "string",
    41  			Event: "hello",
    42  		},
    43  		{
    44  			Name: "struct",
    45  			Event: struct {
    46  				Field1 string `fs:"field1"`
    47  				Field2 string
    48  			}{
    49  				Field1: "f1",
    50  				Field2: "f2",
    51  			},
    52  		},
    53  		{
    54  			Name: "map",
    55  			Event: map[string]string{
    56  				"file1": "content1",
    57  				"file2": "content2",
    58  			},
    59  		},
    60  		{
    61  			Name: "named",
    62  			Event: named{
    63  				Field: "test",
    64  			},
    65  		},
    66  		{
    67  			Name: "map-empty",
    68  			Event: map[string]struct{}{
    69  				"x": {},
    70  				"y": {},
    71  			},
    72  		},
    73  		{
    74  			Name:  "pointer",
    75  			Event: &example,
    76  		},
    77  		{
    78  			Name:  "nil-pointer",
    79  			Event: (*string)(nil),
    80  		},
    81  	} {
    82  		test := test
    83  		t.Run(test.Name, func(t *testing.T) {
    84  			t.Parallel()
    85  			fs := memfs.New()
    86  			err := write(fs, "test", test.Event)
    87  			if err != nil {
    88  				t.Fatal("writing event:", err)
    89  			}
    90  			result := reflect.New(reflect.TypeOf(test.Event))
    91  			err = read(fs, "test", result.Interface())
    92  			if err != nil {
    93  				t.Fatal("reading event:", err)
    94  			}
    95  			if diff := cmp.Diff(test.Event, result.Elem().Interface()); diff != "" {
    96  				t.Error("wrong result:\n", diff)
    97  			}
    98  		})
    99  	}
   100  }