github.com/tuhaihe/gpbackup@v1.0.3/integration/predata_operators_create_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"github.com/tuhaihe/gp-common-go-libs/structmatcher"
     5  	"github.com/tuhaihe/gp-common-go-libs/testhelper"
     6  	"github.com/tuhaihe/gpbackup/backup"
     7  	"github.com/tuhaihe/gpbackup/testutils"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("backup integration create statement tests", func() {
    14  	BeforeEach(func() {
    15  		tocfile, backupfile = testutils.InitializeTestTOC(buffer, "predata")
    16  	})
    17  	Describe("PrintCreateOperatorStatement", func() {
    18  		It("creates operator", func() {
    19  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
    20  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema")
    21  
    22  			testhelper.AssertQueryRuns(connectionPool, "CREATE FUNCTION testschema.\"testFunc\" (path,path) RETURNS path AS 'SELECT $1' LANGUAGE SQL IMMUTABLE")
    23  			defer testhelper.AssertQueryRuns(connectionPool, "DROP FUNCTION testschema.\"testFunc\" (path,path)")
    24  
    25  			operator := backup.Operator{Oid: 0, Schema: "testschema", Name: "##", Procedure: "testschema.\"testFunc\"", LeftArgType: "path", RightArgType: "path", CommutatorOp: "0", NegatorOp: "0", RestrictFunction: "-", JoinFunction: "-", CanHash: false, CanMerge: false}
    26  
    27  			backup.PrintCreateOperatorStatement(backupfile, tocfile, operator, backup.ObjectMetadata{})
    28  
    29  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
    30  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR testschema.##(path, path)")
    31  
    32  			resultOperators := backup.GetOperators(connectionPool)
    33  			Expect(resultOperators).To(HaveLen(1))
    34  			structmatcher.ExpectStructsToMatchExcluding(&operator, &resultOperators[0], "Oid")
    35  		})
    36  		It("creates operator with owner and comment", func() {
    37  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
    38  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema")
    39  
    40  			testhelper.AssertQueryRuns(connectionPool, "CREATE FUNCTION testschema.\"testFunc\" (path,path) RETURNS path AS 'SELECT $1' LANGUAGE SQL IMMUTABLE")
    41  			defer testhelper.AssertQueryRuns(connectionPool, "DROP FUNCTION testschema.\"testFunc\" (path,path)")
    42  
    43  			operatorMetadata := testutils.DefaultMetadata("OPERATOR", false, false, true, false)
    44  			operator := backup.Operator{Oid: 1, Schema: "testschema", Name: "##", Procedure: "testschema.\"testFunc\"", LeftArgType: "path", RightArgType: "path", CommutatorOp: "0", NegatorOp: "0", RestrictFunction: "-", JoinFunction: "-", CanHash: false, CanMerge: false}
    45  
    46  			backup.PrintCreateOperatorStatement(backupfile, tocfile, operator, operatorMetadata)
    47  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
    48  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR testschema.##(path, path)")
    49  
    50  			resultOperators := backup.GetOperators(connectionPool)
    51  			Expect(resultOperators).To(HaveLen(1))
    52  			resultMetadataMap := backup.GetCommentsForObjectType(connectionPool, backup.TYPE_OPERATOR)
    53  			resultMetadata := resultMetadataMap[resultOperators[0].GetUniqueID()]
    54  			structmatcher.ExpectStructsToMatchExcluding(&operator, &resultOperators[0], "Oid")
    55  			structmatcher.ExpectStructsToMatchExcluding(&resultMetadata, &operatorMetadata, "Oid")
    56  		})
    57  	})
    58  	Describe("PrintCreateOperatorFamilyStatements", func() {
    59  		BeforeEach(func() {
    60  			testutils.SkipIfBefore5(connectionPool)
    61  		})
    62  		It("creates operator family", func() {
    63  			operatorFamily := backup.OperatorFamily{Oid: 1, Schema: "public", Name: "testfam", IndexMethod: "hash"}
    64  			operatorFamilies := []backup.OperatorFamily{operatorFamily}
    65  
    66  			backup.PrintCreateOperatorFamilyStatements(backupfile, tocfile, operatorFamilies, backup.MetadataMap{})
    67  
    68  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
    69  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testfam USING hash")
    70  
    71  			resultOperatorFamilies := backup.GetOperatorFamilies(connectionPool)
    72  			Expect(resultOperatorFamilies).To(HaveLen(1))
    73  			structmatcher.ExpectStructsToMatchExcluding(&operatorFamily, &resultOperatorFamilies[0], "Oid")
    74  		})
    75  		It("creates operator family with owner and comment", func() {
    76  			operatorFamily := backup.OperatorFamily{Oid: 1, Schema: "public", Name: "testfam", IndexMethod: "hash"}
    77  			operatorFamilies := []backup.OperatorFamily{operatorFamily}
    78  			operatorFamilyMetadataMap := testutils.DefaultMetadataMap("OPERATOR FAMILY", false, true, true, false)
    79  			operatorFamilyMetadata := operatorFamilyMetadataMap[operatorFamily.GetUniqueID()]
    80  
    81  			backup.PrintCreateOperatorFamilyStatements(backupfile, tocfile, operatorFamilies, operatorFamilyMetadataMap)
    82  
    83  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
    84  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testfam USING hash")
    85  
    86  			resultOperatorFamilies := backup.GetOperatorFamilies(connectionPool)
    87  			Expect(resultOperatorFamilies).To(HaveLen(1))
    88  			resultMetadataMap := backup.GetMetadataForObjectType(connectionPool, backup.TYPE_OPERATORFAMILY)
    89  			resultMetadata := resultMetadataMap[resultOperatorFamilies[0].GetUniqueID()]
    90  			structmatcher.ExpectStructsToMatchExcluding(&operatorFamily, &resultOperatorFamilies[0], "Oid")
    91  			structmatcher.ExpectStructsToMatchExcluding(&resultMetadata, &operatorFamilyMetadata, "Oid")
    92  		})
    93  	})
    94  	Describe("PrintCreateOperatorClassStatement", func() {
    95  		emptyMetadata := backup.ObjectMetadata{}
    96  		It("creates basic operator class", func() {
    97  			operatorClass := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
    98  			if false { // Operator families do not exist prior to GPDB5
    99  				operatorClass.FamilySchema = ""
   100  				operatorClass.FamilyName = ""
   101  			}
   102  
   103  			backup.PrintCreateOperatorClassStatement(backupfile, tocfile, operatorClass, emptyMetadata)
   104  
   105  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
   106  			if false {
   107  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING hash")
   108  			} else {
   109  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING hash CASCADE")
   110  			}
   111  
   112  			resultOperatorClasses := backup.GetOperatorClasses(connectionPool)
   113  			Expect(resultOperatorClasses).To(HaveLen(1))
   114  			structmatcher.ExpectStructsToMatchExcluding(&operatorClass, &resultOperatorClasses[0], "Oid")
   115  		})
   116  		It("creates complex operator class", func() {
   117  			testutils.SkipIfBefore5(connectionPool)
   118  			operatorClass := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testfam", IndexMethod: "gist", Type: "integer", Default: true, StorageType: "-", Operators: nil, Functions: nil}
   119  
   120  			operatorClass.Functions = []backup.OperatorClassFunction{{ClassOid: 0, SupportNumber: 1, RightType: "integer", LeftType: "integer", FunctionName: "abs(integer)"}}
   121  			if false { // Operator families do not exist prior to GPDB5
   122  				operatorClass.FamilySchema = ""
   123  				operatorClass.FamilyName = ""
   124  				operatorClass.Functions = []backup.OperatorClassFunction{{ClassOid: 0, SupportNumber: 1, FunctionName: "abs(integer)"}}
   125  			}
   126  
   127  			expectedRecheck := false
   128  			if false {
   129  				expectedRecheck = true
   130  			}
   131  			operatorClass.Operators = []backup.OperatorClassOperator{{ClassOid: 0, StrategyNumber: 1, Operator: "=(integer,integer)", Recheck: expectedRecheck}}
   132  
   133  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY public.testfam USING gist")
   134  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testfam USING gist CASCADE")
   135  			backup.PrintCreateOperatorClassStatement(backupfile, tocfile, operatorClass, emptyMetadata)
   136  
   137  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
   138  
   139  			resultOperatorClasses := backup.GetOperatorClasses(connectionPool)
   140  			Expect(resultOperatorClasses).To(HaveLen(1))
   141  			structmatcher.ExpectStructsToMatchExcluding(&operatorClass, &resultOperatorClasses[0], "Oid", "Operators.ClassOid", "Functions.ClassOid")
   142  		})
   143  		It("creates an operator class with an operator that has a sort family", func() {
   144  			testutils.SkipIfBefore6(connectionPool)
   145  			operatorClass := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "gist", Type: "integer", Default: true, StorageType: "-", Operators: nil, Functions: nil}
   146  			operatorClass.Operators = []backup.OperatorClassOperator{{ClassOid: 0, StrategyNumber: 1, Operator: "=(integer,integer)", Recheck: false, OrderByFamily: "public.sort_family_name"}}
   147  
   148  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY public.sort_family_name USING btree")
   149  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.sort_family_name USING btree")
   150  
   151  			backup.PrintCreateOperatorClassStatement(backupfile, tocfile, operatorClass, emptyMetadata)
   152  
   153  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
   154  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING gist CASCADE")
   155  
   156  			resultOperatorClasses := backup.GetOperatorClasses(connectionPool)
   157  			Expect(resultOperatorClasses).To(HaveLen(1))
   158  			structmatcher.ExpectStructsToMatchExcluding(&operatorClass, &resultOperatorClasses[0], "Oid", "Operators.ClassOid", "Functions.ClassOid")
   159  		})
   160  		It("creates basic operator class with a comment and owner", func() {
   161  			operatorClass := backup.OperatorClass{Oid: 1, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   162  			operatorClassMetadata := testutils.DefaultMetadata("OPERATOR CLASS", false, true, true, false)
   163  			if false { // Operator families do not exist prior to GPDB5
   164  				operatorClass.FamilySchema = ""
   165  				operatorClass.FamilyName = ""
   166  			}
   167  
   168  			backup.PrintCreateOperatorClassStatement(backupfile, tocfile, operatorClass, operatorClassMetadata)
   169  
   170  			testhelper.AssertQueryRuns(connectionPool, buffer.String())
   171  			if false {
   172  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING hash")
   173  			} else {
   174  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING hash CASCADE")
   175  			}
   176  
   177  			resultOperatorClasses := backup.GetOperatorClasses(connectionPool)
   178  			Expect(resultOperatorClasses).To(HaveLen(1))
   179  			structmatcher.ExpectStructsToMatchExcluding(&operatorClass, &resultOperatorClasses[0], "Oid")
   180  
   181  			resultMetadataMap := backup.GetMetadataForObjectType(connectionPool, backup.TYPE_OPERATORCLASS)
   182  			resultMetadata := resultMetadataMap[resultOperatorClasses[0].GetUniqueID()]
   183  			structmatcher.ExpectStructsToMatchExcluding(&resultMetadata, &operatorClassMetadata, "Oid")
   184  
   185  		})
   186  	})
   187  })