From 70f5b04ea2e607136b9473f0a029e983e416fcf0 Mon Sep 17 00:00:00 2001 From: Tom Harding Date: Mon, 8 Aug 2022 13:20:08 +0100 Subject: [PATCH] Update `InsertVarcharColumnSpec` test PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5353 GitOrigin-RevId: 74f6e0a9322aa8926a162f8cfaae112669003f0b --- server/graphql-engine.cabal | 5 - .../Test/SQLServer/InsertVarcharColumnSpec.hs | 265 ++++++++---------- 2 files changed, 113 insertions(+), 157 deletions(-) diff --git a/server/graphql-engine.cabal b/server/graphql-engine.cabal index 077a83f1451..f3ae5433bc5 100644 --- a/server/graphql-engine.cabal +++ b/server/graphql-engine.cabal @@ -1266,7 +1266,6 @@ test-suite tests-hspec Test.Queries.Simple.OperationNameSpec Test.Queries.Simple.PrimaryKeySpec Test.Queries.SortSpec - Test.Queries.SortSpec Test.Quoter.YamlSpec Test.RemoteRelationship.FromRemoteSchemaSpec Test.RemoteRelationship.MetadataAPI.ClearMetadataSpec @@ -1279,10 +1278,6 @@ test-suite tests-hspec Test.RequestHeadersSpec Test.RunSQLSpec Test.SQLServer.InsertVarcharColumnSpec - Test.SQLServer.InsertVarcharColumnSpec - Test.SQLServer.InsertVarcharColumnSpec - Test.Schema.DataValidation.PermissionSpec - Test.Schema.DataValidation.PermissionSpec Test.Schema.DataValidation.PermissionSpec Test.Schema.DefaultValuesSpec Test.Schema.EnumSpec diff --git a/server/tests-hspec/Test/SQLServer/InsertVarcharColumnSpec.hs b/server/tests-hspec/Test/SQLServer/InsertVarcharColumnSpec.hs index c5fa3505826..d6ef66431bb 100644 --- a/server/tests-hspec/Test/SQLServer/InsertVarcharColumnSpec.hs +++ b/server/tests-hspec/Test/SQLServer/InsertVarcharColumnSpec.hs @@ -1,173 +1,134 @@ {-# LANGUAGE QuasiQuotes #-} --- | Test inserting non-ASCII characters in @'varchar' column type +-- | +-- Tests around inserting non-ASCII characters in @VARCHAR@ column type. module Test.SQLServer.InsertVarcharColumnSpec (spec) where -import Data.List.NonEmpty qualified as NE -import Database.PG.Query.Pool (sql) +import Data.Aeson (Value) import Harness.Backend.Sqlserver qualified as Sqlserver -import Harness.GraphqlEngine qualified as GraphqlEngine +import Harness.GraphqlEngine (postGraphql) import Harness.Quoter.Graphql (graphql) import Harness.Quoter.Yaml (yaml) -import Harness.Test.Context qualified as Context +import Harness.Test.Fixture qualified as Fixture +import Harness.Test.Schema (Table (..), table) +import Harness.Test.Schema qualified as Schema import Harness.TestEnvironment (TestEnvironment) import Harness.Yaml (shouldReturnYaml) import Hasura.Prelude -import Test.Hspec (SpecWith, it) - --- ** Preamble +import Test.Hspec (SpecWith, describe, it) spec :: SpecWith TestEnvironment -spec = - Context.run - ( NE.fromList - [ Context.Context - { name = Context.Backend Context.SQLServer, - mkLocalTestEnvironment = Context.noLocalTestEnvironment, - setup = mssqlSetup, - teardown = mssqlTeardown, - customOptions = Nothing - } - ] - ) +spec = do + Fixture.run + [ (Fixture.fixture $ Fixture.Backend Fixture.SQLServer) + { Fixture.setupTeardown = \(testEnv, _) -> + [ Sqlserver.setupTablesAction schema testEnv + ] + } + ] tests --- ** Setup and teardown +-------------------------------------------------------------------------------- +-- Schema -mssqlSetup :: (TestEnvironment, ()) -> IO () -mssqlSetup (testEnv, ()) = do - -- Clear metadata and configure the source - GraphqlEngine.setSource testEnv Sqlserver.defaultSourceMetadata Nothing +schema :: [Schema.Table] +schema = + [ (table "test") + { tableColumns = + [ Schema.column "id" Schema.TInt, + Schema.column "test" do + Schema.TCustomType + Schema.defaultBackendScalarType + { Schema.bstMssql = Just "varchar(MAX)" + } + ], + tablePrimaryKey = ["id"] + }, + (table "test_bin") + { tableColumns = + [ Schema.column "id" Schema.TInt, + Schema.column "test" do + Schema.TCustomType + Schema.defaultBackendScalarType + { Schema.bstMssql = Just "varchar(MAX) collate SQL_Latin1_General_CP437_BIN" + } + ], + tablePrimaryKey = ["id"] + } + ] - -- Setup DB schema - Sqlserver.run_ setupSQL +-------------------------------------------------------------------------------- +-- Tests - -- Track tables - GraphqlEngine.postMetadata_ - testEnv - [yaml| -type: bulk -args: -- type: mssql_track_table - args: - source: mssql - table: - schema: hasura - name: test -- type: mssql_track_table - args: - source: mssql - table: - schema: hasura - name: test_bin -|] - -setupSQL :: String -setupSQL = - [sql| -CREATE TABLE test ( - id INT PRIMARY KEY, - varchar_column varchar(MAX) -); -CREATE TABLE test_bin ( - id INT PRIMARY KEY, - varchar_column varchar(MAX) collate SQL_Latin1_General_CP437_BIN -); -|] - -mssqlTeardown :: (TestEnvironment, ()) -> IO () -mssqlTeardown (testEnv, ()) = do - -- Untrack table - GraphqlEngine.postMetadata_ - testEnv - [yaml| -type: bulk -args: -- type: mssql_untrack_table - args: - source: mssql - table: - schema: hasura - name: test -- type: mssql_untrack_table - args: - source: mssql - table: - schema: hasura - name: test_bin -|] - - -- Teardown DB schema - Sqlserver.run_ teardownSQL - - -- Clear metadata - GraphqlEngine.clearMetadata testEnv - -teardownSQL :: String -teardownSQL = - [sql| -DROP TABLE test; -DROP TABLE test_bin; -|] - --- * Tests - -tests :: Context.Options -> SpecWith TestEnvironment +tests :: Fixture.Options -> SpecWith TestEnvironment tests opts = do - it "Insert into varchar column with non ASCII value" $ \testEnv -> - shouldReturnYaml - opts - ( GraphqlEngine.postGraphql - testEnv - [graphql| -mutation { - insert_hasura_test( - objects: [{id: 1, varchar_column: "££££"}] - ) { - affected_rows - returning{ - id - varchar_column - } - } -} -|] - ) - [yaml| -data: - insert_hasura_test: - returning: - - id: 1 - varchar_column: "££££" - affected_rows: 1 + let shouldBe :: IO Value -> Value -> IO () + shouldBe = shouldReturnYaml opts -|] + describe "Inserting into varchar columns" do + it "Insert into varchar column with non ASCII value" $ \testEnvironment -> do + let expected :: Value + expected = + [yaml| + data: + insert_hasura_test: + returning: + - id: 1 + test: "££££" + affected_rows: 1 - it "Insert into collated varchar column with non ASCII value" $ \testEnv -> - shouldReturnYaml - opts - ( GraphqlEngine.postGraphql - testEnv - [graphql| -mutation { - insert_hasura_test_bin( - objects: [{id: 1, varchar_column: "££££"}] - ) { - affected_rows - returning{ - id - varchar_column - } - } -} -|] - ) - [yaml| -data: - insert_hasura_test_bin: - returning: - - id: 1 - varchar_column: "££££" - affected_rows: 1 + |] -|] + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation { + insert_hasura_test(objects: + [ { id: 1, test: "££££" } + ] + ) { + affected_rows + returning { + id + test + } + } + } + |] + + actual `shouldBe` expected + + it "Insert into collated varchar column with non ASCII value" \testEnvironment -> do + let expected :: Value + expected = + [yaml| + data: + insert_hasura_test_bin: + returning: + - id: 1 + test: "££££" + affected_rows: 1 + |] + + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation { + insert_hasura_test_bin(objects: + [ { id: 1, test: "££££" } + ] + ) { + affected_rows + returning { + id + test + } + } + } + |] + + actual `shouldBe` expected