github.com/tuhaihe/gpbackup@v1.0.3/integration/predata_operators_queries_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/options"
     8  	"github.com/tuhaihe/gpbackup/testutils"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("backup integration tests", func() {
    15  	Describe("GetOperators", func() {
    16  		It("returns a slice of operators", func() {
    17  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR public.## (LEFTARG = bigint, PROCEDURE = numeric_fac)")
    18  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR public.## (bigint, NONE)")
    19  
    20  			expectedOperator := backup.Operator{Oid: 0, Schema: "public", Name: "##", Procedure: "numeric_fac", LeftArgType: "bigint", RightArgType: "-", CommutatorOp: "0", NegatorOp: "0", RestrictFunction: "-", JoinFunction: "-", CanHash: false, CanMerge: false}
    21  
    22  			results := backup.GetOperators(connectionPool)
    23  
    24  			Expect(results).To(HaveLen(1))
    25  			structmatcher.ExpectStructsToMatchExcluding(&expectedOperator, &results[0], "Oid")
    26  		})
    27  		It("returns a slice of operators with a full-featured operator", func() {
    28  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
    29  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema")
    30  
    31  			testhelper.AssertQueryRuns(connectionPool, `CREATE FUNCTION testschema."testFunc"(path,path) RETURNS BOOLEAN AS 'SELECT true' LANGUAGE SQL IMMUTABLE`)
    32  			defer testhelper.AssertQueryRuns(connectionPool, `DROP FUNCTION testschema."testFunc"(path,path)`)
    33  
    34  			testhelper.AssertQueryRuns(connectionPool, `
    35  			CREATE OPERATOR testschema.## (
    36  				LEFTARG = path,
    37  				RIGHTARG = path,
    38  				PROCEDURE = testschema."testFunc",
    39  				COMMUTATOR = OPERATOR(testschema.##),
    40  				NEGATOR = OPERATOR(public.###),
    41  				RESTRICT = eqsel,
    42  				JOIN = eqjoinsel,
    43  				HASHES,
    44  				MERGES
    45  			)`)
    46  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR testschema.## (path, path)")
    47  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR public.### (path, path)")
    48  
    49  			version4expectedOperator := backup.Operator{Oid: 0, Schema: "testschema", Name: "##", Procedure: `testschema."testFunc"`, LeftArgType: "path", RightArgType: "path", CommutatorOp: "testschema.##", NegatorOp: "public.###", RestrictFunction: "eqsel", JoinFunction: "eqjoinsel", CanHash: true, CanMerge: false}
    50  			expectedOperator := backup.Operator{Oid: 0, Schema: "testschema", Name: "##", Procedure: `testschema."testFunc"`, LeftArgType: "path", RightArgType: "path", CommutatorOp: "testschema.##", NegatorOp: "public.###", RestrictFunction: "eqsel", JoinFunction: "eqjoinsel", CanHash: true, CanMerge: true}
    51  
    52  			results := backup.GetOperators(connectionPool)
    53  
    54  			Expect(results).To(HaveLen(1))
    55  			if false {
    56  				structmatcher.ExpectStructsToMatchExcluding(&version4expectedOperator, &results[0], "Oid")
    57  			} else {
    58  				structmatcher.ExpectStructsToMatchExcluding(&expectedOperator, &results[0], "Oid")
    59  			}
    60  		})
    61  		It("returns a slice of operators from a specific schema", func() {
    62  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR public.## (LEFTARG = bigint, PROCEDURE = numeric_fac)")
    63  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR public.## (bigint, NONE)")
    64  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
    65  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema")
    66  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR testschema.## (LEFTARG = bigint, PROCEDURE = numeric_fac)")
    67  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR testschema.## (bigint, NONE)")
    68  			_ = backupCmdFlags.Set(options.INCLUDE_SCHEMA, "testschema")
    69  
    70  			expectedOperator := backup.Operator{Oid: 0, Schema: "testschema", Name: "##", Procedure: "numeric_fac", LeftArgType: "bigint", RightArgType: "-", CommutatorOp: "0", NegatorOp: "0", RestrictFunction: "-", JoinFunction: "-", CanHash: false, CanMerge: false}
    71  
    72  			results := backup.GetOperators(connectionPool)
    73  
    74  			Expect(results).To(HaveLen(1))
    75  			structmatcher.ExpectStructsToMatchExcluding(&expectedOperator, &results[0], "Oid")
    76  		})
    77  	})
    78  	Describe("GetOperatorFamilies", func() {
    79  		BeforeEach(func() {
    80  			testutils.SkipIfBefore5(connectionPool)
    81  		})
    82  		It("returns a slice of operator families", func() {
    83  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY public.testfam USING hash;")
    84  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testfam USING hash")
    85  
    86  			expectedOperator := backup.OperatorFamily{Oid: 0, Schema: "public", Name: "testfam", IndexMethod: "hash"}
    87  
    88  			results := backup.GetOperatorFamilies(connectionPool)
    89  
    90  			Expect(results).To(HaveLen(1))
    91  			structmatcher.ExpectStructsToMatchExcluding(&expectedOperator, &results[0], "Oid")
    92  		})
    93  		It("returns a slice of operator families in a specific schema", func() {
    94  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY public.testfam USING hash;")
    95  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testfam USING hash")
    96  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
    97  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema")
    98  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY testschema.testfam USING hash;")
    99  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY testschema.testfam USING hash")
   100  			_ = backupCmdFlags.Set(options.INCLUDE_SCHEMA, "testschema")
   101  
   102  			expectedOperator := backup.OperatorFamily{Oid: 0, Schema: "testschema", Name: "testfam", IndexMethod: "hash"}
   103  
   104  			results := backup.GetOperatorFamilies(connectionPool)
   105  
   106  			Expect(results).To(HaveLen(1))
   107  			structmatcher.ExpectStructsToMatchExcluding(&expectedOperator, &results[0], "Oid")
   108  		})
   109  	})
   110  	Describe("GetOperatorClasses", func() {
   111  		It("returns a slice of operator classes", func() {
   112  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING hash AS STORAGE int")
   113  			if false {
   114  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING hash")
   115  			} else {
   116  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING hash")
   117  			}
   118  
   119  			version4expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "", FamilyName: "", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   120  			expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   121  
   122  			results := backup.GetOperatorClasses(connectionPool)
   123  
   124  			Expect(results).To(HaveLen(1))
   125  			if false {
   126  				structmatcher.ExpectStructsToMatchExcluding(&version4expected, &results[0], "Oid")
   127  			} else {
   128  				structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid")
   129  			}
   130  		})
   131  		It("returns a slice of operator classes with an operator family", func() {
   132  			testutils.SkipIfBefore5(connectionPool)
   133  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
   134  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema CASCADE")
   135  
   136  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY testschema.testfam USING gist;")
   137  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY testschema.testfam USING gist CASCADE")
   138  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING gist FAMILY testschema.testfam AS STORAGE int")
   139  
   140  			expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "testschema", FamilyName: "testfam", IndexMethod: "gist", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   141  
   142  			results := backup.GetOperatorClasses(connectionPool)
   143  
   144  			Expect(results).To(HaveLen(1))
   145  			structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid")
   146  		})
   147  		It("returns a slice of operator classes with different type and storage type", func() {
   148  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR CLASS public.testclass DEFAULT FOR TYPE int USING gist AS STORAGE text")
   149  			if false {
   150  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING gist")
   151  			} else {
   152  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING gist")
   153  			}
   154  
   155  			version4expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "", FamilyName: "", IndexMethod: "gist", Type: "integer", Default: true, StorageType: "text", Operators: nil, Functions: nil}
   156  			expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "gist", Type: "integer", Default: true, StorageType: "text", Operators: nil, Functions: nil}
   157  
   158  			results := backup.GetOperatorClasses(connectionPool)
   159  
   160  			Expect(results).To(HaveLen(1))
   161  			if false {
   162  				structmatcher.ExpectStructsToMatchExcluding(&version4expected, &results[0], "Oid")
   163  			} else {
   164  				structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid")
   165  			}
   166  		})
   167  		It("returns a slice of operator classes with operators and functions", func() {
   168  			opClassQuery := ""
   169  			expectedRecheck := false
   170  			if false {
   171  				opClassQuery = "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING gist AS OPERATOR 1 = RECHECK, OPERATOR 2 < , FUNCTION 1 abs(integer), FUNCTION 2 int4out(integer)"
   172  				expectedRecheck = true
   173  			} else {
   174  				opClassQuery = "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING gist AS OPERATOR 1 =, OPERATOR 2 < , FUNCTION 1 abs(integer), FUNCTION 2 int4out(integer)"
   175  			}
   176  
   177  			testhelper.AssertQueryRuns(connectionPool, opClassQuery)
   178  
   179  			if false {
   180  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING gist")
   181  			} else {
   182  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING gist")
   183  			}
   184  
   185  			expectedOperators := []backup.OperatorClassOperator{{ClassOid: 0, StrategyNumber: 1, Operator: "=(integer,integer)", Recheck: expectedRecheck}, {ClassOid: 0, StrategyNumber: 2, Operator: "<(integer,integer)", Recheck: false}}
   186  			expected4Functions := []backup.OperatorClassFunction{{ClassOid: 0, SupportNumber: 1, FunctionName: "abs(integer)"}, {ClassOid: 0, SupportNumber: 2, FunctionName: "int4out(integer)"}}
   187  			expectedFunctions := []backup.OperatorClassFunction{{ClassOid: 0, SupportNumber: 1, LeftType: "integer", RightType: "integer", FunctionName: "abs(integer)"}, {ClassOid: 0, SupportNumber: 2, LeftType: "integer", RightType: "integer", FunctionName: "int4out(integer)"}}
   188  			version4expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "", FamilyName: "", IndexMethod: "gist", Type: "integer", Default: false, StorageType: "-", Operators: expectedOperators, Functions: expected4Functions}
   189  			expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "gist", Type: "integer", Default: false, StorageType: "-", Operators: expectedOperators, Functions: expectedFunctions}
   190  
   191  			results := backup.GetOperatorClasses(connectionPool)
   192  
   193  			Expect(results).To(HaveLen(1))
   194  			if false {
   195  				structmatcher.ExpectStructsToMatchExcluding(&version4expected, &results[0], "Oid", "Operators.ClassOid", "Functions.ClassOid")
   196  			} else {
   197  				structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid", "Operators.ClassOid", "Functions.ClassOid")
   198  			}
   199  		})
   200  		It("returns a slice of operator classes for a specific schema", func() {
   201  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING hash AS STORAGE int")
   202  			if false {
   203  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS public.testclass USING hash")
   204  			} else {
   205  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING hash")
   206  			}
   207  			testhelper.AssertQueryRuns(connectionPool, "CREATE SCHEMA testschema")
   208  			defer testhelper.AssertQueryRuns(connectionPool, "DROP SCHEMA testschema CASCADE")
   209  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR CLASS testschema.testclass FOR TYPE int USING hash AS STORAGE int")
   210  			if false {
   211  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR CLASS testschema.testclass USING hash")
   212  			} else {
   213  				defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY testschema.testclass USING hash")
   214  			}
   215  			_ = backupCmdFlags.Set(options.INCLUDE_SCHEMA, "testschema")
   216  
   217  			version4expected := backup.OperatorClass{Oid: 0, Schema: "testschema", Name: "testclass", FamilySchema: "", FamilyName: "", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   218  			expected := backup.OperatorClass{Oid: 0, Schema: "testschema", Name: "testclass", FamilySchema: "testschema", FamilyName: "testclass", IndexMethod: "hash", Type: "integer", Default: false, StorageType: "-", Operators: nil, Functions: nil}
   219  
   220  			results := backup.GetOperatorClasses(connectionPool)
   221  
   222  			Expect(results).To(HaveLen(1))
   223  			if false {
   224  				structmatcher.ExpectStructsToMatchExcluding(&version4expected, &results[0], "Oid")
   225  			} else {
   226  				structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid")
   227  			}
   228  		})
   229  		It("returns a slice of operator classes with an operator with a sort family", func() {
   230  			testutils.SkipIfBefore6(connectionPool)
   231  
   232  			testhelper.AssertQueryRuns(connectionPool, "CREATE OPERATOR FAMILY public.sort_family USING btree")
   233  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.sort_family USING btree")
   234  			opClassQuery := "CREATE OPERATOR CLASS public.testclass FOR TYPE int USING gist AS OPERATOR 1 = FOR ORDER BY public.sort_family"
   235  			testhelper.AssertQueryRuns(connectionPool, opClassQuery)
   236  			defer testhelper.AssertQueryRuns(connectionPool, "DROP OPERATOR FAMILY public.testclass USING gist")
   237  
   238  			expectedOperators := []backup.OperatorClassOperator{{ClassOid: 0, StrategyNumber: 1, Operator: "=(integer,integer)", Recheck: false, OrderByFamily: "public.sort_family"}}
   239  			expected := backup.OperatorClass{Oid: 0, Schema: "public", Name: "testclass", FamilySchema: "public", FamilyName: "testclass", IndexMethod: "gist", Type: "integer", Default: false, StorageType: "-", Operators: expectedOperators, Functions: nil}
   240  
   241  			results := backup.GetOperatorClasses(connectionPool)
   242  
   243  			Expect(results).To(HaveLen(1))
   244  			structmatcher.ExpectStructsToMatchExcluding(&expected, &results[0], "Oid", "Operators.ClassOid", "Functions.ClassOid")
   245  		})
   246  	})
   247  })