|
|
|
@ -1,8 +1,9 @@ |
|
|
|
|
import { AlbumWithRefs, AlbumWithRefsWithId, ArtistWithRefs, ArtistWithRefsWithId, TagWithRefs, TagWithRefsWithId, TrackWithRefs, TrackWithRefsWithId } from "../../../client/src/api/api"; |
|
|
|
|
import { AlbumBaseWithRefs, AlbumWithRefs, AlbumWithRefsWithId, ArtistBaseWithRefs, ArtistWithRefs, ArtistWithRefsWithId, TagBaseWithRefs, TagWithRefs, TagWithRefsWithId, TrackBaseWithRefs, TrackWithRefs, TrackWithRefsWithId } from "../../../client/src/api/api"; |
|
|
|
|
import { userEndpoints } from "../../endpoints/User"; |
|
|
|
|
import { createAlbum, createArtist, createTag, createTrack, deleteAlbum, deleteArtist, deleteTag, deleteTrack, ReferenceDatabase } from "./DBReferenceModel"; |
|
|
|
|
import { createAlbum, createArtist, createTag, createTrack, deleteAlbum, deleteArtist, deleteTag, deleteTrack, modifyAlbum, modifyArtist, modifyTag, modifyTrack, ReferenceDatabase } from "./DBReferenceModel"; |
|
|
|
|
import * as helpers from '../integration/helpers'; |
|
|
|
|
import { DBErrorKind, isDBError } from "../../endpoints/types"; |
|
|
|
|
let _ = require('lodash'); |
|
|
|
|
|
|
|
|
|
export enum DBActionType { |
|
|
|
|
CreateTrack = "CreateTrack", |
|
|
|
@ -13,6 +14,14 @@ export enum DBActionType { |
|
|
|
|
DeleteAlbum = "DeleteAlbum", |
|
|
|
|
DeleteArtist = "DeleteArtist", |
|
|
|
|
DeleteTag = "DeleteTag", |
|
|
|
|
PutTrack = "PutTrack", |
|
|
|
|
PutAlbum = "PutAlbum", |
|
|
|
|
PutArtist = "PutArtist", |
|
|
|
|
PutTag = "PutTag", |
|
|
|
|
PatchTrack = "PatchTrack", |
|
|
|
|
PatchAlbum = "PatchAlbum", |
|
|
|
|
PatchArtist = "PatchArtist", |
|
|
|
|
PatchTag = "PatchTag", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface DBAction { |
|
|
|
@ -34,6 +43,14 @@ export interface RandomDBActionDistribution { |
|
|
|
|
deleteArtistParams: RandomDeleteObjectDistribution, |
|
|
|
|
deleteAlbumParams: RandomDeleteObjectDistribution, |
|
|
|
|
deleteTagParams: RandomDeleteObjectDistribution, |
|
|
|
|
patchTrackParams: RandomPatchTrackDistribution, |
|
|
|
|
patchAlbumParams: RandomPatchAlbumDistribution, |
|
|
|
|
patchArtistParams: RandomPatchArtistDistribution, |
|
|
|
|
patchTagParams: RandomPatchTagDistribution, |
|
|
|
|
putTrackParams: RandomPutTrackDistribution, |
|
|
|
|
putAlbumParams: RandomPutAlbumDistribution, |
|
|
|
|
putArtistParams: RandomPutArtistDistribution, |
|
|
|
|
putTagParams: RandomPutTagDistribution, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface RandomCreateTrackDistribution { |
|
|
|
@ -47,6 +64,15 @@ export interface RandomCreateTrackDistribution { |
|
|
|
|
} |
|
|
|
|
linkAlbum: Distribution<boolean | 'nonexistent'>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPutTrackDistribution extends RandomCreateTrackDistribution { |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPatchTrackDistribution extends RandomPutTrackDistribution { |
|
|
|
|
replaceName: Distribution<boolean>, |
|
|
|
|
replaceArtists: Distribution<boolean>, |
|
|
|
|
replaceTags: Distribution<boolean>, |
|
|
|
|
replaceAlbum: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface RandomCreateAlbumDistribution { |
|
|
|
|
linkArtists: { |
|
|
|
@ -62,6 +88,15 @@ export interface RandomCreateAlbumDistribution { |
|
|
|
|
numInvalid: Distribution<number>, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
export interface RandomPutAlbumDistribution extends RandomCreateAlbumDistribution { |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPatchAlbumDistribution extends RandomPutAlbumDistribution { |
|
|
|
|
replaceName: Distribution<boolean>, |
|
|
|
|
replaceArtists: Distribution<boolean>, |
|
|
|
|
replaceTags: Distribution<boolean>, |
|
|
|
|
replaceTracks: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface RandomCreateArtistDistribution { |
|
|
|
|
linkAlbums: { |
|
|
|
@ -77,10 +112,27 @@ export interface RandomCreateArtistDistribution { |
|
|
|
|
numInvalid: Distribution<number>, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
export interface RandomPutArtistDistribution extends RandomCreateArtistDistribution { |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPatchArtistDistribution extends RandomPutArtistDistribution { |
|
|
|
|
replaceName: Distribution<boolean>, |
|
|
|
|
replaceAlbums: Distribution<boolean>, |
|
|
|
|
replaceTags: Distribution<boolean>, |
|
|
|
|
replaceTracks: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface RandomCreateTagDistribution { |
|
|
|
|
linkParent: Distribution<boolean | 'nonexistent'>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPutTagDistribution extends RandomCreateTagDistribution { |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
export interface RandomPatchTagDistribution extends RandomPutTagDistribution { |
|
|
|
|
replaceName: Distribution<boolean>, |
|
|
|
|
replaceParent: Distribution<boolean>, |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface RandomDeleteObjectDistribution { |
|
|
|
|
validId: Distribution<boolean>, |
|
|
|
@ -169,6 +221,30 @@ export function applyReferenceDBAction( |
|
|
|
|
status = 200; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case DBActionType.PutTrack: |
|
|
|
|
case DBActionType.PutAlbum: |
|
|
|
|
case DBActionType.PutArtist: |
|
|
|
|
case DBActionType.PutTag: |
|
|
|
|
case DBActionType.PatchTrack: |
|
|
|
|
case DBActionType.PatchAlbum: |
|
|
|
|
case DBActionType.PatchArtist: |
|
|
|
|
case DBActionType.PatchTag: |
|
|
|
|
{ |
|
|
|
|
let funcs = { |
|
|
|
|
[DBActionType.PutTrack]: modifyTrack, |
|
|
|
|
[DBActionType.PutAlbum]: modifyAlbum, |
|
|
|
|
[DBActionType.PutArtist]: modifyArtist, |
|
|
|
|
[DBActionType.PutTag]: modifyTag, |
|
|
|
|
[DBActionType.PatchTrack]: modifyTrack, |
|
|
|
|
[DBActionType.PatchAlbum]: modifyAlbum, |
|
|
|
|
[DBActionType.PatchArtist]: modifyArtist, |
|
|
|
|
[DBActionType.PatchTag]: modifyTag, |
|
|
|
|
} |
|
|
|
|
funcs[action.type](action.userId, action.payload.id, _.omit(action.payload, ['id']), db); |
|
|
|
|
response = {}; |
|
|
|
|
status = 200; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
if (isDBError(e)) { |
|
|
|
@ -228,6 +304,30 @@ export async function applyRealDBAction( |
|
|
|
|
response = res.body; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case DBActionType.PutTrack: |
|
|
|
|
case DBActionType.PutAlbum: |
|
|
|
|
case DBActionType.PutArtist: |
|
|
|
|
case DBActionType.PutTag: |
|
|
|
|
case DBActionType.PatchTrack: |
|
|
|
|
case DBActionType.PatchAlbum: |
|
|
|
|
case DBActionType.PatchArtist: |
|
|
|
|
case DBActionType.PatchTag: |
|
|
|
|
{ |
|
|
|
|
let funcs = { |
|
|
|
|
[DBActionType.PutTrack]: helpers.putTrack, |
|
|
|
|
[DBActionType.PutAlbum]: helpers.putAlbum, |
|
|
|
|
[DBActionType.PutArtist]: helpers.putArtist, |
|
|
|
|
[DBActionType.PutTag]: helpers.putTag, |
|
|
|
|
[DBActionType.PatchTrack]: helpers.patchTrack, |
|
|
|
|
[DBActionType.PatchAlbum]: helpers.patchAlbum, |
|
|
|
|
[DBActionType.PatchArtist]: helpers.patchArtist, |
|
|
|
|
[DBActionType.PatchTag]: helpers.patchTag, |
|
|
|
|
} |
|
|
|
|
let res = await funcs[action.type](req, action.payload.id, _.omit(action.payload, ['id'])); |
|
|
|
|
status = res.status; |
|
|
|
|
response = res.body; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { response: response, status: status }; |
|
|
|
@ -252,20 +352,54 @@ export function randomDBAction( |
|
|
|
|
case DBActionType.CreateArtist: |
|
|
|
|
case DBActionType.CreateAlbum: |
|
|
|
|
case DBActionType.CreateTag: |
|
|
|
|
case DBActionType.PutTrack: |
|
|
|
|
case DBActionType.PutAlbum: |
|
|
|
|
case DBActionType.PutArtist: |
|
|
|
|
case DBActionType.PutTag: |
|
|
|
|
case DBActionType.PatchTrack: |
|
|
|
|
case DBActionType.PatchAlbum: |
|
|
|
|
case DBActionType.PatchArtist: |
|
|
|
|
case DBActionType.PatchTag: |
|
|
|
|
{ |
|
|
|
|
let funcs = { |
|
|
|
|
[DBActionType.CreateTrack]: createRandomTrack, |
|
|
|
|
[DBActionType.CreateArtist]: createRandomArtist, |
|
|
|
|
[DBActionType.CreateAlbum]: createRandomAlbum, |
|
|
|
|
[DBActionType.CreateTag]: createRandomTag, |
|
|
|
|
[DBActionType.PutTrack]: createRandomTrack, |
|
|
|
|
[DBActionType.PutArtist]: createRandomArtist, |
|
|
|
|
[DBActionType.PutAlbum]: createRandomAlbum, |
|
|
|
|
[DBActionType.PutTag]: createRandomTag, |
|
|
|
|
[DBActionType.PatchTrack]: createPartialRandomTrack, |
|
|
|
|
[DBActionType.PatchArtist]: createPartialRandomArtist, |
|
|
|
|
[DBActionType.PatchAlbum]: createPartialRandomAlbum, |
|
|
|
|
[DBActionType.PatchTag]: createPartialRandomTag, |
|
|
|
|
} |
|
|
|
|
let params = { |
|
|
|
|
[DBActionType.CreateTrack]: distribution.createTrackParams, |
|
|
|
|
[DBActionType.CreateArtist]: distribution.createArtistParams, |
|
|
|
|
[DBActionType.CreateAlbum]: distribution.createAlbumParams, |
|
|
|
|
[DBActionType.CreateTag]: distribution.createTagParams, |
|
|
|
|
[DBActionType.PutTrack]: distribution.putTrackParams, |
|
|
|
|
[DBActionType.PutArtist]: distribution.putArtistParams, |
|
|
|
|
[DBActionType.PutAlbum]: distribution.putAlbumParams, |
|
|
|
|
[DBActionType.PutTag]: distribution.putTagParams, |
|
|
|
|
[DBActionType.PatchTrack]: distribution.patchTrackParams, |
|
|
|
|
[DBActionType.PatchArtist]: distribution.patchArtistParams, |
|
|
|
|
[DBActionType.PatchAlbum]: distribution.patchAlbumParams, |
|
|
|
|
[DBActionType.PatchTag]: distribution.patchTagParams, |
|
|
|
|
} |
|
|
|
|
let objectArrays = { |
|
|
|
|
[DBActionType.PatchTrack]: db[userId].tracks, |
|
|
|
|
[DBActionType.PatchArtist]: db[userId].artists, |
|
|
|
|
[DBActionType.PatchAlbum]: db[userId].albums, |
|
|
|
|
[DBActionType.PatchTag]: db[userId].tags, |
|
|
|
|
[DBActionType.PutTrack]: db[userId].tracks, |
|
|
|
|
[DBActionType.PutArtist]: db[userId].artists, |
|
|
|
|
[DBActionType.PutAlbum]: db[userId].albums, |
|
|
|
|
[DBActionType.PutTag]: db[userId].tags, |
|
|
|
|
} |
|
|
|
|
return { |
|
|
|
|
let object: any = { |
|
|
|
|
type: type, |
|
|
|
|
payload: funcs[type]( |
|
|
|
|
db, |
|
|
|
@ -275,6 +409,23 @@ export function randomDBAction( |
|
|
|
|
), |
|
|
|
|
userId: userId, |
|
|
|
|
}; |
|
|
|
|
// For patch and put operations, we have to include an ID.
|
|
|
|
|
if ([DBActionType.PatchTrack, |
|
|
|
|
DBActionType.PatchTag, |
|
|
|
|
DBActionType.PatchAlbum, |
|
|
|
|
DBActionType.PatchArtist, |
|
|
|
|
DBActionType.PutTrack, |
|
|
|
|
DBActionType.PutTag, |
|
|
|
|
DBActionType.PutAlbum, |
|
|
|
|
DBActionType.PutArtist].includes(type)) { |
|
|
|
|
let _type = type as |
|
|
|
|
DBActionType.PatchTrack | DBActionType.PatchTag | DBActionType.PatchAlbum | DBActionType.PatchArtist |
|
|
|
|
| DBActionType.PutTrack | DBActionType.PutTag | DBActionType.PutAlbum | DBActionType.PutArtist |
|
|
|
|
let validIdx = Math.floor(randomNumGen() * objectArrays[_type].length); |
|
|
|
|
let validId = objectArrays[_type][validIdx].id; |
|
|
|
|
object.payload.id = applyDistribution(params[_type].validId, randomNumGen) ? validId : validId + 99999; |
|
|
|
|
} |
|
|
|
|
return object; |
|
|
|
|
} |
|
|
|
|
case DBActionType.DeleteTrack: |
|
|
|
|
case DBActionType.DeleteArtist: |
|
|
|
@ -292,11 +443,11 @@ export function randomDBAction( |
|
|
|
|
[DBActionType.DeleteAlbum]: db[userId].albums, |
|
|
|
|
[DBActionType.DeleteTag]: db[userId].tags, |
|
|
|
|
} |
|
|
|
|
let validIdx = Math.floor(randomNumGen() * objectArrays[type].length); |
|
|
|
|
let validId = objectArrays[type][validIdx].id; |
|
|
|
|
return { |
|
|
|
|
type: type, |
|
|
|
|
payload: applyDistribution(params[type].validId, randomNumGen) ? |
|
|
|
|
Math.floor(randomNumGen() * objectArrays[type].length) + 1 : |
|
|
|
|
Math.floor(randomNumGen() * objectArrays[type].length) + 1 + objectArrays[type].length, |
|
|
|
|
payload: applyDistribution(params[type].validId, randomNumGen) ? validId : validId + 99999, |
|
|
|
|
userId: userId, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -381,6 +532,20 @@ export function createRandomTrack( |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createPartialRandomTrack( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
|
dist: RandomPatchTrackDistribution, |
|
|
|
|
randomNumGen: any, |
|
|
|
|
): TrackBaseWithRefs { |
|
|
|
|
let track: TrackBaseWithRefs = createRandomTrack(db, userId, dist, randomNumGen) |
|
|
|
|
if (!applyDistribution(dist.replaceName, randomNumGen)) { delete track.name } |
|
|
|
|
if (!applyDistribution(dist.replaceTags, randomNumGen)) { delete track.tagIds } |
|
|
|
|
if (!applyDistribution(dist.replaceAlbum, randomNumGen)) { delete track.albumId } |
|
|
|
|
if (!applyDistribution(dist.replaceArtists, randomNumGen)) { delete track.artistIds } |
|
|
|
|
return track; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createRandomArtist( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
@ -415,6 +580,20 @@ export function createRandomArtist( |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createPartialRandomArtist( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
|
dist: RandomPatchArtistDistribution, |
|
|
|
|
randomNumGen: any, |
|
|
|
|
): ArtistBaseWithRefs { |
|
|
|
|
let artist: ArtistBaseWithRefs = createRandomArtist(db, userId, dist, randomNumGen) |
|
|
|
|
if (!applyDistribution(dist.replaceName, randomNumGen)) { delete artist.name } |
|
|
|
|
if (!applyDistribution(dist.replaceTags, randomNumGen)) { delete artist.tagIds } |
|
|
|
|
if (!applyDistribution(dist.replaceAlbums, randomNumGen)) { delete artist.albumIds } |
|
|
|
|
if (!applyDistribution(dist.replaceTracks, randomNumGen)) { delete artist.trackIds } |
|
|
|
|
return artist; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createRandomAlbum( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
@ -449,6 +628,20 @@ export function createRandomAlbum( |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createPartialRandomAlbum( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
|
dist: RandomPatchAlbumDistribution, |
|
|
|
|
randomNumGen: any, |
|
|
|
|
): AlbumBaseWithRefs { |
|
|
|
|
let album: AlbumBaseWithRefs = createRandomAlbum(db, userId, dist, randomNumGen) |
|
|
|
|
if (!applyDistribution(dist.replaceName, randomNumGen)) { delete album.name } |
|
|
|
|
if (!applyDistribution(dist.replaceTags, randomNumGen)) { delete album.tagIds } |
|
|
|
|
if (!applyDistribution(dist.replaceArtists, randomNumGen)) { delete album.artistIds } |
|
|
|
|
if (!applyDistribution(dist.replaceTracks, randomNumGen)) { delete album.trackIds } |
|
|
|
|
return album; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createRandomTag( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
@ -466,4 +659,16 @@ export function createRandomTag( |
|
|
|
|
name: randomString(randomNumGen, 20), |
|
|
|
|
parentId: maybeParent, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createPartialRandomTag( |
|
|
|
|
db: ReferenceDatabase, |
|
|
|
|
userId: number, |
|
|
|
|
dist: RandomPatchTagDistribution, |
|
|
|
|
randomNumGen: any, |
|
|
|
|
): TagBaseWithRefs { |
|
|
|
|
let tag: TagBaseWithRefs = createRandomTag(db, userId, dist, randomNumGen) |
|
|
|
|
if (!applyDistribution(dist.replaceName, randomNumGen)) { delete tag.name } |
|
|
|
|
if (!applyDistribution(dist.replaceParent, randomNumGen)) { delete tag.parentId } |
|
|
|
|
return tag; |
|
|
|
|
} |