<commit_before>var path = require('path')
var jsonBuffStream = require('json-multibuffer-stream')
var concat = require('concat-stream')
var buff = require('multibuffer')
var bops = require('bops')
var docUtils = require(path.join(__dirname, '..', '..', 'lib', 'document'))

module.exports.buffToJson = function(test, common) {
  test('buff <-> json', function(t) {
    var test = {'hello': 'world', 'foo': {'bar': '[baz]', 'pizza': [1,2,3]}}
    var columns = Object.keys(test)
  
    var encoded = jsonBuffStream.encode(test)
    var decoded = jsonBuffStream.decode(columns, encoded)
    decoded.foo = JSON.parse(decoded.foo)
    t.deepEqual(test, decoded, 'encoded/decoded matches')
    t.end()
  })
}

module.exports.rowKeys = function(test, common) {
  test('rowKeys returns correctly formatted keys', function(t) {
    var sep = '\xff'
    
    var keys = {
      seq:  's',
      data: 'd',
      rev:  'r'
    }
    
    var a = docUtils.rowKeys(keys, sep, 'foo', '1-abc', '1', '1')
    t.deepEqual(a, { row: 'ÿdÿfooÿrÿ01-abcÿsÿ01ÿ01', seq: 'ÿsÿ01' })
    
    t.end()
  })
}

module.exports.decodeKey = function(test, common) {
  test('decodeKey parses key format correctly', function(t) {
    var key = 'ÿdÿfooÿrÿ01-abcÿsÿ01ÿ01'
    var obj = docUtils.decodeKey(key)
    var expected = {
      _id: 'foo',
      _rev: '1-abc',
      _seq: 1,
      _ver: 1
    }
    t.deepEqual(obj, expected)
    t.end()
  })
}

module.exports.putJson = function(test, common) {
  test('.put json', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        var cat = dat.createReadStream()
    
        cat.pipe(concat(function(data) {
          t.equal(data.length, 1)
          t.equal(data[0].foo, "bar")
          setImmediate(done)
        }))
      })
    })
  })
}


module.exports.multiplePutJson = function(test, common) {
  test('.put same json multiple times (random id generation)', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err) {
        if (err) throw err
        dat.put({"foo": "bar"}, function(err) {
          if (err) throw err
          var cat = dat.createReadStream()
    
          cat.pipe(concat(function(data) {
            t.equal(data.length, 2)
            t.equal(data[0].foo, "bar")
            t.equal(data[1].foo, "bar")
            done()
          }))
        })
      })
    })
  })
}


module.exports.putBuff = function(test, common) {
  test('.put buff', function(t) {
    common.getDat(t, function(dat, done) {
      var row = buff.pack([bops.from('bar')])
    
      dat.put(row, {columns: ['foo']}, function(err) {
        if (err) throw err
        var cat = dat.createReadStream()
    
        cat.pipe(concat(function(data) {
          t.equal(data.length, 1)
          t.equal(data[0].foo, "bar")
          done()
        }))
      })
    })
  })
}

module.exports.schemaVersion = function(test, common) {
  test('schema version should increment when schema changes', function(t) {
    common.getDat(t, function(dat, done) {
      t.equal(dat.meta.json.schemaVersion, 0, 'schemaVersion 0')
      dat.put({"foo": "bar"}, function(err) {
        if (err) throw err
        t.equal(dat.meta.json.schemaVersion, 1, 'schemaVersion 1')
        dat.put({"foo": "bar", "taco": "pizza"}, function(err) {
          if (err) throw err
          var cat = dat.schemas.createReadStream()
          t.equal(dat.meta.json.schemaVersion, 2, 'schemaVersion 2')
    
          cat.pipe(concat(function(data) {
            t.equal(data.length, 2, '2 schema versions')
            t.equal(data[0].version, 2, 'latest version is 2')
            t.equal(data[1].version, 1, 'older version is 1')
            done()
          }))
        })
      })
    })
  })
}

module.exports.deleteRow = function(test, common) {
  test('delete row', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        dat.delete(doc._id, function(err) {
          t.false(err, 'should delete okay')
          dat.get(doc._id, function(err, doc) {
            t.true(err, 'doc should now be not found')
            t.false(doc, 'doc should be null')
            setTimeout(done, 10) // TODO WHY????
          })
        })
      })
    })
  })
}

module.exports.getAtRev = function(test, common) {
  test('get row at specific rev', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        doc.pizza = 'taco'
        dat.put(doc, function(err, doc) {
          if (err) throw err
          dat.get(doc._id, {rev: doc._rev}, function(err, docAtRev) {
            t.false(err, 'no err')
            t.equal(docAtRev.pizza, 'taco', 'doc is version 2')
            setImmediate(done)
          })
        })
      })<commit_msg>tweak doc at rev test<commit_after>var path = require('path')
var jsonBuffStream = require('json-multibuffer-stream')
var concat = require('concat-stream')
var buff = require('multibuffer')
var bops = require('bops')
var docUtils = require(path.join(__dirname, '..', '..', 'lib', 'document'))

module.exports.buffToJson = function(test, common) {
  test('buff <-> json', function(t) {
    var test = {'hello': 'world', 'foo': {'bar': '[baz]', 'pizza': [1,2,3]}}
    var columns = Object.keys(test)
  
    var encoded = jsonBuffStream.encode(test)
    var decoded = jsonBuffStream.decode(columns, encoded)
    decoded.foo = JSON.parse(decoded.foo)
    t.deepEqual(test, decoded, 'encoded/decoded matches')
    t.end()
  })
}

module.exports.rowKeys = function(test, common) {
  test('rowKeys returns correctly formatted keys', function(t) {
    var sep = '\xff'
    
    var keys = {
      seq:  's',
      data: 'd',
      rev:  'r'
    }
    
    var a = docUtils.rowKeys(keys, sep, 'foo', '1-abc', '1', '1')
    t.deepEqual(a, { row: 'ÿdÿfooÿrÿ01-abcÿsÿ01ÿ01', seq: 'ÿsÿ01' })
    
    t.end()
  })
}

module.exports.decodeKey = function(test, common) {
  test('decodeKey parses key format correctly', function(t) {
    var key = 'ÿdÿfooÿrÿ01-abcÿsÿ01ÿ01'
    var obj = docUtils.decodeKey(key)
    var expected = {
      _id: 'foo',
      _rev: '1-abc',
      _seq: 1,
      _ver: 1
    }
    t.deepEqual(obj, expected)
    t.end()
  })
}

module.exports.putJson = function(test, common) {
  test('.put json', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        var cat = dat.createReadStream()
    
        cat.pipe(concat(function(data) {
          t.equal(data.length, 1)
          t.equal(data[0].foo, "bar")
          setImmediate(done)
        }))
      })
    })
  })
}


module.exports.multiplePutJson = function(test, common) {
  test('.put same json multiple times (random id generation)', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err) {
        if (err) throw err
        dat.put({"foo": "bar"}, function(err) {
          if (err) throw err
          var cat = dat.createReadStream()
    
          cat.pipe(concat(function(data) {
            t.equal(data.length, 2)
            t.equal(data[0].foo, "bar")
            t.equal(data[1].foo, "bar")
            done()
          }))
        })
      })
    })
  })
}


module.exports.putBuff = function(test, common) {
  test('.put buff', function(t) {
    common.getDat(t, function(dat, done) {
      var row = buff.pack([bops.from('bar')])
    
      dat.put(row, {columns: ['foo']}, function(err) {
        if (err) throw err
        var cat = dat.createReadStream()
    
        cat.pipe(concat(function(data) {
          t.equal(data.length, 1)
          t.equal(data[0].foo, "bar")
          done()
        }))
      })
    })
  })
}

module.exports.schemaVersion = function(test, common) {
  test('schema version should increment when schema changes', function(t) {
    common.getDat(t, function(dat, done) {
      t.equal(dat.meta.json.schemaVersion, 0, 'schemaVersion 0')
      dat.put({"foo": "bar"}, function(err) {
        if (err) throw err
        t.equal(dat.meta.json.schemaVersion, 1, 'schemaVersion 1')
        dat.put({"foo": "bar", "taco": "pizza"}, function(err) {
          if (err) throw err
          var cat = dat.schemas.createReadStream()
          t.equal(dat.meta.json.schemaVersion, 2, 'schemaVersion 2')
    
          cat.pipe(concat(function(data) {
            t.equal(data.length, 2, '2 schema versions')
            t.equal(data[0].version, 2, 'latest version is 2')
            t.equal(data[1].version, 1, 'older version is 1')
            done()
          }))
        })
      })
    })
  })
}

module.exports.deleteRow = function(test, common) {
  test('delete row', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        dat.delete(doc._id, function(err) {
          t.false(err, 'should delete okay')
          dat.get(doc._id, function(err, doc) {
            t.true(err, 'doc should now be not found')
            t.false(doc, 'doc should be null')
            setTimeout(done, 10) // TODO WHY????
          })
        })
      })
    })
  })
}

module.exports.getAtRev = function(test, common) {
  test('get row at specific rev', function(t) {
    common.getDat(t, function(dat, done) {
      dat.put({"foo": "bar"}, function(err, doc) {
        if (err) throw err
        var rev1 = doc._rev
        doc.pizza = 'taco'
        dat.put(doc, function(err, doc) {
          if (err) throw err
          dat.get(doc._id, {rev: rev1}, function(err, docAtRev) {
            t.false(err, 'no err')
            t.equal(docAtRev.pizza, undefined, 'doc is version 1')
            setImmediate(done)
          })
        })
      })
{'fraction_non_alphanumeric': 0.1669869525639729, 'fraction_numerical': 0.00970971983412562, 'mean_word_length': 2.2970990330110035, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 5, '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': '3555951', 'n_tokens_mistral': 3420, 'n_tokens_neox': 3375, 'n_words': 725}