Skip to content

Commit

Permalink
chore: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfelipec95 committed Jan 14, 2025
1 parent 8b6cd10 commit f1cbe8e
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions webhook_xblock/test/test_webhook_xblock.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
from django.test import TestCase
from mock import MagicMock, patch
from webhook_xblock.webhook_xblock import WebhookXblock


class WebhookXblockBasicTests(TestCase):
"""
Basic test to validate that WebhookXblock initializes correctly.
"""
class WebhookXblockTests(TestCase):

def test_xblock_initialization(self):
def setUp(self):
"""
Verifies that a WebhookXblock instance initializes with default values.
Initial setup for the tests.
"""
xblock = WebhookXblock()
self.assertIsNotNone(xblock)
self.xblock = WebhookXblock()
self.xblock.webhook_url = "http://example.com/webhook"
self.xblock.name = "test_component"
self.xblock.frequency = "one-time"

@patch("webhook_xblock.webhook_xblock.requests.post")
def test_make_request_success(self, mock_post):
"""
Tests that make_request returns True when the request is successful.
"""
mock_response = MagicMock()
mock_response.ok = True
mock_post.return_value = mock_response

data = {"test_key": "test_value"}
result = self.xblock.make_request(data)
self.assertTrue(result)

@patch("webhook_xblock.webhook_xblock.requests.post")
def test_make_request_failure(self, mock_post):
"""
Tests that make_request returns False when the request fails.
"""
mock_response = MagicMock()
mock_response.ok = False
mock_post.return_value = mock_response

data = {"test_key": "test_value"}
result = self.xblock.make_request(data)
self.assertFalse(result)

@patch("webhook_xblock.webhook_xblock.requests.post", side_effect=Exception("Test exception"))
def test_make_request_exception(self, mock_post):
"""
Tests that make_request handles exceptions correctly.
"""
data = {"test_key": "test_value"}
with self.assertRaises(Exception):
self.xblock.make_request(data)

def test_mark_as_visited_first_time(self):
"""
Tests that mark_as_visited returns False if it has already been visited.
"""
self.xblock.checkpoint = False
result = self.xblock.mark_as_visited()
self.assertTrue(result)

def test_mark_as_visited_already_visited(self):
"""
Prueba que mark_as_visited retorna False si ya ha sido visitado.
"""
self.xblock.checkpoint = True
result = self.xblock.mark_as_visited()
self.assertFalse(result)

def test_flatten_dict_integration(self):
"""
Tests the integration of flatten_dict with the XBlock.
"""
input_dict = {"a": {"b": {"c": 1}}, "d": 2}
expected = {"a_b_c": 1, "d": 2}
result = self.xblock.flatten_dict(input_dict)
self.assertEqual(result, expected)

0 comments on commit f1cbe8e

Please sign in to comment.