func TestBuildInsertQuery(t *testing.T) {

	const expected = `insert into "event" ("key", "key_params", "created", "updated", "payload", "description", "importance", "origin", "entities", "other_references", "actors", "tags") values ($1, $2, $3, $4, $5, $6, $7, $8,ARRAY[$9, $10]::text[], ARRAY[]::text[], ARRAY[$11]::text[], ARRAY[]::text[]) returning "id", "created", "updated";`

	e := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/moo"},
		nil,
		[]string{"someone"},
		nil)

	queryTest(t, expected, e, buildInsertQuery)

}
func TestBuildUpdateQuery(t *testing.T) {

	const expected = `update "event" set "key" = $1, "key_params" = $2, "payload" = $3, "description" = $4, "importance" = $5, "origin" = $6, "entities" = ARRAY[$7, $8]::text[], "other_references" = ARRAY[]::text[], "actors" = ARRAY[$9]::text[], "tags" = ARRAY[]::text[] where "id" = $10 returning "updated";`

	e := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/moo"},
		nil,
		[]string{"someone"},
		nil)

	e.ID = 123

	queryTest(t, expected, e, buildUpdateQuery)

}
示例#3
0
func TestAMQPFeed(t *testing.T) {

	_, err := amqp.Dial(uri)
	if err != nil {
		t.Skip("Skipping amqp test, no rabbitmq available")
	}

	c, err := NewConsumer(uri, exchange, exchangeType, queue, bindingKey, consumerTag, true, true)
	if err != nil {
		t.Logf("%s", err)
	}

	t.Logf("%v", c)

	e := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/bar"},
		[]string{"someone"},
		nil,
		nil)

	err = publish(uri, exchange, exchangeType, bindingKey, e, true, true, true)

	if err != nil {
		t.Logf("%s", err)
	}

	i := <-c.Updates()

	t.Logf("%v", i)

	if c.Close() != nil {
		t.Logf("%s", err)
	}
}
示例#4
0
func TestWebSocketBroadcaster(t *testing.T) {

	url := fmt.Sprintf("ws://%s%s", serverAddr, "/ws")
	conn, err := websocket.Dial(url, "", "http://localhost/")
	if err != nil {
		t.Errorf("WebSocket handshake error: %v", err)
		return
	}

	q := straumur.Query{}
	q.Entities = []string{"ns/moo"}
	t.Logf("Query filter: %+v", q)
	websocket.JSON.Send(conn, q)

	time.Sleep(300 * time.Millisecond)

	e := straumur.NewEvent(
		"myapp.user.login",
		nil,
		nil,
		"User foobar logged in",
		3,
		"myapp",
		[]string{"ns/foo", "ns/moo"},
		nil,
		nil,
		nil)

	broadcaster.Broadcast(e)

	var event straumur.Event
	if err := websocket.JSON.Receive(conn, &event); err != nil {
		t.Errorf("Read: %v", err)
	}

	incoming := make(chan straumur.Event)
	go readEvents(conn, incoming)

	filtered := straumur.NewEvent(
		"Should filter",
		nil,
		nil,
		"This event should be filtered",
		3,
		"myapp",
		[]string{"ns/foo", "ns/boo"},
		nil,
		nil,
		nil)

	if q.Match(*filtered) == true {
		t.Errorf("Query %+v should not pass %+v", q, filtered)
	}

	broadcaster.Broadcast(filtered)

	broadcaster.Broadcast(straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"This event should pass",
		3,
		"myapp",
		[]string{"ns/foo", "ns/moo"},
		nil,
		nil,
		nil))

	ev := <-incoming

	if ev.Key != "foo.bar" {
		t.Errorf("Unexpected %s", ev)
	}

}
示例#5
0
func startServer() {

	errChan = make(chan error)
	d := straumur.NewLocalMemoryStore()

	firstEvent = straumur.NewEvent(
		"myapp.user.login",
		nil,
		nil,
		"User foobar logged in",
		3,
		"myapp",
		[]string{"user/foo", "ns/moo"},
		nil,
		[]string{"actor1", "actor2"},
		nil)

	secondEvent = straumur.NewEvent(
		"myapp.user.logout",
		nil,
		nil,
		"User foobar logged out",
		2,
		"myapp",
		[]string{"user/foo", "ns/moo"},
		nil,
		[]string{"actor1", "actor2", "actor3"},
		nil)

	err := d.Save(firstEvent)
	if err != nil {
		panic(err)
	}

	err = d.Save(secondEvent)
	if err != nil {
		panic(err)
	}

	rest := NewRESTService(d, errChan)
	rest.Store = sessions.NewCookieStore([]byte("something-very-secret"))

	http.Handle("/", rest)

	server := httptest.NewServer(nil)
	serverAddr = server.Listener.Addr().String() + "/api"
	broadcaster = rest.WsServer

	go func() {
		for {
			select {
			case err := <-errChan:
				log.Fatalf("errChan: %+v", err)
			}
		}
	}()

	go func() {
		for {
			select {
			case event := <-rest.Updates():
				d.Save(event)
			}
		}
	}()

	log.Print("Test Server running on ", serverAddr)
	client = http.DefaultClient
	jar, err := cookiejar.New(nil)
	if err != nil {
		panic(err)
	}
	client.Jar = jar
	log.Print("Test Client created")
}
示例#6
0
func TestDuplicateInsertion(t *testing.T) {

	const connection = "dbname=teststream host=localhost sslmode=disable"

	db, err := sql.Open("postgres", connection)
	if err != nil {
		t.Error("Error:", err)
		return
	}

	//truncate table
	clear := func() {
		_, err := db.Exec(`TRUNCATE TABLE event RESTART IDENTITY;`)
		if err != nil {
			t.Fatal(err)
		}
	}

	clear()

	now := time.Now()

	e := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/moo"},
		nil,
		[]string{"someone"},
		nil)
	e.Created = now

	e2 := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/moo"},
		nil,
		[]string{"someone"},
		nil)
	e2.Created = now

	e3 := straumur.NewEvent(
		"foo.bar",
		nil,
		nil,
		"My event",
		3,
		"mysystem",
		[]string{"ns/foo", "ns/moo"},
		nil,
		[]string{"someone"},
		nil)

	p, err := NewPostgresDataSource(connection)
	if err != nil {
		t.Fatal("PostgresDataSource has error:", err)
	}

	p.Save(e)
	p.Save(e2)
	p.Save(e3)
}