package vfpool

import "testing"

var vfpools = ListVFPool{
	Data: []ItemVFPool{
		{
			AccountAccess: []uint64{1, 2},
			Description:   "descr",
			GID:           1,
			ID:            1,
			Name:          "name",
			RGAccess:      []uint64{3, 4},
			Status:        "ENABLED",
		},
		{
			AccountAccess: []uint64{},
			Description:   "",
			GID:           2,
			ID:            2,
			Name:          "name2",
			RGAccess:      []uint64{},
			Status:        "DISABLED",
		},
		{
			AccountAccess: []uint64{7, 8},
			Description:   "",
			GID:           215,
			ID:            3,
			Name:          "name3",
			RGAccess:      []uint64{5, 6},
			Status:        "DISABLED",
		},
	},
}

func TestFilterByID(t *testing.T) {
	actual := vfpools.FilterByID(1).FindOne()

	if actual.ID != 1 {
		t.Fatal("expected ID 1, found: ", actual.ID)
	}
}

func TestFilterByGID(t *testing.T) {
	var gid uint64 = 1
	actual := vfpools.FilterByGID(gid).FindOne()

	if actual.GID != gid {
		t.Fatal("expected ", gid, " found: ", actual.GID)
	}
}

func TestFilterByName(t *testing.T) {
	name := "name"
	actual := vfpools.FilterByName(name).FindOne()

	if actual.Name != name {
		t.Fatal("expected ", name, " found: ", actual.Name)
	}
}

func TestFilterByDescription(t *testing.T) {
	description := "descr"
	actual := vfpools.FilterByDescription(description).FindOne()

	if actual.Description != description {
		t.Fatal("expected ", description, " found: ", actual.Description)
	}
}

func TestFilterByStatus(t *testing.T) {
	actual := vfpools.FilterByStatus("ENABLED")

	if len(actual.Data) != 1 {
		t.Fatal("expected 1 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		if item.Status != "ENABLED" {
			t.Fatal("expected Status 'ENABLED', found: ", item.Status)
		}
	}
}

func TestFilterByAccountAccess(t *testing.T) {
	var account uint64 = 1
	actual := vfpools.FilterByAccountAccess(account)

	if len(actual.Data) != 1 {
		t.Fatal("expected 1 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		var found bool
		for _, a := range item.AccountAccess {
			if a == account {
				found = true
			}
		}

		if !found {
			t.Fatalf("expected account access %d, found: %v", account, item.AccountAccess)
		}
	}
}

func TestFilterByRGAccess(t *testing.T) {
	var rg uint64 = 3
	actual := vfpools.FilterByRGAccess(rg)

	if len(actual.Data) != 1 {
		t.Fatal("expected 1 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		var found bool
		for _, r := range item.RGAccess {
			if r == rg {
				found = true
			}
		}

		if !found {
			t.Fatalf("expected account access %d, found: %v", rg, item.RGAccess)
		}
	}
}

func TestFilterFunc(t *testing.T) {
	actual := vfpools.FilterFunc(func(ivfpool ItemVFPool) bool {
		return ivfpool.GID == ivfpool.ID
	})

	if len(actual.Data) != 2 {
		t.Fatal("expected 2 elements, found: ", len(actual.Data))
	}
}
