File: /var/dev/farhangmoaser/web/test/02-dictionary.js
global.env = 'test';
var request = require('supertest');
var redis = require('../connectors/redis');
var app = require('../app');
describe('Dictionary', function(){
	var rand = (Math.random()*1000).toFixed(3);
	var catid;
	var bookid;
	describe('Dictionary categories', function(){
		it('add a category', function(done){
			request(app)
				.put('/api/1.0/dictionary/cat')
				.set('x-token', global.testtoken)
				.send({title: 'testcat-'+rand})
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					res.body.should.have.property('id');
					catid = res.body.id;
					done();
				});
		});
		it('fail on duplicate category', function(done){
			request(app)
				.put('/api/1.0/dictionary/cat')
				.set('x-token', global.testtoken)
				.send({title: 'testcat-'+rand})
				.expect(400)
				.end(function(err, res) {
					if(err) throw err;
					res.body.should.have.property('error', 202);
					done();
				});
		});
		it('list categories', function(done){
			request(app)
				.get('/api/1.0/dictionary/cat')
				.set('x-token', global.testtoken)
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					res.body.should.have.property('length');
					var flag = false;
					res.body.forEach(function(row){
						if(row.id == catid && row.title=='testcat-'+rand)
							flag=true;
					});
					flag.should.be.equal(true);
					done();
				});
		});
	});
	describe('Dictionaries', function(){
		it('add a book', function(done){
			request(app)
				.put('/api/1.0/dictionary')
				.set('x-token', global.testtoken)
				.send({
					title: 'testbook-'+rand,
					author: 'Babak Vandad',
					pubYear: 1391,
					langs: [
						{from: 1, to: 2},
						{from: 2, to: 1}
					],
					categories: [catid],
					desc: 'check this data.'
				})
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					res.body.should.have.property('uuid');
					bookid = res.body.uuid;
					done();
				});
		});
		it('list books', function(done){
			request(app)
				.get('/api/1.0/dictionary')
				.set('x-token', global.testtoken)
				.expect(200)
				.end(function(err, res){
					if(err) throw err;
					res.body.should.have.property('length');
					var flag = false;
					res.body.forEach(function(row){
						if(row.uuid == bookid && row.title=='testbook-'+rand)
							flag=true;
					});
					flag.should.be.equal(true);
					done();
				});
		});
		it('edit a book', function(done){
			request(app)
				.post('/api/1.0/dictionary/'+bookid)
				.set('x-token', global.testtoken)
				.send({
					title: 'testbook-'+rand+' - transformed',
					author: 'Babak Vandad - transformed',
					pubYear: 1391,
					langs: [
						{from: 1, to: 2},
						{from: 2, to: 1},
						{from: 2, to: 3}
					],
					categories: [catid],
					desc: 'check this data - transformed.'
				})
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					done();
				});
		});
		it('load a book and check it\'s data', function(done){
			request(app)
				.get('/api/1.0/dictionary/'+bookid)
				.set('x-token', global.testtoken)
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					res.body.uuid.should.be.equal(bookid);
					res.body.title.should.be.equal('testbook-'+rand+' - transformed');
					res.body.author.should.be.equal('Babak Vandad - transformed');
					res.body.pubYear.should.be.equal(1391);
					res.body.desc.should.be.equal('check this data - transformed.');
					res.body.categories.length.should.be.equal(1);
					res.body.categories[0].id.should.be.equal(catid);
					langtest = [];
					res.body.langs.forEach(function(lang){
						langtest.push(lang.from+'-'+lang.to);
					});
					langtest.sort().join('|').should.be.equal('1-2|2-1|2-3');
					done();
				});
		});
	});
	describe('Deleting dictionary and category', function(){
		it('delete category', function(done){
			request(app)
				.delete('/api/1.0/dictionary/cat/'+catid)
				.set('x-token', global.testtoken)
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					request(app)
						.get('/api/1.0/dictionary/cat')
						.set('x-token', global.testtoken)
						.expect(200)
						.end(function(err, res) {
							if(err) throw err;
							res.body.should.have.property('length');
							var flag = false;
							res.body.forEach(function(row){
								if(row.id == catid)
									flag=true;
							});
							flag.should.be.equal(false);
							done();
						});
				});
		});
		it('delete book', function(done){
			request(app)
				.delete('/api/1.0/dictionary/'+bookid)
				.set('x-token', global.testtoken)
				.expect(200)
				.end(function(err, res) {
					if(err) throw err;
					request(app)
						.get('/api/1.0/dictionary/'+bookid)
						.set('x-token', global.testtoken)
						.expect(406)
						.end(function(err, res){
							res.body.should.have.property('error');
							res.body.error.should.be.equal(203);
							done();
						});
				});
		});
	});
});