MCPcopy
hub / github.com/ent/ent / TestHasNeighbors

Function TestHasNeighbors

dialect/sql/sqlgraph/graph_test.go:445–636  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

443}
444
445func TestHasNeighbors(t *testing.T) {
446 tests := []struct {
447 name string
448 step *Step
449 selector *sql.Selector
450 wantQuery string
451 }{
452 {
453 name: "O2O/1type",
454 // A nodes sql.Table; linked-list (next->prev). The "prev"
455 // node holds association pointer. The neighbors query
456 // here checks if a node "has-next".
457 step: NewStep(
458 From("nodes", "id"),
459 To("nodes", "id"),
460 Edge(O2O, false, "nodes", "prev_id"),
461 ),
462 selector: sql.Select("*").From(sql.Table("nodes")),
463 wantQuery: "SELECT * FROM `nodes` WHERE EXISTS (SELECT `nodes_edge`.`prev_id` FROM `nodes` AS `nodes_edge` WHERE `nodes`.`id` = `nodes_edge`.`prev_id`)",
464 },
465 {
466 name: "O2O/1type/inverse",
467 // Same example as above, but the neighbors
468 // query checks if a node "has-previous".
469 step: NewStep(
470 From("nodes", "id"),
471 To("nodes", "id"),
472 Edge(O2O, true, "nodes", "prev_id"),
473 ),
474 selector: sql.Select("*").From(sql.Table("nodes")),
475 wantQuery: "SELECT * FROM `nodes` WHERE `nodes`.`prev_id` IS NOT NULL",
476 },
477 {
478 name: "O2M/2type2",
479 step: NewStep(
480 From("users", "id"),
481 To("pets", "id"),
482 Edge(O2M, false, "pets", "owner_id"),
483 ),
484 selector: sql.Select("*").From(sql.Table("users")),
485 wantQuery: "SELECT * FROM `users` WHERE EXISTS (SELECT `pets`.`owner_id` FROM `pets` WHERE `users`.`id` = `pets`.`owner_id`)",
486 },
487 {
488 name: "M2O/2type2",
489 step: NewStep(
490 From("pets", "id"),
491 To("users", "id"),
492 Edge(M2O, true, "pets", "owner_id"),
493 ),
494 selector: sql.Select("*").From(sql.Table("pets")),
495 wantQuery: "SELECT * FROM `pets` WHERE `pets`.`owner_id` IS NOT NULL",
496 },
497 {
498 name: "M2M/2types",
499 step: NewStep(
500 From("users", "id"),
501 To("groups", "id"),
502 Edge(M2M, false, "user_groups", "user_id", "group_id"),

Callers

nothing calls this directly

Calls 14

SelectFunction · 0.92
TableFunction · 0.92
NewStepFunction · 0.85
HasNeighborsFunction · 0.85
RunMethod · 0.80
EmptyMethod · 0.80
FromFunction · 0.70
ToFunction · 0.70
EdgeFunction · 0.70
QueryMethod · 0.65
FromMethod · 0.45
SchemaMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…