Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"google.golang.org/grpc/credentials/insecure"
"log"
"regexp"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -118,12 +120,15 @@ func ExampleRepository_Write() {
if err != nil {
log.Fatalln(err)
}

fmt.Println("cells-for_contact-101")
for _, family := range row {
for _, cell := range family {
fmt.Println(cell.Column, string(cell.Value))
}
}

fmt.Println("cells-for_contact-102")
row, err = tbl.ReadRow(ctx, "contact-102")
if err != nil {
log.Fatalln(err)
Expand All @@ -138,25 +143,29 @@ func ExampleRepository_Write() {
if err != nil {
log.Fatalln(err)
}
fmt.Println("mapped-event-for_contact-102")
for _, event := range readSet.Events["front"] {
fmt.Println(event.Date.UTC())
fmt.Println(event.RowKey)
fmt.Println(event.Cells["event_type"])
fmt.Println(event.Cells["device_type"])
}
// Output:
// front:11 1
// front:12 1
// front:13 1
// cells-for_contact-101
// front:d 1
// front:d 1
// front:d 1
// front:e 13
// front:e 12
// front:e 11
// front:u https://example.org/some/product
// front:u https://example.org/some/product
// front:u https://example.org/some/product
// front:12 1
// cells-for_contact-102
// front:d 2
// front:e 12
// front:u https://example.org/some/product
// mapped-event-for_contact-102
// 2018-01-01 00:02:00 +0000 UTC
// contact-102
// add_to_cart
Expand Down Expand Up @@ -239,7 +248,7 @@ func ExampleRepository_Search() {
if err != nil {
log.Fatalln(err)
}
events := sortByDate(readSet.Events["front"])
events := sortByContactID(readSet.Events["front"])
for _, event := range events {
fmt.Println(event.Date.UTC())
fmt.Println(event.RowKey)
Expand All @@ -248,20 +257,22 @@ func ExampleRepository_Search() {
}

// Output:
// 2018-01-01 00:00:00 +0000 UTC
// 2018-01-01 00:02:00 +0000 UTC
// contactx-101
// page_view
// purchase
// Smartphone
// 2018-01-01 00:02:00 +0000 UTC
// contactx-102
// add_to_cart
// Computer
}

func sortByDate(events []*data.Event) []*data.Event {
func sortByContactID(events []*data.Event) []*data.Event {
for i := 0; i < len(events); i++ {
iID := extractID(events[i].RowKey)
for j := 0; j < len(events); j++ {
if events[i].Date.Unix() < events[j].Date.Unix() {
jID := extractID(events[j].RowKey)
if iID < jID {
perm := events[j]
events[j] = events[i]
events[i] = perm
Expand All @@ -271,6 +282,29 @@ func sortByDate(events []*data.Event) []*data.Event {
return events
}

func TestExtractID(t *testing.T) {
l := "contact-113"
i := extractID(l)
if i != 113 {
t.Fatalf("unexpected value: %+v", i)
}
}

func extractID(literalID string) int {
re := regexp.MustCompile("[0-9]+")
m := re.FindAllString(literalID, 1)
out := 0
for _, f := range m {
i, err := strconv.Atoi(f)
if err != nil {
log.Fatalln(err)
return out
}
out = i
}
return out
}

func ExampleRepository_ReadLast() {
ctx := context.Background()
client := getBigTableClient(ctx)
Expand Down Expand Up @@ -339,9 +373,8 @@ func ExampleRepository_ReadLast() {
fmt.Println(event.Cells["event_type"])
fmt.Println(event.Cells["device_type"])
}

// Output:
// add_to_cart
//
// purchase
// Smartphone
}
Expand Down Expand Up @@ -684,9 +717,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
},
{
Row: row,
Column: "11",
Column: "e",
Timestamp: t1,
Value: []byte("1"),
Value: []byte("11"),
},
{
Row: row,
Expand All @@ -702,9 +735,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
},
{
Row: row,
Column: "12",
Column: "e",
Timestamp: t2,
Value: []byte("1"),
Value: []byte("12"),
},
{
Row: row,
Expand All @@ -720,9 +753,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
},
{
Row: row,
Column: "13",
Column: "e",
Timestamp: t3,
Value: []byte("1"),
Value: []byte("13"),
},
},
}
Expand Down Expand Up @@ -791,12 +824,12 @@ func generateMutations(numEvents int) []*bigtable.Mutation {
t := bigtable.Time(time.Now().Add(-time.Duration(i) * time.Minute))
mut.Set("front", "u", t, []byte(fmt.Sprintf("https://www.example.com/products/%d", mod)))
switch mod {
case 1, 2:
mut.Set("front", "12", t, []byte("1"))
case 2:
mut.Set("front", "e", t, []byte("12"))
case 3:
mut.Set("front", "13", t, []byte("1"))
mut.Set("front", "e", t, []byte("13"))
default:
mut.Set("front", "11", t, []byte("1"))
mut.Set("front", "e", t, []byte("11"))
}
mut.Set("front", "d", t, []byte(fmt.Sprintf("%d", 1+(i%2))))
data = append(data, mut)
Expand Down
8 changes: 3 additions & 5 deletions repository/testdata/mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
"1": "Smartphone",
"2": "Computer"
}
}
},
"reversed": [
{
},
"e": {
"name": "event_type",
"values": {
"11": "page_view",
"12": "add_to_cart",
"13": "purchase"
}
}
]
}
}