Skip to content

Commit

Permalink
Add unit testing for context.py module
Browse files Browse the repository at this point in the history
Signed-off-by: Mihaela Balutoiu <mbalutoiu@cloudbasesolutions.com>
  • Loading branch information
mihaelabalutoiu committed Nov 10, 2023
1 parent 39b3696 commit 49e46b3
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions coriolis/tests/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis import context
from coriolis.tests import test_base
from coriolis.tests import testutils


class RequestContextTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis RequestContext class."""

def setUp(self):
super(RequestContextTestCase, self).setUp()
self.req_context = context.RequestContext(
user="user",
project_id='test_project_id',
roles=['role1', 'role2']
)

@mock.patch.object(context.timeutils, 'utcnow')
def test__init__(self, mock_utcnow):
context_req = testutils.get_wrapped_function(context.RequestContext)(
user="user",
project_id='test_project_id',
roles=['role1', 'role2'],
)

mock_utcnow.assert_called_once_with()
self.assertEqual(context_req.timestamp, mock_utcnow.return_value)
self.assertEqual(context_req.roles, ['role1', 'role2'])

@mock.patch.object(context.timeutils, 'parse_isotime')
def test__init__with_string_timestamp(self, mock_parse_isotime):
new_context = testutils.get_wrapped_function(context.RequestContext)(
user="user",
project_id='test_project_id',
timestamp="2023-11-09T13:31:08Z",
)

mock_parse_isotime.assert_called_once_with("2023-11-09T13:31:08Z")
self.assertEqual(new_context.timestamp,
mock_parse_isotime.return_value)
self.assertEqual(new_context.roles, [])

def test_to_dict(self):
result = self.req_context.to_dict()

self.assertEqual(result['timestamp'],
self.req_context.timestamp.isoformat())

def test_from_dict(self):
result = self.req_context.to_dict()
new_context = self.req_context.from_dict(result)

# We use replace to remove timezone information for comparison
expected_timestamp = self.req_context.timestamp.replace(tzinfo=None)
actual_timestamp = new_context.timestamp.replace(tzinfo=None)

self.assertEqual(expected_timestamp, actual_timestamp)

0 comments on commit 49e46b3

Please sign in to comment.