|
| 1 | +package plan |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestMultiBufferProduct(t *testing.T) { |
| 8 | + db, planner, _ := createTester(t) |
| 9 | + |
| 10 | + // Create a transaction |
| 11 | + tx, err := db.NewTx() |
| 12 | + if err != nil { |
| 13 | + t.Fatalf("Failed to create transaction: %v", err) |
| 14 | + } |
| 15 | + defer tx.Commit() |
| 16 | + |
| 17 | + // Create a query plan for todo table |
| 18 | + plan1, err := planner.CreateQueryPlan("SELECT id, title FROM todo", tx) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to create query plan: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + // create a query plan for todo_detail table |
| 24 | + plan2, err := planner.CreateQueryPlan("SELECT todo_id, detail, some_score FROM todo_detail", tx) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Failed to create query plan for todo_detail: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Create a MultiBufferProductPlan |
| 30 | + multiBufferProductPlan := NewMultiBufferProductPcan(tx, plan1, plan2) |
| 31 | + if multiBufferProductPlan == nil { |
| 32 | + t.Fatal("Failed to create MultiBufferProductPlan") |
| 33 | + } |
| 34 | + |
| 35 | + // check schema |
| 36 | + if len(multiBufferProductPlan.Schema().Fields()) != 5 { |
| 37 | + t.Errorf("Expected 5 fields, got %d", len(multiBufferProductPlan.Schema().Fields())) |
| 38 | + } |
| 39 | + if !multiBufferProductPlan.Schema().HasField("id") || |
| 40 | + !multiBufferProductPlan.Schema().HasField("title") || |
| 41 | + !multiBufferProductPlan.Schema().HasField("todo_id") || |
| 42 | + !multiBufferProductPlan.Schema().HasField("detail") { |
| 43 | + t.Error("Schema does not contain expected fields") |
| 44 | + } |
| 45 | + |
| 46 | + // Open the scan |
| 47 | + scan, err := multiBufferProductPlan.Open() |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to open scan: %v", err) |
| 50 | + } |
| 51 | + defer scan.Close() |
| 52 | + |
| 53 | + // check scan length |
| 54 | + length := 0 |
| 55 | + for { |
| 56 | + ok, err := scan.Next() |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("Failed to get next record: %v", err) |
| 59 | + } |
| 60 | + if !ok { |
| 61 | + break |
| 62 | + } |
| 63 | + length++ |
| 64 | + } |
| 65 | + if length != 12 { |
| 66 | + t.Errorf("Expected 6 records, got %d", length) |
| 67 | + } |
| 68 | +} |
0 commit comments