package main import ( "context" "encoding/json" "testing" "time" "github.com/breel-render/spoc-bot-vr/model" ) func TestModelToPersistenceProcessor(t *testing.T) { t.Parallel() ctx, can := context.WithTimeout(context.Background(), time.Second*10) defer can() d := NewTestDriver(t) s, _ := NewStorage(ctx, d) process := newModelToPersistenceProcess(s) _, _ = ctx, process inputModels := Models{ Event: model.Event{ID: "event", Asset: "event-asset"}, //Thread: {ID: "thread", Channel: "thread-channel"}, Message: model.Message{ID: "message", Plaintext: "message-plaintext"}, } input, _ := json.Marshal(inputModels) var outputModelIDs ModelIDs var n int if output, err := process(ctx, input); err != nil { t.Fatal(err) } else if err := json.Unmarshal(output, &outputModelIDs); err != nil { t.Fatal(err) } else if outputModelIDs != (ModelIDs{Event: "event", Message: "message"}) { t.Error(outputModelIDs) } if row := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM events`); row.Err() != nil { t.Error("cant count events:", row.Err()) } else if err := row.Scan(&n); err != nil { t.Error("cant count events:", err) } else if n != 1 { t.Error("bad event count:", n) } if row := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM threads`); row.Err() != nil { t.Error("cant count threads:", row.Err()) } else if err := row.Scan(&n); err != nil { t.Error("cant count threads:", err) } else if n != 0 { t.Error("bad thread count:", n) } if row := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM messages`); row.Err() != nil { t.Error("cant count messages:", row.Err()) } else if err := row.Scan(&n); err != nil { t.Error("cant count messages:", err) } else if n != 1 { t.Error("bad message count:", n) } }