<commit_before>// Copyright 2016 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package bolthold_test

import (
	"reflect"
	"testing"

	"github.com/timshannon/bolthold"
)

type Nested struct {
	Key int
	Embed
	L1      Nest
	L2      Level2
	Pointer *Nest
}

type Embed struct {
	Color string
}

type Nest struct {
	Name string
}

type Level2 struct {
	Name string
	L3   Nest
}

var nestedData = []Nested{
	Nested{
		Key: 0,
		Embed: Embed{
			Color: "red",
		},
		L1: Nest{
			Name: "Joe",
		},
		L2: Level2{
			Name: "Joe",
			L3: Nest{
				Name: "Joe",
			},
		},
		Pointer: &Nest{
			Name: "Joe",
		},
	},
	Nested{
		Key: 1,
		Embed: Embed{
			Color: "red",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Jill",
			},
		},
		Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 2,
		Embed: Embed{
			Color: "orange",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Jill",
			},
		},
		Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 3,
		Embed: Embed{
			Color: "orange",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Joe",
			},
		}, Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 4,
		Embed: Embed{
			Color: "blue",
		},
		L1: Nest{
			Name: "Abner",
		},
		L2: Level2{
			Name: "Abner",
			L3: Nest{
				Name: "Abner",
			},
		}, Pointer: &Nest{
			Name: "Abner",
		},
	},
}

var nestedTests = []test{
	test{
		name:   "Nested",
		query:  bolthold.Where("L1.Name").Eq("Joe"),
		result: []int{0},
	},
	test{
		name:   "Embedded",
		query:  bolthold.Where("Color").Eq("red"),
		result: []int{0, 1},
	},
	test{
		name:   "Embedded Explicit",
		query:  bolthold.Where("Embed.Color").Eq("red"),
		result: []int{0, 1},
	},
	test{
		name:   "Nested Multiple Levels",
		query:  bolthold.Where("L2.L3.Name").Eq("Joe"),
		result: []int{0, 3},
	},
	test{
		name:   "Pointer",
		query:  bolthold.Where("Pointer.Name").Eq("Jill"),
		result: []int{1, 2, 3},
	},
	test{
		name:   "Sort",
		query:  bolthold.Where("Key").Ge(0).SortBy("L2.L3.Name"),
		result: []int{4, 1, 2, 0, 3},
	},
}

func TestNested(t *testing.T) {
	testWrap(t, func(store *bolthold.Store, t *testing.T) {
		for i := range nestedData {
			err := store.Insert(nestedData[i].Key, nestedData[i])
			if err != nil {
				t.Fatalf("Error inserting nested test data for nested find test: %s", err)
			}
		}
		for _, tst := range nestedTests {
			t.Run(tst.name, func(t *testing.T) {
				var result []Nested
				err := store.Find(&result, tst.query)
				if err != nil {
					t.Fatalf("Error finding data from bolthold: %s", err)<commit_msg>Added test for sorting on nested pointer fields<commit_after>// Copyright 2016 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package bolthold_test

import (
	"reflect"
	"testing"

	"github.com/timshannon/bolthold"
)

type Nested struct {
	Key int
	Embed
	L1      Nest
	L2      Level2
	Pointer *Nest
}

type Embed struct {
	Color string
}

type Nest struct {
	Name string
}

type Level2 struct {
	Name string
	L3   Nest
}

var nestedData = []Nested{
	Nested{
		Key: 0,
		Embed: Embed{
			Color: "red",
		},
		L1: Nest{
			Name: "Joe",
		},
		L2: Level2{
			Name: "Joe",
			L3: Nest{
				Name: "Joe",
			},
		},
		Pointer: &Nest{
			Name: "Joe",
		},
	},
	Nested{
		Key: 1,
		Embed: Embed{
			Color: "red",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Jill",
			},
		},
		Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 2,
		Embed: Embed{
			Color: "orange",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Jill",
			},
		},
		Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 3,
		Embed: Embed{
			Color: "orange",
		},
		L1: Nest{
			Name: "Jill",
		},
		L2: Level2{
			Name: "Jill",
			L3: Nest{
				Name: "Joe",
			},
		}, Pointer: &Nest{
			Name: "Jill",
		},
	},
	Nested{
		Key: 4,
		Embed: Embed{
			Color: "blue",
		},
		L1: Nest{
			Name: "Abner",
		},
		L2: Level2{
			Name: "Abner",
			L3: Nest{
				Name: "Abner",
			},
		}, Pointer: &Nest{
			Name: "Abner",
		},
	},
}

var nestedTests = []test{
	test{
		name:   "Nested",
		query:  bolthold.Where("L1.Name").Eq("Joe"),
		result: []int{0},
	},
	test{
		name:   "Embedded",
		query:  bolthold.Where("Color").Eq("red"),
		result: []int{0, 1},
	},
	test{
		name:   "Embedded Explicit",
		query:  bolthold.Where("Embed.Color").Eq("red"),
		result: []int{0, 1},
	},
	test{
		name:   "Nested Multiple Levels",
		query:  bolthold.Where("L2.L3.Name").Eq("Joe"),
		result: []int{0, 3},
	},
	test{
		name:   "Pointer",
		query:  bolthold.Where("Pointer.Name").Eq("Jill"),
		result: []int{1, 2, 3},
	},
	test{
		name:   "Sort",
		query:  bolthold.Where("Key").Ge(0).SortBy("L2.L3.Name"),
		result: []int{4, 1, 2, 0, 3},
	},
	test{
		name:   "Sort On Pointer",
		query:  bolthold.Where("Key").Ge(0).SortBy("Pointer.Name"),
		result: []int{4, 1, 2, 0, 3},
	},
}

func TestNested(t *testing.T) {
	testWrap(t, func(store *bolthold.Store, t *testing.T) {
		for i := range nestedData {
			err := store.Insert(nestedData[i].Key, nestedData[i])
			if err != nil {
				t.Fatalf("Error inserting nested test data for nested find test: %s", err)
			}
		}
		for _, tst := range nestedTests {
			t.Run(tst.name, func(t *testing.T) {
				var result []Nested
				err := store.Find(&result, tst.query)
				if err != nil {
					t.Fatalf("Error finding data from bolthold: %s", err)
{'fraction_non_alphanumeric': 0.1894234170498762, 'fraction_numerical': 0.020516448532012736, 'mean_word_length': 2.7033398821218073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '79403', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2424, 'n_words': 595}