diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-05-12 10:30:07 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-05-12 10:30:07 +1000 |
| commit | 961c8014ada037712e8c72f23430362e9f14c1ec (patch) | |
| tree | ce13e0a00fc0d0318d46e6dfbecf2360b4cc5e14 /src/database/test | |
| parent | 10eb120878e01579bff2fdfab7bef59639b21155 (diff) | |
| download | tangara-fw-961c8014ada037712e8c72f23430362e9f14c1ec.tar.gz | |
Add some basic tests for the database
Diffstat (limited to 'src/database/test')
| -rw-r--r-- | src/database/test/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/database/test/test_database.cpp | 159 | ||||
| -rw-r--r-- | src/database/test/test_records.cpp | 20 |
3 files changed, 180 insertions, 3 deletions
diff --git a/src/database/test/CMakeLists.txt b/src/database/test/CMakeLists.txt index af42a78a..b5532da1 100644 --- a/src/database/test/CMakeLists.txt +++ b/src/database/test/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( - SRCS "test_records.cpp" + SRCS "test_records.cpp" "test_database.cpp" INCLUDE_DIRS "." - REQUIRES catch2 cmock database) + REQUIRES catch2 cmock database drivers fixtures) diff --git a/src/database/test/test_database.cpp b/src/database/test/test_database.cpp new file mode 100644 index 00000000..d61421ee --- /dev/null +++ b/src/database/test/test_database.cpp @@ -0,0 +1,159 @@ +#include "database.hpp" + +#include <stdint.h> +#include <iomanip> +#include <map> +#include <memory> +#include <string> + +#include "catch2/catch.hpp" +#include "driver_cache.hpp" +#include "esp_log.h" +#include "file_gatherer.hpp" +#include "i2c_fixture.hpp" +#include "leveldb/db.h" +#include "song.hpp" +#include "spi_fixture.hpp" +#include "tag_parser.hpp" + +namespace database { + +class TestBackends : public IFileGatherer, public ITagParser { + public: + std::map<std::string, SongTags> songs; + + auto MakeSong(const std::string& path, const std::string& title) -> void { + SongTags tags; + tags.encoding = Encoding::kMp3; + tags.title = title; + songs[path] = tags; + } + + auto FindFiles(const std::string& root, + std::function<void(const std::string&)> cb) -> void override { + for (auto keyval : songs) { + std::invoke(cb, keyval.first); + } + } + + auto ReadAndParseTags(const std::string& path, SongTags* out) + -> bool override { + if (songs.contains(path)) { + *out = songs.at(path); + return true; + } + return false; + } +}; + +TEST_CASE("song database", "[integration]") { + I2CFixture i2c; + SpiFixture spi; + drivers::DriverCache drivers; + auto storage = drivers.AcquireStorage(); + + Database::Destroy(); + + TestBackends songs; + auto open_res = Database::Open(&songs, &songs); + REQUIRE(open_res.has_value()); + std::unique_ptr<Database> db(open_res.value()); + + SECTION("empty database") { + std::unique_ptr<std::vector<Song>> res(db->GetSongs(10).get().values()); + REQUIRE(res->size() == 0); + } + + SECTION("add new songs") { + songs.MakeSong("song1.mp3", "Song 1"); + songs.MakeSong("song2.wav", "Song 2"); + songs.MakeSong("song3.exe", "Song 3"); + + db->Update(); + + std::unique_ptr<std::vector<Song>> res(db->GetSongs(10).get().values()); + REQUIRE(res->size() == 3); + CHECK(*res->at(0).tags().title == "Song 1"); + CHECK(res->at(0).data().id() == 1); + CHECK(*res->at(1).tags().title == "Song 2"); + CHECK(res->at(1).data().id() == 2); + CHECK(*res->at(2).tags().title == "Song 3"); + CHECK(res->at(2).data().id() == 3); + + SECTION("update with no filesystem changes") { + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + REQUIRE(new_res->size() == 3); + CHECK(res->at(0) == new_res->at(0)); + CHECK(res->at(1) == new_res->at(1)); + CHECK(res->at(2) == new_res->at(2)); + } + + SECTION("update with all songs gone") { + songs.songs.clear(); + + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + CHECK(new_res->size() == 0); + + SECTION("update with one song returned") { + songs.MakeSong("song2.wav", "Song 2"); + + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + REQUIRE(new_res->size() == 1); + CHECK(res->at(1) == new_res->at(0)); + } + } + + SECTION("update with one song gone") { + songs.songs.erase("song2.wav"); + + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + REQUIRE(new_res->size() == 2); + CHECK(res->at(0) == new_res->at(0)); + CHECK(res->at(2) == new_res->at(1)); + } + + SECTION("update with tags changed") { + songs.MakeSong("song3.exe", "The Song 3"); + + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + REQUIRE(new_res->size() == 3); + CHECK(res->at(0) == new_res->at(0)); + CHECK(res->at(1) == new_res->at(1)); + CHECK(*new_res->at(2).tags().title == "The Song 3"); + // The id should not have changed, since this was just a tag update. + CHECK(res->at(2).data().id() == new_res->at(2).data().id()); + } + + SECTION("update with one new song") { + songs.MakeSong("my song.midi", "Song 1 (nightcore remix)"); + + db->Update(); + + std::unique_ptr<std::vector<Song>> new_res( + db->GetSongs(10).get().values()); + REQUIRE(new_res->size() == 4); + CHECK(res->at(0) == new_res->at(0)); + CHECK(res->at(1) == new_res->at(1)); + CHECK(res->at(2) == new_res->at(2)); + CHECK(*new_res->at(3).tags().title == "Song 1 (nightcore remix)"); + CHECK(new_res->at(3).data().id() == 4); + } + } +} + +} // namespace database diff --git a/src/database/test/test_records.cpp b/src/database/test/test_records.cpp index 7c08e335..d84d2b6a 100644 --- a/src/database/test/test_records.cpp +++ b/src/database/test/test_records.cpp @@ -38,7 +38,7 @@ TEST_CASE("database record encoding", "[unit]") { } SECTION("round-trips") { - CHECK(BytesToSongId(as_bytes.data) == id); + CHECK(*BytesToSongId(as_bytes.data) == id); } SECTION("encodes compactly") { @@ -47,6 +47,12 @@ TEST_CASE("database record encoding", "[unit]") { CHECK(small_id.data.size() < large_id.data.size()); } + + SECTION("decoding rejects garbage") { + std::optional<SongId> res = BytesToSongId("i'm gay"); + + CHECK(res.has_value() == false); + } } SECTION("data keys") { @@ -95,6 +101,12 @@ TEST_CASE("database record encoding", "[unit]") { SECTION("round-trips") { CHECK(ParseDataValue(enc.slice) == data); } + + SECTION("decoding rejects garbage") { + std::optional<SongData> res = ParseDataValue("hi!"); + + CHECK(res.has_value() == false); + } } SECTION("hash keys") { @@ -116,6 +128,12 @@ TEST_CASE("database record encoding", "[unit]") { SECTION("round-trips") { CHECK(ParseHashValue(val.slice) == 123456); } + + SECTION("decoding rejects garbage") { + std::optional<SongId> res = ParseHashValue("the first song :)"); + + CHECK(res.has_value() == false); + } } } |
