package functionality

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/pingcap/endless/pkg/matcher"
	. "github.com/pingcap/endless/pkg/workload"
	. "github.com/pingcap/endless/testcase/geopartition/utils"
)

func temporaryTableTest(ctx *SuiteContext) {
	It("Should error if one attempts to set placement to a temporary table", func() {
		MustExec(ctx.DB, "CREATE PLACEMENT POLICY IF NOT EXISTS p1 followers=4")
		defer MustExec(ctx.DB, "DROP PLACEMENT POLICY IF EXISTS p1")
		_, err := ctx.DB.Exec("CREATE TEMPORARY TABLE fta1 (a int) PLACEMENT POLICY p1")
		Expect(GetErrorCode(err)).To(Equal(uint16(8006)))
	})

	It("Should error if one attempts to alter placement to a temporary table", func() {
		MustExec(ctx.DB, "CREATE PLACEMENT POLICY IF NOT EXISTS p1 followers=4")
		defer MustExec(ctx.DB, "DROP PLACEMENT POLICY IF EXISTS p1")
		Try(ctx.DB.Exec("CREATE TEMPORARY TABLE ftb1 (a int)"))

		_, err := ctx.DB.Exec("ALTER TABLE ftb1 PLACEMENT POLICY p1")
		Expect(GetErrorCode(err)).To(Equal(uint16(8200)))
	})

	It("Create a temp table like a base table with placement", func() {
		MustExec(ctx.DB, "CREATE PLACEMENT POLICY IF NOT EXISTS p1 followers=4")
		defer MustExec(ctx.DB, "DROP PLACEMENT POLICY IF EXISTS p1")
		Try(ctx.DB.Exec("CREATE TABLE ftc1 (a int) PLACEMENT POLICY p1"))
		defer MustExec(ctx.DB, "DROP TABLE ftc1")

		// FIXME: behavior not same as the test document
		_, err := ctx.DB.Exec("CREATE TEMPORARY TABLE ftc2 LIKE ftc1")
		Expect(GetErrorCode(err)).To(Equal(uint16(8006)))
	})
}
