|
| 1 | +package translator |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/bytebase/gomongo/types" |
| 8 | +) |
| 9 | + |
| 10 | +// TestParseSingleStatementContract pins the strict single-statement contract: |
| 11 | +// parse errors and extra statements are rejected rather than silently dropped |
| 12 | +// (BYT-9950: dropped statements made migrations appear to skip commands). |
| 13 | +func TestParseSingleStatementContract(t *testing.T) { |
| 14 | + t.Run("valid single statement", func(t *testing.T) { |
| 15 | + op, err := Parse(`db.users.find({ name: "alice" })`) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("unexpected error: %v", err) |
| 18 | + } |
| 19 | + if op.OpType != types.OpFind { |
| 20 | + t.Errorf("expected OpFind, got %v", op.OpType) |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + t.Run("comment-only input is a no-op", func(t *testing.T) { |
| 25 | + op, err := Parse("// just a comment\n") |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("unexpected error: %v", err) |
| 28 | + } |
| 29 | + if op.OpType != types.OpNoOp { |
| 30 | + t.Errorf("expected OpNoOp, got %v", op.OpType) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("invalid statement is rejected", func(t *testing.T) { |
| 35 | + _, err := Parse("db.users.find({ name: })") |
| 36 | + var pe *ParseError |
| 37 | + if !errors.As(err, &pe) { |
| 38 | + t.Fatalf("expected *ParseError, got %T: %v", err, err) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("valid statement followed by invalid is rejected", func(t *testing.T) { |
| 43 | + _, err := Parse("db.users.find();\nthis is not mongosh") |
| 44 | + var pe *ParseError |
| 45 | + if !errors.As(err, &pe) { |
| 46 | + t.Fatalf("expected *ParseError, got %T: %v", err, err) |
| 47 | + } |
| 48 | + if pe.Line != 2 { |
| 49 | + t.Errorf("expected error on line 2, got %d", pe.Line) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("multiple valid statements are rejected", func(t *testing.T) { |
| 54 | + _, err := Parse("db.users.find();\ndb.orders.find();") |
| 55 | + var pe *ParseError |
| 56 | + if !errors.As(err, &pe) { |
| 57 | + t.Fatalf("expected *ParseError, got %T: %v", err, err) |
| 58 | + } |
| 59 | + if pe.Line != 2 { |
| 60 | + t.Errorf("expected error on line 2, got %d", pe.Line) |
| 61 | + } |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// TestParseCreateIndexArithmeticTTL pins the BYT-9950 statement: arithmetic |
| 66 | +// expressions are not supported, and Parse must surface the error instead of |
| 67 | +// silently dropping the statement. |
| 68 | +func TestParseCreateIndexArithmeticTTL(t *testing.T) { |
| 69 | + _, err := Parse(`db.cs_customer_frequency.createIndex( |
| 70 | + { trans_date: 1 }, |
| 71 | + { expireAfterSeconds: 90 * 24 * 60 * 60, name: "cs_customer_frequency_idx2" } |
| 72 | +);`) |
| 73 | + var pe *ParseError |
| 74 | + if !errors.As(err, &pe) { |
| 75 | + t.Fatalf("expected *ParseError, got %T: %v", err, err) |
| 76 | + } |
| 77 | +} |
0 commit comments