Skip to content

Commit

Permalink
🚑 partner_telegram: fix error on empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
yelizariev committed May 21, 2024
1 parent 193bc76 commit f42baed
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
14 changes: 8 additions & 6 deletions partner_telegram/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def _compute_telegram(self):
def _inverse_telegram(self):
for record in self:
value = record.telegram
if value.startswith("@"):
value = value[1:]
elif value.startswith("+"):
value = value.replace("-", "").replace(" ", "")
if not value:
record.telegram_username = False
record.telegram_mobile = False
elif value.startswith("@"):
record.telegram_username = value[1:]
elif value.startswith("https://t.me/"):
value = value[len("https://t.me/") :]
record.telegram_mobile = value
record.telegram_username = value[len("https://t.me/") :]
elif value.startswith("+"):
record.telegram_mobile = value.replace("-", "").replace(" ", "")
2 changes: 2 additions & 0 deletions partner_telegram/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License MIT (https://opensource.org/licenses/MIT).
from . import test_telegram
49 changes: 49 additions & 0 deletions partner_telegram/tests/test_telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2024 Ivan Yelizariev <https://twitter.com/yelizariev>
# License MIT (https://opensource.org/licenses/MIT).
from odoo.tests.common import TransactionCase


class TestResPartnerTelegram(TransactionCase):
def setUp(self):
super(TestResPartnerTelegram, self).setUp()
self.ResPartner = self.env["res.partner"]
# Create a partner without telegram information
self.partner = self.ResPartner.create({"name": "Test"})

def test_telegram_set_username(self):
# Set the telegram field with a username
self.partner.telegram = "@testuser"

# Check the computed fields
self.assertEqual(self.partner.telegram_username, "testuser")
self.assertEqual(self.partner.telegram_url, "https://t.me/testuser")
self.assertEqual(self.partner.telegram, "testuser")

def test_telegram_set_mobile(self):
# Set the telegram field with a mobile number
self.partner.telegram = "+1234567890"

# Check the computed fields
self.assertEqual(self.partner.telegram_mobile, "+1234567890")
self.assertEqual(self.partner.telegram_url, "https://t.me/+1234567890")
self.assertEqual(self.partner.telegram, "+1234567890")

def test_telegram_set_url(self):
# Set the telegram field with a URL
self.partner.telegram = "https://t.me/testuser"

# Check the computed fields
self.assertEqual(self.partner.telegram_username, "testuser")
self.assertEqual(self.partner.telegram_url, "https://t.me/testuser")
self.assertEqual(self.partner.telegram, "testuser")

def test_telegram_clear(self):
# Set the telegram field and then clear it
self.partner.telegram = "@testuser"
self.partner.telegram = ""

# Check the computed fields
self.assertFalse(self.partner.telegram_username)
self.assertFalse(self.partner.telegram_mobile)
self.assertFalse(self.partner.telegram_url)
self.assertFalse(self.partner.telegram)

0 comments on commit f42baed

Please sign in to comment.