summaryrefslogtreecommitdiff
path: root/src/database/test/test_database.cpp
blob: ebaa6307a1b548c72a2ab9615f0d9e45f81e3838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#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<Result<Song>> res(db->GetSongs(10).get());
    REQUIRE(res->values().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<Result<Song>> res(db->GetSongs(10).get());
    REQUIRE(res->values().size() == 3);
    CHECK(*res->values().at(0).tags().title == "Song 1");
    CHECK(res->values().at(0).data().id() == 1);
    CHECK(*res->values().at(1).tags().title == "Song 2");
    CHECK(res->values().at(1).data().id() == 2);
    CHECK(*res->values().at(2).tags().title == "Song 3");
    CHECK(res->values().at(2).data().id() == 3);

    SECTION("update with no filesystem changes") {
      db->Update();

      std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
      REQUIRE(new_res->values().size() == 3);
      CHECK(res->values().at(0) == new_res->values().at(0));
      CHECK(res->values().at(1) == new_res->values().at(1));
      CHECK(res->values().at(2) == new_res->values().at(2));
    }

    SECTION("update with all songs gone") {
      songs.songs.clear();

      db->Update();

      std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
      CHECK(new_res->values().size() == 0);

      SECTION("update with one song returned") {
        songs.MakeSong("song2.wav", "Song 2");

        db->Update();

        std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
        REQUIRE(new_res->values().size() == 1);
        CHECK(res->values().at(1) == new_res->values().at(0));
      }
    }

    SECTION("update with one song gone") {
      songs.songs.erase("song2.wav");

      db->Update();

      std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
      REQUIRE(new_res->values().size() == 2);
      CHECK(res->values().at(0) == new_res->values().at(0));
      CHECK(res->values().at(2) == new_res->values().at(1));
    }

    SECTION("update with tags changed") {
      songs.MakeSong("song3.exe", "The Song 3");

      db->Update();

      std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
      REQUIRE(new_res->values().size() == 3);
      CHECK(res->values().at(0) == new_res->values().at(0));
      CHECK(res->values().at(1) == new_res->values().at(1));
      CHECK(*new_res->values().at(2).tags().title == "The Song 3");
      // The id should not have changed, since this was just a tag update.
      CHECK(res->values().at(2).data().id() ==
            new_res->values().at(2).data().id());
    }

    SECTION("update with one new song") {
      songs.MakeSong("my song.midi", "Song 1 (nightcore remix)");

      db->Update();

      std::unique_ptr<Result<Song>> new_res(db->GetSongs(10).get());
      REQUIRE(new_res->values().size() == 4);
      CHECK(res->values().at(0) == new_res->values().at(0));
      CHECK(res->values().at(1) == new_res->values().at(1));
      CHECK(res->values().at(2) == new_res->values().at(2));
      CHECK(*new_res->values().at(3).tags().title ==
            "Song 1 (nightcore remix)");
      CHECK(new_res->values().at(3).data().id() == 4);
    }

    SECTION("get songs with pagination") {
      std::unique_ptr<Result<Song>> res(db->GetSongs(1).get());

      REQUIRE(res->values().size() == 1);
      CHECK(res->values().at(0).data().id() == 1);
      REQUIRE(res->next_page());

      res.reset(db->GetPage(&res->next_page().value()).get());

      REQUIRE(res->values().size() == 1);
      CHECK(res->values().at(0).data().id() == 2);
      REQUIRE(res->next_page());

      res.reset(db->GetPage(&res->next_page().value()).get());

      REQUIRE(res->values().size() == 1);
      CHECK(res->values().at(0).data().id() == 3);
      REQUIRE(!res->next_page());

      SECTION("page backwards") {
        REQUIRE(res->prev_page());

        res.reset(db->GetPage(&res->prev_page().value()).get());

        REQUIRE(res->values().size() == 1);
        CHECK(res->values().at(0).data().id() == 2);
        REQUIRE(res->prev_page());

        res.reset(db->GetPage(&res->prev_page().value()).get());

        REQUIRE(res->values().size() == 1);
        CHECK(res->values().at(0).data().id() == 1);
        REQUIRE(!res->prev_page());

        SECTION("page forwards again") {
          REQUIRE(res->next_page());

          res.reset(db->GetPage(&res->next_page().value()).get());

          REQUIRE(res->values().size() == 1);
          CHECK(res->values().at(0).data().id() == 2);
          CHECK(res->next_page());
          CHECK(res->prev_page());
        }
      }
    }
  }
}

}  // namespace database