refactor(stats): slim final daily mileage table
This commit is contained in:
@@ -559,7 +559,7 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
t.Fatalf("Append() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 12 {
|
||||
if len(exec.calls) != 16 {
|
||||
t.Fatalf("exec calls = %d", len(exec.calls))
|
||||
}
|
||||
for i, want := range []string{
|
||||
@@ -571,18 +571,23 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
t.Fatalf("schema call %d = %s, want %s", i, exec.calls[i].query, want)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"vehicle_key", "id BIGINT", "AUTO_INCREMENT", "created_at"} {
|
||||
for _, column := range []string{"vehicle_key", "AUTO_INCREMENT", "created_at", "first_total_mileage_km", "trusted_source_key", "trusted_phone", "trusted_source_endpoint", "sample_count"} {
|
||||
if strings.Contains(exec.calls[2].query, column) {
|
||||
t.Fatalf("schema should not include %s: %s", column, exec.calls[2].query)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"source_id BIGINT NULL", "daily_mileage_km", "latest_total_mileage_km", "KEY idx_source_id (source_id)"} {
|
||||
if !strings.Contains(exec.calls[2].query, column) {
|
||||
t.Fatalf("schema should include %s: %s", column, exec.calls[2].query)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(exec.calls[2].query, "PRIMARY KEY (vin, stat_date, protocol)") {
|
||||
t.Fatalf("daily mileage table should key by vin/stat_date/protocol: %s", exec.calls[2].query)
|
||||
}
|
||||
if strings.Contains(exec.calls[2].query, "KEY idx_vin (vin)") {
|
||||
t.Fatalf("daily mileage table should not keep redundant vin index covered by the primary key: %s", exec.calls[2].query)
|
||||
}
|
||||
projectCall := exec.calls[9]
|
||||
projectCall := exec.calls[13]
|
||||
if !strings.Contains(projectCall.query, "INSERT INTO vehicle_daily_mileage") {
|
||||
t.Fatalf("unexpected project sql: %s", projectCall.query)
|
||||
}
|
||||
@@ -592,21 +597,26 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
if strings.Contains(projectCall.query, "metric_key") || strings.Contains(projectCall.query, "metric_unit") {
|
||||
t.Fatalf("daily mileage upsert should not use generic metric columns: %s", projectCall.query)
|
||||
}
|
||||
if !strings.Contains(projectCall.query, "LEFT JOIN vehicle_data_source ds") {
|
||||
if !strings.Contains(projectCall.query, "JOIN vehicle_data_source ds") {
|
||||
t.Fatalf("project sql should join data source: %s", projectCall.query)
|
||||
}
|
||||
if !strings.Contains(projectCall.query, "trusted_source_key") {
|
||||
t.Fatalf("upsert should track trusted source: %s", projectCall.query)
|
||||
if !strings.Contains(projectCall.query, "ds.id") {
|
||||
t.Fatalf("project sql should write source id: %s", projectCall.query)
|
||||
}
|
||||
for _, forbidden := range []string{"trusted_source_key", "trusted_phone", "trusted_source_endpoint", "first_total_mileage_km", "sample_count)"} {
|
||||
if strings.Contains(projectCall.query, forbidden) {
|
||||
t.Fatalf("project sql should not write final-table field %q: %s", forbidden, projectCall.query)
|
||||
}
|
||||
}
|
||||
if got := projectCall.args[0]; got != "LNBVIN00000000002" {
|
||||
t.Fatalf("first upsert arg should be vin, got %#v", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(exec.calls[6].query, "INSERT INTO vehicle_data_source") {
|
||||
t.Fatalf("source upsert query missing: %s", exec.calls[6].query)
|
||||
if !strings.Contains(exec.calls[10].query, "INSERT INTO vehicle_data_source") {
|
||||
t.Fatalf("source upsert query missing: %s", exec.calls[10].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[7].query, "INSERT INTO vehicle_daily_mileage_source") {
|
||||
t.Fatalf("candidate upsert query missing: %s", exec.calls[7].query)
|
||||
if !strings.Contains(exec.calls[11].query, "INSERT INTO vehicle_daily_mileage_source") {
|
||||
t.Fatalf("candidate upsert query missing: %s", exec.calls[11].query)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user