#!/usr/bin/env python3

# vim: set ai et ts=4 sw=4:

import os
import random
import subprocess
import requests
import logging

PORT = 8000 + int(random.random()*1000)
logging.basicConfig(level=logging.WARNING)

def hurmadb_start(port):
    """
    Start HurmaDB on given port, return an object that corresponds to the created process.
    """
    return subprocess.Popen(['./hurmadb', str(port)])

def hurmadb_stop(pobj):
    """
    Stop HurmaDB process created by hurmadb_start()
    """
    # subprocess.call(f"kill {pobj.pid}", shell = True)
    res = requests.put(f'http://localhost:{PORT}/v1/_stop')
    res = requests.get(f'http://localhost:{PORT}/')
    pobj.wait()

class TestBasic:
    def setup_class(self):
        self.log = logging.getLogger('HurmaDB')
        self.pobj = hurmadb_start(PORT)
        self.log.debug(f"HurmaDB started on port {PORT}")

    def teardown_class(self):
        hurmadb_stop(self.pobj)
        self.log.debug("HurmaDB stopped")

    def test_index(self):
        self.log.debug("Running test_index")
        res = requests.get(f'http://localhost:{PORT}/')
        assert(res.status_code == 200)

    def test_not_found(self):
        self.log.debug("Running test_not_found")
        res = requests.get(f'http://localhost:{PORT}/no-such-page/')
        assert(res.status_code == 404)

    def test_kv(self):
        self.log.debug("Running test_kv")
        url = f'http://localhost:{PORT}/v1/kv/Some_Key-123'
        doc = {'foo':'bar', 'baz':['qux']}
        # Make sure there is no such document
        res = requests.get(url)
        assert(res.status_code == 404)
        # Create a new document
        res = requests.put(url, json = doc)
        assert(res.status_code == 200)
        # Verify that the document was created
        res = requests.get(url)
        assert(res.status_code == 200)
        assert(res.json() == doc)
        # Delete the document
        res = requests.delete(url)
        assert(res.status_code == 200)
        # Verify that the document was deleted
        res = requests.get(url)
        assert(res.status_code == 404)
        res = requests.delete(url)
        assert(res.status_code == 404)


@@ -487,64 +487,8 @@
 %22%22%22%0A
-    # subprocess.call(f%22kill %7Bpobj.pid%7D%22, shell = True)%0A
     
@@ -506,17 +506,16 @@
 sts.put(
-f
 'http://
@@ -521,28 +521,24 @@
 /localhost:%7B
-PORT
 %7D/v1/_stop')
@@ -536,16 +536,29 @@
 1/_stop'
+.format(PORT)
 )%0A    re
@@ -566,33 +566,32 @@
  = requests.get(
-f
 'http://localhos
@@ -589,31 +589,40 @@
 /localhost:%7B
+%7D/'.format(
 PORT
-%7D/'
+)
 )%0A    pobj.w
@@ -783,17 +783,16 @@
 g.debug(
-f
 %22HurmaDB
@@ -813,14 +813,23 @@
 rt %7B
+%7D%22.format(
 PORT
-%7D%22
+)
 )%0A%0A 
@@ -1022,33 +1022,32 @@
  = requests.get(
-f
 'http://localhos
@@ -1053,15 +1053,24 @@
 st:%7B
+%7D/'.format(
 PORT
-%7D/'
+)
 )%0A  
@@ -1213,17 +1213,16 @@
 sts.get(
-f
 'http://
@@ -1232,20 +1232,16 @@
 alhost:%7B
-PORT
 %7D/no-suc
@@ -1248,16 +1248,29 @@
 h-page/'
+.format(PORT)
 )%0A      
@@ -1382,17 +1382,16 @@
   url = 
-f
 'http://
@@ -1401,20 +1401,16 @@
 alhost:%7B
-PORT
 %7D/v1/kv/
@@ -1422,16 +1422,29 @@
 Key-123'
+.format(PORT)
 %0A       

Make Python code compatible with Python 3.5