Skip to content

Commit

Permalink
Update test cases.
Browse files Browse the repository at this point in the history
Use typeorm-liked API in unit test.
  • Loading branch information
kevinptt0323 committed Nov 6, 2019
1 parent 6e71b34 commit 04a6d3a
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 189 deletions.
135 changes: 135 additions & 0 deletions test/article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import assert from 'assert';
import pttbot from '../src';
import { username, password } from './config';
import { Article } from '../src/sites/ptt/model';

const newbot = async () => {
const ptt = new pttbot();
await (() => new Promise(resolve => {
ptt.once('connect', resolve);
}))();
const ret = await ptt.login(username, password);
if (!ret) {
throw 'login failed';
}
return ptt;
};

const getArticleInfo = (article: Article) => {
return `${article.id} ${article.author} ${article.title}`;
};

describe('Article', () => {
let ptt;
before('login', async () => {
ptt = await newbot();
});
after('logout', async () => {
await ptt.logout();
});

describe('get', () => {
let articles: Article[];
const boardname = 'Gossiping';
it('should get correct article list from board', async () => {
articles =
await ptt.select(Article)
.where('boardname', boardname)
.get();
assert(articles.length > 0);
});
it('should get correct article list with "id" argument', async () => {
let articles2: Article[] =
await ptt.select(Article)
.where('boardname', boardname)
.where('id', articles[articles.length-1].id-1)
.get();

const article1Info = getArticleInfo(articles[articles.length-1]);
const article2Info = getArticleInfo(articles2[0]);
assert.equal(articles2[0].id, articles[articles.length-1].id-1, `${article1Info}\n${article2Info}`);
});
});

describe('getOne', () => {
const boardname = 'Gossiping';
it('should get correct article from board', async () => {
const article: Article =
await ptt.select(Article)
.where('boardname', boardname)
.where('id', 100000)
.getOne();

assert.strictEqual(article.boardname, boardname);
assert.strictEqual(article.id, 100000);
});
});

describe('where', () => {
let board = 'Gossiping';
let push = '50';
let title = '問卦';
let author = 'kevin';

it('should get correct articles with specified push number from board', async () => {
const articles: Article[] =
await ptt.select(Article)
.where('boardname', board)
.where('push', push)
.get();
assert(articles.length > 0);

articles.forEach(article => {
let pushCheck = false;
let articleInfo = getArticleInfo(article);
let pushNumber = (article.push === '爆') ? '100' : article.push;
assert(Number(pushNumber) >= Number(push), articleInfo);
});
});

it('should get correct articles with specified author name from board', async () => {
const articles: Article[] =
await ptt.select(Article)
.where('boardname', board)
.where('author', author)
.get();
assert(articles.length > 0);

articles.forEach(article => {
let articleInfo = getArticleInfo(article);
assert(article.author.toLowerCase().includes(author.toLowerCase()), articleInfo);
});
});

it('should get correct articles contain specified title word from board', async () => {
const articles: Article[] =
await ptt.select(Article)
.where('boardname', board)
.where('title', title)
.get();
assert(articles.length > 0);

articles.forEach(article => {
let articleInfo = getArticleInfo(article);
assert(article.title.toLowerCase().includes(title), articleInfo);
});
});

it('should get correct articles contain specified title word AND push number from board', async () => {
const articles: Article[] =
await ptt.select(Article)
.where('boardname', board)
.where('title', title)
.where('push', push)
.get();
assert(articles.length > 0);

articles.forEach(article => {
let articleInfo = getArticleInfo(article);
let pushNumber = (article.push === '爆') ? '100' : article.push;
assert(article.title.toLowerCase().includes(title), articleInfo);
assert(Number(pushNumber) >= Number(push), articleInfo);
});
});
})
});
145 changes: 0 additions & 145 deletions test/articles.ts

This file was deleted.

50 changes: 50 additions & 0 deletions test/board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from 'assert';
import pttbot from '../src';
import { username, password } from './config';
import { Board } from '../src/sites/ptt/model';

const newbot = async () => {
const ptt = new pttbot();
await (() => new Promise(resolve => {
ptt.once('connect', resolve);
}))();
const ret = await ptt.login(username, password)
if (!ret) {
throw 'login failed';
}
return ptt;
};

describe('Board', () => {
let ptt;

describe('get', () => {
before('login', async () => {
ptt = await newbot();
});
after('logout', async () => {
await ptt.logout();
});
it('should get class list', async () => {
let boards: Board[] =
await ptt.select(Board)
.where('entry', 'all')
.get();
assert(boards.length > 0);
});
it('should get hot list', async () => {
let boards: Board[] =
await ptt.select(Board)
.where('entry', 'hot')
.get();
assert(boards.length > 0);
});
it('should get favorite list', async () => {
let boards: Board[] =
await ptt.select(Board)
.where('entry', 'favorite')
.get();
assert(boards.length > 0);
});
});
});
42 changes: 0 additions & 42 deletions test/favorite.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ global.WebSocket = require('ws');

const tests = [
'./connection.ts',
'./articles.ts',
'./favorite.ts',
'./article.ts',
'./board.ts',
];

tests.forEach(test => {
Expand Down

0 comments on commit 04a6d3a

Please # to comment.