<commit_before>var test = require('./helpers')
var concat = require('concat-stream')

test('two versions', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('conflict', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', function(err) {
      t.ok(err, 'conflict err')
      t.ok(err.conflict, 'conflict flag set')
      t.same(err.key, 'foo', 'key set')
      t.end()
    })
  })
})

test('no conflict on force', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {force:true}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('version stream', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.createVersionStream('foo').pipe(concat(function(versions) {
        t.same(versions.length, 2, '2 versions')
        t.same(versions[0], {key:'foo', value:'bar', version:1})
        t.same(versions[1], {key:'foo', value:'baz', version:2})
        t.end()
      }))
    })
  })
})
<commit_msg>add test for maxogden/dat#154<commit_after>var test = require('./helpers')
var concat = require('concat-stream')

test('two versions', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('conflict', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', function(err) {
      t.ok(err, 'conflict err')
      t.ok(err.conflict, 'conflict flag set')
      t.same(err.key, 'foo', 'key set')
      t.end()
    })
  })
})

test('no conflict on force', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {force:true}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('version stream', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.createVersionStream('foo').pipe(concat(function(versions) {
        t.same(versions.length, 2, '2 versions')
        t.same(versions[0], {key:'foo', value:'bar', version:1})
        t.same(versions[1], {key:'foo', value:'baz', version:2})
        t.end()
      }))
    })
  })
})

test('no conflict on force stream', function(t, db) {
  var ws = db.createWriteStream({force:true})

  ws.write({
    key: 'foo',
    value: '0'
  })

  ws.write({
    key: 'foo',
    value: '1'
  })

  ws.write({
    key: 'foo',
    value: '2'
  })

  ws.end(function() {
    db.createReadStream().on('data', function(data) {
      t.same(data, {key:'foo', value:'2', version:3})
      t.end()
    })
  })
})
{'fraction_non_alphanumeric': 0.22427077600440287, 'fraction_numerical': 0.007705008255365988, 'mean_word_length': 2.6532663316582914, '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': '68206', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1400, 'n_words': 278}