<commit_before>const test = require('blue-tape')
const Dexie = require('dexie')
const DexieBatch = require('./dexie-batch')

const batchSize = 10
const testEntries = Array(42).fill().map((_, i) => i)

testWithTable('basic operation', (t, table) => {
  let maxIdx = -1
  const readEntries = []

  return table.count()
    .then(n => {
      t.is(n, testEntries.length, 'written right number of entries')
      return new DexieBatch({ batchSize, limit: n })
    })
    .then(db => db.each(table.toCollection(), (entry, i) => {
      readEntries.push(entry)
      maxIdx = Math.max(maxIdx, i)
    }))
    .then(_ => {
      readEntries.sort((a, b) => a - b)
      t.ok(maxIdx > 0, 'indices valid')
      t.ok(maxIdx < batchSize, 'batches sized correctly')
      t.deepEqual(readEntries, testEntries, 'entries read correctly')
    })
})

function testWithTable(name, f) {
  const db = new Dexie('test-db')
  db.version(1).stores({ test: '++' })
  db.test.bulkAdd(testEntries)
    .then(_ => test(name, t => {
      return f(t, db.test)
        .then(_ => db.delete())
        .catch(err => {
          db.delete()
          throw err
        })
    }))
}<commit_msg>Add tests for serial operation<commit_after>const test = require('blue-tape')
const Dexie = require('dexie')
const DexieBatch = require('./dexie-batch')

const batchSize = 10
const testEntries = Array(42).fill().map((_, i) => i)

testWithTable('basic serial operation', (t, table) => {
  return testBasicOperation(t, table, new DexieBatch({ batchSize }))
})

testWithTable('basic parallel operation', (t, table) => {
  return table.count()
    .then(n => new DexieBatch({ batchSize, limit: n }))
    .then(db => testBasicOperation(t, table, db))
})

function testBasicOperation(t, table, db) {
  let maxIdx = -1
  const readEntries = []

  return Promise.resolve(db)
    .then(db => db.each(table.toCollection(), (entry, i) => {
      readEntries.push(entry)
      maxIdx = Math.max(maxIdx, i)
    }))
    .then(_ => {
      readEntries.sort((a, b) => a - b)
      t.equal(maxIdx + 1, batchSize, 'batches sized correctly')
      t.deepEqual(readEntries, testEntries, 'entries read correctly')
    })
}

function testWithTable(name, f) {
  const db = new Dexie(name)
  db.version(1).stores({ test: '++' })
  db.test.bulkAdd(testEntries)
    .then(_ => test(name, t => {
      return f(t, db.test)
        .then(_ => db.delete())
        .catch(err => {
          db.delete()
          throw err
        })
    }))
}
{'fraction_non_alphanumeric': 0.17245557350565427, 'fraction_numerical': 0.005654281098546042, 'mean_word_length': 2.9568690095846644, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 28, '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': '4052185', 'n_tokens_mistral': 859, 'n_tokens_neox': 832, 'n_words': 197}