Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Address-Book/test_AddressbookCreateTask.py.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pytest
from unittest.mock import patch, MagicMock
from tkinter import Toplevel, Label, Button
from Address-Book.addressbook import create_task, onClickAdded

@pytest.fixture
def mock_tkinter():
with patch('Address-Book.addressbook.Toplevel') as mock_toplevel, \
patch('Address-Book.addressbook.Label') as mock_label, \
patch('Address-Book.addressbook.Button') as mock_button:
yield mock_toplevel, mock_label, mock_button

@pytest.fixture
def mock_database():
with patch('Address-Book.addressbook.conn') as mock_conn:
mock_cursor = MagicMock()
mock_conn.cursor.return_value = mock_cursor
yield mock_conn, mock_cursor

@pytest.mark.valid
def test_create_task_with_valid_inputs(mock_tkinter, mock_database):
mock_toplevel, mock_label, mock_button = mock_tkinter
mock_conn, mock_cursor = mock_database

with patch('Address-Book.addressbook.Name.get', return_value='John Doe'), \
patch('Address-Book.addressbook.Number.get', return_value='1234567890'), \
patch('Address-Book.addressbook.list_of_names', []):

result = create_task()

onClickAdded.assert_called_once()
mock_cursor.execute.assert_called_once_with(''' INSERT INTO tasks(name,status_id) VALUES(?,?) ''', ('John Doe', '1234567890'))
mock_conn.commit.assert_called_once()
assert result == mock_cursor.lastrowid

@pytest.mark.invalid
def test_create_task_with_empty_name(mock_tkinter, mock_database):
mock_toplevel, mock_label, mock_button = mock_tkinter
mock_conn, mock_cursor = mock_database

with patch('Address-Book.addressbook.Name.get', return_value=''), \
patch('Address-Book.addressbook.Number.get', return_value='1234567890'):

create_task()

mock_toplevel.assert_called_once()
mock_label.assert_called_once_with(mock_toplevel(), text="NAME IS EMPTY\n")
mock_button.assert_called_once_with(mock_toplevel(), text=' Back ', command=mock_toplevel().destroy)
mock_cursor.execute.assert_not_called()

@pytest.mark.invalid
def test_create_task_with_invalid_phone_number_length(mock_tkinter, mock_database):
mock_toplevel, mock_label, mock_button = mock_tkinter
mock_conn, mock_cursor = mock_database

with patch('Address-Book.addressbook.Name.get', return_value='John Doe'), \
patch('Address-Book.addressbook.Number.get', return_value='12345'), \
patch('Address-Book.addressbook.list_of_names', []):

create_task()

mock_toplevel.assert_called_once()
mock_label.assert_called_once_with(mock_toplevel(), text="Phone no should be 10 digits\n")
mock_button.assert_called_once_with(mock_toplevel(), text=' Back ', command=mock_toplevel().destroy)
mock_cursor.execute.assert_not_called()

@pytest.mark.invalid
def test_create_task_with_duplicate_name(mock_tkinter, mock_database):
mock_toplevel, mock_label, mock_button = mock_tkinter
mock_conn, mock_cursor = mock_database

with patch('Address-Book.addressbook.Name.get', return_value='John Doe'), \
patch('Address-Book.addressbook.Number.get', return_value='1234567890'), \
patch('Address-Book.addressbook.list_of_names', ['John Doe']):

create_task()

mock_toplevel.assert_called_once()
mock_label.assert_called_once_with(mock_toplevel(), text="John Doe Already Exist\n")
mock_button.assert_called_once_with(mock_toplevel(), text=' Back ', command=mock_toplevel().destroy)
mock_cursor.execute.assert_not_called()

@pytest.mark.invalid
def test_create_task_with_empty_phone_number(mock_tkinter, mock_database):
mock_toplevel, mock_label, mock_button = mock_tkinter
mock_conn, mock_cursor = mock_database

with patch('Address-Book.addressbook.Name.get', return_value='John Doe'), \
patch('Address-Book.addressbook.Number.get', return_value=''), \
patch('Address-Book.addressbook.list_of_names', []):

create_task()

mock_toplevel.assert_called_once()
mock_label.assert_called_once_with(mock_toplevel(), text="Phone no should be 10 digits\n")
mock_button.assert_called_once_with(mock_toplevel(), text=' Back ', command=mock_toplevel().destroy)
mock_cursor.execute.assert_not_called()
73 changes: 73 additions & 0 deletions Address-Book/test_AddressbookOnClickAdded.py.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest
from unittest.mock import patch
from tkinter import StringVar
import tkinter.messagebox
from Address-Book.addressbook import onClickAdded

class Test_AddressbookOnClickAdded:

@pytest.fixture(autouse=True)
def setup(self):
global Name
Name = StringVar()

@pytest.mark.valid
def test_successful_addition_message_display(self):
# Arrange
Name.set("John Doe")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
# Act
onClickAdded()

# Assert
mock_showinfo.assert_called_once_with(" ", "John Doe got added")

@pytest.mark.invalid
def test_empty_name_input_handling(self):
# Arrange
Name.set("")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
# Act
onClickAdded()

# Assert
mock_showinfo.assert_called_once_with(" ", " got added")

@pytest.mark.regression
def test_message_box_title(self):
# Arrange
Name.set("Sample Name")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
# Act
onClickAdded()

# Assert
mock_showinfo.assert_called_once_with(" ", "Sample Name got added")

@pytest.mark.valid
def test_special_characters_in_name(self):
# Arrange
Name.set("John@Doe!")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
# Act
onClickAdded()

# Assert
mock_showinfo.assert_called_once_with(" ", "John@Doe! got added")

@pytest.mark.performance
def test_long_name_input(self):
# Arrange
long_name = "A" * 256 # TODO: Adjust the length if needed
Name.set(long_name)

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
# Act
onClickAdded()

# Assert
mock_showinfo.assert_called_once_with(" ", f"{long_name} got added")
58 changes: 58 additions & 0 deletions Address-Book/test_AddressbookOnClickDeleted.py.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
from unittest.mock import patch
from tkinter import Tk, StringVar
import tkinter.messagebox
from Address-Book.addressbook import onClickDeleted

@pytest.fixture
def setup_tkinter_env():
root = Tk()
root.geometry('600x370')
name_var = StringVar()
return root, name_var

@pytest.mark.valid
def test_message_box_display_with_valid_name(setup_tkinter_env):
root, name_var = setup_tkinter_env
name_var.set("John Doe")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
onClickDeleted()
mock_showinfo.assert_called_once_with(" ", "John Doe got deleted")

@pytest.mark.invalid
def test_message_box_display_with_empty_name(setup_tkinter_env):
root, name_var = setup_tkinter_env
name_var.set("")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
onClickDeleted()
mock_showinfo.assert_called_once_with(" ", " got deleted")

@pytest.mark.valid
def test_message_box_display_with_special_characters(setup_tkinter_env):
root, name_var = setup_tkinter_env
name_var.set("@John!")

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
onClickDeleted()
mock_showinfo.assert_called_once_with(" ", "@John! got deleted")

@pytest.mark.valid
def test_message_box_display_with_long_name(setup_tkinter_env):
root, name_var = setup_tkinter_env
long_name = "A" * 101 # TODO: Adjust the length if needed
name_var.set(long_name)

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
onClickDeleted()
mock_showinfo.assert_called_once_with(" ", f"{long_name} got deleted")

@pytest.mark.invalid
def test_message_box_display_with_non_string_input(setup_tkinter_env):
root, name_var = setup_tkinter_env
name_var.set(12345) # Non-string input

with patch('tkinter.messagebox.showinfo') as mock_showinfo:
onClickDeleted()
mock_showinfo.assert_called_once_with(" ", "12345 got deleted")
1 change: 1 addition & 0 deletions requirements-roost.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest