Add slice2 and asResult functions

This commit is contained in:
simojenki
2021-03-02 10:16:56 +11:00
parent 3aa1056aa5
commit bdee01d2c7
6 changed files with 86 additions and 74 deletions

View File

@@ -66,7 +66,10 @@ describe("InMemoryMusicService", () => {
{ id: BLONDIE.id, name: BLONDIE.name },
{ id: METALLICA.id, name: METALLICA.name },
];
expect(await musicLibrary.artists({})).toEqual([artists, 4]);
expect(await musicLibrary.artists({})).toEqual({
results: artists,
total: 4,
});
});
});
@@ -76,10 +79,10 @@ describe("InMemoryMusicService", () => {
{ id: BLONDIE.id, name: BLONDIE.name },
{ id: METALLICA.id, name: METALLICA.name },
];
expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual([
artists,
4,
]);
expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({
results: artists,
total: 4,
});
});
});
@@ -92,7 +95,7 @@ describe("InMemoryMusicService", () => {
];
expect(
await musicLibrary.artists({ _index: 1, _count: 50 })
).toEqual([artists, 4]);
).toEqual({ results: artists, total: 4 });
});
});
});
@@ -123,33 +126,33 @@ describe("InMemoryMusicService", () => {
describe("albums", () => {
describe("fetching with no filtering", () => {
it("should return all the albums for all the artists", async () => {
expect(await musicLibrary.albums({})).toEqual([
ALL_ALBUMS,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({})).toEqual({
results: ALL_ALBUMS,
total: ALL_ALBUMS.length,
});
});
});
describe("fetching for a single artist", () => {
it("should return them all if the artist has some", async () => {
expect(await musicLibrary.albums({ artistId: BLONDIE.id })).toEqual([
BLONDIE.albums,
BLONDIE.albums.length,
]);
expect(await musicLibrary.albums({ artistId: BLONDIE.id })).toEqual({
results: BLONDIE.albums,
total: BLONDIE.albums.length,
});
});
it("should return empty list of the artists does not have any", async () => {
expect(await musicLibrary.albums({ artistId: MADONNA.id })).toEqual([
[],
0,
]);
expect(await musicLibrary.albums({ artistId: MADONNA.id })).toEqual({
results: [],
total: 0,
});
});
it("should return empty list if the artist id is not valid", async () => {
expect(await musicLibrary.albums({ artistId: uuid() })).toEqual([
[],
0,
]);
expect(await musicLibrary.albums({ artistId: uuid() })).toEqual({
results: [],
total: 0,
});
});
});
@@ -161,10 +164,10 @@ describe("InMemoryMusicService", () => {
...MADONNA.albums,
...METALLICA.albums,
];
expect(await musicLibrary.albums({ _index: 2 })).toEqual([
albums,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({ _index: 2 })).toEqual({
results: albums,
total: ALL_ALBUMS.length,
});
});
});
@@ -175,33 +178,33 @@ describe("InMemoryMusicService", () => {
BOB_MARLEY.albums[1],
BOB_MARLEY.albums[2],
];
expect(await musicLibrary.albums({ _count: 3 })).toEqual([
albums,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({ _count: 3 })).toEqual({
results: albums,
total: ALL_ALBUMS.length,
});
});
});
describe("fetching with index and count", () => {
it("should be able to return the first page", async () => {
const albums = [BOB_MARLEY.albums[0], BOB_MARLEY.albums[1]];
expect(await musicLibrary.albums({ _index: 0, _count: 2 })).toEqual([
albums,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({ _index: 0, _count: 2 })).toEqual({
results: albums,
total: ALL_ALBUMS.length,
});
});
it("should be able to return the second page", async () => {
const albums = [BOB_MARLEY.albums[2], BLONDIE.albums[0]];
expect(await musicLibrary.albums({ _index: 2, _count: 2 })).toEqual([
albums,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({ _index: 2, _count: 2 })).toEqual({
results: albums,
total: ALL_ALBUMS.length,
});
});
it("should be able to return the last page", async () => {
expect(await musicLibrary.albums({ _index: 5, _count: 2 })).toEqual([
METALLICA.albums,
ALL_ALBUMS.length,
]);
expect(await musicLibrary.albums({ _index: 5, _count: 2 })).toEqual({
results: METALLICA.albums,
total: ALL_ALBUMS.length,
});
});
});
});

View File

@@ -10,6 +10,8 @@ import {
Artist,
MusicLibrary,
Paging,
slice2,
asResult,
} from "../src/music_service";
const artistWithAlbumsToArtist = (it: ArtistWithAlbums): Artist => ({
@@ -55,12 +57,10 @@ export class InMemoryMusicService implements MusicService {
if (this.users[credentials.username] != credentials.password)
return Promise.reject("Invalid auth token");
return Promise.resolve({
artists: ({ _index, _count }: Paging) => {
const i0 = _index || 0;
const i1 = _count ? i0 + _count : undefined;
const artists = this.artists.map(artistWithAlbumsToArtist);
return Promise.resolve([artists.slice(i0, i1), artists.length]);
},
artists: (paging: Paging) =>
Promise.resolve(this.artists.map(artistWithAlbumsToArtist))
.then(slice2(paging))
.then(asResult),
artist: (id: string) =>
pipe(
this.artists.find((it) => it.id === id),
@@ -76,20 +76,19 @@ export class InMemoryMusicService implements MusicService {
artistId?: string;
_index?: number;
_count?: number;
}) => {
const i0 = _index || 0;
const i1 = _count ? i0 + _count : undefined;
const albums = this.artists
.filter(
}) =>
Promise.resolve(
this.artists.filter(
pipe(
O.fromNullable(artistId),
O.map(artistWithId),
O.getOrElse(() => all)
)
)
.flatMap((it) => it.albums);
return Promise.resolve([albums.slice(i0, i1), albums.length]);
},
)
.then((artists) => artists.flatMap((it) => it.albums))
.then(slice2({ _index, _count }))
.then(asResult),
});
}

View File

@@ -114,7 +114,7 @@ describe("navidrome", () => {
{ id: "3c5113007a6b11eb87173bfb9b07f9b1", name: "AAAB" },
{ id: "3ca781c27a6b11eb897ebbb5773603ad", name: "BAAB" },
];
expect(artists).toEqual([expectedArtists, 4]);
expect(artists).toEqual({ results: expectedArtists, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams,
@@ -133,7 +133,7 @@ describe("navidrome", () => {
{ id: "3c0b9d7a7a6b11eb9773f398e6236ad6", name: "1200 Ounces" },
{ id: "3c5113007a6b11eb87173bfb9b07f9b1", name: "AAAB" },
];
expect(artists).toEqual([expectedArtists, 4]);
expect(artists).toEqual({ results: expectedArtists, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams,