Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Make tests pass on cassandra:3 on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Feb 27, 2017
1 parent 1e9660b commit 0b4545a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 37 deletions.
2 changes: 2 additions & 0 deletions persistence/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package persistence

import (
"fmt"
"time"

"github.com/gocql/gocql"
"github.com/pkg/errors"
Expand Down Expand Up @@ -33,6 +34,7 @@ const createKeySpace = `CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'c

func (cp *CassandraProvider) MigrationSession() (MigrationSession, error) {
cluster := cp.NewCluster()
cluster.Timeout = 2000 * time.Millisecond
cluster.Consistency = gocql.All
sess, err := cluster.CreateSession()
if err != nil {
Expand Down
75 changes: 38 additions & 37 deletions persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main_test

import (
"fmt"
"math/rand"
"time"

. "github.com/scoiatael/archai"
. "github.com/scoiatael/archai/persistence"
Expand All @@ -14,7 +16,7 @@ import (

const testingKeyspace = "archai_test"

const findKeyspace = `select keyspace_name from system.schema_keyspaces where keyspace_name = ?`
const findKeyspace = `select keyspace_name from system_schema.keyspaces where keyspace_name = ?`

var (
config Config
Expand All @@ -34,34 +36,33 @@ func testingKeyspaceExists() (bool, error) {
return exists, err
}

var dropKeyspace = fmt.Sprintf(`DROP KEYSPACE IF EXISTS %s`, testingKeyspace)

func dropTestingKeyspace() error {
return root_sess.Query(dropKeyspace).Exec()
}

var dropMigrations = fmt.Sprintf(`DROP TABLE IF EXISTS %s.archai_migrations`, testingKeyspace)

func dropMigrationTable() error {
return root_sess.Query(dropMigrations).Exec()
}

const findTable = `SELECT columnfamily_name from system.schema_columns where columnfamily_name = ? LIMIT 1 ALLOW FILTERING`
const findTable = `SELECT table_name from system_schema.columns where keyspace_name = ? AND table_name = ? LIMIT 1 ALLOW FILTERING`

func migrationTableExists() (bool, error) {
var (
exists bool
err error
)

iter := root_sess.Query(findTable, "archai_migrations").Iter()
iter := root_sess.Query(findTable, testingKeyspace, "archai_migrations").Iter()
exists = iter.Scan(nil)
err = iter.Close()
return exists, err
}

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func randomString() string {
b := make([]byte, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

var _ = BeforeSuite(func() {
var err error
rand.Seed(time.Now().UnixNano())
config = Config{Keyspace: testingKeyspace, Hosts: []string{"127.0.0.1"}}
provider = CassandraProvider{Hosts: config.Hosts, Keyspace: config.Keyspace}
cluster := provider.NewCluster()
Expand All @@ -73,23 +74,22 @@ var _ = BeforeSuite(func() {
})

var _ = AfterSuite(func() {
root_sess.Query(fmt.Sprintf("truncate table %s.events", testingKeyspace)).Exec()
root_sess.Close()
})

var _ = Describe("Persistence", func() {
Describe("MigrationSession", func() {
BeforeEach(func() {
err := dropTestingKeyspace()
Expect(err).NotTo(HaveOccurred())
})
It("creates keyspace", func() {
var (
exists bool
err error
)
exists, err = testingKeyspaceExists()
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeFalse())
if exists {
Skip("Testing keyspace already exists; drop it to run this test")
}

sess, err := provider.MigrationSession()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -109,13 +109,11 @@ var _ = Describe("Persistence", func() {
BeforeEach(func() {
sess, err = provider.MigrationSession()
Expect(err).NotTo(HaveOccurred())
err = dropMigrationTable()
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
sess.Close()
})
// AfterEach(func() {
// sess.Close()
// })

Context("when there's no migration table", func() {
It("creates migration table", func() {
Expand All @@ -124,9 +122,11 @@ var _ = Describe("Persistence", func() {
)
exists, err = migrationTableExists()
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeFalse())
if exists {
Skip("Migration table exists; drop it to run this test")
}

_, err := sess.ShouldRunMigration("foo")
_, err := sess.ShouldRunMigration(randomString())
Expect(err).NotTo(HaveOccurred())

exists, err = migrationTableExists()
Expand All @@ -137,18 +137,19 @@ var _ = Describe("Persistence", func() {

Context("when migrations were not run", func() {
It("returns true", func() {
should, err := sess.ShouldRunMigration("foo")
should, err := sess.ShouldRunMigration(randomString())
Expect(err).NotTo(HaveOccurred())
Expect(should).To(BeTrue())
})
})

Context("after migration was run", func() {
It("returns false", func() {
err := sess.DidRunMigration("foo")
name := randomString()
err := sess.DidRunMigration(name)
Expect(err).NotTo(HaveOccurred())

should, err := sess.ShouldRunMigration("foo")
should, err := sess.ShouldRunMigration(name)
Expect(err).NotTo(HaveOccurred())
Expect(should).To(BeFalse())
})
Expand All @@ -163,8 +164,6 @@ var _ = Describe("Persistence", func() {
var (
err error
)
err = dropTestingKeyspace()
Expect(err).NotTo(HaveOccurred())
s, err := provider.MigrationSession()
Expect(err).NotTo(HaveOccurred())
defer s.Close()
Expand All @@ -179,7 +178,7 @@ var _ = Describe("Persistence", func() {
})
Context("when there are no events", func() {
It("returns empty array", func() {
es, err := sess.ReadEvents("test-stream", "00000000-0000-1000-8080-808080808080", 10)
es, err := sess.ReadEvents(randomString(), "00000000-0000-1000-8080-808080808080", 10)
Expect(err).NotTo(HaveOccurred())
Expect(es).To(BeEmpty())
})
Expand All @@ -189,18 +188,20 @@ var _ = Describe("Persistence", func() {
err error
events []types.Event
cursor string
stream string
)
BeforeEach(func() {
err = sess.WriteEvent("test-stream", []byte(`{ "a": 1 }`), make(map[string]string))
stream = randomString()
err = sess.WriteEvent(stream, []byte(`{ "a": 1 }`), make(map[string]string))
Expect(err).NotTo(HaveOccurred())
err = sess.WriteEvent("test-stream", []byte(`{ "a": 2 }`), make(map[string]string))
err = sess.WriteEvent(stream, []byte(`{ "a": 2 }`), make(map[string]string))
Expect(err).NotTo(HaveOccurred())
cursor = "00000000-0000-1000-8080-808080808080"
events, err = sess.ReadEvents("test-stream", cursor, 10)
events, err = sess.ReadEvents(stream, cursor, 10)
Expect(err).NotTo(HaveOccurred())
})
JustBeforeEach(func() {
events, err = sess.ReadEvents("test-stream", cursor, 10)
events, err = sess.ReadEvents(stream, cursor, 10)
Expect(err).NotTo(HaveOccurred())
})
It("returns non-empty array", func() {
Expand Down
47 changes: 47 additions & 0 deletions scripts/run_cassandra.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

NAME=archai-cassandra
IMAGE=cassandra:3

PORTS="9042 9160"
PUBLISH=""
for p in $PORTS
do
PUBLISH="$PUBLISH --publish 127.0.0.1:$p:$p"
done

START="docker start $NAME"
RUN="docker run -d $PUBLISH --name $NAME $IMAGE"
KILL="docker kill $NAME"
RM="docker rm $NAME"
BUILD="docker build -t $NAME ."

echo START=$START
echo RUN=$RUN
echo RM=$RM
echo KILL=$KILL
echo BUILD=$BUILD

TARGET=${1:-default}

case $TARGET in
start)
$START
;;
run)
$RUN
;;
default)
$START || $RUN
;;
stop)
$KILL
$RM
;;
build)
$BUILD
;;
*)
echo "$0 (start|run|stop|build)"
;;
esac

0 comments on commit 0b4545a

Please sign in to comment.