github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/validationfile/blocks/relationships_test.go (about)

     1  package blocks
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	yamlv3 "gopkg.in/yaml.v3"
     8  )
     9  
    10  func TestParseRelationships(t *testing.T) {
    11  	tests := []struct {
    12  		name             string
    13  		contents         string
    14  		expectedError    string
    15  		expectedRelCount int
    16  	}{
    17  		{
    18  			name:             "empty",
    19  			contents:         "",
    20  			expectedError:    "",
    21  			expectedRelCount: 0,
    22  		},
    23  		{
    24  			name:             "invalid yaml",
    25  			contents:         `asdkjsad:`,
    26  			expectedError:    "cannot unmarshal !!map into string",
    27  			expectedRelCount: 0,
    28  		},
    29  		{
    30  			name:             "invalid relationship",
    31  			contents:         `document:firstviewer@user:1`,
    32  			expectedError:    "error parsing relationship `document:firstviewer@user:1`",
    33  			expectedRelCount: 0,
    34  		},
    35  		{
    36  			name: "valid",
    37  			contents: `document:first#viewer@user:1
    38  
    39  // this is a comment
    40  
    41  document:second#viewer@user:1`,
    42  			expectedError:    "",
    43  			expectedRelCount: 2,
    44  		},
    45  	}
    46  
    47  	for _, tt := range tests {
    48  		tt := tt
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			pr := ParsedRelationships{}
    51  			err := yamlv3.Unmarshal([]byte(tt.contents), &pr)
    52  			if tt.expectedError != "" {
    53  				require.NotNil(t, err)
    54  				require.Contains(t, err.Error(), tt.expectedError)
    55  			} else {
    56  				require.Nil(t, err)
    57  				require.Equal(t, tt.expectedRelCount, len(pr.Relationships))
    58  			}
    59  		})
    60  	}
    61  }