-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
60 lines (43 loc) · 1.77 KB
/
tests.py
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
59
60
import json
import os
import requests
import tempfile
import unittest
import app
class TestAPI(unittest.TestCase):
def setUp(self):
"""Sets up test database and server for each test"""
app.app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db.sqlite3"
app.app.testing = True
self.app = app.app.test_client()
with app.app.app_context():
app.db.create_all()
def tearDown(self):
"""Deletes test database for each test"""
os.remove("test.db.sqlite3")
def test_create_user(self):
"""Tests creating a user"""
post_json = json.dumps({"username": "TestingName",})
res = self.app.post("/users/", json=post_json)
self.assertEqual(res.status_code, 201)
res = self.app.get("/users/")
self.assertIn("TestingName", res.get_json()["Users"].values())
def test_create_chat(self):
"""Tests creating a chat"""
post_json = json.dumps({"username": "TestingName1",})
self.app.post("/users/", json=post_json)
post_json = json.dumps({"username": "TestingName2",})
self.app.post("/users/", json=post_json)
self.app.post("/?username=TestingName1", json=post_json)
res = self.app.get("/?username=TestingName1")
self.assertIn("chat with TestingName2", res.get_json()['Chats']['1'].keys())
def test_delete_user(self):
"""Tests deleting a user"""
post_json = json.dumps({"username": "TestingName",})
res = self.app.post("/users/", json=post_json)
res = self.app.delete("/users/?username=TestingName")
self.assertEqual(res.status_code, 200)
res = self.app.get("/users/")
self.assertFalse(res.get_json()["Users"])
if __name__ == "__main__":
unittest.main()