From 6eb14bba9fcd44885dea964be86125df6c9472f5 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Wed, 6 Nov 2019 23:53:26 -0800 Subject: [PATCH] dialect/sql: add O2O tests for graph neighbors Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/145 Reviewed By: naor9991 Differential Revision: D18354409 fbshipit-source-id: 0327443cfb8cae6c2136cf6acc4057a17d0e3e0c --- dialect/sql/graph_test.go | 49 ++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/dialect/sql/graph_test.go b/dialect/sql/graph_test.go index 758ce35fa..1af7de664 100644 --- a/dialect/sql/graph_test.go +++ b/dialect/sql/graph_test.go @@ -18,20 +18,40 @@ func TestNeighbors(t *testing.T) { wantArgs []interface{} }{ { - name: "O2M/2types", + name: "O2O/1type", input: func() *Step { step := &Step{} + // Since the relation is on the same table, + // V used as a reference value. step.From.V = 1 step.From.Table = "users" step.From.Column = "id" - step.To.Table = "pets" + step.To.Table = "users" step.To.Column = "id" - step.Edge.Rel = O2M - step.Edge.Table = "pets" - step.Edge.Columns = []string{"owner_id"} + step.Edge.Rel = O2O + step.Edge.Table = "users" + step.Edge.Columns = []string{"spouse_id"} return step }(), - wantQuery: "SELECT * FROM `pets` WHERE `owner_id` = ?", + wantQuery: "SELECT * FROM `users` WHERE `spouse_id` = ?", + wantArgs: []interface{}{1}, + }, + { + name: "O2O/1type/inverse", + input: func() *Step { + step := &Step{} + step.From.V = 1 + step.From.Table = "nodes" + step.From.Column = "id" + step.To.Table = "nodes" + step.To.Column = "id" + step.Edge.Rel = O2O + step.Edge.Table = "nodes" + step.Edge.Inverse = true + step.Edge.Columns = []string{"prev_id"} + return step + }(), + wantQuery: "SELECT * FROM `nodes` JOIN (SELECT `prev_id` FROM `nodes` WHERE `id` = ?) AS `t1` ON `nodes`.`id` = `t1`.`prev_id`", wantArgs: []interface{}{1}, }, { @@ -51,6 +71,23 @@ func TestNeighbors(t *testing.T) { wantQuery: "SELECT * FROM `users` WHERE `parent_id` = ?", wantArgs: []interface{}{1}, }, + { + name: "O2M/2types", + input: func() *Step { + step := &Step{} + step.From.V = 1 + step.From.Table = "users" + step.From.Column = "id" + step.To.Table = "pets" + step.To.Column = "id" + step.Edge.Rel = O2M + step.Edge.Table = "pets" + step.Edge.Columns = []string{"owner_id"} + return step + }(), + wantQuery: "SELECT * FROM `pets` WHERE `owner_id` = ?", + wantArgs: []interface{}{1}, + }, { name: "M2O/2types/inverse", input: func() *Step {