github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/boilingcore/config_test.go (about)

     1  package boilingcore
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/volatiletech/sqlboiler/v4/drivers"
     8  )
     9  
    10  func TestConfig_OutputDirDepth(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := []struct {
    14  		Output string
    15  		Depth  int
    16  	}{
    17  		{
    18  			Output: ".",
    19  			Depth:  0,
    20  		},
    21  		{
    22  			Output: "./",
    23  			Depth:  0,
    24  		},
    25  		{
    26  			Output: "foo",
    27  			Depth:  1,
    28  		},
    29  		{
    30  			Output: "foo/bar",
    31  			Depth:  2,
    32  		},
    33  	}
    34  
    35  	for i, test := range tests {
    36  		cfg := Config{
    37  			OutFolder: test.Output,
    38  		}
    39  		if want, got := test.Depth, cfg.OutputDirDepth(); got != want {
    40  			t.Errorf("%d) wrong depth, want: %d, got: %d", i, want, got)
    41  		}
    42  	}
    43  }
    44  
    45  func TestConvertAliases(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	var intf interface{} = map[string]interface{}{
    49  		"tables": map[string]interface{}{
    50  			"table_name": map[string]interface{}{
    51  				"up_plural":     "a",
    52  				"up_singular":   "b",
    53  				"down_plural":   "c",
    54  				"down_singular": "d",
    55  
    56  				"columns": map[string]interface{}{
    57  					"a": "b",
    58  				},
    59  				"relationships": map[string]interface{}{
    60  					"ib_fk_1": map[string]interface{}{
    61  						"local":   "a",
    62  						"foreign": "b",
    63  					},
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	aliases := ConvertAliases(intf)
    70  
    71  	if len(aliases.Tables) != 1 {
    72  		t.Fatalf("should have one table alias: %#v", aliases.Tables)
    73  	}
    74  
    75  	table := aliases.Tables["table_name"]
    76  	if table.UpPlural != "a" {
    77  		t.Error("value was wrong:", table.UpPlural)
    78  	}
    79  	if table.UpSingular != "b" {
    80  		t.Error("value was wrong:", table.UpSingular)
    81  	}
    82  	if table.DownPlural != "c" {
    83  		t.Error("value was wrong:", table.DownPlural)
    84  	}
    85  	if table.DownSingular != "d" {
    86  		t.Error("value was wrong:", table.DownSingular)
    87  	}
    88  
    89  	if len(table.Columns) != 1 {
    90  		t.Error("should have one column")
    91  	}
    92  
    93  	if table.Columns["a"] != "b" {
    94  		t.Error("column alias was wrong")
    95  	}
    96  
    97  	if len(aliases.Tables) != 1 {
    98  		t.Fatal("should have one relationship alias")
    99  	}
   100  
   101  	rel := table.Relationships["ib_fk_1"]
   102  	if rel.Local != "a" {
   103  		t.Error("value was wrong:", rel.Local)
   104  	}
   105  	if rel.Foreign != "b" {
   106  		t.Error("value was wrong:", rel.Foreign)
   107  	}
   108  }
   109  
   110  func TestConvertAliasesAltSyntax(t *testing.T) {
   111  	t.Parallel()
   112  
   113  	var intf interface{} = map[string]interface{}{
   114  		"tables": []interface{}{
   115  			map[string]interface{}{
   116  				"name":          "table_name",
   117  				"up_plural":     "a",
   118  				"up_singular":   "b",
   119  				"down_plural":   "c",
   120  				"down_singular": "d",
   121  
   122  				"columns": []interface{}{
   123  					map[string]interface{}{
   124  						"name":  "a",
   125  						"alias": "b",
   126  					},
   127  				},
   128  				"relationships": []interface{}{
   129  					map[string]interface{}{
   130  						"name":    "ib_fk_1",
   131  						"local":   "a",
   132  						"foreign": "b",
   133  					},
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	aliases := ConvertAliases(intf)
   140  
   141  	if len(aliases.Tables) != 1 {
   142  		t.Fatalf("should have one table alias: %#v", aliases.Tables)
   143  	}
   144  
   145  	table := aliases.Tables["table_name"]
   146  	if table.UpPlural != "a" {
   147  		t.Error("value was wrong:", table.UpPlural)
   148  	}
   149  	if table.UpSingular != "b" {
   150  		t.Error("value was wrong:", table.UpSingular)
   151  	}
   152  	if table.DownPlural != "c" {
   153  		t.Error("value was wrong:", table.DownPlural)
   154  	}
   155  	if table.DownSingular != "d" {
   156  		t.Error("value was wrong:", table.DownSingular)
   157  	}
   158  
   159  	if len(table.Columns) != 1 {
   160  		t.Error("should have one column")
   161  	}
   162  
   163  	if table.Columns["a"] != "b" {
   164  		t.Error("column alias was wrong")
   165  	}
   166  
   167  	if len(aliases.Tables) != 1 {
   168  		t.Fatal("should have one relationship alias")
   169  	}
   170  
   171  	rel := table.Relationships["ib_fk_1"]
   172  	if rel.Local != "a" {
   173  		t.Error("value was wrong:", rel.Local)
   174  	}
   175  	if rel.Foreign != "b" {
   176  		t.Error("value was wrong:", rel.Foreign)
   177  	}
   178  }
   179  
   180  func TestConvertTypeReplace(t *testing.T) {
   181  	t.Parallel()
   182  
   183  	fullColumn := map[string]interface{}{
   184  		"name":           "a",
   185  		"type":           "b",
   186  		"db_type":        "c",
   187  		"udt_name":       "d",
   188  		"full_db_type":   "e",
   189  		"arr_type":       "f",
   190  		"tables":         []string{"g", "h"},
   191  		"auto_generated": true,
   192  		"nullable":       true,
   193  	}
   194  
   195  	var intf interface{} = []interface{}{
   196  		map[string]interface{}{
   197  			"match":   fullColumn,
   198  			"replace": fullColumn,
   199  			"imports": map[string]interface{}{
   200  				"standard": []interface{}{
   201  					"abc",
   202  				},
   203  				"third_party": []interface{}{
   204  					"github.com/abc",
   205  				},
   206  			},
   207  		},
   208  	}
   209  
   210  	typeReplace := ConvertTypeReplace(intf)
   211  	if len(typeReplace) != 1 {
   212  		t.Error("should have one entry")
   213  	}
   214  
   215  	checkColumn := func(t *testing.T, c drivers.Column) {
   216  		t.Helper()
   217  		if c.Name != "a" {
   218  			t.Error("value was wrong:", c.Name)
   219  		}
   220  		if c.Type != "b" {
   221  			t.Error("value was wrong:", c.Type)
   222  		}
   223  		if c.DBType != "c" {
   224  			t.Error("value was wrong:", c.DBType)
   225  		}
   226  		if c.UDTName != "d" {
   227  			t.Error("value was wrong:", c.UDTName)
   228  		}
   229  		if c.FullDBType != "e" {
   230  			t.Error("value was wrong:", c.FullDBType)
   231  		}
   232  		if *c.ArrType != "f" {
   233  			t.Error("value was wrong:", c.ArrType)
   234  		}
   235  		if c.AutoGenerated != true {
   236  			t.Error("value was wrong:", c.AutoGenerated)
   237  		}
   238  		if c.Nullable != true {
   239  			t.Error("value was wrong:", c.Nullable)
   240  		}
   241  	}
   242  
   243  	r := typeReplace[0]
   244  	checkColumn(t, r.Match)
   245  	checkColumn(t, r.Replace)
   246  
   247  	if got := r.Imports.Standard[0]; got != "abc" {
   248  		t.Error("standard import wrong:", got)
   249  	}
   250  	if got := r.Imports.ThirdParty[0]; got != "github.com/abc" {
   251  		t.Error("standard import wrong:", got)
   252  	}
   253  	if got := r.Tables; !reflect.DeepEqual(r.Tables, []string{"g", "h"}) {
   254  		t.Error("tables in types.match wrong:", got)
   255  	}
   256  }