-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial DB tests for CrateStorage #1309
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "test/librarytest.h" | ||
|
||
#include "library/crate/cratestorage.h" | ||
|
||
|
||
class CrateStorageTest : public LibraryTest { | ||
protected: | ||
CrateStorageTest() { | ||
m_crateStorage.connectDatabase(dbConnection()); | ||
} | ||
|
||
CrateStorage m_crateStorage; | ||
}; | ||
|
||
TEST_F(CrateStorageTest, persistentLifecycle) { | ||
const uint kNumCrates = 10; | ||
|
||
// Insert some crates | ||
for (auto i = kNumCrates; i > 0; --i) { | ||
Crate crate; | ||
crate.setName(QString("Crate %1").arg(i)); | ||
ASSERT_TRUE(m_crateStorage.onInsertingCrate(crate)); | ||
} | ||
EXPECT_EQ(kNumCrates, m_crateStorage.countCrates()); | ||
|
||
// The "middle" crate | ||
const auto kCrateIndex = (kNumCrates + 1) / 2; | ||
const auto kCrateName = QString("Crate %1").arg(kCrateIndex); | ||
CrateId crateId; | ||
|
||
// Find crate by name | ||
Crate crateByName; | ||
{ | ||
ASSERT_TRUE(m_crateStorage.readCrateByName(kCrateName, &crateByName)); | ||
EXPECT_EQ(kCrateName, crateByName.getName()); | ||
crateId = crateByName.getId(); | ||
ASSERT_TRUE(crateId.isValid()); | ||
} | ||
|
||
// Find crate by id | ||
Crate crateById; | ||
{ | ||
ASSERT_TRUE(m_crateStorage.readCrateById(crateId, &crateById)); | ||
EXPECT_EQ(crateId, crateById.getId()); | ||
EXPECT_EQ(kCrateName, crateById.getName()); | ||
} | ||
|
||
// Update crate name | ||
const auto kNewCrateName = QString("New%1").arg(kCrateName); | ||
Crate crateUpdated = crateById; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why create a new Crate variable here? I think the intention would be clearer by reusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. During each step a new, empty crate object should be populated directly from the database. Otherwise you can not be sure if it got its values from the previous read operation. Ideally the test would be split up into smaller tests, each with a special purpose. But here it was convenient to cover the whole lifecycle by a single test. |
||
crateUpdated.setName(kNewCrateName); | ||
ASSERT_TRUE(m_crateStorage.onUpdatingCrate(crateUpdated)); | ||
EXPECT_TRUE(m_crateStorage.readCrateById(crateId)); | ||
EXPECT_FALSE(m_crateStorage.readCrateByName(kCrateName)); | ||
|
||
// Find crate by new name | ||
Crate crateByNewName; | ||
{ | ||
ASSERT_TRUE(m_crateStorage.readCrateByName(kNewCrateName, &crateByNewName)); | ||
EXPECT_EQ(crateId, crateByNewName.getId()); | ||
EXPECT_EQ(kNewCrateName, crateByNewName.getName()); | ||
} | ||
|
||
// Delete crate | ||
ASSERT_TRUE(m_crateStorage.onDeletingCrate(crateId)); | ||
EXPECT_FALSE(m_crateStorage.readCrateById(crateId)); | ||
EXPECT_FALSE(m_crateStorage.readCrateByName(kCrateName)); | ||
EXPECT_FALSE(m_crateStorage.readCrateByName(kNewCrateName)); | ||
EXPECT_EQ(kNumCrates - 1, m_crateStorage.countCrates()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to clarify this comment a little: "Find the same crate as above, this time by ID instead of name"