forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_place.py
executable file
·82 lines (66 loc) · 2.24 KB
/
main_place.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
#!/usr/bin/python3
import inspect
import io
import sys
import cmd
import shutil
import os
"""
Backup console file
"""
if os.path.exists("tmp_console_main.py"):
shutil.copy("tmp_console_main.py", "console.py")
shutil.copy("console.py", "tmp_console_main.py")
"""
Updating console to remove "__main__"
"""
with open("tmp_console_main.py", "r") as file_i:
console_lines = file_i.readlines()
with open("console.py", "w") as file_o:
in_main = False
for line in console_lines:
if "__main__" in line:
in_main = True
elif in_main:
if "cmdloop" not in line:
file_o.write(line.lstrip(" "))
else:
file_o.write(line)
import console
"""
Create console
"""
console_obj = "HBNBCommand"
for name, obj in inspect.getmembers(console):
if inspect.isclass(obj) and issubclass(obj, cmd.Cmd):
console_obj = obj
my_console = console_obj(stdout=io.StringIO(), stdin=io.StringIO())
my_console.use_rawinput = False
"""
Exec command
"""
def exec_command(my_console, the_command, last_lines = 1):
my_console.stdout = io.StringIO()
real_stdout = sys.stdout
sys.stdout = my_console.stdout
my_console.onecmd(the_command)
sys.stdout = real_stdout
lines = my_console.stdout.getvalue().split("\n")
return "\n".join(lines[(-1*(last_lines+1)):-1])
"""
Tests
"""
state_id = exec_command(my_console, "create State name=\"California\"")
if state_id is None or state_id == "":
print("FAIL: Can't create State")
city_id = exec_command(my_console, "create City state_id=\"{}\" name=\"Fremont\"".format(state_id))
if city_id is None or city_id == "":
print("FAIL: Can't create City")
user_id = exec_command(my_console, "create User email=\"[email protected]\" password=\"pwd\" first_name=\"fn\" last_name=\"ln\"")
if user_id is None or user_id == "":
print("FAIL: Can't create User")
place_id = exec_command(my_console, "create Place city_id=\"{}\" user_id=\"{}\" name=\"House\" number_bathrooms=2 max_guest=6 price_by_night=100 latitude=1.3 longitude=2.3".format(city_id, user_id))
if place_id is None or place_id == "":
print("FAIL: Can't create Place")
print("OK", end="")
shutil.copy("tmp_console_main.py", "console.py")