dialect/sql: add hasneigbors for predicates (#184)

Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/184

This diff is part of the ongoing effort for moving the sql logic out from codegen.

Reviewed By: alexsn

Differential Revision: D18656817

fbshipit-source-id: 71001d37eb78032414c2f83f200d3011b50c114b
This commit is contained in:
Ariel Mashraki
2019-11-24 03:26:03 -08:00
committed by Facebook Github Bot
parent c355ed49be
commit 2543d90167
2 changed files with 92 additions and 0 deletions

View File

@@ -311,3 +311,61 @@ JOIN
})
}
}
func TestHasNeighbors(t *testing.T) {
tests := []struct {
name string
step *Step
selector *Selector
wantQuery string
}{
{
name: "O2O/1type",
step: func() *Step {
// A nodes table; linked-list (next->prev). The "prev"
// node holds association pointer. The neighbors query
// here checks if a node "has-next".
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.Columns = []string{"prev_id"}
return step
}(),
selector: Select("*").From(Table("nodes")),
wantQuery: "SELECT * FROM `nodes` WHERE `nodes`.`id` IN (SELECT `nodes`.`prev_id` FROM `nodes` WHERE `nodes`.`prev_id` IS NOT NULL)",
},
{
name: "O2O/1type/inverse",
step: func() *Step {
// Same example as above, but the neighbors
// query checks if a node "has-previous".
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.Inverse = true
step.Edge.Table = "nodes"
step.Edge.Columns = []string{"prev_id"}
return step
}(),
selector: Select("*").From(Table("nodes")),
wantQuery: "SELECT * FROM `nodes` WHERE `nodes`.`prev_id` IS NOT NULL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
HasNeighbors(tt.selector, tt.step)
query, args := tt.selector.Query()
require.Equal(t, tt.wantQuery, query)
require.Empty(t, args)
})
}
}