github.com/popeskul/qna-go@v0.0.0-20230109215716-6e2a125005c8/internal/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path"
     7  	"runtime"
     8  	"testing"
     9  )
    10  
    11  func init() {
    12  	_, filename, _, _ := runtime.Caller(0)
    13  	dir := path.Join(path.Dir(filename), "./../../")
    14  	err := os.Chdir(dir)
    15  	if err != nil {
    16  		log.Fatal(err)
    17  	}
    18  }
    19  
    20  func TestNew(t *testing.T) {
    21  	type args struct {
    22  		folder   string
    23  		filename string
    24  	}
    25  	tests := []struct {
    26  		name  string
    27  		args  args
    28  		want  *Config
    29  		error bool
    30  	}{
    31  		{
    32  			name: "ok",
    33  			args: args{
    34  				folder:   "configs",
    35  				filename: "config",
    36  			},
    37  			want: &Config{
    38  				DB: Postgres{
    39  					Host:     "localhost",
    40  					Port:     5432,
    41  					User:     "postgres",
    42  					Password: "******",
    43  					DBName:   "postgres",
    44  					SSLMode:  "disable",
    45  				},
    46  				Server: struct {
    47  					Port int `mapstructure:"port"`
    48  				}{
    49  					Port: 8080,
    50  				},
    51  			},
    52  			error: false,
    53  		},
    54  		{
    55  			name: "bad",
    56  			args: args{
    57  				folder:   ".",
    58  				filename: "config1",
    59  			},
    60  			want:  nil,
    61  			error: true,
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			got, err := New(tt.args.folder, tt.args.filename)
    68  			if (err != nil) != tt.error {
    69  				t.Errorf("New() error = %v, wantErr %v", err, tt.error)
    70  			}
    71  
    72  			if got != nil {
    73  				got.DB.Password = "******"
    74  			}
    75  
    76  			if got != nil && tt.want != nil {
    77  				if got.DB.Host != tt.want.DB.Host {
    78  					t.Errorf("New() got = %v, want %v", got.DB.Host, tt.want.DB.Host)
    79  				}
    80  				if got.DB.Port != tt.want.DB.Port {
    81  					t.Errorf("New() got = %v, want %v", got.DB.Port, tt.want.DB.Port)
    82  				}
    83  				if got.DB.User != tt.want.DB.User {
    84  					t.Errorf("New() got = %v, want %v", got.DB.User, tt.want.DB.User)
    85  				}
    86  				if got.DB.Password != tt.want.DB.Password {
    87  					t.Errorf("New() got = %v, want %v", got.DB.Password, tt.want.DB.Password)
    88  				}
    89  				if got.DB.DBName != tt.want.DB.DBName {
    90  					t.Errorf("New() got = %v, want %v", got.DB.DBName, tt.want.DB.DBName)
    91  				}
    92  				if got.DB.SSLMode != tt.want.DB.SSLMode {
    93  					t.Errorf("New() got = %v, want %v", got.DB.SSLMode, tt.want.DB.SSLMode)
    94  				}
    95  				if got.Server.Port != tt.want.Server.Port {
    96  					t.Errorf("New() got = %v, want %v", got.Server.Port, tt.want.Server.Port)
    97  				}
    98  			}
    99  
   100  		})
   101  	}
   102  }