-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitDB.py
112 lines (104 loc) · 4.51 KB
/
InitDB.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
"""
Fills the mongoDB database with the necessary data for Test_Client to run
properly.
"""
import pymongo
import Test_Data
import time
import copy as cp
def clearDB():
client = pymongo.MongoClient("mongodb+srv://De1-SoC:l2b-11_sabotage@"
"sabotage.q6bsa.mongodb.net/Sabotage?"
"retryWrites=true&w=majority")
db = client["Sabotage"]
userIDCluster = db["IDs"]
usersCluster = db["Users"]
sessionsCluster = db["Sessions"]
userIDCluster.delete_many({})
usersCluster.delete_many({})
sessionsCluster.delete_many({})
def initDB():
client = pymongo.MongoClient("mongodb+srv://De1-SoC:l2b-11_sabotage@"
"sabotage.q6bsa.mongodb.net/Sabotage?"
"retryWrites=true&w=majority")
db = client["Sabotage"]
userIDCluster = db["IDs"]
usersCluster = db["Users"]
sessionsCluster = db["Sessions"]
test_POST_De1 = cp.deepcopy(Test_Data.test_POST_De1)
test_POST_app = cp.deepcopy(Test_Data.test_POST_app)
# Initialize for De1-SoC POST test
if sessionsCluster.find_one({
"SessionID": test_POST_De1["SessionID"]}) is None:
sessionsCluster.insert_one({
"SessionID": test_POST_De1["SessionID"],
"Coord": [test_POST_De1["Coord"]["lat"],
test_POST_De1["Coord"]["lon"]],
"Metrics": {
"NumPlayers": int(test_POST_De1["NumPlayers"])
}
})
user = usersCluster.find_one({"UserID": test_POST_De1["Win"]})
if user is None:
usersCluster.insert_one({"UserID": test_POST_De1["Win"]})
usersCluster.update_one({"UserID": test_POST_De1["Win"]},
[{"$set": {
"CurrSession": {
"ID": test_POST_De1["SessionID"],
"Start": time.time(),
"Win": False
},
"NearbySessions": []
}}])
usersCluster.update_one({"UserID": test_POST_De1["Win"]},
{"$set": {"SabTokens": 1}})
# Initialize for Mobile App POST test
user = usersCluster.find_one({"UserID": test_POST_app["UserID"]})
if user is None:
usersCluster.insert_one({"UserID": test_POST_app["UserID"]})
nearby = [{"SessionID": loc["SessionID"], "Coord": loc["Coord"]}
for loc in Test_Data.locations]
usersCluster.update_one({"UserID": test_POST_app["UserID"]},
[{"$set": {
"CurrSession": {
"ID": Test_Data.loc0["SessionID"],
"Start": time.time(),
"Win": False
},
"NearbySessions": nearby,
"SabTokens": 1
}}])
session = sessionsCluster.find_one(
{"SessionID": test_POST_app["SessionID"]})
if session is None:
sessionsCluster.insert_one(
{"SessionID": test_POST_app["SessionID"],
"Coord": ["49.49", "-123.11"],
"Metrics": {"NumPlayers": 66}})
# Initialize user ID records for GET /lobby
IDs = [int(test_POST_De1["Win"]), int(test_POST_app["UserID"])]
for id in IDs:
if userIDCluster.find_one({"ID": id}) is None:
userIDCluster.insert_one({"ID": id})
# Locations
for loc in Test_Data.locations:
if sessionsCluster.find_one({"SessionID": loc["SessionID"]}) is None:
x = cp.deepcopy(loc)
x["Metrics"]["NumPlayers"] = int(loc["Metrics"]["NumPlayers"])
sessionsCluster.insert_one(x)
# Test leave with -1 flag
testLeaveSession = cp.deepcopy(Test_Data.testLeaveSession)
testLeaveSession["Metrics"]["NumPlayers"] =\
int(testLeaveSession["Metrics"]["NumPlayers"])
sessionsCluster.update_one({
"SessionID": testLeaveSession["SessionID"],
}, {"$set": testLeaveSession}, upsert=True) # Always overwrite
if (userIDCluster.find_one(
{"ID": Test_Data.testLeaveUser["UserID"]}) is None):
userIDCluster.insert_one(
{"ID": int(Test_Data.testLeaveUser["UserID"])})
usersCluster.update_one({
"UserID": Test_Data.testLeaveUser["UserID"]
}, {"$set": Test_Data.testLeaveUser}, upsert=True) # Always overwrite
if __name__ == "__main__":
initDB()