Skip to content

Commit 5c289ab

Browse files
authored
Merge pull request #485 from kiwix/fix_for_issue478
2 parents ba44033 + ec9186b commit 5c289ab

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/library.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ bool Library::removeBookmark(const std::string& zimId, const std::string& url)
106106

107107
bool Library::removeBookById(const std::string& id)
108108
{
109+
m_bookDB->delete_document("Q" + id);
110+
m_readers.erase(id);
109111
return m_books.erase(id) == 1;
110112
}
111113

@@ -330,7 +332,7 @@ Library::BookIdCollection Library::filter(const Filter& filter)
330332
{
331333
BookIdCollection result;
332334
for(auto id : getBooksByTitleOrDescription(filter)) {
333-
if(filter.acceptByNonQueryCriteria(m_books[id])) {
335+
if(filter.acceptByNonQueryCriteria(m_books.at(id))) {
334336
result.push_back(id);
335337
}
336338
}

test/library.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,49 @@ TEST_F(LibraryTest, getBookByPath)
283283
EXPECT_EQ(lib.getBookByPath(path).getId(), book.getId());
284284
EXPECT_THROW(lib.getBookByPath("non/existant/path.zim"), std::out_of_range);
285285
}
286+
287+
class XmlLibraryTest : public ::testing::Test {
288+
protected:
289+
void SetUp() override {
290+
kiwix::Manager manager(&lib);
291+
manager.readFile( "./test/library.xml", true, true);
292+
}
293+
294+
kiwix::Library lib;
295+
};
296+
297+
TEST_F(XmlLibraryTest, removeBookByIdRemovesTheBook)
298+
{
299+
EXPECT_EQ(3U, lib.getBookCount(true, true));
300+
EXPECT_NO_THROW(lib.getBookById("raycharles"));
301+
lib.removeBookById("raycharles");
302+
EXPECT_EQ(2U, lib.getBookCount(true, true));
303+
EXPECT_THROW(lib.getBookById("raycharles"), std::out_of_range);
304+
};
305+
306+
TEST_F(XmlLibraryTest, removeBookByIdDropsTheReader)
307+
{
308+
EXPECT_NE(nullptr, lib.getReaderById("raycharles"));
309+
lib.removeBookById("raycharles");
310+
EXPECT_THROW(lib.getReaderById("raycharles"), std::out_of_range);
311+
};
312+
313+
TEST_F(XmlLibraryTest, removeBookByIdUpdatesTheSearchDB)
314+
{
315+
kiwix::Filter f;
316+
f.local(true).valid(true).query(R"(title:"ray charles")", false);
317+
318+
EXPECT_NO_THROW(lib.getBookById("raycharles"));
319+
EXPECT_EQ(1U, lib.filter(f).size());
320+
321+
lib.removeBookById("raycharles");
322+
323+
EXPECT_THROW(lib.getBookById("raycharles"), std::out_of_range);
324+
EXPECT_EQ(0U, lib.filter(f).size());
325+
326+
// make sure that Library::filter() doesn't add an empty book with
327+
// an id surviving in the search DB
328+
EXPECT_THROW(lib.getBookById("raycharles"), std::out_of_range);
329+
};
330+
286331
};

0 commit comments

Comments
 (0)