You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: instance_test.go
    path of file: ./repos/cds/engine/api/workflowtemplate
    
    the code of the file until where you have to start completion: package workflowtemplate_test

import (
	"context"
	"encoding/base64"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/ovh/cds/engine/api/authentication"
	"github.com/ovh/cds/engine/api/bootstrap"
	"github.com/ovh/cds/engine/api/project"
	"github.com/ovh/cds/engine/api/test"
	"github.com/ovh/cds/engine/api/test/assets"
	"github.com/ovh/cds/engine/api/workflow"
	"github.com/ovh/cds/engine/api/workflowtemplate"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/exportentities"
	v2 "github.com/ovh/cds/sdk/exportentities/v2"
)

func TestCheckAndExecuteTemplate(t *testing.T) {
	db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)

	proj := assets.InsertTestProject(t, db, cache, sdk.RandomString(10), sdk.RandomString(10))
	grp := proj.ProjectGroups[0].Group
	usr, _ := assets.InsertLambdaUser(t, db, &grp)
	consumer, err := authentication.LoadUserConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, usr.ID,
		authentication.LoadUserConsumerOptions.WithAuthentifiedUser,
		authentication.LoadUserConsumerOptions.WithConsumerGroups,
	)
	require.NoError(t, err)

	tmplV1 := sdk.WorkflowTemplate{
		Slug:    "my-template",
		Name:    "my-template",
		GroupID: grp.ID,
		Parameters: []sdk.WorkflowTemplateParameter{
			{Key: "param1", Type: sdk.ParameterTypeString},
		},
		Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: v1-[[.name]]
version: v2.0`)),
	}
	_, err = workflowtemplate.Push(context.TODO(), db, &tmplV1, consumer)
	require.NoError(t, err)
	require.NoError(t, workflowtemplate.LoadOptions.WithGroup(context.TODO(), db, &tmplV1))

	tmplV2 := sdk.WorkflowTemplate{
		Slug:    "my-template",
		Name:    "my-template",
		GroupID: grp.ID,
		Parameters: []sdk.WorkflowTemplateParameter{
			{Key: "param1", Type: sdk.ParameterTypeString},
		},
		Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: v2-[[.name]]
version: v2.0`)),
	}
	_, err = workflowtemplate.Push(context.TODO(), db, &tmplV2, consumer)
	require.NoError(t, err)
	require.NoError(t, workflowtemplate.LoadOptions.WithGroup(context.TODO(), db, &tmplV2))

	cases := []struct {
		Name             string
		Data             exportentities.WorkflowComponents
		Detached         bool
		ErrorExists      bool
		InstanceStored   bool
		ExpectedInstance sdk.WorkflowTemplateInstance
		WorkflowExists   bool
		ExpectedWorkflow exportentities.Workflow
	}{{
		Name: "No template given in data should return no error and instance",
		Data: exportentities.WorkflowComponents{},
	}, {
		Name: "Given data is a workflow",
		Data: exportentities.WorkflowComponents{
			Workflow: v2.Workflow{Name: "my-workflow"},
		},
		WorkflowExists:   true,
		ExpectedWorkflow: v2.Workflow{Name: "my-workflow"},
	}, {
		Name: "Given data is the template with v1 version",
		Data: exportentities.WorkflowComponents{
			Template: exportentities.TemplateInstance{
				Name:       "my-workflow",
				From:       tmplV1.PathWithVersion(),
				Parameters: map[string]string{"param1": "value1"},
			},
		},
		InstanceStored: true,
		ExpectedInstance: sdk.WorkflowTemplateInstance{
			Request: sdk.WorkflowTemplateRequest{
				WorkflowName: "my-workflow",
				ProjectKey:   proj.Key,
				Parameters:   map[string]string{"param1": "value1"},
			},
		},
		WorkflowExi
    
func TestCheckAndExecuteTemplate(t *testing.T) {
	db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)

	proj := assets.InsertTestProject(t, db, cache, sdk.RandomString(10), sdk.RandomString(10))
	grp := proj.ProjectGroups[0].Group
	usr, _ := assets.InsertLambdaUser(t, db, &grp)
	consumer, err := authentication.LoadUserConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, usr.ID,
		authentication.LoadUserConsumerOptions.WithAuthentifiedUser,
		authentication.LoadUserConsumerOptions.WithConsumerGroups,
	)
	require.NoError(t, err)

	tmplV1 := sdk.WorkflowTemplate{
		Slug:    "my-template",
		Name:    "my-template",
		GroupID: grp.ID,
		Parameters: []sdk.WorkflowTemplateParameter{
			{Key: "param1", Type: sdk.ParameterTypeString},
		},
		Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: v1-[[.name]]
version: v2.0`)),
	}
	_, err = workflowtemplate.Push(context.TODO(), db, &tmplV1, consumer)
	require.NoError(t, err)
	require.NoError(t, workflowtemplate.LoadOptions.WithGroup(context.TODO(), db, &tmplV1))

	tmplV2 := sdk.WorkflowTemplate{
		Slug:    "my-template",
		Name:    "my-template",
		GroupID: grp.ID,
		Parameters: []sdk.WorkflowTemplateParameter{
			{Key: "param1", Type: sdk.ParameterTypeString},
		},
		Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: v2-[[.name]]
version: v2.0`)),
	}
	_, err = workflowtemplate.Push(context.TODO(), db, &tmplV2, consumer)
	require.NoError(t, err)
	require.NoError(t, workflowtemplate.LoadOptions.WithGroup(context.TODO(), db, &tmplV2))

	cases := []struct {
		Name             string
		Data             exportentities.WorkflowComponents
		Detached         bool
		ErrorExists      bool
		InstanceStored   bool
		ExpectedInstance sdk.WorkflowTemplateInstance
		WorkflowExists   bool
		ExpectedWorkflow exportentities.Workflow
	}{{
		Name: "No template given in data should return no error and instance",
		Data: exportentities.WorkflowComponents{},
	}, {
		Name: "Given data is a workflow",
		Data: exportentities.WorkflowComponents{
			Workflow: v2.Workflow{Name: "my-workflow"},
		},
		WorkflowExists:   true,
		ExpectedWorkflow: v2.Workflow{Name: "my-workflow"},
	}, {
		Name: "Given data is the template with v1 version",
		Data: exportentities.WorkflowComponents{
			Template: exportentities.TemplateInstance{
				Name:       "my-workflow",
				From:       tmplV1.PathWithVersion(),
				Parameters: map[string]string{"param1": "value1"},
			},
		},
		InstanceStored: true,
		ExpectedInstance: sdk.WorkflowTemplateInstance{
			Request: sdk.WorkflowTemplateRequest{
				WorkflowName: "my-workflow",
				ProjectKey:   proj.Key,
				Parameters:   map[string]string{"param1": "value1"},
			},
		},
		WorkflowExists:   true,
		ExpectedWorkflow: v2.Workflow{Name: "v1-my-workflow"},
	}, {
		Name: "Given data is the template with latest version",
		Data: exportentities.WorkflowComponents{
			Template: exportentities.TemplateInstance{
				Name:       "my-workflow",
				From:       tmplV1.Path(),
				Parameters: map[string]string{"param1": "value1"},
			},
		},
		InstanceStored: true,
		ExpectedInstance: sdk.WorkflowTemplateInstance{
			Request: sdk.WorkflowTemplateRequest{
				WorkflowName: "my-workflow",
				ProjectKey:   proj.Key,
				Parameters:   map[string]string{"param1": "value1"},
			},
		},
		WorkflowExists:   true,
		ExpectedWorkflow: v2.Workflow{Name: "v2-my-workflow"},
	}, {
		Name: "Given data is a template with detached option",
		Data: exportentities.WorkflowComponents{
			Template: exportentities.TemplateInstance{
				Name:       "my-workflow",
				From:       tmplV1.PathWithVersion(),
				Parameters: map[string]string{"param1": "value1"},
			},
		},
		Detached:         true,
		WorkflowExists:   true,
		ExpectedWorkflow: v2.Workflow{Name: "v1-my-workflow"},
	}, {
		Name: "Invalid given template from",
		Data: exportentities.WorkflowComponents{
			Template: exportentities.TemplateInstance{
				Name: "my-workflow",
				From: "unknown-group/unknown-template",
			},
		},
		ErrorExists: true,
	}}

	for _, c := range cases {
		t.Run(c.Name, func(t *testing.T) {
			var mods []workflowtemplate.TemplateRequestModifierFunc
			if c.Detached {
				mods = append(mods, workflowtemplate.TemplateRequestModifiers.Detached)
			}
			_, wti, err := workflowtemplate.CheckAndExecuteTemplate(context.TODO(), db.DbMap, cache, *consumer, *proj, &c.Data, mods...)
			if c.ErrorExists {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
			}

			instanceStored := wti != nil && !wti.Request.Detached
			require.Equal(t, c.InstanceStored, instanceStored, "Instance stored should be %t buf is %t", c.InstanceStored, instanceStored)
			if instanceStored {
				assert.Equal(t, c.ExpectedInstance.Request, wti.Request)
			}

			workflowExists := c.Data.Workflow != nil && c.Data.Workflow.GetName() != ""
			require.Equal(t, c.WorkflowExists, workflowExists, "Workflow exists should be %t buf is %t", c.WorkflowExists, workflowExists)
			if workflowExists {
				assert.Equal(t, c.ExpectedWorkflow.GetName(), c.Data.Workflow.GetName())
			}
		})
	}
}