Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial DB tests for CrateStorage #1309

Merged
merged 2 commits into from
Jul 13, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/test/cratestorage_test.cpp
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
Copy link
Contributor

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"

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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 crateById

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());
}