github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/boilingcore/aliases_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 TestAliasesTables(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tables := []drivers.Table{
    14  		{
    15  			Name: "videos",
    16  			Columns: []drivers.Column{
    17  				{Name: "id"},
    18  				{Name: "name"},
    19  				{Name: "1number"},
    20  			},
    21  		},
    22  	}
    23  
    24  	t.Run("Automatic", func(t *testing.T) {
    25  		expect := TableAlias{
    26  			UpPlural:     "Videos",
    27  			UpSingular:   "Video",
    28  			DownPlural:   "videos",
    29  			DownSingular: "video",
    30  			Columns: map[string]string{
    31  				"id":      "ID",
    32  				"name":    "Name",
    33  				"1number": "C1number",
    34  			},
    35  			Relationships: make(map[string]RelationshipAlias),
    36  		}
    37  
    38  		a := Aliases{}
    39  		FillAliases(&a, tables)
    40  
    41  		if got := a.Tables["videos"]; !reflect.DeepEqual(expect, got) {
    42  			t.Errorf("it should fill in the blanks: %#v", got)
    43  		}
    44  	})
    45  
    46  	t.Run("UserOverride", func(t *testing.T) {
    47  		expect := TableAlias{
    48  			UpPlural:     "NotVideos",
    49  			UpSingular:   "NotVideo",
    50  			DownPlural:   "notVideos",
    51  			DownSingular: "notVideo",
    52  			Columns: map[string]string{
    53  				"id":   "NotID",
    54  				"name": "NotName",
    55  			},
    56  			Relationships: make(map[string]RelationshipAlias),
    57  		}
    58  
    59  		a := Aliases{}
    60  		a.Tables = map[string]TableAlias{"videos": expect}
    61  		FillAliases(&a, tables)
    62  
    63  		if !reflect.DeepEqual(expect, a.Tables["videos"]) {
    64  			t.Error("it should not alter things that were specified by user")
    65  		}
    66  	})
    67  }
    68  
    69  func TestAliasesRelationships(t *testing.T) {
    70  	t.Parallel()
    71  
    72  	tables := []drivers.Table{
    73  		{
    74  			Name: "videos",
    75  			Columns: []drivers.Column{
    76  				{Name: "id"},
    77  				{Name: "name"},
    78  			},
    79  			FKeys: []drivers.ForeignKey{
    80  				{
    81  					Name:          "fkey_1",
    82  					Table:         "videos",
    83  					Column:        "user_id",
    84  					ForeignTable:  "users",
    85  					ForeignColumn: "id",
    86  				},
    87  				{
    88  					Name:          "fkey_2",
    89  					Table:         "videos",
    90  					Column:        "publisher_id",
    91  					ForeignTable:  "users",
    92  					ForeignColumn: "id",
    93  				},
    94  				{
    95  					Name:          "fkey_3",
    96  					Table:         "videos",
    97  					Column:        "one_id",
    98  					Unique:        true,
    99  					ForeignTable:  "ones",
   100  					ForeignColumn: "one",
   101  				},
   102  			},
   103  		},
   104  	}
   105  
   106  	t.Run("Automatic", func(t *testing.T) {
   107  		expect1 := RelationshipAlias{
   108  			Local:   "Videos",
   109  			Foreign: "User",
   110  		}
   111  		expect2 := RelationshipAlias{
   112  			Local:   "PublisherVideos",
   113  			Foreign: "Publisher",
   114  		}
   115  		expect3 := RelationshipAlias{
   116  			Local:   "Video",
   117  			Foreign: "One",
   118  		}
   119  
   120  		a := Aliases{}
   121  		FillAliases(&a, tables)
   122  
   123  		table := a.Tables["videos"]
   124  		if got := table.Relationships["fkey_1"]; !reflect.DeepEqual(expect1, got) {
   125  			t.Errorf("bad values: %#v", got)
   126  		}
   127  		if got := table.Relationships["fkey_2"]; !reflect.DeepEqual(expect2, got) {
   128  			t.Errorf("bad values: %#v", got)
   129  		}
   130  		if got := table.Relationships["fkey_3"]; !reflect.DeepEqual(expect3, got) {
   131  			t.Errorf("bad values: %#v", got)
   132  		}
   133  	})
   134  
   135  	t.Run("UserOverride", func(t *testing.T) {
   136  		expect1 := RelationshipAlias{
   137  			Local:   "Videos",
   138  			Foreign: "TheUser",
   139  		}
   140  		expect2 := RelationshipAlias{
   141  			Local:   "PublishedVideos",
   142  			Foreign: "Publisher",
   143  		}
   144  		expect3 := RelationshipAlias{
   145  			Local:   "AwesomeOneVideo",
   146  			Foreign: "TheOne",
   147  		}
   148  
   149  		a := Aliases{
   150  			Tables: map[string]TableAlias{
   151  				"videos": {
   152  					Relationships: map[string]RelationshipAlias{
   153  						"fkey_1": {Foreign: "TheUser"},
   154  						"fkey_2": {Local: "PublishedVideos"},
   155  						"fkey_3": {Local: "AwesomeOneVideo", Foreign: "TheOne"},
   156  					},
   157  				},
   158  			},
   159  		}
   160  		FillAliases(&a, tables)
   161  
   162  		table := a.Tables["videos"]
   163  		if got := table.Relationships["fkey_1"]; !reflect.DeepEqual(expect1, got) {
   164  			t.Errorf("bad values: %#v", got)
   165  		}
   166  		if got := table.Relationships["fkey_2"]; !reflect.DeepEqual(expect2, got) {
   167  			t.Errorf("bad values: %#v", got)
   168  		}
   169  		if got := table.Relationships["fkey_3"]; !reflect.DeepEqual(expect3, got) {
   170  			t.Errorf("bad values: %#v", got)
   171  		}
   172  	})
   173  }
   174  
   175  func TestAliasesRelationshipsJoinTable(t *testing.T) {
   176  	t.Parallel()
   177  
   178  	tables := []drivers.Table{
   179  		{
   180  			Name: "videos",
   181  		},
   182  		{
   183  			Name: "tags",
   184  		},
   185  		{
   186  			Name:        "video_tags",
   187  			IsJoinTable: true,
   188  			FKeys: []drivers.ForeignKey{
   189  				{
   190  					Name:          "fk_video_id",
   191  					Table:         "video_tags",
   192  					Column:        "video_id",
   193  					ForeignTable:  "videos",
   194  					ForeignColumn: "id",
   195  				},
   196  				{
   197  					Name:          "fk_tag_id",
   198  					Table:         "video_tags",
   199  					Column:        "tags_id",
   200  					ForeignTable:  "tags",
   201  					ForeignColumn: "id",
   202  				},
   203  			},
   204  		},
   205  	}
   206  
   207  	t.Run("Automatic", func(t *testing.T) {
   208  		expect1 := RelationshipAlias{
   209  			Local:   "Tags",
   210  			Foreign: "Videos",
   211  		}
   212  		expect2 := RelationshipAlias{
   213  			Local:   "Videos",
   214  			Foreign: "Tags",
   215  		}
   216  
   217  		a := Aliases{}
   218  		FillAliases(&a, tables)
   219  
   220  		table := a.Tables["video_tags"]
   221  		if got := table.Relationships["fk_video_id"]; !reflect.DeepEqual(expect1, got) {
   222  			t.Errorf("bad values: %#v", got)
   223  		}
   224  		if got := table.Relationships["fk_tag_id"]; !reflect.DeepEqual(expect2, got) {
   225  			t.Errorf("bad values: %#v", got)
   226  		}
   227  	})
   228  
   229  	t.Run("UserOverride", func(t *testing.T) {
   230  		expect1 := RelationshipAlias{
   231  			Local:   "NotTags",
   232  			Foreign: "NotVideos",
   233  		}
   234  		expect2 := RelationshipAlias{
   235  			Local:   "NotVideos",
   236  			Foreign: "NotTags",
   237  		}
   238  
   239  		a := Aliases{
   240  			Tables: map[string]TableAlias{
   241  				"video_tags": {
   242  					Relationships: map[string]RelationshipAlias{
   243  						"fk_video_id": {Local: "NotTags", Foreign: "NotVideos"},
   244  					},
   245  				},
   246  			},
   247  		}
   248  		FillAliases(&a, tables)
   249  
   250  		table := a.Tables["video_tags"]
   251  		if got := table.Relationships["fk_video_id"]; !reflect.DeepEqual(expect1, got) {
   252  			t.Errorf("bad values: %#v", got)
   253  		}
   254  		if got := table.Relationships["fk_tag_id"]; !reflect.DeepEqual(expect2, got) {
   255  			t.Errorf("bad values: %#v", got)
   256  		}
   257  	})
   258  }
   259  
   260  func TestAliasHelpers(t *testing.T) {
   261  	t.Parallel()
   262  
   263  	a := Aliases{
   264  		Tables: map[string]TableAlias{
   265  			"videos": {
   266  				UpPlural: "Videos",
   267  				Columns: map[string]string{
   268  					"id": "NotID",
   269  				},
   270  				Relationships: map[string]RelationshipAlias{
   271  					"fk_user_id": {Local: "Videos", Foreign: "User"},
   272  				},
   273  			},
   274  			"video_tags": {
   275  				Relationships: map[string]RelationshipAlias{
   276  					"fk_video_id": {Local: "NotTags", Foreign: "NotVideos"},
   277  				},
   278  			},
   279  		},
   280  	}
   281  
   282  	if got := a.Table("videos").UpPlural; got != "Videos" {
   283  		t.Error("videos upPlural wrong:", got)
   284  	}
   285  
   286  	if got := a.Table("videos").Relationship("fk_user_id"); got.Local != "Videos" {
   287  		t.Error("videos relationship wrong:", got)
   288  	}
   289  
   290  	got := a.ManyRelationship("videos", "fk_user_id", "video_tags", "fk_video_id")
   291  	if got.Foreign != "NotVideos" {
   292  		t.Error("relationship wrong:", got)
   293  	}
   294  
   295  	got = a.ManyRelationship("videos", "fk_user_id", "", "")
   296  	if got.Foreign != "User" {
   297  		t.Error("relationship wrong:", got)
   298  	}
   299  }