Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the test cases #112

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 39 additions & 22 deletions tests/test_models.py
tannuiscoding marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import os
import pytest
import shutil
from package.models import display
import subprocess
import sys
from datetime import datetime
from package.models import display, copy_to_clipboard

def test_display_success(monkeypatch):
def mock_copyfile(src, dstn):
pass
monkeypatch.setattr(shutil,"copyfile",mock_copyfile)
try:
current_time = datetime.now().strftime("%H%M")
display("test", current_time)
except Exception as e:
pytest.fail("Unexpected Error raised: {e}")

# will work on later
# due to some imposter uploaded write operations into models now this chaos has unfolded
# def test_display_failed(monkeypatch):
# def mock_copyfile(src,dstn):
# raise IOError("File not found")
# monkeypatch.setattr(shutil,"copyfile",mock_copyfile)

# with pytest.raises(IOError) as excinfo:
# current_time = f"{int(datetime.now().strftime('%H%M')) + 1:04d}"
# display("test", current_time)
# assert "File not found" in str(excinfo.value)
# Mock datetime class
class MockDateTime(datetime):
@classmethod
def now(cls):
return datetime.strptime("1234", "%H%M") # Mock time to 12:34

# Mock functions for clipboard operations
def mock_subprocess_run(*args, **kwargs):
return None # Assume successful run

def mock_open_file_content(*args, **kwargs):
class MockFile:
def __enter__(self):
return self
def __exit__(self, *args):
pass
def read(self):
return "Sample content"
return MockFile()


def test_display_incorrect_password(monkeypatch):
monkeypatch.setattr("package.piechart.datetime", MockDateTime) # Mock datetime

snippet_name = "test"
incorrect_password = "1111" # Different from "1234"

with pytest.raises(ValueError, match="syntax error: incorrect password"):
display(snippet_name, incorrect_password)


def test_copy_to_clipboard_unsupported_os(monkeypatch):
monkeypatch.setattr(sys, "platform", "unsupported_os")

with pytest.raises(OSError, match="Unsupported operating system"):
copy_to_clipboard("test content")
59 changes: 54 additions & 5 deletions tests/test_piechart.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
import os
import pytest
tannuiscoding marked this conversation as resolved.
Show resolved Hide resolved
import shutil
import glob
from datetime import datetime
from package.piechart import plot

def test_plot(monkeypatch):
def mock_copyfile(inputPath, outputPath):
pass
#we cannot mock this function, so mocking the whole class
class MockDateTime(datetime):
@classmethod
def now(cls):
return datetime.strptime("1234", "%H%M")

def mock_copyfile(src, dst):
pass

def mock_glob_single(pattern):
return ["stash/test.py"]

def mock_glob_none(pattern):
return []

def mock_glob_multiple(pattern):
return ["stash/test.py", "stash/test2.py"]

def test_plot_success(monkeypatch, capfd):
monkeypatch.setattr("package.piechart.datetime", MockDateTime)
monkeypatch.setattr(shutil, "copyfile", mock_copyfile)
monkeypatch.setattr(glob, "glob", mock_glob_single)

snippet_name = "test"
plot(snippet_name)
password = "1234"
plot(snippet_name, password)

assert True
# Verify output
captured = capfd.readouterr()
assert "File 'stash/test.py' copied successfully" in captured.out


def test_plot_no_file_found(monkeypatch, capfd):
monkeypatch.setattr("package.piechart.datetime", MockDateTime)
monkeypatch.setattr(glob, "glob", mock_glob_none)

snippet_name = "nonexistent"
password = "1234"
plot(snippet_name, password)

# Verify output
captured = capfd.readouterr()
assert "File is not found" in captured.out

def test_plot_multiple_files_found(monkeypatch, capfd):
monkeypatch.setattr("package.piechart.datetime", MockDateTime)
monkeypatch.setattr(glob, "glob", mock_glob_multiple)

snippet_name = "test"
password = "1234"
plot(snippet_name, password)

# Verify output
captured = capfd.readouterr()
assert "The given values are not supported" in captured.out
Loading