{"size": 2787, "ext": "js", "max_stars_repo_path": "test/insert.js", "max_stars_repo_name": "jhalag/better-sqlite3-helper", "max_stars_repo_stars_event_min_datetime": "2018-06-11T23:14:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-11T07:39:23.000Z", "max_issues_repo_path": "test/insert.js", "max_issues_repo_name": "jhalag/better-sqlite3-helper", "max_issues_repo_issues_event_min_datetime": "2019-01-18T07:40:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-04T04:33:20.000Z", "max_forks_repo_path": "test/insert.js", "max_forks_repo_name": "jhalag/better-sqlite3-helper", "max_forks_repo_forks_event_min_datetime": "2018-08-06T11:16:37.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-19T22:05:56.000Z"}
{"max_stars_count": 37, "max_issues_count": 16, "max_forks_count": 10, "avg_line_length": 23.225, "max_line_length": 113, "alphanum_fraction": 0.5288841048}
const { describe, it, afterEach } = require('mocha')
const { expect } = require('chai')
const DB = require('../src/database')
const fs = require('fs')
const path = require('path')
const appRoot = require('app-root-path').path
let db = null

describe('Database Insert', function () {
  afterEach(() => {
    db && db.close()
    db = null
    try {
      fs.unlinkSync(path.resolve(appRoot, './data/sqlite3.db'))
      fs.rmdirSync(path.resolve(appRoot, './data'))
    } catch (e) {}
  })

  it('can insert one line', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.insert('Setting', {
      key: 'test2',
      value: '1234',
      type: 0
    })).to.be.equal(3)
  })

  it('can insert more than one line', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.insert('Setting', [{
      key: 'test2',
      value: '1234',
      type: 0
    }, {
      key: 'test3',
      value: '12345',
      type: 0
    }])).to.be.equal(4)

    expect(db.queryFirstCell('SELECT COUNT(1) FROM Setting WHERE key IN (?,?)', 'test2', 'test3')).to.be.equal(2)
  })

  it('can insert with whitelist', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.insert('Setting', [{
      key: 'test2',
      value: '1234',
      type: 0
    }, {
      key: 'test3',
      value: '12345',
      type: 0
    }], ['key'])).to.be.equal(4)

    // eslint-disable-next-line no-unused-expressions
    expect(db.queryFirstCell('SELECT value FROM Setting WHERE key = ?', 'test2')).to.be.null
  })

  it('can insert with blacklist', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.insertWithBlackList('Setting', [{
      key: 'test2',
      value: '1234',
      type: 1
    }, {
      key: 'test3',
      value: '12345',
      type: 0
    }], ['type'])).to.be.equal(4)
    expect(db.queryFirstCell('SELECT type FROM Setting WHERE key = ?', 'test2')).to.equal(0)
  })

  it('can insert one line with replace', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.replace('Setting', {
      key: 'test2',
      value: '12349',
      type: 0
    })).to.be.equal(3)
  })

  it('replace will return new ID if it overwrites', function () {
    db = new DB({
      migrate: {
        migrationsPath: './test/migrations'
      }
    })
    expect(db.replace('Setting', {
      key: 'test',
      value: 'new value',
      type: 0
    })).to.be.equal(3)
    expect(db.replace('Setting', {
      key: 'test',
      value: 'new value',
      type: 0
    })).to.be.equal(4)
  })
})
