| 22 | ) |
| 23 | |
| 24 | func TestNeighbors(t *testing.T) { |
| 25 | tests := []struct { |
| 26 | name string |
| 27 | input *Step |
| 28 | wantQuery string |
| 29 | wantArgs []any |
| 30 | }{ |
| 31 | { |
| 32 | name: "O2O/1type", |
| 33 | // Since the relation is on the same sql.Table, |
| 34 | // V used as a reference value. |
| 35 | input: NewStep( |
| 36 | From("users", "id", 1), |
| 37 | To("users", "id"), |
| 38 | Edge(O2O, false, "users", "spouse_id"), |
| 39 | ), |
| 40 | wantQuery: "SELECT * FROM `users` WHERE `spouse_id` = ?", |
| 41 | wantArgs: []any{1}, |
| 42 | }, |
| 43 | { |
| 44 | name: "O2O/1type/inverse", |
| 45 | input: NewStep( |
| 46 | From("nodes", "id", 1), |
| 47 | To("nodes", "id"), |
| 48 | Edge(O2O, true, "nodes", "prev_id"), |
| 49 | ), |
| 50 | wantQuery: "SELECT * FROM `nodes` JOIN (SELECT `prev_id` FROM `nodes` WHERE `id` = ?) AS `t1` ON `nodes`.`id` = `t1`.`prev_id`", |
| 51 | wantArgs: []any{1}, |
| 52 | }, |
| 53 | { |
| 54 | name: "O2M/1type", |
| 55 | input: NewStep( |
| 56 | From("users", "id", 1), |
| 57 | To("users", "id"), |
| 58 | Edge(O2M, false, "users", "parent_id"), |
| 59 | ), |
| 60 | wantQuery: "SELECT * FROM `users` WHERE `parent_id` = ?", |
| 61 | wantArgs: []any{1}, |
| 62 | }, |
| 63 | { |
| 64 | name: "O2O/2types", |
| 65 | input: NewStep( |
| 66 | From("users", "id", 2), |
| 67 | To("card", "id"), |
| 68 | Edge(O2O, false, "cards", "owner_id"), |
| 69 | ), |
| 70 | wantQuery: "SELECT * FROM `card` WHERE `owner_id` = ?", |
| 71 | wantArgs: []any{2}, |
| 72 | }, |
| 73 | { |
| 74 | name: "O2O/2types/inverse", |
| 75 | input: NewStep( |
| 76 | From("cards", "id", 2), |
| 77 | To("users", "id"), |
| 78 | Edge(O2O, true, "cards", "owner_id"), |
| 79 | ), |
| 80 | wantQuery: "SELECT * FROM `users` JOIN (SELECT `owner_id` FROM `cards` WHERE `id` = ?) AS `t1` ON `users`.`id` = `t1`.`owner_id`", |
| 81 | wantArgs: []any{2}, |