Skip to content

Commit

Permalink
Merge pull request OpenG2P#210 from RamakrishnaVellala/17.0-develop
Browse files Browse the repository at this point in the history
added unit tests for cycleless and program_documents models
  • Loading branch information
shibu-narayanan authored Mar 28, 2024
2 parents d091691 + 4a2643a commit 724d90f
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 2 deletions.
2 changes: 1 addition & 1 deletion g2p_program_cycleless/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.
from . import models
from . import models, tests
3 changes: 3 additions & 0 deletions g2p_program_cycleless/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import test_entitlement_manager
from . import test_program_manager
from . import test_programs
88 changes: 88 additions & 0 deletions g2p_program_cycleless/tests/test_entitlement_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from datetime import datetime, timedelta

from odoo.tests.common import TransactionCase


class TestG2PEntitlementManagerDefault(TransactionCase):
def setUp(self):
super(TestG2PEntitlementManagerDefault, self).setUp()
self.entitlement_manager = self.env["g2p.program.entitlement.manager.default"]
self.program_model = self.env["g2p.program"]
self.cycle_model = self.env["g2p.cycle"]

def test_open_entitlements_form_reimbursement(self):
reimbursement_program = self.program_model.create(
{"name": "Reimbursement Program", "is_reimbursement_program": True}
)
reimbursement_cycle = self.cycle_model.create(
{
"name": "Reimbursement Cycle",
"program_id": reimbursement_program.id,
"start_date": datetime.now(),
"end_date": datetime.now() + timedelta(days=30),
}
)

action_reimbursement = self.entitlement_manager.open_entitlements_form(
reimbursement_cycle
)

self.assertNotEqual(action_reimbursement["name"], "Reimbursements")
self.assertNotEqual(action_reimbursement["name"], "Entitlements")

def test_open_entitlements_form_cycleless(self):
program = self.program_model.create(
{
"name": "Cycleless Program",
"is_cycleless": True,
}
)

cycle = self.cycle_model.create(
{
"name": "Test Cycle",
"program_id": program.id,
"start_date": datetime.now(),
"end_date": datetime.now() + timedelta(days=30),
}
)

manager = self.entitlement_manager.create(
{
"program_id": program.id,
"name": "Test Manager",
}
)

result = manager.open_entitlements_form(cycle)

self.assertEqual(result["name"], "Entitlements")

def test_open_entitlements_form_default(self):
program = self.program_model.create(
{
"name": "Regular Program",
}
)

cycle = self.cycle_model.create(
{
"name": "Test Cycle",
"program_id": program.id,
"start_date": datetime.now(),
"end_date": datetime.now() + timedelta(days=30),
}
)

manager = self.entitlement_manager.create(
{
"program_id": program.id,
"name": "Test Manager",
}
)

result = manager.open_entitlements_form(cycle)

# Add assertions for the default behavior, if any
self.assertNotEqual(result["name"], "Reimbursements")
self.assertNotEqual(result["name"], "Entitlements")
66 changes: 66 additions & 0 deletions g2p_program_cycleless/tests/test_program_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from odoo.exceptions import UserError
from odoo.tests import common

from odoo.addons.g2p_programs.models import constants


class TestDefaultProgramManager(common.TransactionCase):
def setUp(self):
super(TestDefaultProgramManager, self).setUp()

# Create a program and enroll a registrant (required for create_new_cycle)
self.program = self.env["g2p.program"].create({"name": "Test Program"})
self.partner = self.env["res.partner"].create({"name": "Test Partner"})
self.membership = self.env["g2p.program_membership"].create(
{
"program_id": self.program.id,
"partner_id": self.partner.id,
"state": "active",
}
)

# Create the default program manager for the program
self.manager = self.program.get_manager(constants.MANAGER_PROGRAM)

def test_onchange_is_cycless_true(self):
self.manager.is_cycleless = True

# Assert program's is_cycleless field is also set to True
self.assertEqual(self.program.is_cycleless, True)

def test_onchange_is_cycless_false(self):
# Create a cycle to test for its removal
self.program.create_new_cycle()

self.manager.is_cycleless = True # Set cycleless to True to create a cycle
self.manager.onchange_is_cycless()

self.manager.is_cycleless = False

# Assert program's is_cycleless field is also set to False
self.assertEqual(self.program.is_cycleless, False)

# Assert the previously created cycle is removed
with self.assertRaises(AssertionError):
self.program.default_active_cycle

def test_onchange_is_cycless_not_original_manager(self):
# Create another program manager with a name
new_manager = self.env["g2p.program.manager.default"].create(
{
"program_id": self.program.id,
"name": "Another Manager",
}
)

new_manager.is_cycleless = True
new_manager.onchange_is_cycless()

# Assert changing is_cycleless on a non-original manager has no effect
self.assertFalse(self.program.is_cycleless)

def test_onchange_is_cycleless_no_enrolled_registrants(self):
# Set manager to cycleless without enrolled registrants
with self.assertRaises(UserError):
self.manager.is_cycleless = True
self.manager.onchange_is_cycless()
67 changes: 67 additions & 0 deletions g2p_program_cycleless/tests/test_programs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from unittest import mock

from odoo.tests import common


class TestG2PPrograms(common.TransactionCase):
def setUp(self):
super(TestG2PPrograms, self).setUp()

# Create a cycle-based and a cycleless program
self.cycle_based_program = self.env["g2p.program"].create(
{"name": "Cycle-Based Program"}
)
self.cycleless_program = self.env["g2p.program"].create(
{"name": "Cycleless Program", "is_cycleless": True}
)

def test_default_active_cycle_cycle_based(self):
# Create two cycles with different states
cycle1 = self.cycle_based_program.create_new_cycle()
cycle2 = self.cycle_based_program.create_new_cycle()
cycle1.state = "approved"
cycle2.state = "to_approve"

# Assert that the most recently approved cycle is the default active cycle
self.assertEqual(self.cycle_based_program.default_active_cycle, cycle1)

def test_default_active_cycle_cycleless(self):
# Create a cycle for the cycleless program
self.cycleless_program.create_new_cycle()

# Assert that the cycle is not assigned as the default active cycle (should be None)
self.assertFalse(self.cycleless_program.default_active_cycle)

def test_show_cycleless_fields_cycle_based(self):
# Assert that cycleless fields are not shown for a cycle-based program
self.assertFalse(self.cycle_based_program.show_prepare_payments_button)
self.assertFalse(self.cycle_based_program.show_send_payments_button)

def test_show_cycleless_fields_cycleless_active_with_payment_manager(self):
# Assign a payment manager to the cycleless program
self.cycleless_program.write(
{"manager_id": self.env["g2p.program.manager.phee"].create({}).id}
)

# Assert that cycleless payment buttons are displayed
self.assertTrue(self.cycleless_program.show_prepare_payments_button)
self.assertTrue(self.cycleless_program.show_send_payments_button)

def test_show_cycleless_fields_cycleless_inactive(self):
# Change the program state to inactive
self.cycleless_program.state = "inactive"

# Assert that cycleless payment buttons are not displayed
self.assertFalse(self.cycleless_program.show_prepare_payments_button)
self.assertFalse(self.cycleless_program.show_send_payments_button)

def test_open_entitlements_form(self):
# Create a cycle for the program
cycle = self.cycle_based_program.create_new_cycle()

# Call open_entitlements_form and assert that it calls the correct method on the cycle
with mock.patch.object(
cycle, "open_entitlements_form"
) as mock_open_entitlements:
self.cycle_based_program.open_entitlements_form()
mock_open_entitlements.assert_called_once()
2 changes: 1 addition & 1 deletion g2p_program_documents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.
from . import models
from . import models, tests
6 changes: 6 additions & 0 deletions g2p_program_documents/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import test_document_file
from . import test_document_store
from . import test_entitlement
from . import test_entitlement_manager
from . import test_program
from . import test_program_membership
86 changes: 86 additions & 0 deletions g2p_program_documents/tests/test_document_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from datetime import datetime, timedelta

from odoo.exceptions import AccessError
from odoo.tests.common import TransactionCase


class TestG2PDocument(TransactionCase):
def setUp(self):
super(TestG2PDocument, self).setUp()

# Create necessary records or perform any setup needed for the tests
self.program = self.env["g2p.program"].create({"name": "Test Program"})
self.partner = self.env["res.partner"].create({"name": "Test Partner"})
self.g2p_program_membership = self.env["g2p.program_membership"].create(
{
"name": "Test Membership",
"partner_id": self.partner.id,
"program_id": self.program.id,
}
)
self.cycle = self.env["g2p.cycle"].create(
{
"name": "Test Cycle",
"program_id": self.program.id,
"sequence": 1,
"start_date": datetime.now().strftime("%Y-%m-%d"),
"end_date": (datetime.now() + timedelta(days=30)).strftime("%Y-%m-%d"),
}
)

self.g2p_entitlement = self.env["g2p.entitlement"].create(
{
"name": "Test Entitlement",
"partner_id": self.partner.id,
"program_id": self.program.id,
"cycle_id": self.cycle.id,
"initial_amount": 100,
}
)

self.storage_file = self.env["storage.file"].create(
{
"name": "Test File",
"program_membership_id": self.g2p_program_membership.id,
"entitlement_id": self.g2p_entitlement.id,
"backend_id": 1,
}
)

def test_get_binary(self):
# Ensure that get_binary method creates an attachment and returns the correct result
self.storage_file.get_binary()

# Check if the attachment is created
self.assertTrue(self.storage_file.attachment_id)

# Check if the attachment is associated with the correct file
self.assertEqual(self.storage_file.attachment_id.res_id, self.storage_file.id)

# Check if the attachment type is binary
self.assertEqual(self.storage_file.attachment_id.type, "binary")

# Check if the method returns a dictionary with expected keys
binary_data = self.storage_file.get_binary()
self.assertIsInstance(binary_data, dict)
self.assertIn("id", binary_data)
self.assertIn("mimetype", binary_data)
self.assertIn("index_content", binary_data)
self.assertIn("url", binary_data)

def test_get_binary_no_attachment(self):
# Ensure that get_binary method handles the case when no attachment is present
self.storage_file.attachment_id = False

# Check if the method creates an attachment when there is no attachment
self.storage_file.get_binary()

# Check if the attachment is created
self.assertTrue(self.storage_file.attachment_id)

def test_get_binary_no_permission(self):
# Ensure that get_binary method raises AccessError when user has no permission
# For example, you might check if the user has read access to the file
with self.assertRaises(AccessError):
self.env.user.write({"groups_id": [(3, self.ref("base.group_user"))]})
self.storage_file.get_binary()
59 changes: 59 additions & 0 deletions g2p_program_documents/tests/test_document_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from odoo.tests.common import TransactionCase


class TestG2PDocumentStore(TransactionCase):
def setUp(self):
super(TestG2PDocumentStore, self).setUp()
self.G2PDocumentStore = self.env["storage.backend"]
self.program = self.env["g2p.program"].create({"name": "Test Program"})
self.partner = self.env["res.partner"].create({"name": "Test Partner"})
self.g2p_program_membership = self.env["g2p.program_membership"].create(
{
"name": "Test Membership",
"partner_id": self.partner.id,
"program_id": self.program.id,
}
)
self.g2p_document_store = self.G2PDocumentStore.create(
{
"name": "Test Document Store",
# Add any other required fields
}
)
# Create a G2PDocumentStore instance
document_store = self.env["storage.backend"].create(
{
"name": "Test Document Store",
# ... other required fields ...
}
)
# Call add_file with a program_membership
file = document_store.add_file(
data=b"Some test data",
name="Test File",
extension="txt",
)
# Assert that the program_membership_id is set
self.assertEqual(file.program_membership_id, self.g2p_program_membership)

def test_add_file_calls_super_method(self):
"""Test that add_file calls the superclass method correctly."""
with self.mock_with_context(
self.env["storage.backend"]._patch_method("add_file")
) as mock_add_file:
document_store = self.env["storage.backend"].create(
{
"name": "Test Document Store",
# ... other required fields ...
}
)
document_store.add_file(
data=b"Some test data",
name="Test File",
extension="txt",
)
mock_add_file.assert_called_once_with(
data=b"Some test data",
name="Test File",
extension="txt",
)
Loading

0 comments on commit 724d90f

Please sign in to comment.