File size: 1,899 Bytes
458fa79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import unittest
import threading
import time
import json
import urllib.request

from app.server import run_server


class TestHTTPAPI(unittest.TestCase):
    _server_started = False

    @classmethod
    def setUpClass(cls):
        if not cls._server_started:
            t = threading.Thread(target=run_server, kwargs={"host": "127.0.0.1", "port": 8765}, daemon=True)
            t.start()
            time.sleep(1.0)
            cls._server_started = True

    def test_api_ask(self):
        body = json.dumps({"query": "Simulate adding 3 MW of load on feeder F1"}).encode("utf-8")
        req = urllib.request.Request(
            "http://127.0.0.1:8765/api/ask",
            data=body,
            headers={"Content-Type": "application/json"},
            method="POST",
        )
        with urllib.request.urlopen(req, timeout=5) as resp:
            self.assertEqual(resp.status, 200)
            data = json.loads(resp.read().decode("utf-8"))
            self.assertIn("answer", data)
            self.assertIn("steps", data)
            self.assertIsInstance(data["steps"], list)

    def test_api_upload_grid(self):
        payload = {
            "raw": "feeder_id,name,base_kv,num_customers,peak_mw,pv_mw\n"
                   "U1,Feeder U1,13.8,100,7.0,1.0\n",
            "format": "csv",
        }
        body = json.dumps(payload).encode("utf-8")
        req = urllib.request.Request(
            "http://127.0.0.1:8765/api/upload-grid",
            data=body,
            headers={"Content-Type": "application/json"},
            method="POST",
        )
        with urllib.request.urlopen(req, timeout=5) as resp:
            self.assertEqual(resp.status, 200)
            data = json.loads(resp.read().decode("utf-8"))
            self.assertEqual(data["status"], "ok")
            self.assertIn("U1", data["feeders_loaded"])


if __name__ == "__main__":
    unittest.main()