Skip to content

Commit

Permalink
- Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenziv committed Jan 16, 2021
1 parent c1022be commit 611c37b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/domain/inventory/inventorycontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ INVENTORYAPISTATUS InventoryController::removeUOM(const std::string& id) {
mCachedUOMs.find(id, &entity::UnitOfMeasurement::ID);
if (it == mCachedUOMs.endOfData()) {
LOG_ERROR("Unit of measurement ID %s was not found.", id.c_str());
mView->showDataNotReadyScreen();
return INVENTORYAPISTATUS::NOT_FOUND;
}
LOG_INFO("Removing %s", it->name().c_str());
Expand Down
99 changes: 99 additions & 0 deletions core/domain/unittest/test_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ class TestInventory : public testing::Test {
entity::Product validProduct;
};

TEST_F(TestInventory, InitWithViewNotInitialized) {
try {
InventoryController dummyController(dpMock, nullptr);
FAIL() << "Expected std::invalid_argument";
}
catch(std::invalid_argument const & err) {
EXPECT_EQ(err.what(), std::string("Received a nulltpr argument"));
}
catch(...) {
FAIL() << "Expected std::invalid_argument";
}
}

TEST_F(TestInventory, InitWithDataNotInitialized) {
try {
InventoryController dummyController(nullptr, viewMock);
FAIL() << "Expected std::invalid_argument";
}
catch(std::invalid_argument const & err) {
EXPECT_EQ(err.what(), std::string("Received a nulltpr argument"));
}
catch(...) {
FAIL() << "Expected std::invalid_argument";
}
}

TEST_F(TestInventory, TestGetProductsList) {
// Fake that there is at least one product data on record
EXPECT_CALL(*dpMock, getProducts())
Expand Down Expand Up @@ -290,6 +316,79 @@ TEST_F(TestInventory, TestUpdateProduct) {
newStockValue.c_str());
}

TEST_F(TestInventory, TestSaveUOM) {
// Must perform the create call
EXPECT_CALL(*dpMock, createUOM(_));

ASSERT_EQ(inventoryController.save(entity::UnitOfMeasurement("2", "Kilogram", "kg")),
INVENTORYAPISTATUS::SUCCESS);
}

TEST_F(TestInventory, TestSaveUOMWithExistingID) {
// Setup an existing UOM
EXPECT_CALL(*dpMock, getUOMs())
.WillOnce(Return(
std::vector<entity::UnitOfMeasurement>{
entity::UnitOfMeasurement("1", "Liter", "L")}));
inventoryController.getMeasurementList();

// Fake that we're saving the same ID (1)
ASSERT_EQ(inventoryController.save(entity::UnitOfMeasurement("1", "Kilogram", "kg")),
INVENTORYAPISTATUS::FAILED);
}

TEST_F(TestInventory, TestSaveUOMWithExistingName) {
// Setup an existing UOM
EXPECT_CALL(*dpMock, getUOMs())
.WillOnce(Return(
std::vector<entity::UnitOfMeasurement>{
entity::UnitOfMeasurement("1", "Liter", "L")}));
inventoryController.getMeasurementList();

// Fake that we're saving the same name (Liter)
ASSERT_EQ(inventoryController.save(entity::UnitOfMeasurement("2", "Liter", "kg")),
INVENTORYAPISTATUS::FAILED);
}

TEST_F(TestInventory, TestSaveUOMWithExistingAbbreviation) {
// Setup an existing UOM
EXPECT_CALL(*dpMock, getUOMs())
.WillOnce(Return(
std::vector<entity::UnitOfMeasurement>{
entity::UnitOfMeasurement("1", "Liter", "L")}));
inventoryController.getMeasurementList();

// Fake that we're saving the same name (L)
ASSERT_EQ(inventoryController.save(entity::UnitOfMeasurement("2", "Kilogram", "L")),
INVENTORYAPISTATUS::FAILED);
}

TEST_F(TestInventory, TestRemoveUOM) {
// Setup an existing UOM
EXPECT_CALL(*dpMock, getUOMs())
.WillOnce(Return(
std::vector<entity::UnitOfMeasurement>{
entity::UnitOfMeasurement("1", "Liter", "L")}));
inventoryController.getMeasurementList();

// Must perform the removeUOM call
EXPECT_CALL(*dpMock, removeUOM(_));

ASSERT_EQ(inventoryController.removeUOM("1"), INVENTORYAPISTATUS::SUCCESS);
}

TEST_F(TestInventory, TestRemoveUOMWithIDNotFound) {
// Setup an existing UOM
EXPECT_CALL(*dpMock, getUOMs())
.WillOnce(Return(
std::vector<entity::UnitOfMeasurement>{
entity::UnitOfMeasurement("1", "Liter", "L")}));
inventoryController.getMeasurementList();

// Fake that we're removing a non-exisitng ID (2)
ASSERT_EQ(inventoryController.removeUOM("2"), INVENTORYAPISTATUS::NOT_FOUND);
}

} // namespace test
} // namespace inventory
} // namespace domain

0 comments on commit 611c37b

Please sign in to comment.