<commit_before>

var pg     = require('pg');
var async  = require('async');
var should = require('should');
var path   = require('path');
var fs     = require('fs');
var debug  = require('debug')('OSMBC:test:blog.test');

var config = require('../config.js');

var testutil = require('./testutil.js');

var logModule     = require('../model/logModule.js');
var blogModule    = require('../model/blog.js');







describe('model/blog', function() {
  before(function (bddone) {
    testutil.clearDB(bddone);
    process.env.TZ = 'Europe/Amsterdam';
  }) 

  describe('createNewBlog',function() {
    beforeEach(function (bddone) {
      testutil.clearDB(bddone);
    }) 
    it('should createNewArticle with prototype',function(bddone) {
      var newBlog = blogModule.createNewBlog({name:"test",status:"open"},function (err,result){
        should.not.exist(err);
        var id = result.id;
        testutil.getJsonWithId("blog",id,function(err,result){
          should.not.exist(err);
          should(result.name).equal('test');
          should(result.status).equal('open');
          var start = new Date();
          var end = new Date();
          start.setDate(start.getDate()+1);
          end.setDate(end.getDate()+7);


          should(new Date(result.startDate).toLocaleDateString()).equal(start.toLocaleDateString());
          should(new Date(result.endDate).toLocaleDateString()).equal(end.toLocaleDateString());
          bddone();
        })
      })
    });
    it('should createNewArticle without prototype',function(bddone) {
      var newBlog = blogModule.createNewBlog(function (err,result){
        should.not.exist(err);
        var id = result.id;
        testutil.getJsonWithId("blog",id,function(err,result){
          should.not.exist(err);
          should.exist(result);
          should(result.name).equal('WN251');
          bddone();
        })
      });
    })
    it('should createNewArticle with existing WN',function(bddone) {
      var previousBlog = blogModule.createNewBlog({name:"WN100",endDate:new Date("1.1.2000")},function(err,result){

        should.not.exist(err);
        should.exist(result);
        result.save(function(err) {
          should.not.exist(err);
          var newBlog = blogModule.createNewBlog(function (err,result){
            should.not.exist(err);
            var id = result.id;
            testutil.getJsonWithId("blog",id,function(err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.name).equal('WN101');
              should(new Date(result.startDate).toLocaleDateString()).eql(new Date("2000-01-02").toLocaleDateString());
              should(new Date(result.endDate).toLocaleDateString()).eql(new Date("2000-01-08").toLocaleDateString());
              bddone();
            })
          });       

        })
      })
    })
    it('should create no New Article with ID',function(bddone){
      (function() {
        var newBlog = blogModule.createNewBlog({id:2,name:"test",status:"**"},function (err,result){
        })
      }).should.throw();
      bddone();
    })
  })
  describe('isEditable',function(){
    it('should be editable if no review or closed state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**"});
        should(newBlog.isEditable("DE")).be.True();
    })
    it('should be editable if  review state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentDE:["comment"]});
        should(newBlog.isEditable("DE")).be.False();
    })
    it('should be editable if no review and closed state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentEN:["comment"],closeEN:true});
        should(newBlog.isEditable("EN")).be.False();
    })
    it('should be editable if reopened',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentDE:["comment"],closeDE:false});
        should(newBlog.isEditable("DE")).be.True();
    })

  })
  describe('setAndSave',function() {
    it('should set only the one Value in the database', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.name = "New Title";
        newBlog.setAndSave("user",{status:"published",field:"test"},function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);
            delete result._meta;
            delete result.categories;
            delete result.startDate;
            delete result.endDate;
            should(result).eql({id:id,name:"New Title",status:"published",field:"test",version:2});
            logModule.find({},{column:"property"},function (err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.length).equal(2);
              delete result[0].id;
              delete result[1].id;
              var t0 = result[0].timestamp;
              var t1 = result[1].timestamp;
              var now = new Date();
              var t0diff = ((new Date(t0)).getTime()-now.getTime());
              var t1diff = ((new Date(t1)).getTime()-now.getTime());

              // The Value for comparison should be small, but not to small
              // for the test machine.
              should(t0diff).be.below(10);
              should(t1diff).be.below(10);
              delete result[0].timestamp;
              delete result[1].timestamp;

              should(result).containEql({oid:id,user:"user",table:"blog",property:"status",from:"TEST",to:"published"});
              should(result).containEql({oid:id,user:"user",table:"blog",property:"field",to:"test"});
              bddone();
            })
          })
        })
      })
    })
  })
  describe('closeBlog',function() {
    it('should close the Blog and write a log Message', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.closeBlog("DE","user",true,function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);
            delete result._meta;
            delete result.categories;
            delete result.startDate;
            delete result.endDate;
            should(result).eql({id:id,name:"Title",status:"TEST",closeDE:true,version:2});
            logModule.find({},{column:"property"},function (err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.length).equal(1);
              delete result[0].id;
              var t0 = result[0].timestamp;
              var now = new Date();
              var t0diff = ((new Date(t0)).getTime()-now.getTime());
        
              // The Value for comparison should be small, but not to small
              // for the test machine.
              should(t0diff).be.below(10);
              delete result[0].timestamp;
        
              should(result).containEql({oid:id,user:"user",table:"blog",property:"closeDE",to:true});
              bddone();
            })
          })
        })
      })
    })
  })
  describe('reviewComment',function() {
    it.only('should review the Blog and write a log Message', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.setReviewComment("DE","user","it is approved.",function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);<commit_msg>Review Comment Test.<commit_after>

var pg     = require('pg');
var async  = require('async');
var should = require('should');
var path   = require('path');
var fs     = require('fs');
var debug  = require('debug')('OSMBC:test:blog.test');

var config = require('../config.js');

var testutil = require('./testutil.js');

var logModule     = require('../model/logModule.js');
var blogModule    = require('../model/blog.js');







describe('model/blog', function() {
  before(function (bddone) {
    testutil.clearDB(bddone);
    process.env.TZ = 'Europe/Amsterdam';
  }) 

  describe('createNewBlog',function() {
    beforeEach(function (bddone) {
      testutil.clearDB(bddone);
    }) 
    it('should createNewArticle with prototype',function(bddone) {
      var newBlog = blogModule.createNewBlog({name:"test",status:"open"},function (err,result){
        should.not.exist(err);
        var id = result.id;
        testutil.getJsonWithId("blog",id,function(err,result){
          should.not.exist(err);
          should(result.name).equal('test');
          should(result.status).equal('open');
          var start = new Date();
          var end = new Date();
          start.setDate(start.getDate()+1);
          end.setDate(end.getDate()+7);


          should(new Date(result.startDate).toLocaleDateString()).equal(start.toLocaleDateString());
          should(new Date(result.endDate).toLocaleDateString()).equal(end.toLocaleDateString());
          bddone();
        })
      })
    });
    it('should createNewArticle without prototype',function(bddone) {
      var newBlog = blogModule.createNewBlog(function (err,result){
        should.not.exist(err);
        var id = result.id;
        testutil.getJsonWithId("blog",id,function(err,result){
          should.not.exist(err);
          should.exist(result);
          should(result.name).equal('WN251');
          bddone();
        })
      });
    })
    it('should createNewArticle with existing WN',function(bddone) {
      var previousBlog = blogModule.createNewBlog({name:"WN100",endDate:new Date("1.1.2000")},function(err,result){

        should.not.exist(err);
        should.exist(result);
        result.save(function(err) {
          should.not.exist(err);
          var newBlog = blogModule.createNewBlog(function (err,result){
            should.not.exist(err);
            var id = result.id;
            testutil.getJsonWithId("blog",id,function(err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.name).equal('WN101');
              should(new Date(result.startDate).toLocaleDateString()).eql(new Date("2000-01-02").toLocaleDateString());
              should(new Date(result.endDate).toLocaleDateString()).eql(new Date("2000-01-08").toLocaleDateString());
              bddone();
            })
          });       

        })
      })
    })
    it('should create no New Article with ID',function(bddone){
      (function() {
        var newBlog = blogModule.createNewBlog({id:2,name:"test",status:"**"},function (err,result){
        })
      }).should.throw();
      bddone();
    })
  })
  describe('isEditable',function(){
    it('should be editable if no review or closed state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**"});
        should(newBlog.isEditable("DE")).be.True();
    })
    it('should be editable if  review state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentDE:["comment"]});
        should(newBlog.isEditable("DE")).be.False();
    })
    it('should be editable if no review and closed state',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentEN:["comment"],closeEN:true});
        should(newBlog.isEditable("EN")).be.False();
    })
    it('should be editable if reopened',function(){
        var newBlog = blogModule.create({id:2,name:"test",status:"**",reviewCommentDE:["comment"],closeDE:false});
        should(newBlog.isEditable("DE")).be.True();
    })

  })
  describe('setAndSave',function() {
    it('should set only the one Value in the database', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.name = "New Title";
        newBlog.setAndSave("user",{status:"published",field:"test"},function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);
            delete result._meta;
            delete result.categories;
            delete result.startDate;
            delete result.endDate;
            should(result).eql({id:id,name:"New Title",status:"published",field:"test",version:2});
            logModule.find({},{column:"property"},function (err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.length).equal(2);
              delete result[0].id;
              delete result[1].id;
              var t0 = result[0].timestamp;
              var t1 = result[1].timestamp;
              var now = new Date();
              var t0diff = ((new Date(t0)).getTime()-now.getTime());
              var t1diff = ((new Date(t1)).getTime()-now.getTime());

              // The Value for comparison should be small, but not to small
              // for the test machine.
              should(t0diff).be.below(10);
              should(t1diff).be.below(10);
              delete result[0].timestamp;
              delete result[1].timestamp;

              should(result).containEql({oid:id,user:"user",table:"blog",property:"status",from:"TEST",to:"published"});
              should(result).containEql({oid:id,user:"user",table:"blog",property:"field",to:"test"});
              bddone();
            })
          })
        })
      })
    })
  })
  describe('closeBlog',function() {
    it('should close the Blog and write a log Message', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.closeBlog("DE","user",true,function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);
            delete result._meta;
            delete result.categories;
            delete result.startDate;
            delete result.endDate;
            should(result).eql({id:id,name:"Title",status:"TEST",closeDE:true,version:2});
            logModule.find({},{column:"property"},function (err,result){
              should.not.exist(err);
              should.exist(result);
              should(result.length).equal(1);
              delete result[0].id;
              var t0 = result[0].timestamp;
              var now = new Date();
              var t0diff = ((new Date(t0)).getTime()-now.getTime());
        
              // The Value for comparison should be small, but not to small
              // for the test machine.
              should(t0diff).be.below(10);
              delete result[0].timestamp;
        
              should(result).containEql({oid:id,user:"user",table:"blog",property:"closeDE",to:true});
              bddone();
            })
          })
        })
      })
    })
  })
  describe('reviewComment',function() {
    it('should review the Blog and write a log Message', function (bddone){
      blogModule.createNewBlog({name:"Title",status:"TEST"},function(err,newBlog){
        should.not.exist(err);
        should.exist(newBlog);
        var id =newBlog.id;
        newBlog.setReviewComment("DE","user","it is approved.",function(err,result) {
          should.not.exist(err);
          testutil.getJsonWithId("blog",id,function(err,result){
            should.not.exist(err);
{'fraction_non_alphanumeric': 0.16205038225341523, 'fraction_numerical': 0.00864770021305928, 'mean_word_length': 2.5346622369878182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4085803', 'n_tokens_mistral': 4759, 'n_tokens_neox': 4584, 'n_words': 723}