language-extensions/R/test/src/RExtensionGetOutputParamTests.cpp
//----------------------------------------------------------------------------------------------
// Name: GetDateTimeOutputParamTest
//
// Description:
//  Tests multiple DATETIME values.
//
GetDateTimeOutputParamTest
Description:
Tests multiple DATETIME values.
GetDateTimeOutputParamTest Description : Tests multiple DATETIME values .
TEST_F(RExtensionApiTests, GetDateTimeOutputParamTest)
	{
		string scriptString =
			"Sys.setenv(TZ = 'UTC');"
			 Max Datetime value
			"param1 <- as.POSIXct(strptime('9999/12/31 23:59:59.000092', '%Y/%m/%d %H:%M:%OS'));"
			 Min Datetime value
			"param2 <- as.POSIXct(strptime('1/1/1 0:0:0.0', '%Y/%m/%d %H:%M:%OS'));"
			 Normal Datetime value
			"param3 <- as.POSIXct(strptime('1470/7/27 17:47:52.000123456', '%Y/%m/%d %H:%M:%OS'));"
			 Nanoseconds are divisible by 10^9
			"param4 <- as.POSIXct(strptime('2020/9/01 18:23:58.100000000', '%Y/%m/%d %H:%M:%OS'));"
			 Nanoseconds are all 9 digits
			"param5 <- as.POSIXct(strptime('1970/10/31 8:30:2.123654489', '%Y/%m/%d %H:%M:%OS'));"
			 Unset the time zone.
			"Sys.unsetenv('TZ');"
			 Default is the local time zone on Linux, whereas it is UTC on Windows - test that now
			"param6 <- as.POSIXct(strptime('1970/10/31 8:30:2.123654489', '%Y/%m/%d %H:%M:%OS'));"
			 Following NA values don't matter in which time zone they are tested.
			 Explicit NA value
			"param7 <- as.POSIXct(NA);"
			 0 Datetime value
			"param8 <- as.POSIXct(strptime('0000/00/00 00:00:00.00000', '%Y/%m/%d %H:%M:%OS'));"
			 Invalid Datetime is also NA
			"param9 <- as.POSIXct(strptime('1000/13/09 00:59:00.00000', '%Y/%m/%d %H:%M:%OS'));";
		int paramsNumber = 9;
		int numberOfNAs = 3;
		vector<SQL_TIMESTAMP_STRUCT> initParamValues(paramsNumber, { 0, 0, 0, 0, 0, 0, 0 });
		vector<SQL_TIMESTAMP_STRUCT> expectedParamValues = {
			{ 9999, 12, 31, 23, 59, 59, 92000 },
			{ 1, 1, 1, 0, 0, 0, 0 },
			{ 1470, 7, 27, 17, 47, 52, 124'000 },
			{ 2020, 9, 01, 18, 23, 58, 100'000'000 },
			{ 1970, 10, 31, 8, 30, 2, 123'654'000 } };
		 If R has this value in the local time zone - true on Linux,
		 convert the last dateTime value into UTC since Rcpp and hence RExtension
		 return in UTC
		 On Windows, the default time zone is UTC itself so no need to do the conversion.
#ifdef _WIN64
		expectedParamValues.push_back(expectedParamValues[expectedParamValues.size()-1]);
#else
		expectedParamValues.push_back(Utilities::ToUtc<SQL_TIMESTAMP_STRUCT>
				(expectedParamValues[expectedParamValues.size()-1]));
#endif
		expectedParamValues.insert(expectedParamValues.end(), numberOfNAs, { 0, 0, 0, 0, 0, 0, 0 });
		vector<SQLSMALLINT> inputOutputTypes(paramsNumber, SQL_PARAM_INPUT_OUTPUT);
		vector<SQLINTEGER> expectedStrLenOrInd(paramsNumber - numberOfNAs, sizeof(SQL_TIMESTAMP_STRUCT));
		expectedStrLenOrInd.insert(expectedStrLenOrInd.end(), numberOfNAs, SQL_NULL_DATA);
		 Initialize with a Session that executes the above script
		 that sets output parameters.
		InitializeSession(
			0,               inputSchemaColumnsNumber
			scriptString,
			paramsNumber);
		InitDateTimeParam<SQL_TIMESTAMP_STRUCT, Rcpp::DatetimeVector, Rcpp::Datetime, SQL_C_TYPE_TIMESTAMP>(
			initParamValues,
			expectedStrLenOrInd,
			inputOutputTypes,
			false);  validate
		SQLUSMALLINT outputSchemaColumnsNumber = 0;
		SQLRETURN result = (*sm_executeFuncPtr)(
			*m_sessionId,
			m_taskId,
			0,        rowsNumber
			nullptr,  dataSet
			nullptr,  strLen_or_Ind
			&outputSchemaColumnsNumber);
		ASSERT_EQ(result, SQL_SUCCESS);
		EXPECT_EQ(outputSchemaColumnsNumber, 0);
		 Verify that the parameters we get back are what we expect
		GetDateTimeOutputParam<SQL_TIMESTAMP_STRUCT>(
			expectedParamValues,
			expectedStrLenOrInd);
	}
TEST_F ( RExtensionApiTests , GetDateTimeOutputParamTest ) { string scriptString = " " " " " " " " " " " " " " " " " " " " " " ; int paramsNumber = 9 ; int numberOfNAs = 3 ; vector < SQL_TIMESTAMP_STRUCT > initParamValues ( paramsNumber , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } ) ; vector < SQL_TIMESTAMP_STRUCT > expectedParamValues = { { 9999 , 12 , 31 , 23 , 59 , 59 , 92000 } , { 1 , 1 , 1 , 0 , 0 , 0 , 0 } , { 1470 , 7 , 27 , 17 , 47 , 52 , 124'000 } , { 2020 , 9 , 01 , 18 , 23 , 58 , 100'000'000 } , { 1970 , 10 , 31 , 8 , 30 , 2 , 123'654'000 } } ; #ifdef _WIN64 expectedParamValues . push_back ( expectedParamValues [ expectedParamValues . size ( ) - 1 ] ) ; #else expectedParamValues . push_back ( Utilities :: ToUtc < SQL_TIMESTAMP_STRUCT > ( expectedParamValues [ expectedParamValues . size ( ) - 1 ] ) ) ; #endif expectedParamValues . insert ( expectedParamValues . end ( ) , numberOfNAs , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } ) ; vector < SQLSMALLINT > inputOutputTypes ( paramsNumber , SQL_PARAM_INPUT_OUTPUT ) ; vector < SQLINTEGER > expectedStrLenOrInd ( paramsNumber - numberOfNAs , sizeof ( SQL_TIMESTAMP_STRUCT ) ) ; expectedStrLenOrInd . insert ( expectedStrLenOrInd . end ( ) , numberOfNAs , SQL_NULL_DATA ) ; InitializeSession ( 0 , scriptString , paramsNumber ) ; InitDateTimeParam < SQL_TIMESTAMP_STRUCT , Rcpp :: DatetimeVector , Rcpp :: Datetime , SQL_C_TYPE_TIMESTAMP > ( initParamValues , expectedStrLenOrInd , inputOutputTypes , false ) ; SQLUSMALLINT outputSchemaColumnsNumber = 0 ; SQLRETURN result = ( * sm_executeFuncPtr ) ( * m_sessionId , m_taskId , 0 , nullptr , nullptr , & outputSchemaColumnsNumber ) ; ASSERT_EQ ( result , SQL_SUCCESS ) ; EXPECT_EQ ( outputSchemaColumnsNumber , 0 ) ; GetDateTimeOutputParam < SQL_TIMESTAMP_STRUCT > ( expectedParamValues , expectedStrLenOrInd ) ; }
Name: GetDateTimeOutputParamTest
Description:
Tests multiple DATETIME values.
Name : GetDateTimeOutputParamTest Description : Tests multiple DATETIME values .
// Set the time zone to UTC explicitly // // Max Datetime value // // Min Datetime value // // Normal Datetime value // // Nanoseconds are divisible by 10^9 // // Nanoseconds are all 9 digits // // Unset the time zone. // // Default is the local time zone on Linux, whereas it is UTC on Windows - test that now // // Following NA values don't matter in which time zone they are tested. // Explicit NA value // // 0 Datetime value // // Invalid Datetime is also NA // // If R has this value in the local time zone - true on Linux, // convert the last dateTime value into UTC since Rcpp and hence RExtension // return in UTC // On Windows, the default time zone is UTC itself so no need to do the conversion. // // Initialize with a Session that executes the above script // that sets output parameters. // // inputSchemaColumnsNumber // validate // rowsNumber // dataSet // strLen_or_Ind // Verify that the parameters we get back are what we expect //