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

     1  package drivers
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestColumnNames(t *testing.T) {
     9  	t.Parallel()
    10  
    11  	cols := []Column{
    12  		{Name: "one"},
    13  		{Name: "two"},
    14  		{Name: "three"},
    15  	}
    16  
    17  	out := strings.Join(ColumnNames(cols), " ")
    18  	if out != "one two three" {
    19  		t.Error("output was wrong:", out)
    20  	}
    21  }
    22  
    23  func TestColumnDBTypes(t *testing.T) {
    24  	cols := []Column{
    25  		{Name: "test_one", DBType: "integer"},
    26  		{Name: "test_two", DBType: "interval"},
    27  	}
    28  
    29  	res := ColumnDBTypes(cols)
    30  	if res["TestOne"] != "integer" {
    31  		t.Errorf(`Expected res["TestOne"]="integer", got: %s`, res["TestOne"])
    32  	}
    33  	if res["TestTwo"] != "interval" {
    34  		t.Errorf(`Expected res["TestOne"]="interval", got: %s`, res["TestOne"])
    35  	}
    36  }
    37  
    38  func TestFilterColumnsByDefault(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	cols := []Column{
    42  		{Name: "col1", Default: ""},
    43  		{Name: "col2", Default: "things"},
    44  		{Name: "col3", Default: ""},
    45  		{Name: "col4", Default: "things2"},
    46  	}
    47  
    48  	res := FilterColumnsByDefault(false, cols)
    49  	if res[0].Name != `col1` {
    50  		t.Errorf("Invalid result: %#v", res)
    51  	}
    52  	if res[1].Name != `col3` {
    53  		t.Errorf("Invalid result: %#v", res)
    54  	}
    55  
    56  	res = FilterColumnsByDefault(true, cols)
    57  	if res[0].Name != `col2` {
    58  		t.Errorf("Invalid result: %#v", res)
    59  	}
    60  	if res[1].Name != `col4` {
    61  		t.Errorf("Invalid result: %#v", res)
    62  	}
    63  
    64  	res = FilterColumnsByDefault(false, []Column{})
    65  	if res != nil {
    66  		t.Errorf("Invalid result: %#v", res)
    67  	}
    68  }
    69  
    70  func TestFilterColumnsByEnum(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	cols := []Column{
    74  		{Name: "col1", DBType: "enum('hello')"},
    75  		{Name: "col2", DBType: "enum('hello','there')"},
    76  		{Name: "col3", DBType: "enum"},
    77  		{Name: "col4", DBType: ""},
    78  		{Name: "col5", DBType: "int"},
    79  	}
    80  
    81  	res := FilterColumnsByEnum(cols)
    82  	if res[0].Name != `col1` {
    83  		t.Errorf("Invalid result: %#v", res)
    84  	}
    85  	if res[1].Name != `col2` {
    86  		t.Errorf("Invalid result: %#v", res)
    87  	}
    88  }