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

     1  package boilingcore
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/volatiletech/sqlboiler/v4/drivers"
     7  )
     8  
     9  func TestTxtNameToOne(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	tests := []struct {
    13  		Table               string
    14  		Column              string
    15  		Unique              bool
    16  		ForeignTable        string
    17  		ForeignColumn       string
    18  		ForeignColumnUnique bool
    19  
    20  		LocalFn   string
    21  		ForeignFn string
    22  	}{
    23  		{"jets", "airport_id", false, "airports", "id", true, "Jets", "Airport"},
    24  		{"jets", "airport_id", true, "airports", "id", true, "Jet", "Airport"},
    25  
    26  		{"jets", "holiday_id", false, "airports", "id", true, "HolidayJets", "Holiday"},
    27  		{"jets", "holiday_id", true, "airports", "id", true, "HolidayJet", "Holiday"},
    28  
    29  		{"jets", "holiday_airport_id", false, "airports", "id", true, "HolidayAirportJets", "HolidayAirport"},
    30  		{"jets", "holiday_airport_id", true, "airports", "id", true, "HolidayAirportJet", "HolidayAirport"},
    31  
    32  		{"jets", "jet_id", false, "jets", "id", true, "Jets", "Jet"},
    33  		{"jets", "jet_id", true, "jets", "id", true, "Jet", "Jet"},
    34  		{"jets", "plane_id", false, "jets", "id", true, "PlaneJets", "Plane"},
    35  		{"jets", "plane_id", true, "jets", "id", true, "PlaneJet", "Plane"},
    36  
    37  		{"videos", "user_id", false, "users", "id", true, "Videos", "User"},
    38  		{"videos", "producer_id", false, "users", "id", true, "ProducerVideos", "Producer"},
    39  		{"videos", "user_id", true, "users", "id", true, "Video", "User"},
    40  		{"videos", "producer_id", true, "users", "id", true, "ProducerVideo", "Producer"},
    41  
    42  		{"videos", "user", false, "users", "id", true, "Videos", "VideoUser"},
    43  		{"videos", "created_by", false, "users", "id", true, "CreatedByVideos", "CreatedByUser"},
    44  		{"videos", "director", false, "users", "id", true, "DirectorVideos", "DirectorUser"},
    45  		{"videos", "user", true, "users", "id", true, "Video", "VideoUser"},
    46  		{"videos", "created_by", true, "users", "id", true, "CreatedByVideo", "CreatedByUser"},
    47  		{"videos", "director", true, "users", "id", true, "DirectorVideo", "DirectorUser"},
    48  
    49  		{"industries", "industry_id", false, "industries", "id", true, "Industries", "Industry"},
    50  		{"industries", "parent_id", false, "industries", "id", true, "ParentIndustries", "Parent"},
    51  		{"industries", "industry_id", true, "industries", "id", true, "Industry", "Industry"},
    52  		{"industries", "parent_id", true, "industries", "id", true, "ParentIndustry", "Parent"},
    53  
    54  		{"race_result_scratchings", "results_id", false, "race_results", "id", true, "ResultRaceResultScratchings", "Result"},
    55  	}
    56  
    57  	for i, test := range tests {
    58  		fk := drivers.ForeignKey{
    59  			Table: test.Table, Column: test.Column, Unique: test.Unique,
    60  			ForeignTable: test.ForeignTable, ForeignColumn: test.ForeignColumn, ForeignColumnUnique: test.ForeignColumnUnique,
    61  		}
    62  
    63  		local, foreign := txtNameToOne(fk)
    64  		if local != test.LocalFn {
    65  			t.Error(i, "local wrong:", local, "want:", test.LocalFn)
    66  		}
    67  		if foreign != test.ForeignFn {
    68  			t.Error(i, "foreign wrong:", foreign, "want:", test.ForeignFn)
    69  		}
    70  	}
    71  }
    72  
    73  func TestTxtNameToMany(t *testing.T) {
    74  	t.Parallel()
    75  
    76  	tests := []struct {
    77  		LHSTable  string
    78  		LHSColumn string
    79  
    80  		RHSTable  string
    81  		RHSColumn string
    82  
    83  		LHSFn string
    84  		RHSFn string
    85  	}{
    86  		{"pilots", "pilot_id", "languages", "language_id", "Pilots", "Languages"},
    87  		{"pilots", "captain_id", "languages", "lingo_id", "CaptainPilots", "LingoLanguages"},
    88  
    89  		{"pilots", "pilot_id", "pilots", "mentor_id", "Pilots", "MentorPilots"},
    90  		{"pilots", "mentor_id", "pilots", "pilot_id", "MentorPilots", "Pilots"},
    91  		{"pilots", "captain_id", "pilots", "mentor_id", "CaptainPilots", "MentorPilots"},
    92  
    93  		{"videos", "video_id", "tags", "tag_id", "Videos", "Tags"},
    94  		{"tags", "tag_id", "videos", "video_id", "Tags", "Videos"},
    95  	}
    96  
    97  	for i, test := range tests {
    98  		lhsFk := drivers.ForeignKey{
    99  			ForeignTable: test.LHSTable,
   100  			Column:       test.LHSColumn,
   101  		}
   102  		rhsFk := drivers.ForeignKey{
   103  			ForeignTable: test.RHSTable,
   104  			Column:       test.RHSColumn,
   105  		}
   106  
   107  		lhs, rhs := txtNameToMany(lhsFk, rhsFk)
   108  		if lhs != test.LHSFn {
   109  			t.Error(i, "local wrong:", lhs, "want:", test.LHSFn)
   110  		}
   111  		if rhs != test.RHSFn {
   112  			t.Error(i, "foreign wrong:", rhs, "want:", test.RHSFn)
   113  		}
   114  	}
   115  }
   116  
   117  func TestTrimSuffixes(t *testing.T) {
   118  	t.Parallel()
   119  
   120  	for _, s := range identifierSuffixes {
   121  		a := "hello" + s
   122  
   123  		if z := trimSuffixes(a); z != "hello" {
   124  			t.Errorf("got %s", z)
   125  		}
   126  	}
   127  }