diff options
author | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-23 14:17:23 +0100 |
---|---|---|
committer | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-23 14:17:23 +0100 |
commit | d560cb64e86e39bad06eb8ad02bdba0d40a7630f (patch) | |
tree | 034386db0e347bc264ab78172bbb84a2a4dc58fd | |
parent | 930b690d5b93bde10f7218ebd6f0183e548ca50d (diff) |
added test for whois and mgo
-rw-r--r-- | main.go | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + whois "github.com/likexian/whois-go" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "time" +) + +const ( + dburl = "localhost" + dropOldDB = true +) + +type Person struct { + ID bson.ObjectId `bson:"_id,omitempty"` + Name string + Phone string + Timestamp time.Time +} + +func main() { + whoistest() +} + +func whoistest() { + result, err := whois.Whois("123.123.123.123", "whois.cymru.com") + if err != nil { + panic(err) + } + + fmt.Println(result) +} + +func mongodbtest() { + session, err := mgo.Dial(dburl) + if err != nil { + panic(err) + } + defer session.Close() + + session.SetMode(mgo.Monotonic, true) + + if dropOldDB { + err = session.DB("test").DropDatabase() + if err != nil { + panic(err) + } + } + + // Collection people + c := session.DB("test").C("people") + + index := mgo.Index{ + Key: []string{"name", "phone"}, + Unique: true, + DropDups: true, + Background: true, + Sparse: true, + } + + err = c.EnsureIndex(index) + if err != nil { + panic(err) + } + + err = c.Insert(&Person{Name: "Person 1", Phone: "+46 123 45 67", Timestamp: time.Now()}, + &Person{Name: "Person 2", Phone: "+46 765 43 21", Timestamp: time.Now()}) + + if err != nil { + panic(err) + } + + // Query one + result := Person{} + err = c.Find(bson.M{"name": "Person 1"}).Select(bson.M{"phone": 0}).One(&result) + + if err != nil { + panic(err) + } + + fmt.Println("Phone", result) +} |