-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfile1.py
executable file
·149 lines (127 loc) · 6.64 KB
/
testfile1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
from flask import Flask, request, jsonify
import json
from myapp import app
import unittest
import psycopg2
from flask_testing import TestCase
app.testing = True
# Testing /post_location
class MyTest(unittest.TestCase):
con = None
cursor = None
def setUp(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
self.con, self.cursor = database_setUp()
return app
#Test1: pincode type is not a string
def test_pincode_type(self):
with app.test_client() as client:
sent = {"pincode" : 110026,"place": "Punjabi Bagh","city": "New Delhi","latitude": 28.6488,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 1:' , result.data
self.assertEqual(result.data, "pincode is not a string")
#Test2: place type is not a string
def test_place_type(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": 5.0,"city": "New Delhi","latitude": 28.6488,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 2:' , result.data
self.assertEqual(result.data, "place is not a string")
#Test3: city type is not a string
def test_city_type(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": "Punjabi Bagh","city": 0,"latitude": 28.6488,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 3:' , result.data
self.assertEqual(result.data, "city is not a string")
#Test4: latitude type is not a float
def test_lat_type1(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": "Punjabi Bagh","city": "New Delhi","latitude": 28,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 4:' , result.data
self.assertEqual(result.data, "latitude is not a float")
#Test5: latitude type is not a float
def test_lat_type2(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": "Punjabi Bagh","city": "New Delhi","latitude": "hello","longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 5:' , result.data
self.assertEqual(result.data, "latitude is not a float")
#Test6: longitude type is not a float
def test_lon_type1(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": "Punjabi Bagh","city": "New Delhi","latitude": 28.6488,"longitude": 77, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 6:' , result.data
self.assertEqual(result.data, "longitude is not a float")
#Test7: longitude type is not a float
def test_long_type2(self):
with app.test_client() as client:
sent = {"pincode" : "110026","place": "Punjabi Bagh","city": "New Delhi","latitude": 28.6488,"longitude":"hello", "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 7:' , result.data
self.assertEqual(result.data, "longitude is not a float")
#Test8: place wth same pincode already exists in the database
def test_existing_pincode(self):
with app.test_client() as client:
sent = {"pincode" : "IN/110026","place": "Punjabi Bagh","city": "New Delhi","latitude": 28.6488,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 8:' , result.data
self.assertEqual(result.data, "place with the same pincode exists")
#Test9: place with different pincode but same coordinates exist
def test_same_lat_lon(self):
with app.test_client() as client:
sent = {"pincode" : "IN/111111","place": "Punjabi Bagh","city": "New Delhi","latitude": 28.6488,"longitude":77.1726, "accuracy": 0}
sent = json.dumps(sent)
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json')
print 'Test 9:' , result.data
self.assertEqual(result.data, "place with similar coordinates exists")
#Test10: different pincode but coordinates are close enough be same
def test_similar_coordinates(self):
with app.test_client() as client:
sent = {"pincode" : "IN/110334","place": "Bharat Nagar","city": "New Delhi","latitude": 28.6400,"longitude": 77.2150, "accuracy": 0}
sent = json.dumps(sent)
client.post('http://localhost:5000/delete_post_location', data = sent, content_type = 'application/json') #Delete the test entry if it exists
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json') #Add test entry
print 'Test 10:' , result.data
client.post('http://localhost:5000/delete_post_location', data = sent, content_type = 'application/json') #Delete the test entry if it exists
self.assertEqual(result.data, "place with similar coordinates exists")
#Test11: Add an entry to the database
def test_add_location(self):
with app.test_client() as client:
sent = {"pincode" : "IN/500052","place": "Hasannagar","city": "Telangana","latitude": 17.387140,"longitude":78.491684, "accuracy": 0}
sent = json.dumps(sent)
client.post('http://localhost:5000/delete_post_location', data = sent, content_type = 'application/json') #Delete the test entry if it exists
result = client.post('http://localhost:5000/post_location', data = sent, content_type = 'application/json') #Add test entry
print 'Test 11:' , result.data
client.post('http://localhost:5000/delete_post_location', data = sent, content_type = 'application/json') #Delete the test entry if it exists
self.assertEqual(result.data, "location saved")
def tearDown(self):
self.cursor.close()
self.con.close()
def database_setUp():
con = None
cursor = None
try:
con = psycopg2.connect("host='localhost' dbname='pincodes' user='postgres' password='password'")
cursor = con.cursor()
#cursor.execute('DELETE FROM loc WHERE key = "IN/500052"')
except:
print ("I am unable to connect to the database")
return con, cursor
if __name__ == "__main__":
unittest.main()
cursor.close()
con.close()