Skip to content

Commit

Permalink
Add test_us_buffer.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-auc committed Sep 1, 2024
1 parent 60e801e commit 7b370a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 93 additions & 0 deletions test/utils/test_us_buffer.cpp
Original file line number Diff line number Diff line change
@@ -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<MockUS_DB2> 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<QString, US_BufferComponent> 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<MockUS_DB2> mockDb("dummy_password");
// Mock::AllowLeak(&mockDb);
//
// // Simulate a database connection error
// EXPECT_CALL(mockDb, lastErrno()).WillOnce(Return(US_DB2::NOT_CONNECTED));
//
// QMap<QString, US_BufferComponent> componentList;
//
// US_BufferComponent bufferComponent;
// bufferComponent.getAllFromDB("dummy_password", componentList);
//
// ASSERT_EQ(componentList.size(), 0);
}

//TEST_F(US_BufferComponentTest, GetInfoFromDB_Success) {
// NiceMock<MockUS_DB2> 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<MockUS_DB2> 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);
}
2 changes: 1 addition & 1 deletion test/utils/test_us_datafiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 7b370a8

Please sign in to comment.