Files
Andre Neto db490b0354 Fix the defects the suite pinned; turn xfail/known_bug into regressions
The suite documented every defect with two `xfail` and four `test_known_bug_*`
tests. With the tests as the specification, all of them are fixed and those
tests are now regression tests that fail against the previous code:

- findby_director() returned None on the success path (it printed the matches
  and fell off the end); it returns the rows now.
- findby_actor() raised UnboundLocalError when no actor matched, and returned
  only the last article of an actor with several; it returns the list, or None.
- findby_name() and findby_type() returned the integer 0 on no match, which
  main.py indexed into; they return an empty list and the menus skip it.
- purchase() debited USERID (the account of the last log_in) instead of the
  userid argument, while the rent went to the argument.
- the message list looked the author up with row[4] (the recipient) instead of
  row[5] (the sender), in the unread and in the read list.
- remove_article() checked only the open rents, so the foreign keys on the
  older rents and on pricehistory turned the refusal into a traceback; it now
  explains which records keep the article and rolls back as a backstop.
- show_unread_messages() returned None when there was nothing; it returns an
  empty list like the read side, and the messages menu skips an empty list.
- database.USERID did not exist before a login, so leaving the menu with 0
  raised AttributeError; it starts as None and the menu code handles 0.
- every connection is opened and closed by one context manager:
  findby_director(), findby_actor() and findby_type() returned before
  conn.close() and statistics() never closed, leaving sessions
  `idle in transaction`.

make test: 40 passed, 0 xfailed (was 30 passed, 2 xfailed). No xfail and no
test_known_bug_* is left. The README's previous "what is broken" paragraph and
its test output are updated, and a "Known limitations" section lists what was
deliberately not touched: SQL built by string concatenation, menu selections
past the end of the list, and input() inside the data layer.
2026-10-08 14:38:11 +01:00

387 lines
16 KiB
Python

import database
#para ser admin tem de conter @netflox.com
def menu():
menu_interface = """
-- Netflox --
1) Sign Up
2) Log in
0) Exit
Your Selection: """
while (user_input := input(menu_interface)) != '0':
if user_input == '1':
print("\n\tSign up")
name = input("\tEnter your name: ")
email = input("\tEnter your e-mail: ")
password = input("\tEnter your password: ")
database.create_user(name, email, password)
elif user_input == '2':
print("\n\tLog in")
email = input("\tEnter your e-mail: ")
password = input("\tEnter your password: ")
user = database.log_in(email, password)
if user == -1:
return user # admin
elif user == 1:
return user # client
else:
print("Invalid input, please try again")
def client():
order_interface = """
1) Title
2) Director
3) Release Year
4) IMDB Rating
5) Genre
6) Price
7) Type
0) Exit
Your Selection: """
article_interface = """
1) View details
2) Purchase
0) Exit
Your Selection: """
search_interface = """
1) Title
2) Director
3) Type
4) Actors
Your Selection: """
client_interface = """
1) Search articles
2) List all articles
3) Order by
4) View my articles
5) My history
6) Messages
0) Exit
Your Selection: """
while (client_input := input(client_interface)) != '0':
if client_input == '1':
while (client_search := input(search_interface)) != '0':
if client_search == '1':
client_search_typo=input("\tInsert movie name: ")
article=database.findby_name(client_search_typo)
if not article:
print("\n\tNo article matches that search")
continue
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
pass
else:
pass
break
if client_search == '2':
client_search_typo=input("\tInsert movie director: ")
article=database.findby_director(client_search_typo)
if not article:
print("\n\tNo article matches that search")
continue
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
pass
else:
pass
break
if client_search == '3':
client_search_typo=input("\tInsert movie type: ")
article=database.findby_type(client_search_typo)
if not article:
print("\n\tNo article matches that search")
continue
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
pass
else:
pass
break
if client_search == '4':
client_search_typo=input("\tInsert movie actor: ")
article=database.findby_actor(client_search_typo)
if not article:
print("\n\tNo article matches that search")
continue
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
pass
else:
pass
break
if client_input == '2':
article = database.list_all()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
pass
else:
pass
break
if client_input == '3':
while (client_in := input(order_interface)) != '0':
if client_in == '1':
article = database.order_title()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_in == '2':
article = database.order_director()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_in == '3':
article = database.order_year()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x-1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x-1][0], USERID)
break
else:
pass
break
break
if client_in == '4':
article = database.order_imdb()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_in == '5':
article = database.order_genre()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_in == '6':
article = database.order_price()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_in == '7':
article = database.order_type()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (client_in := input(article_interface)) != '0':
if client_in == '1':
database.view_details(article[x - 1][0])
if client_in == '2':
if str(input("\n\tAre you sure (y/n): ")) == "y":
database.purchase(article[x - 1][0], USERID)
break
else:
pass
break
break
if client_input == '4':
article_id = database.my_articles(USERID)
if article_id != 0:
database.time_left(article_id, USERID)
if client_input == '5':
if database.my_history(USERID) == 0:
pass
if client_input == '6':
message_interface = """
1) View unread messages
2) View read messages
0) Exit
Your Selection: """
while (client_input := input(message_interface)) != '0':
if client_input == '1':
y = database.show_unread_messages(USERID)
if not y:
continue
while (x := int(input("\tYour Selection: "))) != 0:
database.read_message(y[x - 1][0])
print("\n\t" + y[x - 1][1])
print("\n\t0) Go back")
if client_input == '2':
y = database.show_read_messages(USERID)
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + y[x - 1][1])
print("\n\t0) Go back")
def admin():
admin_interface = """
1) Add new article
2) View all
3) Change price
4) Remove article
5) Messages
6) Alter user balance
7) Statistics
0) Exit
Your Selection: """
admin_article = """
1) View details
0) Exit
Your Selection: """
while (admin_input := input(admin_interface)) != '0':
if admin_input == '1':
print("\n\tAdd new article")
name = str(input("\tName: "))
director = str(input("\tDirector: "))
imbdrating = str(input("\tIMDB rating: "))
genre = str(input("\tGenre: "))
price = str(input("\tPrice: "))
year = str(input("\tYear of release: "))
days_available = str(input("\tDays available: "))
type = str(input("\tType: "))
database.add_article(name, director, imbdrating, genre, price, year, days_available, type)
if admin_input == '2':
article = database.list_all()
while (x := int(input("\tYour Selection: "))) != 0:
print("\n\t" + article[x - 1][1])
while (admin_in := input(admin_article)) != '0':
if admin_in == '1':
database.view_details(article[x - 1][0])
else:
break
break
if admin_input == '3':
print("\n\tChange price")
n_id = input("\tMovie name or id: ")
new_price = input("\tNew Price: ")
database.change_price(n_id, new_price)
if admin_input == '4':
print("\n\tRemove article")
n_id = input("\tMovie name or id: ")
database.remove_article(n_id)
if admin_input == '5':
message_interface = """
1) Send message to all
2) Send message to a client
0) Exit
Your Selection: """
while (admin_input := input(message_interface)) != '0':
if admin_input == '1':
print("\n\tSend message to all")
msg = input("\tMessage: ")
database.message_all(msg, database.USERID)
if admin_input == '2':
print("\n\tSend message to a client")
msg = input("\tMessage: ")
recieverid = input("\tClient id: ")
database.message_client(msg, recieverid, database.USERID)
if admin_input == '6':
print("\n\tAlter user balance")
userid = input("\tUser id: ")
balance = input("\tNew balance: ")
database.alter_balance(userid, balance)
if admin_input == '7':
database.statistics()
# menu() returns 1 for a client, -1 for an administrator and None when the
# user leaves with 0. That last case used to be tested with `!= -1`, so it
# went into the client branch and read database.USERID, which did not exist
# until somebody logged in (AttributeError). Nobody is logged in there, so
# neither menu is opened.
role = menu()
if role == -1:
USERID = database.USERID
admin()
elif role == 1:
USERID = database.USERID
client()
else:
print("\n\tGoodbye")