Skip to content

Instantly share code, notes, and snippets.

@thrawn01
Created November 28, 2016 20:10
Show Gist options
  • Save thrawn01/a30a09abe5356afd76aa853a3578de80 to your computer and use it in GitHub Desktop.
Save thrawn01/a30a09abe5356afd76aa853a3578de80 to your computer and use it in GitHub Desktop.
package tagger
import (
"time"
"fmt"
"github.com/mailgun/sandra"
"github.com/mailgun/scout/config"
"github.com/mailgun/scout/model/types"
. "gopkg.in/check.v1"
)
type GoCqlSuite struct {
cassandra sandra.Cassandra
}
var _ = Suite(&GoCqlSuite{})
func (s *GoCqlSuite) SetUpSuite(c *C) {
cassandra, err := sandra.NewCassandra(config.GetTestConfig().Cassandra)
if err != nil {
c.Fatal(err)
}
s.cassandra = cassandra
CreateTagsTable(cassandra)
// Wait for the table to become available
err = sandra.WaitForTables(s.cassandra, time.Second*30, "tags")
c.Assert(err, IsNil)
}
func (s *GoCqlSuite) Insert(id types.LevelID, timeStamp time.Time) error {
return s.cassandra.ExecuteQuery(`insert into tags (id, tag, value, lastSeen) values (?, ?, ?, ?)`,
id, "tag", "value", timeStamp)
}
func (s *GoCqlSuite) InsertNoLastSeen(id types.LevelID) error {
return s.cassandra.ExecuteQuery(`insert into tags (id, tag, value) values (?, ?, ?)`,
id, "tag", "value")
}
func (s *GoCqlSuite) TearDownSuite(c *C) {
s.cassandra.ExecuteQuery("drop table tags")
}
func (s *GoCqlSuite) TestTime(c *C) {
c.Assert(s.InsertNoLastSeen(types.NewDomainID("id0")), IsNil)
c.Assert(s.Insert(types.NewDomainID("id1"), time.Now()), IsNil)
c.Assert(s.Insert(types.NewDomainID("id2"), time.Now()), IsNil)
c.Assert(s.Insert(types.NewDomainID("id3"), time.Now()), IsNil)
c.Assert(s.InsertNoLastSeen(types.NewDomainID("id4")), IsNil)
fmt.Printf("time\n")
var id string
var lastSeen time.Time
iter := s.cassandra.IterQuery(`select id, lastSeen from tags`, nil, &id, &lastSeen)
for _, hasNext, err := iter(); hasNext; _, hasNext, err = iter() {
if err != nil {
fmt.Printf("err: %s\n", err)
c.Fatal(err.Error())
}
fmt.Printf("id: %s lastSeen: %s\n", id, lastSeen)
}
fmt.Printf("done\n")
}
/usr/local/go/bin/go test -v -check.f GoCqlSuite -check.vv ./...
START: gocql_test.go:20: GoCqlSuite.SetUpSuite
START: gocql_test.go:48: GoCqlSuite.TestTime
time
id: d:id4 lastSeen: 0001-01-01 00:00:00 +0000 UTC
id: d:id1 lastSeen: 2016-11-28 20:08:39.233 +0000 UTC
id: d:id0 lastSeen: 0001-01-01 00:00:00 +0000 UTC
id: d:id2 lastSeen: 2016-11-28 20:08:39.239 +0000 UTC
id: d:id3 lastSeen: 2016-11-28 20:08:39.24 +0000 UTC
done
START: gocql_test.go:44: GoCqlSuite.TearDownSuite
OK: 1 passed
ok github.com/mailgun/scout/model/tagger 1.007s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment