func TestTable_AddRelationship_oneToOneSelfReferencing(t *testing.T) { user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())) user.AddRelationship(pqt.OneToOne( pqt.SelfReference(), pqt.WithInversedName("child"), pqt.WithOwnerName("parent"), )) if len(user.OwnedRelationships) != 1 { t.Fatalf("user should have 1 owned relationship, but has %d", len(user.OwnedRelationships)) } if user.OwnedRelationships[0].OwnerName != "parent" { t.Errorf("user relationship to user should be mapped by parent") } if user.OwnedRelationships[0].OwnerTable != user { t.Errorf("user relationship to user should be mapped by user table") } if user.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToOne { t.Errorf("user relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToOne, user.OwnedRelationships[0].Type) } if len(user.InversedRelationships) != 0 { t.Fatalf("user should have 0 inversed relationship, but has %d", len(user.InversedRelationships)) } }
func TestWithColumnName(t *testing.T) { icn := "author" t1 := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())) t2 := pqt.NewTable("comment") t2.AddRelationship(pqt.OneToOne(t1, pqt.WithColumnName(icn))) if len(t1.InversedRelationships) != 0 { t.Fatalf("user table should have exactly 0 relationship, got %d", len(t1.InversedRelationships)) } if len(t2.OwnedRelationships) != 1 { t.Fatalf("comment table should have exactly 1 relationship, got %d", len(t2.OwnedRelationships)) } var exists bool for _, c := range t2.Columns { if c.Name == icn { exists = true break } } if !exists { t.Errorf("comment table should have column with name %s", icn) } }
func TestTable_AddRelationship_oneToOneUnidirectional(t *testing.T) { user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())) userDetail := pqt.NewTable("user_detail").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())). AddRelationship(pqt.OneToOne( user, pqt.WithInversedName("user"), pqt.WithOwnerName("details"), )) if len(user.InversedRelationships) != 0 { t.Fatalf("user should have 0 relationship, but has %d", len(user.InversedRelationships)) } if len(userDetail.OwnedRelationships) != 1 { t.Fatalf("user_detail should have 1 relationship, but has %d", len(userDetail.OwnedRelationships)) } if userDetail.OwnedRelationships[0].InversedName != "user" { t.Errorf("user_detail relationship to user should be mapped by user") } if userDetail.OwnedRelationships[0].InversedTable != user { t.Errorf("user_detail relationship to user should be mapped by user table") } if userDetail.OwnedRelationships[0].Type != pqt.RelationshipTypeOneToOne { t.Errorf("user_detail relationship to user should be %d, but is %d", pqt.RelationshipTypeOneToOne, userDetail.OwnedRelationships[0].Type) } }