github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/store/mongo_test.go (about) 1 // MIT License 2 3 // Copyright (c) 2021 Tree Xie 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package store 24 25 import ( 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestFillMongoStoreOptions(t *testing.T) { 33 assert := assert.New(t) 34 35 ms := &mongoStore{} 36 fillMongoStoreOptions("", ms) 37 assert.Equal(defaultMongoDatabase, ms.db) 38 assert.Equal(3*time.Second, ms.timeout) 39 40 ms = &mongoStore{} 41 fillMongoStoreOptions("mongodb://localhost:27017", ms) 42 assert.Equal(defaultMongoDatabase, ms.db) 43 assert.Equal(3*time.Second, ms.timeout) 44 45 ms = &mongoStore{} 46 fillMongoStoreOptions("mongodb://localhost:27017/abc?timeout=5s", ms) 47 assert.Equal("abc", ms.db) 48 assert.Equal(5*time.Second, ms.timeout) 49 } 50 51 func TestNewMongoStore(t *testing.T) { 52 assert := assert.New(t) 53 store, err := newMongoStore("mongodb://localhost:27017/pike") 54 assert.Nil(err) 55 56 key := []byte("key") 57 value := []byte("value") 58 _, err = store.Get(key) 59 assert.Equal(ErrNotFound, err) 60 61 err = store.Set(key, value, time.Second) 62 assert.Nil(err) 63 64 data, err := store.Get(key) 65 assert.Nil(err) 66 assert.Equal(value, data) 67 68 err = store.Delete(key) 69 assert.Nil(err) 70 store.Close() 71 }