Make tests
This commit is contained in:
parent
a8d485ffe1
commit
daff0dc8c9
4 changed files with 161 additions and 28 deletions
|
|
@ -1,14 +1,14 @@
|
||||||
pub const Alias = struct {
|
pub const Alias = struct {
|
||||||
@"sort-name": []const u8,
|
@"sort-name": []const u8,
|
||||||
//sort_name: []const u8,
|
//sort_name: []const u8,
|
||||||
@"type-id": []const u8,
|
@"type-id": ?[]const u8 = null,
|
||||||
//type_id: []const u8,
|
//type_id: []const u8,
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
locale: ?[]const u8,
|
locale: ?[]const u8 = null,
|
||||||
type: []const u8,
|
type: ?[]const u8 = null,
|
||||||
primary: ?[]const u8,
|
primary: ?[]const u8 = null,
|
||||||
@"begin-date": ?[]const u8,
|
@"begin-date": ?[]const u8 = null,
|
||||||
//begin_date: []const u8,
|
//begin_date: []const u8,
|
||||||
@"end-date": ?[]const u8,
|
@"end-date": ?[]const u8 = null,
|
||||||
//end_date: []const u8,
|
//end_date: []const u8,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,11 @@ pub const Artist = struct {
|
||||||
@"sort-name": []const u8,
|
@"sort-name": []const u8,
|
||||||
//sort_name: []const u8,
|
//sort_name: []const u8,
|
||||||
aliases: ?[]Alias = null,
|
aliases: ?[]Alias = null,
|
||||||
|
disambiguation: ?[]const u8 = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ArtistCredit = struct {
|
pub const ArtistCredit = struct {
|
||||||
|
joinphrase: ?[]const u8 = null,
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
artist: Artist,
|
artist: Artist,
|
||||||
};
|
};
|
||||||
|
|
@ -23,15 +25,18 @@ pub const Recording = struct {
|
||||||
video: ?bool,
|
video: ?bool,
|
||||||
@"artist-credit": []ArtistCredit,
|
@"artist-credit": []ArtistCredit,
|
||||||
//artist_credit: []ArtistCredit,
|
//artist_credit: []ArtistCredit,
|
||||||
@"first-release-date": []const u8,
|
@"first-release-date": ?[]const u8 = null,
|
||||||
//first_release_date: []const u8,
|
//first_release_date: []const u8,
|
||||||
releases: []Release,
|
releases: []Release,
|
||||||
|
isrcs: ?[][]const u8 = null,
|
||||||
|
tags: ?[]Tag = null,
|
||||||
|
disambiguation: ?[]const u8 = null,
|
||||||
};
|
};
|
||||||
pub const Release = struct {
|
pub const Release = struct {
|
||||||
id: []const u8,
|
id: []const u8,
|
||||||
@"status-id": ?[]const u8 = null,
|
@"status-id": ?[]const u8 = null,
|
||||||
//status_id: ?[]const u8 = null,
|
//status_id: ?[]const u8 = null,
|
||||||
count: i32,
|
count: u8,
|
||||||
title: []const u8,
|
title: []const u8,
|
||||||
@"artist-credit": []ArtistCredit,
|
@"artist-credit": []ArtistCredit,
|
||||||
//artist_credit: []ArtistCredit,
|
//artist_credit: []ArtistCredit,
|
||||||
|
|
@ -45,10 +50,11 @@ pub const Release = struct {
|
||||||
country: ?[]const u8 = null,
|
country: ?[]const u8 = null,
|
||||||
@"release-events": ?[]ReleaseEvent = null,
|
@"release-events": ?[]ReleaseEvent = null,
|
||||||
//release_events: ?[]ReleaseEvent = null,
|
//release_events: ?[]ReleaseEvent = null,
|
||||||
|
disambiguation: ?[]const u8 = null,
|
||||||
};
|
};
|
||||||
pub const Medium = struct {
|
pub const Medium = struct {
|
||||||
position: u8,
|
position: u8,
|
||||||
format: []const u8,
|
format: ?[]const u8 = null,
|
||||||
track: []Track,
|
track: []Track,
|
||||||
@"track-count": u8,
|
@"track-count": u8,
|
||||||
//track_count: u8,
|
//track_count: u8,
|
||||||
|
|
@ -82,4 +88,11 @@ pub const ReleaseGroup = struct {
|
||||||
title: []const u8,
|
title: []const u8,
|
||||||
@"primary-type": []const u8,
|
@"primary-type": []const u8,
|
||||||
//primary_type: []const u8,
|
//primary_type: []const u8,
|
||||||
|
@"secondary-types": ?[][]const u8 = null,
|
||||||
|
@"secondary-type-ids": ?[][]const u8 = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Tag = struct {
|
||||||
|
count: u8,
|
||||||
|
name: []const u8,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,74 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
const Entities = @import("Entities.zig");
|
const Entities = @import("Entities.zig");
|
||||||
|
const Artist = Entities.Artist;
|
||||||
const Recording = Entities.Recording;
|
const Recording = Entities.Recording;
|
||||||
|
|
||||||
pub const Result = struct {
|
pub const Result = struct {
|
||||||
created: ?[]const u8 = null,
|
created: ?[]const u8 = null,
|
||||||
count: ?u32 = null,
|
count: ?u32 = null,
|
||||||
offset: ?u32 = null,
|
offset: ?u32 = null,
|
||||||
|
artists: ?[]Artist = null,
|
||||||
recordings: ?[]Recording = null,
|
recordings: ?[]Recording = null,
|
||||||
@"error": ?[]const u8 = null,
|
@"error": ?[]const u8 = null,
|
||||||
help: ?[]const u8 = null,
|
help: ?[]const u8 = null,
|
||||||
|
|
||||||
|
pub fn getSpecifiedARID(self: *const Result, an: []const u8, tn: []const u8) ?[]const u8 {
|
||||||
|
if (self.count) |count| { // Otherwise there was an error
|
||||||
|
switch (count) {
|
||||||
|
0 => return null, // No results
|
||||||
|
1 => {
|
||||||
|
switch (self.recordings.?[0].@"artist-credit".len) {
|
||||||
|
0 => unreachable, // All recordings have at least one ArtistCredit
|
||||||
|
1 => return self.recordings.?[0].@"artist-credit"[0].artist.id,
|
||||||
|
else => {
|
||||||
|
for (self.recordings.?[0].@"artist-credit") |ac| {
|
||||||
|
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
for (self.recordings.?) |rc| {
|
||||||
|
if (std.mem.eql(u8, rc.title, tn)) { // I'd really prefer not including track name, but for complete accuracy
|
||||||
|
for (rc.@"artist-credit") |ac| {
|
||||||
|
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null; // Maybe return error here instead
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not sure if I want to get a ReleaseGroup or Release yet. Start with ReleaseGroup
|
||||||
|
pub fn getSpecifiedRGID(self: *const Result, tn: []const u8, rgn: []const u8) ?[]const u8 {
|
||||||
|
if (self.count) |count| {
|
||||||
|
switch (count) {
|
||||||
|
0 => return null,
|
||||||
|
1 => {
|
||||||
|
switch (self.recordings.?[0].releases.len) {
|
||||||
|
0 => unreachable,
|
||||||
|
1 => return self.recordings.?[0].releases[0].id,
|
||||||
|
else => {
|
||||||
|
for (self.recordings.?[0].releases) |re| {
|
||||||
|
if (std.mem.eql(u8, re.title, rgn)) return re.id;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => { // This is not ideal, limit the number of results
|
||||||
|
for (self.recordings.?) |rc| {
|
||||||
|
if (std.mem.eql(u8, rc.title, tn)) {
|
||||||
|
for (rc.releases) |re| {
|
||||||
|
if (std.mem.eql(u8, re.@"release-group".title, rgn)) return re.@"release-group".id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
95
src/root.zig
95
src/root.zig
|
|
@ -37,18 +37,10 @@ pub fn mbSearch(allocator: std.mem.Allocator, ar: *std.ArrayList(u8), track_name
|
||||||
return ar.items;
|
return ar.items;
|
||||||
}
|
}
|
||||||
|
|
||||||
//pub fn getAlbumMBID(query: QR.Result)
|
|
||||||
//pub fn getArtistMBID(query: QR.Result)
|
|
||||||
//pub fn getSongMBID(query: QR.Result)
|
|
||||||
|
|
||||||
//test "basic add functionality" {
|
|
||||||
// try testing.expect(add(3, 7) == 10);
|
|
||||||
//}
|
|
||||||
|
|
||||||
// This test is very volatile, but I think
|
// This test is very volatile, but I think
|
||||||
// these params are specific enough that
|
// these params are specific enough that
|
||||||
// it shouldn't need changing too often
|
// it shouldn't need changing too often
|
||||||
test "iamthemorning" {
|
test "arid_via_recording" {
|
||||||
const test_alloc = std.testing.allocator;
|
const test_alloc = std.testing.allocator;
|
||||||
const track: []const u8 = "Veni%20Veni%20Emmanuel";
|
const track: []const u8 = "Veni%20Veni%20Emmanuel";
|
||||||
const album: []const u8 = "Counting%20the%20Ghosts";
|
const album: []const u8 = "Counting%20the%20Ghosts";
|
||||||
|
|
@ -62,15 +54,80 @@ test "iamthemorning" {
|
||||||
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
|
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
|
||||||
|
|
||||||
if (search_result) |sr| {
|
if (search_result) |sr| {
|
||||||
const result = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
|
const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
|
||||||
defer result.deinit();
|
defer json.deinit();
|
||||||
try testing.expect(std.mem.eql(u8, result.value.recordings.?[0].@"artist-credit"[0].artist.id, iatm_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
//defer result.deinit();
|
const result: QR.Result = json.value;
|
||||||
//std.log.err("{s}", .{search_result.?.recordings.?[0].id});
|
|
||||||
//if (search_result) |sr| {
|
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID(artist, "Veni veni Emmanuel").?, iatm_id));
|
||||||
// const recording = sr.recordings.?[0];
|
}
|
||||||
// try testing.expect(std.mem.eql(u8, recording.id, iatm_id));
|
}
|
||||||
//}
|
|
||||||
|
test "arid_via_recording_multiple_artists_1" {
|
||||||
|
const test_alloc = std.testing.allocator;
|
||||||
|
const track: []const u8 = "Roll%20Me%20Up%20And%20Smoke%20Me%20When%20I%20Die";
|
||||||
|
const album: []const u8 = "Willie%20Nelson%20American%20Outlaw";
|
||||||
|
const artist: []const u8 = "Lyle%20Lovett";
|
||||||
|
|
||||||
|
const ll_id: []const u8 = "7241e3ed-5ad4-4849-94df-6858ea833472";
|
||||||
|
|
||||||
|
var mb_result = std.ArrayList(u8).init(test_alloc);
|
||||||
|
defer mb_result.deinit();
|
||||||
|
|
||||||
|
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
|
||||||
|
|
||||||
|
if (search_result) |sr| {
|
||||||
|
const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
|
||||||
|
defer json.deinit();
|
||||||
|
|
||||||
|
const result: QR.Result = json.value;
|
||||||
|
|
||||||
|
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID("Lyle Lovett", "Roll Me Up and Smoke Me When I Die").?, ll_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "rgid_via_recording" {
|
||||||
|
const test_alloc = std.testing.allocator;
|
||||||
|
const track: []const u8 = "I%20Of%20The%20Storm";
|
||||||
|
const album: []const u8 = "Beneath%20The%20Skin";
|
||||||
|
const artist: []const u8 = "Of%20Monsters%20and%20Men";
|
||||||
|
|
||||||
|
const bts_id: []const u8 = "2e1e605d-5090-420d-beae-e7ff73791082";
|
||||||
|
|
||||||
|
var mb_result = std.ArrayList(u8).init(test_alloc);
|
||||||
|
defer mb_result.deinit();
|
||||||
|
|
||||||
|
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
|
||||||
|
|
||||||
|
if (search_result) |sr| {
|
||||||
|
const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
|
||||||
|
defer json.deinit();
|
||||||
|
|
||||||
|
const result: QR.Result = json.value;
|
||||||
|
|
||||||
|
try testing.expect(std.mem.eql(u8, result.getSpecifiedRGID("I of the Storm", "Beneath the Skin").?, bts_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "rgid_via_recording_multiple_artists_2" {
|
||||||
|
const test_alloc = std.testing.allocator;
|
||||||
|
const track: []const u8 = "Hesitating%20Beauty";
|
||||||
|
const album: []const u8 = "Mermaid%Avenue";
|
||||||
|
const artist: []const u8 = "Wilco";
|
||||||
|
|
||||||
|
const wilco_id: []const u8 = "9e53f84d-ef44-4c16-9677-5fd4d78cbd7d";
|
||||||
|
|
||||||
|
var mb_result = std.ArrayList(u8).init(test_alloc);
|
||||||
|
defer mb_result.deinit();
|
||||||
|
|
||||||
|
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
|
||||||
|
|
||||||
|
if (search_result) |sr| {
|
||||||
|
const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
|
||||||
|
defer json.deinit();
|
||||||
|
|
||||||
|
const result: QR.Result = json.value;
|
||||||
|
|
||||||
|
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID("Wilco", "Hesitating Beauty").?, wilco_id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue