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

     1  package drivers
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestToOneRelationships(t *testing.T) {
     9  	t.Parallel()
    10  
    11  	tables := []Table{
    12  		{
    13  			Name:    "pilots",
    14  			Columns: []Column{{Name: "id", Unique: true}, {Name: "name", Unique: true}}},
    15  		{
    16  			Name:    "airports",
    17  			Columns: []Column{{Name: "id", Unique: true}, {Name: "size", Unique: true}},
    18  		},
    19  		{
    20  			Name:    "jets",
    21  			Columns: []Column{{Name: "id", Unique: true}, {Name: "pilot_id", Unique: true}, {Name: "airport_id", Unique: true}},
    22  			FKeys: []ForeignKey{
    23  				{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
    24  				{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Unique: true},
    25  			},
    26  		},
    27  		{
    28  			Name:    "licenses",
    29  			Columns: []Column{{Name: "id", Unique: true}, {Name: "pilot_id", Unique: true}},
    30  			FKeys: []ForeignKey{
    31  				{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
    32  			},
    33  		},
    34  		{
    35  			Name:    "hangars",
    36  			Columns: []Column{{Name: "id", Unique: true}, {Name: "name", Unique: true}},
    37  		},
    38  		{
    39  			Name:    "languages",
    40  			Columns: []Column{{Name: "id", Unique: true}, {Name: "language", Unique: true}},
    41  		},
    42  		{
    43  			Name:        "pilot_languages",
    44  			IsJoinTable: true,
    45  			Columns:     []Column{{Name: "pilot_id", Unique: true}, {Name: "language_id", Unique: true}},
    46  			FKeys: []ForeignKey{
    47  				{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
    48  				{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Unique: true},
    49  			},
    50  		},
    51  	}
    52  
    53  	relationships := ToOneRelationships("pilots", tables)
    54  
    55  	expected := []ToOneRelationship{
    56  		{
    57  			Name:     "jets_pilot_id_fk",
    58  			Table:    "pilots",
    59  			Column:   "id",
    60  			Nullable: false,
    61  			Unique:   false,
    62  
    63  			ForeignTable:          "jets",
    64  			ForeignColumn:         "pilot_id",
    65  			ForeignColumnNullable: false,
    66  			ForeignColumnUnique:   true,
    67  		},
    68  		{
    69  			Name:     "licenses_pilot_id_fk",
    70  			Table:    "pilots",
    71  			Column:   "id",
    72  			Nullable: false,
    73  			Unique:   false,
    74  
    75  			ForeignTable:          "licenses",
    76  			ForeignColumn:         "pilot_id",
    77  			ForeignColumnNullable: false,
    78  			ForeignColumnUnique:   true,
    79  		},
    80  	}
    81  
    82  	if len(relationships) != 2 {
    83  		t.Error("wrong # of relationships", len(relationships))
    84  	}
    85  
    86  	for i, v := range relationships {
    87  		if !reflect.DeepEqual(v, expected[i]) {
    88  			t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
    89  		}
    90  	}
    91  }
    92  
    93  func TestToManyRelationships(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	tables := []Table{
    97  		{
    98  			Name:    "pilots",
    99  			Columns: []Column{{Name: "id"}, {Name: "name"}},
   100  		},
   101  		{
   102  			Name:    "airports",
   103  			Columns: []Column{{Name: "id"}, {Name: "size"}},
   104  		},
   105  		{
   106  			Name:    "jets",
   107  			Columns: []Column{{Name: "id"}, {Name: "pilot_id"}, {Name: "airport_id"}},
   108  			FKeys: []ForeignKey{
   109  				{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
   110  				{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id"},
   111  			},
   112  		},
   113  		{
   114  			Name:    "licenses",
   115  			Columns: []Column{{Name: "id"}, {Name: "pilot_id"}},
   116  			FKeys: []ForeignKey{
   117  				{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
   118  			},
   119  		},
   120  		{
   121  			Name:    "hangars",
   122  			Columns: []Column{{Name: "id"}, {Name: "name"}},
   123  		},
   124  		{
   125  			Name:    "languages",
   126  			Columns: []Column{{Name: "id"}, {Name: "language"}},
   127  		},
   128  		{
   129  			Name:        "pilot_languages",
   130  			IsJoinTable: true,
   131  			Columns:     []Column{{Name: "pilot_id"}, {Name: "language_id"}},
   132  			FKeys: []ForeignKey{
   133  				{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
   134  				{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id"},
   135  			},
   136  		},
   137  	}
   138  
   139  	relationships := ToManyRelationships("pilots", tables)
   140  
   141  	expected := []ToManyRelationship{
   142  		{
   143  			Name:     "jets_pilot_id_fk",
   144  			Table:    "pilots",
   145  			Column:   "id",
   146  			Nullable: false,
   147  			Unique:   false,
   148  
   149  			ForeignTable:          "jets",
   150  			ForeignColumn:         "pilot_id",
   151  			ForeignColumnNullable: false,
   152  			ForeignColumnUnique:   false,
   153  
   154  			ToJoinTable: false,
   155  		},
   156  		{
   157  			Name:     "licenses_pilot_id_fk",
   158  			Table:    "pilots",
   159  			Column:   "id",
   160  			Nullable: false,
   161  			Unique:   false,
   162  
   163  			ForeignTable:          "licenses",
   164  			ForeignColumn:         "pilot_id",
   165  			ForeignColumnNullable: false,
   166  			ForeignColumnUnique:   false,
   167  
   168  			ToJoinTable: false,
   169  		},
   170  		{
   171  			Table:    "pilots",
   172  			Column:   "id",
   173  			Nullable: false,
   174  			Unique:   false,
   175  
   176  			ForeignTable:          "languages",
   177  			ForeignColumn:         "id",
   178  			ForeignColumnNullable: false,
   179  			ForeignColumnUnique:   false,
   180  
   181  			ToJoinTable: true,
   182  			JoinTable:   "pilot_languages",
   183  
   184  			JoinLocalFKeyName:       "pilot_id_fk",
   185  			JoinLocalColumn:         "pilot_id",
   186  			JoinLocalColumnNullable: false,
   187  			JoinLocalColumnUnique:   false,
   188  
   189  			JoinForeignFKeyName:       "language_id_fk",
   190  			JoinForeignColumn:         "language_id",
   191  			JoinForeignColumnNullable: false,
   192  			JoinForeignColumnUnique:   false,
   193  		},
   194  	}
   195  
   196  	if len(relationships) != 3 {
   197  		t.Error("wrong # of relationships:", len(relationships))
   198  	}
   199  
   200  	for i, v := range relationships {
   201  		if !reflect.DeepEqual(v, expected[i]) {
   202  			t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
   203  		}
   204  	}
   205  }
   206  
   207  func TestToManyRelationshipsNull(t *testing.T) {
   208  	t.Parallel()
   209  
   210  	tables := []Table{
   211  		{
   212  			Name:    "pilots",
   213  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "name", Nullable: true}}},
   214  		{
   215  			Name:    "airports",
   216  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "size", Nullable: true}},
   217  		},
   218  		{
   219  			Name:    "jets",
   220  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}, {Name: "airport_id", Nullable: true}},
   221  			FKeys: []ForeignKey{
   222  				{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
   223  				{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
   224  			},
   225  		},
   226  		{
   227  			Name:    "licenses",
   228  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}},
   229  			FKeys: []ForeignKey{
   230  				{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
   231  			},
   232  		},
   233  		{
   234  			Name:    "hangars",
   235  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "name", Nullable: true}},
   236  		},
   237  		{
   238  			Name:    "languages",
   239  			Columns: []Column{{Name: "id", Nullable: true}, {Name: "language", Nullable: true}},
   240  		},
   241  		{
   242  			Name:        "pilot_languages",
   243  			IsJoinTable: true,
   244  			Columns:     []Column{{Name: "pilot_id", Nullable: true}, {Name: "language_id", Nullable: true}},
   245  			FKeys: []ForeignKey{
   246  				{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
   247  				{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
   248  			},
   249  		},
   250  	}
   251  
   252  	relationships := ToManyRelationships("pilots", tables)
   253  	if len(relationships) != 3 {
   254  		t.Error("wrong # of relationships:", len(relationships))
   255  	}
   256  
   257  	expected := []ToManyRelationship{
   258  		{
   259  			Name:     "jets_pilot_id_fk",
   260  			Table:    "pilots",
   261  			Column:   "id",
   262  			Nullable: true,
   263  			Unique:   false,
   264  
   265  			ForeignTable:          "jets",
   266  			ForeignColumn:         "pilot_id",
   267  			ForeignColumnNullable: true,
   268  			ForeignColumnUnique:   false,
   269  
   270  			ToJoinTable: false,
   271  		},
   272  		{
   273  			Name:     "licenses_pilot_id_fk",
   274  			Table:    "pilots",
   275  			Column:   "id",
   276  			Nullable: true,
   277  			Unique:   false,
   278  
   279  			ForeignTable:          "licenses",
   280  			ForeignColumn:         "pilot_id",
   281  			ForeignColumnNullable: true,
   282  			ForeignColumnUnique:   false,
   283  
   284  			ToJoinTable: false,
   285  		},
   286  		{
   287  			Table:    "pilots",
   288  			Column:   "id",
   289  			Nullable: true,
   290  			Unique:   false,
   291  
   292  			ForeignTable:          "languages",
   293  			ForeignColumn:         "id",
   294  			ForeignColumnNullable: true,
   295  			ForeignColumnUnique:   false,
   296  
   297  			ToJoinTable: true,
   298  			JoinTable:   "pilot_languages",
   299  
   300  			JoinLocalFKeyName:       "pilot_id_fk",
   301  			JoinLocalColumn:         "pilot_id",
   302  			JoinLocalColumnNullable: true,
   303  			JoinLocalColumnUnique:   false,
   304  
   305  			JoinForeignFKeyName:       "language_id_fk",
   306  			JoinForeignColumn:         "language_id",
   307  			JoinForeignColumnNullable: true,
   308  			JoinForeignColumnUnique:   false,
   309  		},
   310  	}
   311  
   312  	for i, v := range relationships {
   313  		if !reflect.DeepEqual(v, expected[i]) {
   314  			t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
   315  		}
   316  	}
   317  }