summaryrefslogtreecommitdiffstats
path: root/test/enginelibrary/crate_test.cpp
blob: 287d634742741e88035f0d00cdb07cdd2ed72375 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
    This file is part of libdjinterop.

    libdjinterop is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libdjinterop is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with libdjinterop.  If not, see <http://www.gnu.org/licenses/>.
 */

#define BOOST_TEST_MODULE crate_test

#include <iostream>
#include <stdexcept>
#include <string>

#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>

#include <djinterop/crate.hpp>
#include <djinterop/database.hpp>
#include <djinterop/enginelibrary.hpp>
#include <djinterop/exceptions.hpp>
#include <djinterop/track.hpp>

#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x

namespace el = djinterop::enginelibrary;
namespace fs = boost::filesystem;

const std::string sample_path{STRINGIFY(TESTDATA_DIR) "/el3"};

static fs::path create_temp_dir()
{
    fs::path temp_dir{fs::temp_directory_path()};
    temp_dir /= fs::unique_path();
    if (!fs::create_directory(temp_dir))
    {
        throw std::runtime_error{"Failed to create tmp_dir"};
    }
    std::cout << "Created temp dir at " << temp_dir.string() << std::endl;
    return temp_dir;
}

static void remove_temp_dir(const fs::path &temp_dir)
{
    fs::remove_all(temp_dir);
    std::cout << "Removed temp dir at " << temp_dir.string() << std::endl;
}

static void copy_test_db_to_temp_dir(const fs::path &temp_dir)
{
    auto db = el::load_database(sample_path);
    fs::path music_db_path{el::music_db_path(db)};
    fs::path perfdata_db_path{el::perfdata_db_path(db)};
    fs::copy_file(music_db_path, temp_dir / music_db_path.filename());
    fs::copy_file(perfdata_db_path, temp_dir / perfdata_db_path.filename());
}

static void check_crate_2(djinterop::crate c)
{
    BOOST_CHECK(c.is_valid());
    BOOST_CHECK_EQUAL(c.name(), "Bar Crate");
    BOOST_CHECK(!c.parent());
    BOOST_CHECK(c.children().empty());
    BOOST_CHECK(c.descendants().empty());
    BOOST_CHECK(c.tracks().empty());
}

/*
// Not currently used in any test cases.
static void populate_crate_3(djinterop::crate &c)
{
    c.set_name("Sub-Foo Crate");
    c.set_parent_id(1);
    std::unordered_set<int> track_ids;
    track_ids.insert(1);
    c.set_tracks(std::begin(track_ids), std::end(track_ids));
}
*/

static void check_crate_3(djinterop::crate &c)
{
    BOOST_CHECK_EQUAL(c.name(), "Sub-Foo Crate");
    BOOST_CHECK(c.parent());
    BOOST_CHECK_EQUAL(c.parent()->id(), 1);

    auto children = c.children();
    BOOST_REQUIRE_EQUAL(children.size(), 1);
    BOOST_CHECK_EQUAL(children[0].id(), 4);

    auto tracks = c.tracks();
    BOOST_REQUIRE_EQUAL(tracks.size(), 1);
    BOOST_CHECK_EQUAL(tracks[0].id(), 1);
}

BOOST_AUTO_TEST_CASE(all_crates__sample_db__expected_ids)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act
    auto results = db.crates();

    // Assert
    BOOST_CHECK_EQUAL(results.size(), 4);
    BOOST_CHECK_EQUAL(results[0].id(), 1);
    BOOST_CHECK_EQUAL(results[1].id(), 2);
    BOOST_CHECK_EQUAL(results[2].id(), 3);
    BOOST_CHECK_EQUAL(results[3].id(), 4);
}

BOOST_AUTO_TEST_CASE(all_root_crates__sample_db__expected_ids)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act
    auto results = db.root_crates();

    // Assert
    BOOST_CHECK_EQUAL(results.size(), 2);
    BOOST_CHECK_EQUAL(results[0].id(), 1);
    BOOST_CHECK_EQUAL(results[1].id(), 2);
}

BOOST_AUTO_TEST_CASE(ctor__crate3__correct_fields)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act
    auto c = *db.crate_by_id(3);

    // Assert
    BOOST_CHECK_EQUAL(c.id(), 3);
    check_crate_3(c);
}

BOOST_AUTO_TEST_CASE(ctor__nonexistent_crate__none)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act / Assert
    BOOST_CHECK(!db.crate_by_id(123));
}

BOOST_AUTO_TEST_CASE(create_root_crate__good_values__succeeds)
{
    // Arrange/Act
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto c = db.create_root_crate("Bar Crate");

    // Assert
    BOOST_CHECK_NE(c.id(), 0);
    check_crate_2(c);
    auto c_reloaded = *db.crate_by_id(c.id());
    check_crate_2(c_reloaded);
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(ctor_copy__saved_track__copied_fields)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto c = db.create_root_crate("Bar Crate");

    // Act
    djinterop::crate copy{c};

    // Assert
    BOOST_CHECK_EQUAL(copy.id(), c.id());
    check_crate_2(c);
    check_crate_2(copy);
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(set_name__existing_track_good_values__saves)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto c = db.create_root_crate("Foo Crate");
    auto crate_id = c.id();

    // Act
    c.set_name("Bar Crate");

    // Assert
    BOOST_CHECK_EQUAL(c.id(), crate_id);
    auto c_reloaded = *db.crate_by_id(c.id());
    check_crate_2(c);
    check_crate_2(c_reloaded);
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(set_parent__change_hierarchy__saves)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    // Arrange a hierarchy of c1 (root) -> c2 -> c3
    auto c1 = db.create_root_crate("Grandfather");
    auto c2 = db.create_root_crate("Father");
    auto c3 = db.create_root_crate("Son");

    // Act
    c2.set_parent(c1);
    c3.set_parent(c2);
    // Change c2's parent
    c2.set_parent({});

    // Assert
    BOOST_CHECK(!c1.parent());
    BOOST_CHECK(!c2.parent());
    BOOST_CHECK(c3.parent());
    BOOST_CHECK_EQUAL(c3.parent()->id(), c2.id());
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(add_track__valid_track__saves)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    copy_test_db_to_temp_dir(temp_dir);
    auto db = el::load_database(temp_dir.string());
    auto c = *db.crate_by_id(2);

    // Act
    c.add_track(*db.track_by_id(1));

    // Assert
    auto c_reloaded = *db.crate_by_id(2);
    auto tracks = c_reloaded.tracks();
    BOOST_REQUIRE_EQUAL(tracks.size(), 1);
    BOOST_CHECK_EQUAL(tracks[0].id(), 1);
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(op_copy_assign__saved_track__copied_fields)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto c = db.create_root_crate("Bar Crate");

    // Act
    auto copy = c;

    // Assert
    BOOST_CHECK_EQUAL(copy.id(), c.id());
    check_crate_2(copy);
    remove_temp_dir(temp_dir);
}

BOOST_AUTO_TEST_CASE(crate_by_name__crate2__found)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act
    auto crates = db.crates_by_name("Bar Crate");

    // Assert
    BOOST_CHECK_EQUAL(crates.size(), 1);
    BOOST_CHECK_EQUAL(crates[0].id(), 2);
}

BOOST_AUTO_TEST_CASE(crate_by_name__invalid_crate__not_found)
{
    // Arrange
    auto db = el::load_database(sample_path);

    // Act
    auto crates = db.crates_by_name("Non-existent Crate");

    // Assert
    BOOST_CHECK(crates.empty());
}

BOOST_AUTO_TEST_CASE(set_name__invalid_name__throws)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto c = db.create_root_crate("Root");

    // Act/Assert
    BOOST_CHECK_THROW(
        c.set_name(""), djinterop::crate_invalid_name);
    BOOST_CHECK_THROW(
        c.set_name("Contains ; semicolon"), djinterop::crate_invalid_name);
}

BOOST_AUTO_TEST_CASE(create_sub_crate__valid_name__succeeds)
{
    // Arrange
    auto temp_dir = create_temp_dir();
    auto db = el::create_database(temp_dir.string(), el::version_1_7_1);
    auto