Skip to content

Commit

Permalink
validate MTN function transfer from manifest app version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham4401 committed Apr 19, 2024
1 parent 3edead0 commit 1fc953a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
13 changes: 13 additions & 0 deletions server/apps/manif/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import re

from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _


def draft_mtn():
Expand All @@ -7,6 +11,15 @@ def draft_mtn():
return f"{str(mtn_count).zfill(9)}DFT"


def validate_mtn(value):
"""Validate manifest tracking number format"""
if not re.match(r"[0-9]{9}[A-Z]{3}", value):
raise ValidationError(
_("%(value)s is not in valid MTN format: [0-9]{9}[A-Z]{3}"),
params={"value": value},
)


# Create your models here.
class Manifest2(models.Model):
mtn = models.CharField(
Expand Down
14 changes: 0 additions & 14 deletions server/apps/manif/tests/test_foo.py

This file was deleted.

26 changes: 26 additions & 0 deletions server/apps/manif/tests/test_manifest_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import re
from unittest.mock import MagicMock, patch

import pytest
from django.core.exceptions import ValidationError

from apps.manif.models import draft_mtn, validate_mtn


class TestManifestModel:
def test_draft_mtn_follow_rcrainfo_pattern_with_dft_suffix(self, mocker):
mock = MagicMock()
mock.count.return_value = 5
with patch("apps.manif.models.Manifest2.objects.all", return_value=mock):
result = draft_mtn()
assert isinstance(result, str)
assert re.match(r"\d{9}DFT", result)

@pytest.mark.parametrize("mtn", ["123456789ELC", "111111111DFT", "100200300JJK"])
def test_mtn_validation_raises_no_error(self, mtn):
assert validate_mtn(mtn) is None

@pytest.mark.parametrize("mtn", ["foo_bar", "111111DFT", "123456789"])
def test_mtn_validation_raises_error(self, mtn):
with pytest.raises(ValidationError):
assert validate_mtn(mtn) is None

0 comments on commit 1fc953a

Please sign in to comment.