{"size": 1952, "ext": "js", "max_stars_repo_path": "test/setup.unit.js", "max_stars_repo_name": "KenGunn/postcodes.io", "max_stars_repo_stars_event_min_datetime": "2020-07-04T17:38:40.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-04T17:38:40.000Z", "max_issues_repo_path": "test/setup.unit.js", "max_issues_repo_name": "KenGunn/postcodes.io", "max_issues_repo_issues_event_min_datetime": "2020-04-06T23:56:03.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T01:55:27.000Z", "max_forks_repo_path": "test/setup.unit.js", "max_forks_repo_name": "KenGunn/postcodes.io", "max_forks_repo_forks_event_min_datetime": "2020-02-21T10:02:20.000Z", "max_forks_repo_forks_event_max_datetime": "2020-02-21T10:02:20.000Z"}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": 1, "avg_line_length": 26.0266666667, "max_line_length": 77, "alphanum_fraction": 0.6101434426}
"use strict";

const { assert } = require("chai");
const {
  setupSupportTables,
  setupOutcodeTable,
  SUPPORT_TABLES,
} = require("../app/lib/setup.js");
const { series, parallel } = require("async");
const {
  listDatabaseRelations,
  clearPostcodeDb,
  seedPostcodeDb,
  Outcode,
} = require("./helper/index.js");

const clearSupportTables = () => {
  return new Promise((resolve, reject) => {
    const instructions = SUPPORT_TABLES.map(m => m._destroyRelation.bind(m));
    parallel(instructions, (error, result) => {
      if (error) return reject(error);
      return resolve(result);
    });
  });
};

describe("Setup methods", () => {
	before(function (done) {
		this.timeout(0);
		series([
			clearPostcodeDb,
			seedPostcodeDb
		], done);
	});

	after(clearPostcodeDb);

  describe("setupSupportTables", () => {
    before(async () => {
      await clearSupportTables()
    });

    after(async function () {
      this.timeout(0);
      await setupSupportTables();
    });

    it ("creates support tables", async function () {
      this.timeout(0);
      const existing = await listDatabaseRelations();   
      SUPPORT_TABLES.forEach(( { relation } ) => {
        assert.isFalse(existing.rows.some(({ Name }) => Name === relation));
      });
      await setupSupportTables();
      const created = await listDatabaseRelations();
      SUPPORT_TABLES.forEach(( { relation } ) => {
        assert.isTrue(created.rows.some(({ Name }) => Name === relation));
      });
    });
  });

  describe("setupOutcodeTable", () => {
    before(done => Outcode._destroyRelation(done));
    
    it ("creates outcode table", async () => {
      const existing = await listDatabaseRelations();
      assert.isFalse(existing.rows.some(({ Name }) => Name === "outcodes"));
      await setupOutcodeTable();
      const created = await listDatabaseRelations();
      assert.isTrue(created.rows.some(({ Name }) => Name === "outcodes"));
    });
  });
});

