github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/database_test.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/crowdsecurity/go-cs-lib/cstest"
     9  	"github.com/crowdsecurity/go-cs-lib/ptr"
    10  )
    11  
    12  func TestLoadDBConfig(t *testing.T) {
    13  	tests := []struct {
    14  		name        string
    15  		input       *Config
    16  		expected    *DatabaseCfg
    17  		expectedErr string
    18  	}{
    19  		{
    20  			name: "basic valid configuration",
    21  			input: &Config{
    22  				DbConfig: &DatabaseCfg{
    23  					Type:         "sqlite",
    24  					DbPath:       "./testdata/test.db",
    25  					MaxOpenConns: ptr.Of(10),
    26  				},
    27  				Cscli: &CscliCfg{},
    28  				API: &APICfg{
    29  					Server: &LocalApiServerCfg{},
    30  				},
    31  			},
    32  			expected: &DatabaseCfg{
    33  				Type:         "sqlite",
    34  				DbPath:       "./testdata/test.db",
    35  				MaxOpenConns: ptr.Of(10),
    36  				UseWal:       ptr.Of(true),
    37  				DecisionBulkSize: defaultDecisionBulkSize,
    38  			},
    39  		},
    40  		{
    41  			name:        "no configuration path",
    42  			input:       &Config{},
    43  			expected:    nil,
    44  			expectedErr: "no database configuration provided",
    45  		},
    46  	}
    47  
    48  	for _, tc := range tests {
    49  		tc := tc
    50  		t.Run(tc.name, func(t *testing.T) {
    51  			err := tc.input.LoadDBConfig(false)
    52  			cstest.RequireErrorContains(t, err, tc.expectedErr)
    53  			if tc.expectedErr != "" {
    54  				return
    55  			}
    56  
    57  			assert.Equal(t, tc.expected, tc.input.DbConfig)
    58  		})
    59  	}
    60  }