<commit_before>import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import streamutil from 'streamutil';

import * as sqlutil from '../lib/sqlutil';

chai.use(chaiAsPromised);
let expect = chai.expect;

describe('table with foreign keys', () => {
  let db;
  let table;
  let parentTable;
  let childTable;

  beforeEach(() => {
    db = new sqlutil.Db();

    parentTable = new sqlutil.Table(db, {
      name: 'parent',
      columns: {
        id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
        name: {type: sqlutil.DataType.TEXT, unique: true}
      }
    });

    childTable = new sqlutil.Table(db, {
      name: 'child',
      columns: {
        id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
        value: {type: sqlutil.DataType.TEXT, unique: true},
        parentId: {type: sqlutil.DataType.INTEGER}
      },
      foreignKeys: [
        {from: 'parentId', references: {parent: 'id'}}
      ]
    });

    return db.open(':memory:')
      .then(() => db.enableForeignKeys())
      .then(() => Promise.all([
        parentTable.createTable(),
        childTable.createTable()
      ]));
  });

  function insertSomeData() {
    return parentTable.insert({id: 1, name: 'Banan'})
      .then(() => childTable.insert({parentId: 1, value: 'Apa'}))
      .then(() => childTable.insert({parentId: 1, value: 'Ape'}));
  }
  
  it('should be possible to insert valid rows', () => {
    return insertSomeData();
  });

  it('should not be possible to insert foreign key validations', () => {
    return expect(childTable.insert({parentId: 42, value: 'Ape'})).to.eventually.be.rejectedWith(/FOREIGN KEY constraint failed/);
  });

  it('should be possible to remove foreign keys from a table', () => {
    return insertSomeData()
      .then(() => {
        let newChildTable = new sqlutil.Table(db, {
          name: 'child',
          columns: {
            id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
            value: {type: sqlutil.DataType.TEXT, unique: true},
            parentId: {type: sqlutil.DataType.INTEGER}
          }
        });

        return newChildTable.createTable()
          .then(status => expect(status.wasRecreated).to.be.true)
          .then(() => newChildTable.insert({parentId: 42, value: 'Elephant'}));
      });
  });

  it('should be possible to add foreign keys to a table');
  it('table should only be recreated when needed');
});<commit_msg>Add documentation for more foreign key test cases<commit_after>import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import streamutil from 'streamutil';

import * as sqlutil from '../lib/sqlutil';

chai.use(chaiAsPromised);
let expect = chai.expect;

describe('table with foreign keys', () => {
  let db;
  let table;
  let parentTable;
  let childTable;

  beforeEach(() => {
    db = new sqlutil.Db();

    parentTable = new sqlutil.Table(db, {
      name: 'parent',
      columns: {
        id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
        name: {type: sqlutil.DataType.TEXT, unique: true}
      }
    });

    childTable = new sqlutil.Table(db, {
      name: 'child',
      columns: {
        id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
        value: {type: sqlutil.DataType.TEXT, unique: true},
        parentId: {type: sqlutil.DataType.INTEGER}
      },
      foreignKeys: [
        {from: 'parentId', references: {parent: 'id'}}
      ]
    });

    return db.open(':memory:')
      .then(() => db.enableForeignKeys())
      .then(() => Promise.all([
        parentTable.createTable(),
        childTable.createTable()
      ]));
  });

  function insertSomeData() {
    return parentTable.insert({id: 1, name: 'Banan'})
      .then(() => childTable.insert({parentId: 1, value: 'Apa'}))
      .then(() => childTable.insert({parentId: 1, value: 'Ape'}));
  }
  
  it('should be possible to insert valid rows', () => {
    return insertSomeData();
  });

  it('should not be possible to insert foreign key validations', () => {
    return expect(childTable.insert({parentId: 42, value: 'Ape'})).to.eventually.be.rejectedWith(/FOREIGN KEY constraint failed/);
  });

  it('should be possible to remove foreign keys from a table', () => {
    return insertSomeData()
      .then(() => {
        let newChildTable = new sqlutil.Table(db, {
          name: 'child',
          columns: {
            id: {type: sqlutil.DataType.INTEGER, primaryKey: true},
            value: {type: sqlutil.DataType.TEXT, unique: true},
            parentId: {type: sqlutil.DataType.INTEGER}
          }
        });

        return newChildTable.createTable()
          .then(status => expect(status.wasRecreated).to.be.true)
          .then(() => newChildTable.insert({parentId: 42, value: 'Elephant'}));
      });
  });

  it('should be possible to add foreign keys to a table');
  it('table should only be recreated when needed');
  it('should be possible to update table with foreign key target');
});
{'fraction_non_alphanumeric': 0.15606121627064035, 'fraction_numerical': 0.002819170358437374, 'mean_word_length': 2.7916030534351144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '86029', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1528, 'n_words': 393}