github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/random/random_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 random_test 22 23 import ( 24 "context" 25 "testing" 26 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/uber-go/dosa" 31 "github.com/uber-go/dosa/connectors/random" 32 ) 33 34 var sut = random.Connector{} 35 36 type AllTypes struct { 37 dosa.Entity `dosa:"primaryKey=BoolType"` 38 BoolType bool 39 Int32Type int32 40 Int64Type int64 41 DoubleType float64 42 StringType string 43 BlobType []byte 44 TimeType time.Time 45 UUIDType dosa.UUID 46 } 47 48 var ( 49 testTable, _ = dosa.TableFromInstance((*AllTypes)(nil)) 50 testInfo = &dosa.EntityInfo{ 51 Def: &testTable.EntityDefinition, 52 Ref: &dosa.SchemaRef{ 53 Scope: "testScope", 54 NamePrefix: "testPrefix", 55 EntityName: "testEntityName", 56 }, 57 } 58 testConditions = make(map[string][]*dosa.Condition) 59 testPairs = dosa.FieldNameValuePair{} 60 testValues = make(map[string]dosa.FieldValue) 61 testMultiValues = make([]map[string]dosa.FieldValue, 50) 62 minimumFields = []string{"booltype", "int32type", "int64type", "doubletype", "stringtype", "blobtype", "timetype", "uuidtype"} 63 ctx = context.Background() 64 ) 65 66 func TestRandom_CreateIfNotExists(t *testing.T) { 67 assert.NoError(t, sut.CreateIfNotExists(ctx, testInfo, testValues)) 68 } 69 70 func TestRandom_Read(t *testing.T) { 71 val, err := sut.Read(ctx, testInfo, testValues, minimumFields) 72 assert.NoError(t, err) 73 assert.NotNil(t, val) 74 for _, field := range minimumFields { 75 assert.NotNil(t, val[field]) 76 } 77 } 78 79 func TestRandom_MultiRead(t *testing.T) { 80 v, e := sut.MultiRead(ctx, testInfo, testMultiValues, minimumFields) 81 assert.NotNil(t, v) 82 assert.Nil(t, e) 83 assert.Equal(t, len(testMultiValues), len(v)) 84 for i := range v { 85 for _, field := range minimumFields { 86 assert.NotNil(t, v[i].Values[field]) 87 } 88 } 89 } 90 91 func TestRandom_Upsert(t *testing.T) { 92 err := sut.Upsert(ctx, testInfo, testValues) 93 assert.Nil(t, err) 94 } 95 96 func TestRandom_MultiUpsert(t *testing.T) { 97 errs, err := sut.MultiUpsert(ctx, testInfo, testMultiValues) 98 assert.NotNil(t, errs) 99 assert.Nil(t, err) 100 } 101 102 func TestRandom_Remove(t *testing.T) { 103 err := sut.Remove(ctx, testInfo, testValues) 104 assert.NoError(t, err) 105 } 106 107 func TestRandom_RemoveRange(t *testing.T) { 108 err := sut.RemoveRange(ctx, testInfo, testConditions) 109 assert.NoError(t, err) 110 } 111 112 func TestRandom_MultiRemove(t *testing.T) { 113 errs, err := sut.MultiRemove(ctx, testInfo, testMultiValues) 114 assert.NotNil(t, errs) 115 assert.Nil(t, err) 116 } 117 118 func TestRandom_Range(t *testing.T) { 119 vals, _, err := sut.Range(ctx, testInfo, testConditions, minimumFields, "", 32) 120 assert.NotNil(t, vals) 121 assert.NoError(t, err) 122 } 123 124 func TestRandom_Scan(t *testing.T) { 125 vals, _, err := sut.Scan(ctx, testInfo, minimumFields, "", 32) 126 assert.NotNil(t, vals) 127 assert.NoError(t, err) 128 } 129 130 func TestRandom_CheckSchema(t *testing.T) { 131 defs := make([]*dosa.EntityDefinition, 4) 132 versions, err := sut.CheckSchema(ctx, "testScope", "testPrefix", defs) 133 assert.NotNil(t, versions) 134 assert.NoError(t, err) 135 } 136 137 func TestRandom_UpsertSchema(t *testing.T) { 138 defs := make([]*dosa.EntityDefinition, 4) 139 status, err := sut.UpsertSchema(ctx, "testScope", "testPrefix", defs) 140 assert.NotNil(t, status) 141 assert.NoError(t, err) 142 } 143 144 func TestRandom_CreateScope(t *testing.T) { 145 assert.NoError(t, sut.CreateScope(ctx, "")) 146 } 147 148 func TestRandom_TruncateScope(t *testing.T) { 149 assert.NoError(t, sut.TruncateScope(ctx, "")) 150 } 151 152 func TestRandom_DropScope(t *testing.T) { 153 assert.NoError(t, sut.DropScope(ctx, "")) 154 } 155 156 func TestRandom_ScopeExists(t *testing.T) { 157 exists, err := sut.ScopeExists(ctx, "") 158 assert.NoError(t, err) 159 assert.True(t, exists) 160 } 161 162 func TestRandom_Shutdown(t *testing.T) { 163 assert.Nil(t, sut.Shutdown()) 164 } 165 166 // this test is primarily just for 100% coverage 167 func TestRandom_badTypePanic(t *testing.T) { 168 testInfo.Def.Columns[0].Type = dosa.Invalid 169 assert.Panics(t, func() { 170 random.Data(testInfo, minimumFields) 171 }) 172 }