const Parse = require('parse/node').Parse;
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter');
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';

//public schema
const databaseOptions1 = {
  initOptions: {
    connect: function (client, dc, isFresh) {
      if (isFresh) {
        client.query('SET search_path = public');
      }
    }
  }
};

//not exists schema
const databaseOptions2 = {
  initOptions: {
    connect: function (client, dc, isFresh) {
      if (isFresh) {
        client.query('SET search_path = not_exists_schema');
      }
    }
  }
};

const GameScore = Parse.Object.extend({
  className: "GameScore"
});

describe('Postgres database init options', () => {
  it('create server with public schema databaseOptions,shoud be ok', (done) => {
    reconfigureServer({
      databaseAdapter: new PostgresStorageAdapter({
        uri: postgresURI, collectionPrefix: 'test_',
        databaseOptions: databaseOptions1
      })
    }).then(done, fail);
  });

  it("save new GameScore in public schema", function (done) {
    var score = new GameScore({ "score": 1337, "playerName": "Sean Plott", "cheatMode": false });
    score.save().then(done, fail);
  });

  it('create server with not exists schema databaseOptions,shoud be fail', (done) => {
    reconfigureServer({
      databaseAdapter: new PostgresStorageAdapter({
        uri: postgresURI, collectionPrefix: 'test_',
        databaseOptions: databaseOptions2
      })
    }).catch(error => {
      expect(error.code).toEqual('42P01');
      done();
    });
  });
});

@@ -1531,16 +1531,57 @@
 )%0A    %7D)
+.then(() =%3E %7B%0A      done();%0A    %7D)%0A      
 .catch(e
@@ -1586,24 +1586,26 @@
 (error =%3E %7B%0A
+  
       expect
@@ -1637,32 +1637,34 @@
 01');%0A      
+  
 done();%0A
     %7D);%0A  %7D)
@@ -1647,24 +1647,26 @@
     done();%0A
+  
     %7D);%0A  %7D)

fix reconfigureServer callback