diff --git a/db/comments.db b/db/comments.db index 3b4680d0878fb201e5136b1f329efd2f2301f6d8..986328e8d39c5d6c219826486584b02fe6c8e2a1 100644 Binary files a/db/comments.db and b/db/comments.db differ diff --git a/db/posts.db b/db/posts.db index 74a4367cc8052819d04064c3ffda17caf0d31838..6ac96c4c168ec5394dd2238eb687e0b0b553c055 100644 Binary files a/db/posts.db and b/db/posts.db differ diff --git a/db/users.db b/db/users.db index c37b2335cbf6e8cb96d19b1f0d567dd4003e3a0b..e6d76785f577848bdd591f541e525eeffd4a64ff 100644 Binary files a/db/users.db and b/db/users.db differ diff --git a/templates/tailwindUI/post.html.jinja b/templates/tailwindUI/post.html.jinja index 5cd33eb6feb8cbfaa618bc26bf10097ff97e5540..e49e9073559ac173d39bb2ab882e2108898ddeec 100644 --- a/templates/tailwindUI/post.html.jinja +++ b/templates/tailwindUI/post.html.jinja @@ -79,7 +79,7 @@ <!-- Comments Section --> <div> {% for comment in comments %} - <div class="flex flex-col mx-auto mt-8 mb-4 p-2 max-w-lg"> + <div data-testid="comment-{{ comment[0] }}" class="flex flex-col mx-auto mt-8 mb-4 p-2 max-w-lg"> <div class="flex w-full justify-between"> <div class="mx-2"> <a href="/user/{{ comment[3] }}" class="flex items-center hover:text-rose-500 duration-150 select-none"> diff --git a/tests/conftest.py b/tests/conftest.py index e711c6621ee2b9959c8ae52702cd5523c7ca34f6..6e80a3d3345e11ba90045b2021114246ffe15b25 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,7 @@ from playwright.sync_api import sync_playwright, Browser @pytest.fixture(scope="session") def browser(): with sync_playwright() as p: - browser = p.chromium.launch(headless=USE_HEADLESS) + browser = p.chromium.launch(headless=False) yield browser print('\nClosing Browser') browser.close() diff --git a/tests/functions/delete_comment.py b/tests/functions/delete_comment.py index 0e2b59a82222d077bb59694b39c49a3c1916f5fb..2eb71a2b75d83eeb0a78faea4ff3304600503860 100644 --- a/tests/functions/delete_comment.py +++ b/tests/functions/delete_comment.py @@ -1,9 +1,9 @@ from playwright.sync_api import Page from tests.utils import URL -def delete_comment(page: Page, post_id: int) -> Page: +def delete_comment(page: Page, post_id: int, comment_id: int) -> Page: page.goto(URL + '/post/' + str(post_id)) - while page.locator("[name=commentDeleteButton]").count() > 0: - page.locator("[name=commentDeleteButton]").first.click() - + comment_section = page.get_by_test_id(f"comment-{str(comment_id)}") + comment_section.locator("[name=commentDeleteButton]").click() + return page \ No newline at end of file diff --git a/tests/test_comment.py b/tests/test_comment.py new file mode 100644 index 0000000000000000000000000000000000000000..9c2f2dafc048d1a3fd9b4ec6dc6990e0b5c99dca --- /dev/null +++ b/tests/test_comment.py @@ -0,0 +1,34 @@ +# Import the playwright module and the constants file +from playwright.sync_api import expect, Page +import pytest +from tests.utils import URL, UserTest, delete_comment_utils, is_comment_exist +from tests.functions import login, create_comment, new_comment, delete_comment + +@pytest.fixture(autouse=True) +def user (): + user = UserTest("User_A") + yield user + +# Define a function that takes a playwright object as an argument +def test_create_comment(page: Page, user: UserTest) -> None: + page.goto(URL) + + page = login(page, user) + + page, comment_id = create_comment(page, 1) + + expect(page.get_by_test_id(f"comment-{str(comment_id)}")).to_be_visible() + + delete_comment_utils(comment_id) + +def test_delete_comment(page: Page, user: UserTest) -> None: + post_id = 1 + + page.goto(URL) + + page = login(page, user) + page, comment_id = create_comment(page, post_id) + + page = delete_comment(page, post_id, comment_id) + + assert is_comment_exist(comment_id) == False \ No newline at end of file diff --git a/tests/test_create_comment.py b/tests/test_create_comment.py deleted file mode 100644 index 54d6b51223b6467e3ccaec915012a796dc2750f1..0000000000000000000000000000000000000000 --- a/tests/test_create_comment.py +++ /dev/null @@ -1,46 +0,0 @@ -# Import the playwright module and the constants file -from playwright.sync_api import Playwright, expect, Page -import pytest, sqlite3 -from tests.utils import USE_HEADLESS, URL, DB_COMMENTS_ROOT, UserTest -from tests.functions import login, create_comment, new_comment - -@pytest.fixture(autouse=True) -def user (): - user = UserTest("User_A") - yield user - -@pytest.fixture(autouse=True) -def delete_comment(): - yield - print('\nDeleting created comment') - connection = sqlite3.connect(DB_COMMENTS_ROOT) - cursor = connection.cursor() - comment_id = get_newest_comment() - cursor.execute('DELETE FROM comments where id = ?', [(comment_id)]) - connection.commit() - -# Define a function that takes a playwright object as an argument -def test_create_comment(page: Page, user: UserTest) -> None: - page.goto(URL) - - page = login(page, user) - - page, comment_id = create_comment(page, 1) - - expect(page.get_by_text(new_comment)).to_be_visible() - -def get_newest_comment(): - connection = sqlite3.connect(DB_COMMENTS_ROOT) - cursor = connection.cursor() - query = ''' - SELECT id FROM comments where id = ( - SELECT max(id) FROM comments - ) - ''' - comment_id = -1 - - for row in cursor.execute(query): - comment_id = row[0] - - connection.close() - return comment_id \ No newline at end of file diff --git a/tests/test_create_post.py b/tests/test_create_post.py deleted file mode 100644 index 808535cec1d0c25fedc60ac2880082e6f4fa259d..0000000000000000000000000000000000000000 --- a/tests/test_create_post.py +++ /dev/null @@ -1,23 +0,0 @@ -from playwright.sync_api import Playwright, expect, Page -import pytest -from tests.utils import USE_HEADLESS, URL, UserTest, delete_post -from tests.functions import login, create_post - -@pytest.fixture(autouse=True) -def user (): - user = UserTest("User_A") - yield user - -# Define a function that takes a playwright object as an argument -def test_create_post(page: Page, user: UserTest) -> None: - page.goto(URL) - - page = login(page, user) - - page, post_id = create_post(page) - - response = page.request.get(URL + '/post/' + str(post_id)) - expect(response).to_be_ok() - - delete_post(post_id) - diff --git a/tests/test_delete_comment.py b/tests/test_delete_comment.py deleted file mode 100644 index b97d901b1f7836c40793aa37e2544a9f6f801a5d..0000000000000000000000000000000000000000 --- a/tests/test_delete_comment.py +++ /dev/null @@ -1,37 +0,0 @@ -from playwright.sync_api import Playwright, expect, Page - -from tests.utils import URL, DB_COMMENTS_ROOT, USE_HEADLESS, UserTest -from tests.functions import login, create_comment, delete_comment -import pytest, sqlite3 - -post_id = 1 - -@pytest.fixture(autouse=True) -def user(): - user = UserTest("User_A") - yield user - -def test_delete_comment(page: Page, user) -> None: - page.goto(URL) - - page = login(page, user) - page, comment_id = create_comment(page, post_id) - - - page = delete_comment(page, post_id) - - assert is_comment_exist(comment_id) == False - -def is_comment_exist(id): - connection = sqlite3.connect(DB_COMMENTS_ROOT) - cursor = connection.cursor() - query = ''' - SELECT * FROM comments where id = ? - ''' - exist = False - - for row in cursor.execute(query,[(id)]): - exist = True - - connection.close() - return exist \ No newline at end of file diff --git a/tests/test_delete_post.py b/tests/test_delete_post.py deleted file mode 100644 index b0bd2399845b2271c82f1306a28cf20e1524398c..0000000000000000000000000000000000000000 --- a/tests/test_delete_post.py +++ /dev/null @@ -1,33 +0,0 @@ -from playwright.sync_api import Playwright, expect, Page -import pytest, sqlite3 -from tests.utils import USE_HEADLESS, URL, DB_POSTS_ROOT, UserTest, delete_post -from tests.functions import login, create_post, delete_post - -@pytest.fixture(autouse=True) -def user (): - user = UserTest("User_A") - yield user - - -def test_delete_post(page: Page, user) -> None: - page.goto(URL) - - page = login(page, user) - page, post_id = create_post(page) - page = delete_post(page, post_id) - - assert is_post_exist(post_id) == False - -def is_post_exist(id): - connection = sqlite3.connect(DB_POSTS_ROOT) - cursor = connection.cursor() - query = ''' - SELECT * FROM posts where id = ? - ''' - exist = False - - for row in cursor.execute(query,[(id)]): - exist = True - - connection.close() - return exist \ No newline at end of file diff --git a/tests/test_edit_post.py b/tests/test_edit_post.py deleted file mode 100644 index 822c46491e8c5dd63370fafbf8574c1c61c4735e..0000000000000000000000000000000000000000 --- a/tests/test_edit_post.py +++ /dev/null @@ -1,23 +0,0 @@ -from playwright.sync_api import Playwright, expect, Page - -import pytest -from tests.utils import USE_HEADLESS, URL, DB_POSTS_ROOT, UserTest, delete_post -from tests.functions import login, create_post, edit_post, new_title - -@pytest.fixture(autouse=True) -def user (): - user = UserTest("User_A") - yield user - -def test_edit_post(page: Page, user) -> None: - page.goto(URL) - - page = login(page, user) - page, post_id = create_post(page) - - page = edit_post(page, post_id) - - expect(page).to_have_url(URL + '/post/' + str(post_id)) - expect(page).to_have_title(new_title) - - delete_post(post_id) \ No newline at end of file diff --git a/tests/test_post.py b/tests/test_post.py new file mode 100644 index 0000000000000000000000000000000000000000..44821f3e80b4113f8409c64bf3a5768a943aa956 --- /dev/null +++ b/tests/test_post.py @@ -0,0 +1,43 @@ +from playwright.sync_api import Page, expect +import pytest +from tests.utils import URL, UserTest, delete_post_utils, is_post_exist +from tests.functions import login, create_post, delete_post, edit_post, new_title + +@pytest.fixture(autouse=True) +def user (): + user = UserTest("User_A") + yield user + +def test_create_post(page: Page, user: UserTest) -> None: + page.goto(URL) + + page = login(page, user) + + page, post_id = create_post(page) + + response = page.request.get(URL + '/post/' + str(post_id)) + expect(response).to_be_ok() + + delete_post_utils(post_id) + +def test_delete_post(page: Page, user) -> None: + page.goto(URL) + + page = login(page, user) + page, post_id = create_post(page) + page = delete_post(page, post_id) + + assert is_post_exist(post_id) == False + +def test_edit_post(page: Page, user) -> None: + page.goto(URL) + + page = login(page, user) + page, post_id = create_post(page) + + page = edit_post(page, post_id) + + expect(page).to_have_url(URL + '/post/' + str(post_id)) + expect(page).to_have_title(new_title) + + delete_post_utils(post_id) \ No newline at end of file diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index 2403e914afaaa30891e56db51edbc019712b4b6b..f616f168660f51a538a50183c64e4f8733775543 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -4,4 +4,6 @@ from .get_newest_comment import * from .get_newest_post import * from .is_comment_exist import * from .is_user_exist import * +from .is_post_exist import * +from .delete_comment import * from .user import * \ No newline at end of file diff --git a/tests/utils/delete_comment.py b/tests/utils/delete_comment.py new file mode 100644 index 0000000000000000000000000000000000000000..3f8db2e80c9235fb315f106a8033d56ce2d222c4 --- /dev/null +++ b/tests/utils/delete_comment.py @@ -0,0 +1,9 @@ +import sqlite3 +from tests.utils import DB_COMMENTS_ROOT, get_newest_comment + +def delete_comment_utils(comment_id): + print('\nDeleting created comment') + connection = sqlite3.connect(DB_COMMENTS_ROOT) + cursor = connection.cursor() + cursor.execute('DELETE FROM comments where id = ?', [(comment_id)]) + connection.commit() \ No newline at end of file diff --git a/tests/utils/delete_post.py b/tests/utils/delete_post.py index a0ebf8a9971388bcf8c69239b7e19d6de16e0a20..b915c1174e5903ef32047abe82af8530badbae67 100644 --- a/tests/utils/delete_post.py +++ b/tests/utils/delete_post.py @@ -1,7 +1,7 @@ import sqlite3 from tests.utils import DB_POSTS_ROOT -def delete_post (post_id: int): +def delete_post_utils (post_id: int): print('\nDeleting newest post') connection = sqlite3.connect(DB_POSTS_ROOT) cursor = connection.cursor() diff --git a/tests/utils/is_post_exist.py b/tests/utils/is_post_exist.py new file mode 100644 index 0000000000000000000000000000000000000000..bd358c08b366b4e3c08bbef493c23c292f4637ab --- /dev/null +++ b/tests/utils/is_post_exist.py @@ -0,0 +1,16 @@ +import sqlite3 +from tests.utils import DB_POSTS_ROOT + +def is_post_exist(id): + connection = sqlite3.connect(DB_POSTS_ROOT) + cursor = connection.cursor() + query = ''' + SELECT * FROM posts where id = ? + ''' + exist = False + + for row in cursor.execute(query,[(id)]): + exist = True + + connection.close() + return exist \ No newline at end of file