github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/validationfile/blocks/schema_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 TestParseSchema(t *testing.T) {
    11  	tests := []struct {
    12  		name             string
    13  		contents         string
    14  		expectedError    string
    15  		expectedDefCount int
    16  	}{
    17  		{
    18  			name:             "empty",
    19  			contents:         "",
    20  			expectedError:    "",
    21  			expectedDefCount: 0,
    22  		},
    23  		{
    24  			name:             "invalid schema",
    25  			contents:         "asdasd",
    26  			expectedError:    "error when parsing schema: Unexpected token at root level: TokenTypeIdentifier",
    27  			expectedDefCount: 0,
    28  		},
    29  		{
    30  			name:             "valid schema",
    31  			contents:         "definition foo/user {}",
    32  			expectedError:    "",
    33  			expectedDefCount: 1,
    34  		},
    35  		{
    36  			name:             "valid schema no prefix",
    37  			contents:         "definition user {}",
    38  			expectedError:    "",
    39  			expectedDefCount: 1,
    40  		},
    41  	}
    42  
    43  	for _, tt := range tests {
    44  		tt := tt
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			ps := ParsedSchema{}
    47  			err := yamlv3.Unmarshal([]byte(tt.contents), &ps)
    48  			if tt.expectedError != "" {
    49  				require.NotNil(t, err)
    50  				require.Contains(t, err.Error(), tt.expectedError)
    51  			} else {
    52  				require.Nil(t, err)
    53  				if tt.expectedDefCount > 0 {
    54  					require.NotNil(t, ps.CompiledSchema)
    55  					require.Equal(t, tt.expectedDefCount, len(ps.CompiledSchema.OrderedDefinitions))
    56  					require.Equal(t, tt.contents, ps.Schema)
    57  				}
    58  			}
    59  		})
    60  	}
    61  }