/* global require console process describe it */

var should = require('should')
var viewer = require('../.')

var _ = require('lodash')
var superagent = require('superagent')

var env = process.env;
var cuser = env.COUCHDB_USER ;
var cpass = env.COUCHDB_PASS ;
var chost = env.COUCHDB_HOST || 'localhost';
var cport = env.COUCHDB_PORT || 5984;

var test_db = env.COUCHDB_TESTDB
if(!test_db){ throw new Error('need valide db defined in environment variable COUCHDB_TESTDB')}
var couch = 'http://'+chost+':'+cport+'/'+test_db
console.log('testing couchdb='+couch)

/**
 * fixme, I should really create a test db, and populate it with data
 * and with a view
 *
 * Instead, because I want to get some real work done, this test is
 * going to just use an exsiting db that I have with existing views
 *
 * If your name is not James E. Marca, you should change this
 *
 */


var views = ['_design/imputedchecks/_view/missing_wim_neighbors'
            ,'_design/vdsml/_view/mainline'
            ,'_design/properties/_view/segment_length_ml']

describe('query view 1',function(){

    it('should get all missing wim neighbors in district 3, reducing all'
      ,function(done){
           viewer({'db':test_db
                  ,'view':views[0]
                  ,'startkey':[2007, 3]
                  ,'endkey':[2007,3,{}]
                  }
                 ,function(err,docs){
                      should.not.exist(err)
                      docs.should.eql({"rows":[
                          {"key":null,"value":294}
                      ]})
                      return done()
                  })
       })
    it('should get all missing wim neighbors in district 3, no reduce'
      ,function(done){
           viewer({'db':test_db
                  ,'view':views[0]
                  ,'startkey':[2007, 3,5]
                  ,'endkey':[2007,3,5,{}]
                  ,'reduce':false
                  }
                 ,function(err,docs){
                      should.not.exist(err)
                      docs.rows.should.have.property('length',42)
                      _.each(docs.rows,function(doc){
                          doc.key.should.eql([2007,3,5])
                      });
                      return done()
                  })
       })
    it('should get all missing wim neighbors in district 3, no reduce, using key'
      ,function(done){
           viewer({'db':test_db
                  ,'view':views[0]
                  ,'key':[2007, 3,5]
                  ,'reduce':false
                  }
                 ,function(err,docs){
                      should.not.exist(err)
                      docs.rows.should.have.property('length',42)
                      _.each(docs.rows,function(doc){
                          doc.key.should.eql([2007,3,5])
                      });
                      return done()
                  })
       })
    it('should get all missing wim neighbors in district 3, no reduce, using keys'
      ,function(done){
           viewer({'db':test_db
                  ,'doc':'doc1'
                  ,'view':views[0]
                  ,'keys':[[2007, 3,5],[2008,3,5]]
                  ,'reduce':false
                  }
                 ,function(err,docs){
                      should.not.exist(err)
                      docs.rows.should.have.property('length',82)
                      _.each(docs.rows,function(doc){
                          doc.key[0].should.match(/200(7|8)/)
                          doc.key[1].should.eql(3)
                          doc.key[2].should.eql(5)
                      });
                      return done()
                  })
       })
})

@@ -3642,11 +3642,951 @@
      %7D)%0A
+    it('should get docs with include doc'%0A      ,function(done)%7B%0A           viewer(%7B'db':test_db%0A                  ,'view':views%5B0%5D%0A                  ,'key':%5B2007,3,5%5D%0A                  ,'reduce':false%0A                  ,'include_docs':true%0A                  %7D%0A                 ,function(err,docs)%7B%0A                      should.not.exist(err)%0A                      docs.rows.should.have.property('length',42)%0A                      _.each(docs.rows,function(doc)%7B%0A                          doc.key.should.eql(%5B2007,3,5%5D)%0A                          doc.should.have.property('doc')%0A                          var docdoc = doc.doc%0A                          docdoc.should.have.property(2007)%0A                          docdoc%5B2007%5D.should.have.property('properties')%0A                          docdoc%5B2007%5D%5B'properties'%5D%5B0%5D.should.have.property('geojson')%0A                      %7D);%0A                      return done()%0A                  %7D)%0A       %7D)%0A
 %7D)%0A

add a test to make sure include_docs=true works oka