package catalogpublication

import (
	"strings"
	"testing"
)

func TestAssignmentIdentityArtifactSchemaIsProvisionedExactlyOnce(t *testing.T) {
	joined := strings.Join(SchemaStatementsForTest(), "\n")
	tables := []string{
		"_System_CatalogAssignmentIdentityView",
		"_System_CatalogAssignmentIdentityViewObject",
		"_System_CatalogAssignmentIdentityViewRecordType",
		"_System_CatalogAssignmentIdentityViewProductApp",
		"_System_CatalogAssignmentIdentityViewNavigationApp",
	}
	for _, table := range tables {
		create := "CREATE TABLE IF NOT EXISTS " + table + " "
		if count := strings.Count(joined, create); count != 1 {
			t.Fatalf(
				"Catalog publication schema provisions %s %d times, want exactly once",
				table,
				count,
			)
		}
	}
	if count := strings.Count(
		joined,
		"CREATE TABLE IF NOT EXISTS _System_CatalogAssignmentIdentity",
	); count != len(tables) {
		t.Fatalf(
			"Catalog publication schema provisions %d assignment-identity tables, want %d",
			count,
			len(tables),
		)
	}
}
