From 7b370a88e30b380b1e40382cecb82aa1310eba3f Mon Sep 17 00:00:00 2001 From: aaron Date: Sun, 1 Sep 2024 19:32:38 -0400 Subject: [PATCH] Add test_us_buffer.cpp --- test/utils/test_us_buffer.cpp | 93 ++++++++++++++++++++++++++++++++ test/utils/test_us_datafiles.cpp | 2 +- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/utils/test_us_buffer.cpp diff --git a/test/utils/test_us_buffer.cpp b/test/utils/test_us_buffer.cpp new file mode 100644 index 000000000..47bc3259b --- /dev/null +++ b/test/utils/test_us_buffer.cpp @@ -0,0 +1,93 @@ +// us_buffer_test.cpp + +#include "gtest/gtest.h" +#include "gmock/gmock.h" + +#include "us_buffer.h" +#include "mock/mock_us_db2.h" + +using namespace testing; + +class US_BufferComponentTest : public ::testing::Test { +protected: + void SetUp() override { + // Set up any shared resources or initial conditions + } + + void TearDown() override { + // Clean up any shared resources or reset conditions + } +}; + +//TEST_F(US_BufferComponentTest, GetAllFromDB_Success) { +// NiceMock mockDb("dummy_password"); +// Mock::AllowLeak(&mockDb); +// +// // Set up expectations for the mock object +// EXPECT_CALL(mockDb, lastErrno()).WillOnce(Return(US_DB2::OK)); +// EXPECT_CALL(mockDb, query(_)).Times(1); +// EXPECT_CALL(mockDb, next()).WillOnce(Return(true)) +// .WillOnce(Return(false)); // Simulate one result +// EXPECT_CALL(mockDb, value(0)).WillOnce(Return("1")); +// +// QMap componentList; +// +// US_BufferComponent bufferComponent; +// bufferComponent.getAllFromDB("dummy_password", componentList); +// +// ASSERT_EQ(componentList.size(), 1); +// EXPECT_EQ(componentList.begin().key(), "1"); +//} + +TEST_F(US_BufferComponentTest, GetAllFromDB_DBError) { + qDebug() << "Refer to this issue https://github.com/ehb54/ultrascan-tickets/issues/350"; +// NiceMock mockDb("dummy_password"); +// Mock::AllowLeak(&mockDb); +// +// // Simulate a database connection error +// EXPECT_CALL(mockDb, lastErrno()).WillOnce(Return(US_DB2::NOT_CONNECTED)); +// +// QMap componentList; +// +// US_BufferComponent bufferComponent; +// bufferComponent.getAllFromDB("dummy_password", componentList); +// +// ASSERT_EQ(componentList.size(), 0); +} + +//TEST_F(US_BufferComponentTest, GetInfoFromDB_Success) { +// NiceMock mockDb("dummy_password"); +// Mock::AllowLeak(&mockDb); +// +// // Set up expectations for the mock object +// EXPECT_CALL(mockDb, query(_)).Times(1); +// EXPECT_CALL(mockDb, next()).WillOnce(Return(true)); +// EXPECT_CALL(mockDb, value(_)).WillRepeatedly(Return("test_value")); +// +// US_BufferComponent bufferComponent; +// bufferComponent.componentID = "1"; +// bufferComponent.getInfoFromDB(&mockDb); +// +// EXPECT_EQ(bufferComponent.unit, "test_value"); +// EXPECT_EQ(bufferComponent.name, "test_value"); +// // Continue with other field checks... +//} + +TEST_F(US_BufferComponentTest, GetSpectrum_Success) { + NiceMock mockDb("dummy_password"); + Mock::AllowLeak(&mockDb); + + // Set up expectations for the mock object + EXPECT_CALL(mockDb, query(_)).Times(1); + EXPECT_CALL(mockDb, next()).WillOnce(Return(true)) + .WillOnce(Return(false)); // Simulate one result + EXPECT_CALL(mockDb, value(1)).WillOnce(Return(500.0)); // Wavelength + EXPECT_CALL(mockDb, value(2)).WillOnce(Return(1.5)); // Value + + US_Buffer buffer; + buffer.bufferID = "1"; + buffer.getSpectrum(&mockDb, "Extinction"); + + ASSERT_EQ(buffer.extinction.size(), 1); + EXPECT_EQ(buffer.extinction[500.0], 1.5); +} \ No newline at end of file diff --git a/test/utils/test_us_datafiles.cpp b/test/utils/test_us_datafiles.cpp index 3d869969f..b8cef063e 100755 --- a/test/utils/test_us_datafiles.cpp +++ b/test/utils/test_us_datafiles.cpp @@ -98,4 +98,4 @@ void TestUSDataFiles::test_gap_in_numbering() QString filename = US_DataFiles::get_filename(path, guid, lfchar, lkupTag, lkupAtt, newFile); QCOMPARE(filename, path + QDir::separator() + "M0000001.xml"); // Should fill the gap QVERIFY(newFile); -} +} \ No newline at end of file