github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/finder_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package dosa
    22  
    23  import (
    24  	"fmt"
    25  	"io/ioutil"
    26  	"os"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestUnparseableGoCode(t *testing.T) {
    33  	const tmpdir = ".testgen"
    34  	defer os.RemoveAll(tmpdir)
    35  	if err := os.Mkdir(tmpdir, 0770); err != nil {
    36  		t.Fatalf("can't create %s: %s", tmpdir, err)
    37  	}
    38  	if err := ioutil.WriteFile(tmpdir+"/broken.go", []byte("package broken\nfunc broken\n"), 0644); err != nil {
    39  		t.Fatalf("can't create %s/broken.go: %s", tmpdir, err)
    40  	}
    41  	entities, errs, err := FindEntities([]string{tmpdir}, []string{})
    42  	assert.Nil(t, entities)
    43  	assert.Nil(t, errs)
    44  	assert.Contains(t, err.Error(), "expected '('")
    45  }
    46  
    47  func TestNonExistentDirectory(t *testing.T) {
    48  	const nonExistentDirectory = "ThisDirectoryBetterNotExist"
    49  	entities, errs, err := FindEntities([]string{nonExistentDirectory}, []string{})
    50  	assert.Nil(t, entities)
    51  	assert.Nil(t, errs)
    52  	assert.Contains(t, err.Error(), nonExistentDirectory)
    53  }
    54  
    55  func TestParser(t *testing.T) {
    56  	entities, errs, err := FindEntities([]string{"."}, []string{})
    57  
    58  	assert.Equal(t, 19, len(entities), fmt.Sprintf("%s", entities))
    59  	assert.Equal(t, 20, len(errs), fmt.Sprintf("%v", errs))
    60  	assert.Nil(t, err)
    61  
    62  	for _, entity := range entities {
    63  		var e *Table
    64  		switch entity.Name {
    65  		case "singleprimarykeynoparen":
    66  			e, _ = TableFromInstance(&SinglePrimaryKeyNoParen{})
    67  		case "singleprimarykey":
    68  			e, _ = TableFromInstance(&SinglePrimaryKey{})
    69  		case "singlepartitionkey":
    70  			e, _ = TableFromInstance(&SinglePartitionKey{})
    71  		case "primarykeywithsecondaryrange":
    72  			e, _ = TableFromInstance(&PrimaryKeyWithSecondaryRange{})
    73  		case "primarykeywithdescendingrange":
    74  			e, _ = TableFromInstance(&PrimaryKeyWithDescendingRange{})
    75  		case "multicomponentprimarykey":
    76  			e, _ = TableFromInstance(&MultiComponentPrimaryKey{})
    77  		case "nullabletype":
    78  			e, _ = TableFromInstance(&NullableType{})
    79  		case "alltypes":
    80  			e, _ = TableFromInstance(&AllTypes{})
    81  		case "unexportedfieldtype":
    82  			e, _ = TableFromInstance(&UnexportedFieldType{})
    83  		case "ignoretagtype":
    84  			e, _ = TableFromInstance(&IgnoreTagType{})
    85  		case "badcolnamebutrenamed":
    86  			e, _ = TableFromInstance(&BadColNameButRenamed{})
    87  		case "clienttestentity1": // skip, see https://jira.uberinternal.com/browse/DOSA-788
    88  			continue
    89  		case "clienttestentity2": // skip, same as above
    90  			continue
    91  		case "registrytestvalid": // skip, same as above
    92  			continue
    93  		case "allfieldtypes":
    94  			continue
    95  		case "alltypesscantestentity": // skipping test entity defined in scan_test.go
    96  			continue
    97  		case "singleindexnoparen":
    98  			e, _ = TableFromInstance(&SingleIndexNoParen{})
    99  		case "multipleindexes":
   100  			e, _ = TableFromInstance(&MultipleIndexes{})
   101  		case "complexindexes":
   102  			e, _ = TableFromInstance(&ComplexIndexes{})
   103  		default:
   104  			t.Errorf("entity %s not expected", entity.Name)
   105  			continue
   106  		}
   107  
   108  		assert.Equal(t, e, entity)
   109  	}
   110  }
   111  
   112  func TestExclusion(t *testing.T) {
   113  	entities, errs, err := FindEntities([]string{"."}, []string{"*_test.go"})
   114  	assert.Equal(t, 0, len(entities))
   115  	assert.Equal(t, 0, len(errs))
   116  	assert.Nil(t, err)
   117  }
   118  
   119  func TestFindEntitiesInOtherPkg(t *testing.T) {
   120  	entities, warnings, err := FindEntities([]string{"testentity"}, []string{})
   121  	assert.NoError(t, err)
   122  	assert.Equal(t, 6, len(entities))
   123  	assert.Empty(t, warnings)
   124  }
   125  
   126  func BenchmarkFinder(b *testing.B) {
   127  	for i := 0; i < b.N; i++ {
   128  		FindEntities([]string{"."}, []string{})
   129  	}
   130  }
   131  
   132  func TestStringToDosaType(t *testing.T) {
   133  	data := []struct {
   134  		inType    string
   135  		pkg       string
   136  		expected  Type
   137  		isPointer bool
   138  	}{
   139  		// Tests without package name
   140  		{"string", "", String, false},
   141  		{"[]byte", "", Blob, false},
   142  		{"bool", "", Bool, false},
   143  		{"int32", "", Int32, false},
   144  		{"int64", "", Int64, false},
   145  		{"float64", "", Double, false},
   146  		{"time.Time", "", Timestamp, false},
   147  		{"UUID", "", TUUID, false},
   148  
   149  		{"*string", "", String, true},
   150  		{"*bool", "", Bool, true},
   151  		{"*int32", "", Int32, true},
   152  		{"*int64", "", Int64, true},
   153  		{"*float64", "", Double, true},
   154  		{"*time.Time", "", Timestamp, true},
   155  		{"*UUID", "", TUUID, true},
   156  
   157  		// Tests with package name that doesn't end with dot.
   158  		{"dosa.UUID", "dosa", TUUID, false},
   159  		{"*dosa.UUID", "dosa", TUUID, true},
   160  
   161  		// Tests with package name that ends with dot.
   162  		{"dosav2.UUID", "dosav2.", TUUID, false},
   163  		{"*dosav2.UUID", "dosav2.", TUUID, true},
   164  
   165  		{"unknown", "", Invalid, false},
   166  	}
   167  
   168  	for _, tc := range data {
   169  		actual, isPointer := stringToDosaType(tc.inType, tc.pkg)
   170  		assert.Equal(t, isPointer, tc.isPointer)
   171  		assert.Equal(t, tc.expected, actual,
   172  			fmt.Sprintf("stringToDosaType(%q, %q) != %d -- actual: %d", tc.inType, tc.pkg, tc.expected, actual))
   173  	}
   174  }