Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Feb 21, 2024
1 parent 034e6b0 commit 613f5c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
27 changes: 27 additions & 0 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from lemarche.users.factories import UserFactory
from lemarche.users.models import User
from lemarche.utils.admin.admin_site import MarcheAdminSite, get_admin_change_view_url
from lemarche.www.tenders import utils as tender_utils


date_tomorrow = timezone.now() + timedelta(days=1)
Expand Down Expand Up @@ -971,3 +972,29 @@ def test_edit_form_no_matching_on_validate_submission(self):
self.assertTrue(hasattr(tender_response, "siae_count_annotated"))
self.assertEqual(tender_response.siae_count_annotated, 1)
self.assertEqual(tender_response.siae_count_annotated, self.tender.tendersiae_set.count())


class TenderUtilsTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user_siae = UserFactory(kind=User.KIND_SIAE)
cls.user_buyer = UserFactory(kind=User.KIND_BUYER)
cls.siae_with_tender_1 = SiaeFactory(users=[cls.user_siae])
siae_with_tender_2 = SiaeFactory(users=[cls.user_siae])
cls.sector = SectorFactory()
cls.tender_with_siae = TenderFactory(
siaes=[cls.siae_with_tender_1, siae_with_tender_2],
sectors=[cls.sector],
author=cls.user_buyer,
deadline_date=date_tomorrow,
status=tender_constants.STATUS_SENT,
first_sent_at=timezone.now(),
)

def test_duplicate(self):
new_tender = tender_utils.duplicate(self.tender_with_siae)
self.assertEqual(self.tender_with_siae.title, new_tender.title)
self.assertEqual(self.tender_with_siae.author, new_tender.author)
self.assertNotEqual(self.tender_with_siae.status, new_tender.status)
self.assertEqual(self.tender_with_siae.sectors().count(), new_tender.sectors().count())
self.assertNotEqual(self.tender_with_siae.siaes().count(), new_tender.siaes().count())
9 changes: 6 additions & 3 deletions lemarche/www/tenders/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings

from lemarche.tenders import constants as tender_constants
from lemarche.tenders.models import Tender, TenderQuestion
from lemarche.users import constants as user_constants
from lemarche.users.models import User
Expand Down Expand Up @@ -101,7 +102,9 @@ def get_or_create_user(request_user, tender_dict: dict, source=user_constants.SO

def duplicate(tender: Tender) -> Tender:
FIELDS_TO_REMOVE = (
["_state"] + ["id", "slug", "siae_transactioned", "extra_data", "import_raw_object"] + Tender.READONLY_FIELDS
["_state", "_django_version"]
+ ["id", "slug", "siae_transactioned", "extra_data", "import_raw_object"]
+ Tender.READONLY_FIELDS
)
FIELDS_TO_KEEP = [field for field in tender.__dict__.keys() if field not in FIELDS_TO_REMOVE]
# sectors # managed post-create
Expand All @@ -111,8 +114,8 @@ def duplicate(tender: Tender) -> Tender:
new_tender_dict[key] = tender.__dict__[key]

# overwrite some fields
new_tender_dict["status"] = Tender.STATUS_PUBLISHED
new_tender_dict["source"] = Tender.SOURCE_STAFF_C4_CREATED
new_tender_dict["status"] = tender_constants.STATUS_PUBLISHED
new_tender_dict["source"] = tender_constants.SOURCE_STAFF_C4_CREATED
new_tender_dict["logs"] = [{"action": "tender_duplicated", "from": tender.id}]

# create duplicate tender
Expand Down

0 comments on commit 613f5c1

Please sign in to comment.