<commit_before>package ngorm

import (
	"reflect"
	"sort"
	"testing"
)

type Cat struct {
	ID   int64
	Name string
	Toy  Toy `gorm:"polymorphic:Owner;"`
}

type Dog struct {
	ID   int64
	Name string
	Toys []Toy `gorm:"polymorphic:Owner;"`
}

type Hamster struct {
	ID           int
	Name         string
	PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"`
	OtherToy     Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"`
}

type Toy struct {
	ID        int
	Name      string
	OwnerID   int64
	OwnerType string
}

var compareToys = func(toys []Toy, contents []string) bool {
	var toyContents []string
	for _, toy := range toys {
		toyContents = append(toyContents, toy.Name)
	}
	sort.Strings(toyContents)
	sort.Strings(contents)
	return reflect.DeepEqual(toyContents, contents)
}

func TestPolymorphic(t *testing.T) {
	for _, d := range allTestDB() {
		runWrapDB(t, d, testPolymorphic,
			&Cat{}, &Dog{}, &Hamster{}, &Toy{})

	}
}

func testPolymorphic(t *testing.T, db *DB) {
	_, err := db.Automigrate(&Cat{}, &Dog{}, &Hamster{}, &Toy{})
	if err != nil {
		t.Fatal(err)
	}

	cat := Cat{Name: "Mr. Bigglesworth", Toy: Toy{Name: "cat toy"}}
	dog := Dog{Name: "Pluto", Toys: []Toy{{Name: "dog toy 1"}, {Name: "dog toy 2"}}}
	db.Begin().Save(&cat)
	db.Begin().Save(&dog)

	a, err := db.Begin().Model(&cat).Association("Toy")
	if err != nil {
		t.Fatal(err)
	}
	count, err := a.Count()
	if err != nil {
		t.Fatal(err)
	}
	if count != 1 {
		t.Errorf("expected 1 got %d", count)
	}

	a, err = db.Begin().Model(&dog).Association("Toys")
	if err != nil {
		t.Fatal(err)
	}
	count, err = a.Count()
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Errorf("expected 2 got %d", count)
	}

	// Query
	var catToys []Toy
	err = db.Begin().Model(&cat).Related(&catToys, "Toy")
	if err != nil {
		t.Error(err)
	}
	// if db.Model(&cat).Related(&catToys, "Toy").RecordNotFound() {
	// 	t.Errorf("Did not find any has one polymorphic association")
	// } else if len(catToys) != 1 {
	// 	t.Errorf("Should have found only one polymorphic has one association")
	// } else if catToys[0].Name != cat.Toy.Name {
	// 	t.Errorf("Should have found the proper has one polymorphic association")
	// }

	// var dogToys []Toy
	// if db.Model(&dog).Related(&dogToys, "Toys").RecordNotFound() {
	// 	t.Errorf("Did not find any polymorphic has many associations")
	// } else if len(dogToys) != len(dog.Toys) {
	// 	t.Errorf("Should have found all polymorphic has many associations")
	// }

	// var catToy Toy
	// db.Model(&cat).Association("Toy").Find(&catToy)
	// if catToy.Name != cat.Toy.Name {
	// 	t.Errorf("Should find has one polymorphic association")
	// }

	// var dogToys1 []Toy<commit_msg>[ngorm] more tests<commit_after>package ngorm

import (
	"reflect"
	"sort"
	"testing"
)

type Cat struct {
	ID   int64
	Name string
	Toy  Toy `gorm:"polymorphic:Owner;"`
}

type Dog struct {
	ID   int64
	Name string
	Toys []Toy `gorm:"polymorphic:Owner;"`
}

type Hamster struct {
	ID           int
	Name         string
	PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"`
	OtherToy     Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"`
}

type Toy struct {
	ID        int
	Name      string
	OwnerID   int64
	OwnerType string
}

var compareToys = func(toys []Toy, contents []string) bool {
	var toyContents []string
	for _, toy := range toys {
		toyContents = append(toyContents, toy.Name)
	}
	sort.Strings(toyContents)
	sort.Strings(contents)
	return reflect.DeepEqual(toyContents, contents)
}

func TestPolymorphic(t *testing.T) {
	for _, d := range allTestDB() {
		runWrapDB(t, d, testPolymorphic,
			&Cat{}, &Dog{}, &Hamster{}, &Toy{})

	}
}

func testPolymorphic(t *testing.T, db *DB) {
	_, err := db.Automigrate(&Cat{}, &Dog{}, &Hamster{}, &Toy{})
	if err != nil {
		t.Fatal(err)
	}

	cat := Cat{Name: "Mr. Bigglesworth", Toy: Toy{Name: "cat toy"}}
	dog := Dog{Name: "Pluto", Toys: []Toy{{Name: "dog toy 1"}, {Name: "dog toy 2"}}}
	db.Begin().Save(&cat)
	db.Begin().Save(&dog)

	a, err := db.Begin().Model(&cat).Association("Toy")
	if err != nil {
		t.Fatal(err)
	}
	count, err := a.Count()
	if err != nil {
		t.Fatal(err)
	}
	if count != 1 {
		t.Errorf("expected 1 got %d", count)
	}

	a, err = db.Begin().Model(&dog).Association("Toys")
	if err != nil {
		t.Fatal(err)
	}
	count, err = a.Count()
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Errorf("expected 2 got %d", count)
	}

	// Query
	var catToys []Toy
	err = db.Begin().Model(&cat).Related(&catToys, "Toy")
	if err != nil {
		t.Error(err)
	}
	if len(catToys) != 1 {
		t.Errorf("expected 1 got %d", len(catToys))
	}
	if catToys[0].Name != cat.Toy.Name {
		t.Errorf("expected %s got %s", cat.Toy.Name, catToys[0].Name)
	}

	var dogToys []Toy
	err = db.Begin().Model(&dog).Related(&dogToys, "Toys")
	if err != nil {
		t.Error(err)
	}
	if len(dogToys) != len(dog.Toys) {
		t.Errorf("expected %d got %d", len(dog.Toys), len(dogToys))
	}

	var catToy Toy
	a, err = db.Begin().Model(&cat).Association("Toy")
	if err != nil {
		t.Fatal(err)
	}
	err = a.Find(&catToy)
	if err != nil {
		t.Error(err)
	}
	if catToy.Name != cat.Toy.Name {
		t.Errorf("expected %s got %s", cat.Toy.Name, catToy.Name)
	}
	// var dogToys1 []Toy
{'fraction_non_alphanumeric': 0.17767175572519084, 'fraction_numerical': 0.0061068702290076335, 'mean_word_length': 3.533737024221453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5588069', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2079, 'n_words': 498}