Skip to content

Commit

Permalink
Feat: toJSON() custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Sep 3, 2017
1 parent 3a775c0 commit ca7a0ca
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/TorrentLibrary.js
Expand Up @@ -531,6 +531,18 @@ class TorrentLibrary extends EventEmitter {
get allFilesWithCategory() {
return this.categoryForFile;
}

toJSON() {
const tvSeries = this.allTvSeries;
return `{
"paths":${JSON.stringify([...this.paths])},
"allFilesWithCategory":${JSON.stringify([...this.allFilesWithCategory])},
"movies":${JSON.stringify([...this.allMovies])},
"tv-series":${JSON.stringify([...tvSeries].map(serie =>
// serie[0] contains the title and [1] the wrong JSON ; let fix it
[serie[0], [...tvSeries.get(serie[0])]]))}
}`;
}
}

export default TorrentLibrary;
17 changes: 16 additions & 1 deletion test/APITests.js
Expand Up @@ -3,6 +3,7 @@ import assert from 'assert';
import path from 'path';
import { parse as nameParser } from 'parse-torrent-title';
import _ from 'lodash';
import * as fs from 'fs';
import TorrentLibrary from '../lib/TorrentLibrary';

describe('TorrentLibrary tests', () => {
Expand All @@ -19,8 +20,9 @@ describe('TorrentLibrary tests', () => {
'Bad.Ass.2012.LiMiTED.TRUEFRENCH.DVDRiP.XviD' +
'-www.zone-telechargement.ws.avi'),
];
const expectedJsonLocation = path.join(__dirname, 'example.json');

// initialization
// initialization
before(() => {
libInstance = new TorrentLibrary();
tempInstance = new TorrentLibrary();
Expand Down Expand Up @@ -145,6 +147,19 @@ describe('TorrentLibrary tests', () => {
});
});

context('toJSON()', () => {
it('Should return a valid JSON', () => {
const data = fs.readFileSync(expectedJsonLocation);
const expectedJson = JSON.stringify(JSON.parse(data));
const dataFromInstance = libInstance.toJSON();
assert.deepEqual(
expectedJson,
JSON.stringify(JSON.parse(dataFromInstance)),
'not the same JSON',
);
});
});

context('Remove Old files', () => {
it('Should not be able to remove not present files', () => {
const wrongFile = path.join(__dirname, 'folder1',
Expand Down
58 changes: 58 additions & 0 deletions test/example.json
@@ -0,0 +1,58 @@
{
"paths":[
"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder1",
"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder2"
],
"allFilesWithCategory":[
[
"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder1\\Bad.Ass.2012.LiMiTED.TRUEFRENCH.DVDRiP.XviD-www.zone-telechargement.ws.avi",
"MOVIES"
],
[
"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder1\\The.Blacklist.S04E21.FRENCH.WEBRip.XviD.avi",
"TV_SERIES"
],
[
"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder2\\The.Blacklist.S04E14.FRENCH.WEBRip.XviD.avi",
"TV_SERIES"
]
],
"movies":[
{
"year":2012,
"container":"avi",
"source":"dvdrip",
"codec":"xvid",
"language":"truefrench",
"title":"Bad Ass",
"filePath":"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder1\\Bad.Ass.2012.LiMiTED.TRUEFRENCH.DVDRiP.XviD-www.zone-telechargement.ws.avi"
}
],
"tv-series":[
[
"The Blacklist",
[
{
"container":"avi",
"source":"webrip",
"codec":"xvid",
"season":4,
"episode":21,
"language":"french",
"title":"The Blacklist",
"filePath":"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder1\\The.Blacklist.S04E21.FRENCH.WEBRip.XviD.avi"
},
{
"container":"avi",
"source":"webrip",
"codec":"xvid",
"season":4,
"episode":14,
"language":"french",
"title":"The Blacklist",
"filePath":"D:\\workspaceNodeJs\\torrent-files-library\\test\\folder2\\The.Blacklist.S04E14.FRENCH.WEBRip.XviD.avi"
}
]
]
]
}

0 comments on commit ca7a0ca

Please sign in to comment.