From 1dc336c717125b2b3dfe7ff8edabf57a9713cd9b Mon Sep 17 00:00:00 2001 From: Ben Hood <0x6e6562@gmail.com> Date: Mon, 24 Feb 2014 17:20:59 +0000 Subject: [PATCH] Added integration test and fixed a bug with zero length data --- cassandra_test.go | 14 +++++++++----- marshal.go | 8 +++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cassandra_test.go b/cassandra_test.go index f33ecfae2..03d70c604 100644 --- a/cassandra_test.go +++ b/cassandra_test.go @@ -7,6 +7,7 @@ package gocql import ( "bytes" "flag" + "math/big" "reflect" "sort" "strings" @@ -78,6 +79,7 @@ func TestCRUD(t *testing.T) { views bigint, protected boolean, modified timestamp, + rating decimal, tags set, attachments map, PRIMARY KEY (title, revid) @@ -87,10 +89,10 @@ func TestCRUD(t *testing.T) { for _, page := range pageTestData { if err := session.Query(`INSERT INTO page - (title, revid, body, views, protected, modified, tags, attachments) - VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, + (title, revid, body, views, protected, modified, rating, tags, attachments) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, page.Title, page.RevId, page.Body, page.Views, page.Protected, - page.Modified, page.Tags, page.Attachments).Exec(); err != nil { + page.Modified, page.Rating, page.Tags, page.Attachments).Exec(); err != nil { t.Fatal("insert:", err) } } @@ -106,11 +108,11 @@ func TestCRUD(t *testing.T) { for _, original := range pageTestData { page := new(Page) err := session.Query(`SELECT title, revid, body, views, protected, modified, - tags, attachments + tags, attachments, rating FROM page WHERE title = ? AND revid = ? LIMIT 1`, original.Title, original.RevId).Scan(&page.Title, &page.RevId, &page.Body, &page.Views, &page.Protected, &page.Modified, &page.Tags, - &page.Attachments) + &page.Attachments, &page.Rating) if err != nil { t.Error("select page:", err) continue @@ -301,6 +303,7 @@ type Page struct { Views int64 Protected bool Modified time.Time + Rating *big.Rat Tags []string Attachments map[string]Attachment } @@ -312,6 +315,7 @@ var pageTestData = []*Page{ Title: "Frontpage", RevId: TimeUUID(), Body: "Welcome to this wiki page!", + Rating: big.NewRat(871298379, 243), Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC), Tags: []string{"start", "important", "test"}, Attachments: map[string]Attachment{ diff --git a/marshal.go b/marshal.go index c15c8389c..d982efb84 100644 --- a/marshal.go +++ b/marshal.go @@ -704,9 +704,11 @@ func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error { case Unmarshaler: return v.UnmarshalCQL(info, data) case **big.Rat: - denom := new(big.Int).SetBytes(data[0:4]) - num := new(big.Int).SetBytes(data[4:]) - *v = new(big.Rat).SetFrac(num, denom) + if len(data) > 4 { + denom := new(big.Int).SetBytes(data[0:4]) + num := new(big.Int).SetBytes(data[4:]) + *v = new(big.Rat).SetFrac(num, denom) + } return nil } return unmarshalErrorf("can not unmarshal %s into %T", info, value)