diff --git a/src/backend/.coveragerc b/src/backend/.coveragerc index 01f00d3d2..b0c3b029b 100644 --- a/src/backend/.coveragerc +++ b/src/backend/.coveragerc @@ -1,4 +1,3 @@ [run] omit = *tests* - *init* - *decorators* \ No newline at end of file + *init* \ No newline at end of file diff --git a/src/backend/app.py b/src/backend/app.py index 0df8a82e3..42148ee58 100644 --- a/src/backend/app.py +++ b/src/backend/app.py @@ -98,12 +98,18 @@ def mainpage_get(mainpage_obj: MainPage, args: MultiDict): apts = [] price_sort = 0 rating_sort = 0 + apt_id = -1 if param.price_sort is not None: price_sort = param.price_sort if param.rating_sort is not None: rating_sort = param.rating_sort - apts = mainpage_obj.populate_apartments(param.num_apts, price_sort, rating_sort) + + if param.apt_id is not None: + apt_id = param.apt_id + apts = mainpage_obj.populate_apartments( + param.num_apts, price_sort, rating_sort, apt_id + ) apts_dict = [dataclasses.asdict(apt) for apt in apts] query_result = json.dumps(apts_dict) diff --git a/src/backend/config.py b/src/backend/config.py new file mode 100644 index 000000000..4a1a5c050 --- /dev/null +++ b/src/backend/config.py @@ -0,0 +1,3 @@ +"""Special configurations""" + +DB_NAME = "database_prod.db" diff --git a/src/backend/database/database.db b/src/backend/database/database_prod.db similarity index 95% rename from src/backend/database/database.db rename to src/backend/database/database_prod.db index d1963d1a2..5b6bde7c8 100644 Binary files a/src/backend/database/database.db and b/src/backend/database/database_prod.db differ diff --git a/src/backend/database/database_test.db b/src/backend/database/database_test.db new file mode 100644 index 000000000..1b1b8e6d5 Binary files /dev/null and b/src/backend/database/database_test.db differ diff --git a/src/backend/database/init_db.py b/src/backend/database/init_db.py index f90d6306c..ba5a0c8a0 100644 --- a/src/backend/database/init_db.py +++ b/src/backend/database/init_db.py @@ -1,7 +1,9 @@ """Initialize database""" import sqlite3 +import sys -connection = sqlite3.connect("database.db") +ARG = sys.argv[1] +connection = sqlite3.connect(ARG) with open("schema.sql", "r", encoding="utf-8") as f: connection.executescript(f.read()) cur = connection.cursor() diff --git a/src/backend/decorators.py b/src/backend/decorators.py index 71da9660b..971e76bf8 100644 --- a/src/backend/decorators.py +++ b/src/backend/decorators.py @@ -1,6 +1,7 @@ """Stores decorators for common functionalities""" import sqlite3 import functools +import config def use_database(func): @@ -8,8 +9,8 @@ def use_database(func): @functools.wraps(func) def wrapped(*args): - """Wrapper about the actual function""" - connection = sqlite3.connect("database/database.db") + """Wrapper around the actual function""" + connection = sqlite3.connect(f"database/{config.DB_NAME}") cursor = connection.cursor() wrapped.cursor = cursor wrapped.connection = connection @@ -25,3 +26,17 @@ def wrapped(*args): return caller return wrapped + + +def use_test(func): + """Decorator to switch database for testing""" + + @functools.wraps(func) + def wrapped(*args, **kwargs): + """Wrapper around the actual function""" + config.DB_NAME = "database_test.db" + func_instance = func(*args, **kwargs) + config.DB_NAME = "database_prod.db" + return func_instance + + return wrapped diff --git a/src/backend/pages/mainpage.py b/src/backend/pages/mainpage.py index eb20b1fce..f14cd7218 100644 --- a/src/backend/pages/mainpage.py +++ b/src/backend/pages/mainpage.py @@ -1,16 +1,32 @@ """Contains Main page class""" -import sqlite3 from datetime import date from typing import List from typing import Tuple +from dataclasses import dataclass from dataholders.apt import Apt from dataholders.review import Review from decorators import use_database +@dataclass(frozen=True) +class LatestPopulatedApt: + """Stores details related to the latest apt in a scroll""" + + apt_name: str + apt_id: int + + class MainPage: """Mainpage class, interacts with the mainpage frontend""" + populate_query = "WITH temp(id, name, address, total_vote, p_min, p_max, p_avg) AS \ + (SELECT Apartments.apt_id, Apartments.apt_name, Apartments.apt_address, \ + COALESCE(SUM(Reviews.vote), 0), \ + Apartments.price_min, Apartments.price_max, \ + (Apartments.price_min + Apartments.price_max)/2 \ + FROM Apartments LEFT JOIN Reviews ON Apartments.apt_id = Reviews.apt_id \ + GROUP BY Apartments.apt_id) " + def __init__(self) -> None: """Constructor""" @@ -37,96 +53,153 @@ def search_apartments(self, query: str) -> List[Apt]: @use_database def populate_apartments( - self, num_apts: int, price_sort: int, rating_sort: int + self, num_apts: int, price_sort: int, rating_sort: int, apt_id: int ) -> List[Apt]: """Returns num_apts apartments with sorting criterias""" apts = [] apt_query = [] + apt_name = "" + if apt_id >= 0: + apt_name = self.populate_apartments.cursor.execute( + "SELECT apt_name FROM Apartments WHERE apt_id = ?", (apt_id,) + ).fetchone()[0] + latest_row = LatestPopulatedApt(apt_name, apt_id) if price_sort == 0: - apt_query = self.rating_sort_helper( - num_apts, rating_sort, self.populate_apartments.cursor - ) + apt_query = self.rating_sort_helper(num_apts, rating_sort, latest_row) elif rating_sort == 0 and price_sort != 0: - apt_query = self.price_sort_helper( - num_apts, price_sort, self.populate_apartments.cursor - ) + apt_query = self.price_sort_helper(num_apts, price_sort, latest_row) else: apt_query = self.both_sort_helper( - num_apts, price_sort, rating_sort, self.populate_apartments.cursor + num_apts, price_sort, rating_sort, latest_row ) for entry in apt_query: apts.append(Apt(entry[0], entry[1], entry[2], entry[3], entry[4], entry[5])) return apts + @use_database def rating_sort_helper( - self, num_apts: int, rating_sort: int, cursor: sqlite3.Cursor + self, num_apts: int, rating_sort: int, latest_row: LatestPopulatedApt ) -> List[Tuple]: """Helper for rating-only sort""" - rating_order = "" - if rating_sort in (0, 1): - rating_order = "DESC" - apt_query = cursor.execute( - f"SELECT Apartments.apt_id, Apartments.apt_name, Apartments.apt_address, \ - COALESCE(SUM(Reviews.vote), 0) AS 'total_vote', \ - Apartments.price_min, Apartments.price_max \ - FROM Apartments LEFT JOIN Reviews ON Apartments.apt_id = Reviews.apt_id \ - GROUP BY Apartments.apt_id \ - ORDER BY total_vote {rating_order}, \ - Apartments.apt_name \ - LIMIT ?", - (num_apts,), - ).fetchall() - + rating_order = "DESC" if rating_sort in (0, 1) else "" + rating_comp = "<" if rating_sort in (0, 1) else ">" + apt_query = [] + if latest_row.apt_id >= 0: + apt_query = self.rating_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + WHERE \ + (total_vote = (SELECT total_vote FROM temp WHERE id = ?) \ + AND (name, id) > (?, ?)) \ + OR total_vote {rating_comp} (SELECT total_vote FROM temp WHERE id = ?) \ + ORDER BY total_vote {rating_order}, name, id \ + LIMIT ?", + ( + latest_row.apt_id, + latest_row.apt_name, + latest_row.apt_id, + latest_row.apt_id, + num_apts, + ), + ).fetchall() + else: + apt_query = self.rating_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + ORDER BY total_vote {rating_order}, name, id \ + LIMIT ?", + (num_apts,), + ).fetchall() return apt_query + @use_database def price_sort_helper( - self, num_apts: int, price_sort: int, cursor: sqlite3.Cursor + self, num_apts: int, price_sort: int, latest_row: LatestPopulatedApt ) -> List[Tuple]: """Helper for price-only sorts""" - price_order = "" - if price_sort == 1: - price_order = "DESC" - apt_query = cursor.execute( - f"SELECT Apartments.apt_id, Apartments.apt_name, Apartments.apt_address, \ - COALESCE(SUM(Reviews.vote), 0) AS 'total_vote', \ - Apartments.price_min, Apartments.price_max \ - FROM Apartments LEFT JOIN Reviews ON Apartments.apt_id = Reviews.apt_id \ - GROUP BY Apartments.apt_id \ - ORDER BY (Apartments.price_min + Apartments.price_max)/2 {price_order}, \ - Apartments.apt_name \ - LIMIT ?", - (num_apts,), - ).fetchall() - + price_order = "DESC" if price_sort == 1 else "" + price_comp = "<" if price_sort == 1 else ">" + apt_query = [] + if latest_row.apt_id >= 0: + apt_query = self.price_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + WHERE \ + (p_avg = (SELECT p_avg FROM temp WHERE id = ?) \ + AND (name, id) > (?, ?)) \ + OR p_avg {price_comp} (SELECT p_avg FROM temp WHERE id = ?) \ + ORDER BY p_avg {price_order}, name, id \ + LIMIT ?", + ( + latest_row.apt_id, + latest_row.apt_name, + latest_row.apt_id, + latest_row.apt_id, + num_apts, + ), + ).fetchall() + else: + apt_query = self.price_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + ORDER BY p_avg {price_order}, name, id \ + LIMIT ?", + (num_apts,), + ).fetchall() return apt_query + @use_database def both_sort_helper( - self, num_apts: int, price_sort: int, rating_sort: int, cursor: sqlite3.Cursor + self, + num_apts: int, + price_sort: int, + rating_sort: int, + latest_row: LatestPopulatedApt, ) -> List[Tuple]: """Helper to sort both params""" - price_order = "" - if price_sort == 1: - price_order = "DESC" - rating_order = "" - if rating_sort == 1: - rating_order = "DESC" + price_comp = "<" if price_sort == 1 else ">" + price_order = "DESC" if price_sort == 1 else "" - apt_query = cursor.execute( - f"SELECT Apartments.apt_id, Apartments.apt_name, Apartments.apt_address, \ - COALESCE(SUM(Reviews.vote), 0) AS 'total_vote', \ - Apartments.price_min, Apartments.price_max \ - FROM Apartments LEFT JOIN Reviews ON Apartments.apt_id = Reviews.apt_id \ - GROUP BY Apartments.apt_id \ - ORDER BY (Apartments.price_min + Apartments.price_max)/2 {price_order}, \ - total_vote {rating_order}, \ - Apartments.apt_name \ - LIMIT ?", - (num_apts,), - ).fetchall() + rating_comp = "<" if rating_sort == 1 else ">" + rating_order = "DESC" if rating_sort == 1 else "" + + if latest_row.apt_id >= 0: + apt_query = self.both_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + WHERE p_avg {price_comp} (SELECT p_avg FROM temp WHERE id = ?) \ + OR ( \ + p_avg = (SELECT p_avg FROM temp WHERE id = ?) \ + AND ( \ + total_vote {rating_comp} (SELECT total_vote FROM temp WHERE id = ?) \ + OR (total_vote = (SELECT total_vote FROM temp WHERE id = ?) \ + AND (name, id) > (?, ?)) \ + ) \ + ) \ + ORDER BY p_avg {price_order}, total_vote {rating_order}, name, id \ + LIMIT ?", + ( + latest_row.apt_id, + latest_row.apt_id, + latest_row.apt_id, + latest_row.apt_id, + latest_row.apt_name, + latest_row.apt_id, + num_apts, + ), + ).fetchall() + else: + apt_query = self.both_sort_helper.cursor.execute( + self.populate_query + + f"SELECT * FROM temp \ + ORDER BY p_avg {price_order}, total_vote {rating_order}, name, id \ + LIMIT ?", + (num_apts,), + ).fetchall() return apt_query diff --git a/src/backend/tests/mainpage_staging.py b/src/backend/tests/mainpage_staging.py index a04a88b1e..c7db4c10d 100644 --- a/src/backend/tests/mainpage_staging.py +++ b/src/backend/tests/mainpage_staging.py @@ -1,5 +1,6 @@ """Stage the database for""" import sqlite3 +from decorators import use_database class MainPageStaging: @@ -10,8 +11,8 @@ def insert_apartments(self, cursor: sqlite3.Cursor, connection: sqlite3.Connecti args = [ ("Sherman", "909 S 5th St", 5500, 6500, ""), ("FAR", "901 W College Ct", 6000, 7000, ""), - ("Lincoln", "1005 S Lincoln Ave", 5000, 6000, ""), ("PAR", "901 W College Ct", 5000, 6000, ""), + ("Lincoln", "1005 S Lincoln Ave", 5000, 6000, ""), ("ISR", "918 W Illinois", 6000, 7000, ""), ] cursor.executemany( @@ -130,22 +131,22 @@ def clean_up_pics(self, cursor: sqlite3.Cursor, connection: sqlite3.Connection): cursor.execute("DELETE FROM AptPics WHERE apt_id = ?", (sherman_id,)) connection.commit() + @use_database def initialize_all(self): """Initialize test data""" - connection = sqlite3.connect("database/database.db") - cursor = connection.cursor() - self.insert_apartments(cursor, connection) - self.insert_users(cursor, connection) - self.insert_reviews(cursor, connection) - self.insert_pics(cursor, connection) - connection.close() + conn = self.initialize_all.connection + cur = self.initialize_all.cursor + self.insert_apartments(cur, conn) + self.insert_users(cur, conn) + self.insert_reviews(cur, conn) + self.insert_pics(cur, conn) + @use_database def clean_all(self): """Clean up test data""" - connection = sqlite3.connect("database/database.db") - cursor = connection.cursor() - self.clean_up_reviews(cursor, connection) - self.clean_up_pics(cursor, connection) - self.clean_up_apartments(cursor, connection) - self.clean_up_users(cursor, connection) - connection.close() + conn = self.clean_all.connection + cur = self.clean_all.cursor + self.clean_up_reviews(cur, conn) + self.clean_up_pics(cur, conn) + self.clean_up_apartments(cur, conn) + self.clean_up_users(cur, conn) diff --git a/src/backend/tests/test_app.py b/src/backend/tests/test_app.py index d78204c0b..d8d63f54b 100644 --- a/src/backend/tests/test_app.py +++ b/src/backend/tests/test_app.py @@ -3,6 +3,7 @@ import pytest from app import app from tests.mainpage_staging import MainPageStaging +from decorators import use_test @pytest.fixture(name="config_app") @@ -18,6 +19,7 @@ def fixture_client(config_app): return config_app.test_client() +@use_test def test_register_valid(client): """Test register handles valid request""" reg_info = { @@ -27,7 +29,7 @@ def test_register_valid(client): "phone": "0003335555", } res = client.post("/register", json=reg_info) - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() cursor.execute("DELETE FROM Users WHERE username = ?", ("big_finger",)) connection.commit() @@ -35,6 +37,7 @@ def test_register_valid(client): assert res.status_code == 201 +@use_test def test_register_invalid_input(client): """Test register handles invalid register attempt""" reg_info = { @@ -53,7 +56,7 @@ def test_register_invalid_input(client): client.post("/register", json=reg_info) res_2 = client.post("/register", json=reg_info_2) - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() cursor.execute("DELETE FROM Users WHERE username = ?", ("big_finger",)) connection.commit() @@ -69,9 +72,10 @@ def test_register_not_json(client): assert res.status_code == 400 +@use_test def test_login_valid(client): """Test login handles valid request""" - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() cursor.execute( "INSERT INTO Users (username, password, email, phone)\ @@ -88,6 +92,7 @@ def test_login_valid(client): assert res.status_code == 200 +@use_test def test_login_invalid_user(client): """Test handles non-existant user""" log_info = {"user": "big_finger", "password": "123456789"} @@ -101,11 +106,12 @@ def test_login_not_json(client): assert res.status_code == 400 +@use_test def test_mainpage_get_valid_review(client): """Test mainpage handles valid reviwew request""" mainpage = MainPageStaging() mainpage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() far_id = cursor.execute( @@ -126,12 +132,13 @@ def test_mainpage_get_valid_review(client): assert res.text == sample_json +@use_test def test_mainpage_get_valid_search(client): """Test mainpage handles valid search request""" mainpage = MainPageStaging() mainpage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() isr_id = cursor.execute( @@ -155,12 +162,13 @@ def test_mainpage_get_valid_search(client): assert res.text == sample_json +@use_test def test_mainpage_get_valid_pictures(client): """Test mainpage handles valid picture query""" mainpage = MainPageStaging() mainpage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sample_id = cursor.execute( @@ -179,6 +187,7 @@ def test_mainpage_get_valid_pictures(client): assert res.text == sample_json +@use_test def test_mainpage_get_valid_populate(client): """Test mainpage handles valid populate query""" mainpage = MainPageStaging() @@ -186,14 +195,18 @@ def test_mainpage_get_valid_populate(client): query_1 = {"populate": "True", "numApts": 1} query_2 = {"populate": "True", "numApts": 1, "priceSort": 0, "ratingSort": 0} + query_3 = {"populate": "True", "numApts": 1, "aptId": -1} res_1 = client.get("/main", query_string=query_1) res_2 = client.get("/main", query_string=query_2) + res_3 = client.get("/main", query_string=query_3) mainpage.clean_all() assert res_1.status_code == 200 assert res_2.status_code == 200 + assert res_3.status_code == 200 assert res_1.text == res_2.text + assert res_1.text == res_3.text def test_mainpage_get_invalid_query(client): @@ -202,11 +215,12 @@ def test_mainpage_get_invalid_query(client): assert res.status_code == 400 +@use_test def test_mainpage_post_valid(client): """Test mainpage handles valid post request""" mainpage = MainPageStaging() mainpage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() isr_id = cursor.execute( diff --git a/src/backend/tests/test_decorators.py b/src/backend/tests/test_decorators.py new file mode 100644 index 000000000..16c9ddc3d --- /dev/null +++ b/src/backend/tests/test_decorators.py @@ -0,0 +1,30 @@ +"""Test decorators.py""" +import sqlite3 +import pytest +import decorators + + +@decorators.use_database +def insert_review(): + """Uses use_database for inserting a review""" + insert_review.cursor.execute( + "INSERT INTO Reviews (apt_id, user_id, date_of_rating, comment, vote) \ + VALUES (?, ?, ?, ?, ?)", + (5, 5, "2022-11-06", "mock comment", 1), + ) + + +def test_use_database_raise_exception(): + """use_database correctly raises an exception""" + connection = sqlite3.connect("database/database_test.db") + cursor = connection.cursor() + cursor.execute( + "INSERT INTO Reviews (apt_id, user_id, date_of_rating, comment, vote) \ + VALUES (?, ?, ?, ?, ?)", + (5, 5, "2022-11-06", "mock comment", 1), + ) + connection.commit() + with pytest.raises(Exception): + insert_review() + cursor.execute("DELETE FROM Reviews WHERE apt_id = ?", (5,)) + connection.commit() diff --git a/src/backend/tests/test_login.py b/src/backend/tests/test_login.py index 268b411b0..6669a5a79 100644 --- a/src/backend/tests/test_login.py +++ b/src/backend/tests/test_login.py @@ -1,6 +1,7 @@ """Test login.py""" import sqlite3 from pages.login import Login +from decorators import use_test class TestLogin: @@ -19,13 +20,14 @@ def register_setup(self) -> bool: def delete_register(self) -> str: """Remove fake data from database""" - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() cursor.execute("DELETE FROM Users WHERE username = ?", (self.username,)) connection.commit() connection.close() return self.username + @use_test def test_register(self): """Tests register function""" self.register_helper() @@ -41,6 +43,7 @@ def register_helper(self): delete = self.delete_register() assert delete == self.username # correct data deleted + @use_test def test_register_invalid_email(self): """Invalid email string""" register = self.first_login.register( @@ -52,6 +55,7 @@ def test_register_invalid_email(self): assert register.status is False assert register_2.status is False + @use_test def test_register_missing_field(self): """Missing certain fields""" register = self.first_login.register( @@ -59,6 +63,7 @@ def test_register_missing_field(self): ) assert register.status is False + @use_test def test_register_short_password(self): """Password is too short""" register = self.first_login.register( @@ -66,6 +71,7 @@ def test_register_short_password(self): ) assert register.status is False + @use_test def test_register_invalid_phone_length(self): """Invalid phone number length""" register = self.first_login.register( @@ -73,6 +79,7 @@ def test_register_invalid_phone_length(self): ) assert register.status is False + @use_test def test_login(self): """Tests login function""" self.register_setup() @@ -80,6 +87,7 @@ def test_login(self): self.delete_register() assert user is True + @use_test def test_login_invalid(self): """Test invalid login attempt""" user = self.first_login.login(self.username, "123456789") diff --git a/src/backend/tests/test_mainpage.py b/src/backend/tests/test_mainpage.py index c77978699..e945192b3 100644 --- a/src/backend/tests/test_mainpage.py +++ b/src/backend/tests/test_mainpage.py @@ -5,6 +5,7 @@ from dataholders.apt import Apt from dataholders.review import Review from tests.mainpage_staging import MainPageStaging +from decorators import use_test class TestMainPage: @@ -13,11 +14,12 @@ class TestMainPage: main_page = MainPage() main_page_stage = MainPageStaging() + @use_test def test_search_apartments(self): """Test search_apartment() returns correct list""" self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -33,11 +35,12 @@ def test_search_apartments(self): self.main_page_stage.clean_all() assert sample_search_apts == res + @use_test def test_populate_apartments_default(self): """Test apartments_sorted() returns correct list""" self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -48,6 +51,9 @@ def test_populate_apartments_default(self): far_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'FAR')" ).fetchone()[0] + lincoln_id = cursor.execute( + "SELECT apt_id FROM Apartments WHERE (apt_name = 'Lincoln')" + ).fetchone()[0] connection.close() sample_apts_sorted = [] sample_apts_sorted.append(Apt(far_id, "FAR", "901 W College Ct", 1, 6000, 7000)) @@ -56,16 +62,25 @@ def test_populate_apartments_default(self): ) sample_apts_sorted.append(Apt(isr_id, "ISR", "918 W Illinois", 0, 6000, 7000)) - res = self.main_page.populate_apartments(3, 0, 0) + res = self.main_page.populate_apartments(3, 0, 0, -1) + res_2 = self.main_page.populate_apartments(3, 0, 0, far_id) self.main_page_stage.clean_all() assert sample_apts_sorted == res + sample_apts_sorted.pop(0) + sample_apts_sorted.append( + Apt(lincoln_id, "Lincoln", "1005 S Lincoln Ave", 0, 5000, 6000) + ) + + assert sample_apts_sorted == res_2 + + @use_test def test_populate_apartments_rating_reversed(self): """Test returns list rating from low to high""" self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() par_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'PAR')" @@ -86,16 +101,17 @@ def test_populate_apartments_rating_reversed(self): Apt(lincoln_id, "Lincoln", "1005 S Lincoln Ave", 0, 5000, 6000) ) - res = self.main_page.populate_apartments(3, 0, -1) + res = self.main_page.populate_apartments(3, 0, -1, -1) self.main_page_stage.clean_all() assert sample_apts_sorted == res + @use_test def test_populate_apartments_price_reversed(self): """Test returns price from low to high""" self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() lincoln_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Lincoln')" @@ -103,6 +119,9 @@ def test_populate_apartments_price_reversed(self): par_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'PAR')" ).fetchone()[0] + far_id = cursor.execute( + "SELECT apt_id FROM Apartments WHERE (apt_name = 'FAR')" + ).fetchone()[0] sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" ).fetchone()[0] @@ -118,16 +137,22 @@ def test_populate_apartments_price_reversed(self): Apt(sherman_id, "Sherman", "909 S 5th St", 1, 5500, 6500) ) - res = self.main_page.populate_apartments(3, -1, 0) + res = self.main_page.populate_apartments(3, -1, 0, -1) + res_2 = self.main_page.populate_apartments(3, -1, 0, lincoln_id) self.main_page_stage.clean_all() assert sample_apts_sorted == res + sample_apts_sorted.pop(0) + sample_apts_sorted.append(Apt(far_id, "FAR", "901 W College Ct", 1, 6000, 7000)) + assert sample_apts_sorted == res_2 + + @use_test def test_populate_apartments_price(self): """Test returns price from high to low""" self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -146,11 +171,12 @@ def test_populate_apartments_price(self): Apt(sherman_id, "Sherman", "909 S 5th St", 1, 5500, 6500) ) - res = self.main_page.populate_apartments(3, 1, 0) + res = self.main_page.populate_apartments(3, 1, 0, -1) self.main_page_stage.clean_all() assert sample_apts_sorted == res + @use_test def test_populate_apartments_price_rating_reversed(self): """ Test price from high to low @@ -158,7 +184,7 @@ def test_populate_apartments_price_rating_reversed(self): """ self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -166,6 +192,9 @@ def test_populate_apartments_price_rating_reversed(self): isr_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'ISR')" ).fetchone()[0] + par_id = cursor.execute( + "SELECT apt_id FROM Apartments WHERE (apt_name = 'PAR')" + ).fetchone()[0] far_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'FAR')" ).fetchone()[0] @@ -177,11 +206,19 @@ def test_populate_apartments_price_rating_reversed(self): Apt(sherman_id, "Sherman", "909 S 5th St", 1, 5500, 6500) ) - res = self.main_page.populate_apartments(3, 1, -1) + res = self.main_page.populate_apartments(3, 1, -1, -1) + res_2 = self.main_page.populate_apartments(3, 1, -1, isr_id) self.main_page_stage.clean_all() assert sample_apts_sorted == res + sample_apts_sorted.pop(0) + sample_apts_sorted.append( + Apt(par_id, "PAR", "901 W College Ct", -1, 5000, 6000) + ) + assert sample_apts_sorted == res_2 + + @use_test def test_populate_apartments_price_reversed_rating(self): """ Test price from low to high @@ -189,7 +226,7 @@ def test_populate_apartments_price_reversed_rating(self): """ self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() lincoln_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Lincoln')" @@ -197,6 +234,9 @@ def test_populate_apartments_price_reversed_rating(self): par_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'PAR')" ).fetchone()[0] + far_id = cursor.execute( + "SELECT apt_id FROM Apartments WHERE (apt_name = 'FAR')" + ).fetchone()[0] sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" ).fetchone()[0] @@ -212,18 +252,24 @@ def test_populate_apartments_price_reversed_rating(self): Apt(sherman_id, "Sherman", "909 S 5th St", 1, 5500, 6500) ) - res = self.main_page.populate_apartments(3, -1, 1) + res = self.main_page.populate_apartments(3, -1, 1, -1) + res_2 = self.main_page.populate_apartments(3, -1, 1, lincoln_id) self.main_page_stage.clean_all() assert sample_apts_sorted == res + sample_apts_sorted.pop(0) + sample_apts_sorted.append(Apt(far_id, "FAR", "901 W College Ct", 1, 6000, 7000)) + assert sample_apts_sorted == res_2 + + @use_test def test_get_apartments_pictures(self): """Test get_apartments_picture()""" sample_apts_picture = ["Link1", "Link2", "Link3"] self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -234,11 +280,12 @@ def test_get_apartments_pictures(self): self.main_page_stage.clean_all() assert sample_apts_picture == res + @use_test def test_write_apartment_review(self): """Test write_apartment_review()""" self.main_page_stage.initialize_all() sample_comment = "Bruh this really sucks" - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -260,6 +307,7 @@ def test_write_apartment_review(self): self.main_page_stage.clean_all() assert write_result == sample_apts_review + @use_test def test_get_apartments_reviews(self): """Test get_apartments_reviews()""" sample_apts_review = [] @@ -273,7 +321,7 @@ def test_get_apartments_reviews(self): self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -284,6 +332,7 @@ def test_get_apartments_reviews(self): self.main_page_stage.clean_all() assert sample_apts_review == res + @use_test def test_search_apartments_invalid(self): """Test invalid query""" sample_search_apts = [] @@ -295,13 +344,14 @@ def test_search_apartments_invalid(self): self.main_page_stage.clean_all() assert sample_search_apts == res + @use_test def test_get_apartments_pictures_invalid(self): """Test get pics of invalid apartment""" sample_apts_picture = [] self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')" @@ -313,11 +363,12 @@ def test_get_apartments_pictures_invalid(self): self.main_page_stage.clean_all() assert sample_apts_picture == res + @use_test def test_get_apartments_reviews_empty(self): """Test get reviews of invalid apartments""" sample_apts_review = [] self.main_page_stage.initialize_all() - connection = sqlite3.connect("database/database.db") + connection = sqlite3.connect("database/database_test.db") cursor = connection.cursor() sherman_id = cursor.execute( "SELECT apt_id FROM Apartments WHERE (apt_name = 'Sherman')"