63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package parser
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestJSONParser(t *testing.T) {
|
|
p := &JSONParser{}
|
|
body := []byte(`{"event":"trade.open","data":{"symbol":"BTC","price":65000}}`)
|
|
msg, err := p.Parse(body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if msg.Event != "trade.open" {
|
|
t.Errorf("expected event 'trade.open', got '%s'", msg.Event)
|
|
}
|
|
if msg.Data["symbol"] != "BTC" {
|
|
t.Errorf("expected symbol 'BTC', got '%v'", msg.Data["symbol"])
|
|
}
|
|
}
|
|
|
|
func TestJSONParser_NoDataKey(t *testing.T) {
|
|
p := &JSONParser{}
|
|
body := []byte(`{"event":"alert","symbol":"ETH"}`)
|
|
msg, err := p.Parse(body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if msg.Data["symbol"] != "ETH" {
|
|
t.Errorf("expected symbol 'ETH' in data, got '%v'", msg.Data["symbol"])
|
|
}
|
|
}
|
|
|
|
func TestRegexParser(t *testing.T) {
|
|
p, err := NewRegexParser(`(?P<event>[\w.]+) (?P<message>.+)`)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
body := []byte("trade.open BTC long at 65000")
|
|
msg, err := p.Parse(body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if msg.Event != "trade.open" {
|
|
t.Errorf("expected event 'trade.open', got '%s'", msg.Event)
|
|
}
|
|
if msg.Data["message"] != "BTC long at 65000" {
|
|
t.Errorf("expected message, got '%v'", msg.Data["message"])
|
|
}
|
|
}
|
|
|
|
func TestTextParser(t *testing.T) {
|
|
p := &TextParser{}
|
|
body := []byte("raw notification text here")
|
|
msg, err := p.Parse(body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if msg.Data["Body"] != "raw notification text here" {
|
|
t.Errorf("expected Body in data, got '%v'", msg.Data["Body"])
|
|
}
|
|
}
|