<commit_before>package database

import (
	"github.com/pagarme/teleport/action"
	"github.com/pagarme/teleport/batcher/ddldiff"
	"reflect"
	"testing"
)

func init() {
	schema = &Schema{
		"123",
		"test_schema",
		[]*Class{},
		nil,
	}
}

func TestTypeDiff(t *testing.T) {
	var tests = []struct {
		pre    *Type
		post   *Type
		output []action.Action
	}{
		{
			// Diff a type creation
			nil,
			&Type{
				"789",
				"test_type",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			[]action.Action{
				&action.CreateType{
					"test_schema",
					"test_type",
					[]string{
						"test_enum1",
						"test_enum2",
					},
				},
			},
		},
		{
			// Diff a type rename
			&Type{
				"789",
				"test_type",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			&Type{
				"789",
				"test_type_renamed",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			[]action.Action{
				&action.AlterType{
					"test_schema",
					"test_type",
					"test_type_renamed",
				},
			},
		},
	}

	for _, test := range tests {
		// Avoid passing a interface with nil pointer
		// to Diff and breaking comparisons with nil.
		var preObj ddldiff.Diffable
		if test.pre == nil {
			preObj = nil
		} else {
			preObj = test.pre
		}

		actions := test.post.Diff(preObj)

		if !reflect.DeepEqual(actions, test.output) {
			t.Errorf(
				"diff %#v with %#v => %v, want %d",
				test.pre,
				test.post,
				actions,
				test.output,
			)
		}
	}
}

func TestChildrenDiff(t *testing.T) {
	enums := []*Enum{
		&Enum{
			"123",
			"test_enum1",
			nil,
		},
	}

	typ := &Type{
		"789",
		"test_type",
		enums,
		nil,
	}

	children := typ.Children()

	if len(children) != 1 {
		t.Errorf("children => %d, want %d", len(children), 1)
	}

	for i, child := range children {
		if child != enums[i] {
			t.Errorf("child %i => %v, want %v", i, child, enums[i])
		}
	}
}

func TestTypeDrop(t *testing.T) {
	typ := &Type{
		"789",
		"test_type",
		[]*Enum{
			&Enum{
				"123",
				"test_enum1",
				nil,
			},
		},
		schema,
	}

	actions := typ.Drop()

	if len(actions) != 1 {
		t.Errorf("actions => %d, want %d", len(actions), 1)
	}

	dropAction, ok := actions[0].(*action.DropType)

	if !ok {
		t.Errorf("action is not DropType")
	}

	if dropAction.SchemaName != schema.Name {
		t.Errorf("drop action schema name => %s, want %s", dropAction.SchemaName, schema.Name)
	}

	if dropAction.TypeName != typ.Name {
		t.Errorf("drop action type name => %s, want %s", dropAction.TypeName, typ.Name)
	}
}<commit_msg>Test IsEqual().<commit_after>package database

import (
	"github.com/pagarme/teleport/action"
	"github.com/pagarme/teleport/batcher/ddldiff"
	"reflect"
	"testing"
)

func init() {
	schema = &Schema{
		"123",
		"test_schema",
		[]*Class{},
		nil,
	}
}

func TestTypeDiff(t *testing.T) {
	var tests = []struct {
		pre    *Type
		post   *Type
		output []action.Action
	}{
		{
			// Diff a type creation
			nil,
			&Type{
				"789",
				"test_type",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			[]action.Action{
				&action.CreateType{
					"test_schema",
					"test_type",
					[]string{
						"test_enum1",
						"test_enum2",
					},
				},
			},
		},
		{
			// Diff a type rename
			&Type{
				"789",
				"test_type",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			&Type{
				"789",
				"test_type_renamed",
				[]*Enum{
					&Enum{
						"123",
						"test_enum1",
						nil,
					},
					&Enum{
						"124",
						"test_enum2",
						nil,
					},
				},
				schema,
			},
			[]action.Action{
				&action.AlterType{
					"test_schema",
					"test_type",
					"test_type_renamed",
				},
			},
		},
	}

	for _, test := range tests {
		// Avoid passing a interface with nil pointer
		// to Diff and breaking comparisons with nil.
		var preObj ddldiff.Diffable
		if test.pre == nil {
			preObj = nil
		} else {
			preObj = test.pre
		}

		actions := test.post.Diff(preObj)

		if !reflect.DeepEqual(actions, test.output) {
			t.Errorf(
				"diff %#v with %#v => %v, want %d",
				test.pre,
				test.post,
				actions,
				test.output,
			)
		}
	}
}

func TestTypeChildren(t *testing.T) {
	enums := []*Enum{
		&Enum{
			"123",
			"test_enum1",
			nil,
		},
	}

	typ := &Type{
		"789",
		"test_type",
		enums,
		nil,
	}

	children := typ.Children()

	if len(children) != 1 {
		t.Errorf("children => %d, want %d", len(children), 1)
	}

	for i, child := range children {
		if child != enums[i] {
			t.Errorf("child %i => %v, want %v", i, child, enums[i])
		}
	}
}

func TestTypeDrop(t *testing.T) {
	typ := &Type{
		"789",
		"test_type",
		[]*Enum{
			&Enum{
				"123",
				"test_enum1",
				nil,
			},
		},
		schema,
	}

	actions := typ.Drop()

	if len(actions) != 1 {
		t.Errorf("actions => %d, want %d", len(actions), 1)
	}

	dropAction, ok := actions[0].(*action.DropType)

	if !ok {
		t.Errorf("action is not DropType")
	}

	if dropAction.SchemaName != schema.Name {
		t.Errorf("drop action schema name => %s, want %s", dropAction.SchemaName, schema.Name)
	}

	if dropAction.TypeName != typ.Name {
		t.Errorf("drop action type name => %s, want %s", dropAction.TypeName, typ.Name)
	}
}

func TestTypeIsEqual(t *testing.T) {
	pre := &Type{
		"789",
		"test_type",
		[]*Enum{
			&Enum{
				"123",
				"test_enum1",
				nil,
			},
			&Enum{
				"124",
				"test_enum2",
				nil,
			},
		},
		schema,
	}

	post := &Type{
		"789",
		"test_type_renamed",
		[]*Enum{
			&Enum{
				"123",
				"test_enum1",
				nil,
			},
			&Enum{
				"124",
				"test_enum2",
				nil,
			},
		},
		schema,
	}

	preOtherType := &Class{
		"123",
		"r",
		"test_table",
		[]*Attribute{
			&Attribute{
				"test_col",
				1,
				"int4",
				"0",
				nil,
			},
		},
		schema,
	}

	if !post.IsEqual(pre) {
		t.Errorf("expect types to be equal")
	}

	post.Name = pre.Name
	post.Oid = "890"

	if post.IsEqual(pre) {
		t.Errorf("expect types not to be equal")
	}

	if post.IsEqual(preOtherType) {
		t.Errorf("expect two different types not to be equal")
	}
}
{'fraction_non_alphanumeric': 0.1793411330049261, 'fraction_numerical': 0.022321428571428572, 'mean_word_length': 2.1341051616015436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3702946', 'n_tokens_mistral': 3502, 'n_tokens_neox': 2623, 'n_words': 538}