Skip to content

Commit

Permalink
[TESTS][REFACTO]
Browse files Browse the repository at this point in the history
- DossierState complete tests
- TestUnitField -> renaming + base class test, all unit test fields extends from this class verifying common fields attributes and behaviors
  • Loading branch information
Z3ZEL committed Aug 30, 2023
1 parent 399a9bb commit 99aeb02
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
116 changes: 48 additions & 68 deletions tests/test_fields_unit.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import pytest
import sys
from demarches_simpy import TextField
sys.path.append('..')

from tests.fake_api import FakeRequestBuilder, FakeResponse

from src.demarches_simpy import Dossier, Profile, MapField, TextField, DateField, AttachedFileField, MultipleDropDownField
from src.demarches_simpy.fields import FieldFactory
from src.demarches_simpy.utils import GeoArea, GeoSource
from src.demarches_simpy.connection import RequestBuilder

class TestUnitMapField():
def CONTENT(request : RequestBuilder):

class TestFieldUnit():
TYPE = 'Nothing'
def CONTENT(self, request : RequestBuilder):
return {
"data":{
}
}
@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, self.CONTENT)
request = FakeRequestBuilder(profile, resp)
dossier = Dossier(123,profile,request=request)
factory = FieldFactory(dossier)
return factory.create_field('123', 'foo', '', self.TYPE, request=request)

def test_field(self, field : TextField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == self.TYPE

class TestFieldUnitMap(TestFieldUnit):
TYPE = 'CarteChamp'
def CONTENT(self, request : RequestBuilder):
return {
"data":{
"dossier":{
Expand Down Expand Up @@ -79,13 +104,8 @@ def CONTENT(request : RequestBuilder):
}
}

@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, TestUnitMapField.CONTENT)
request = FakeRequestBuilder(profile, resp)
field = MapField('123', 'foo', '', 'CarteChamp', Dossier(123,profile,request=request),request=request)
return field



def test_field_geo_areas(self, field : MapField):
assert len(field.geo_areas) == 1
Expand Down Expand Up @@ -143,13 +163,10 @@ def test_field_geo_areas(self, field : MapField):
}
}
assert area.geometry_type == 'Polygon'
def test_field(self, field : MapField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == 'CarteChamp'

class TestUnitTextField():
def CONTENT(request : RequestBuilder):
class TestFieldUnitText(TestFieldUnit):
TYPE = 'TextChamp'
def CONTENT(self, request : RequestBuilder):
return {
"data":
{
Expand All @@ -169,22 +186,15 @@ def CONTENT(request : RequestBuilder):
}
}

@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, TestUnitTextField.CONTENT)
request = FakeRequestBuilder(profile, resp)
field = TextField('123', 'foo', '', 'TextField', Dossier(123,profile,request=request),request=request)
return field

def test_field(self, field : TextField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == 'TextField'
def test_field(self, field: TextField):
assert field.value == 'bar'
return super().test_field(field)

class TestUnitDateField():
def CONTENT(request : RequestBuilder):

class TestFieldUnitDate(TestFieldUnit):
TYPE = 'DateChamp'
def CONTENT(self, request : RequestBuilder):
return {
"data":
{
Expand All @@ -203,24 +213,15 @@ def CONTENT(request : RequestBuilder):
}
}
}

@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, TestUnitDateField.CONTENT)
request = FakeRequestBuilder(profile, resp)
field = DateField('123', 'foo', '', 'DateField', Dossier(123,profile,request=request),request=request)
return field

def test_field(self, field : DateField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == 'DateField'
def test_field(self, field: TextField):
assert field.date == '2020-08-10'
assert field.timestamp == 1597010400
return super().test_field(field)

class TestUnitAttachedFileField():
def CONTENT(request : RequestBuilder):
class TestFieldUnitAttachedFile(TestFieldUnit):
TYPE = 'PieceJustificativeChamp'
def CONTENT(self, request : RequestBuilder):
return {
"data":
{
Expand Down Expand Up @@ -254,30 +255,20 @@ def CONTENT(request : RequestBuilder):
}


@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, TestUnitAttachedFileField.CONTENT)
request = FakeRequestBuilder(profile, resp)
field = AttachedFileField('123', 'foo', '', 'PieceJustificativeChamp', Dossier(123,profile,request=request),request=request)
return field

def test_field(self, field : AttachedFileField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == 'PieceJustificativeChamp'

assert len(field.files.keys()) == 2
urls = list(field.files.keys())
url = urls[0]
assert field.files[url]["filename"] == 'foo.pdf'
assert field.files[url]["url"] == 'https://foo.bar'
assert field.files[url]["type"] == 'application/pdf'
assert field.files[url]["size"] == 6000
return super().test_field(field)


class TestUnitMultipleDropDownField():
def CONTENT(request : RequestBuilder):
class TestFieldUnitMultipleDropDown(TestFieldUnit):
TYPE = 'MultipleDropDownListChamp'
def CONTENT(self, request : RequestBuilder):
return {
"data":
{
Expand All @@ -299,21 +290,10 @@ def CONTENT(request : RequestBuilder):
}
}
}

@pytest.fixture
def field(self):
profile = Profile('')
resp = FakeResponse(200, TestUnitMultipleDropDownField.CONTENT)
request = FakeRequestBuilder(profile, resp)
field = MultipleDropDownField('123', 'foo', '', 'MultipleDropDownListChamp', Dossier(123,profile,request=request),request=request)
return field

def test_field(self, field : MultipleDropDownField):
assert field.id == '123'
assert field.label == 'foo'
assert field.type == 'MultipleDropDownListChamp'
assert isinstance(field.values, list)
assert len(field.values) == 2
assert field.values[0] == 'foo'
assert field.values[1] == 'bar'
return super().test_field(field)

25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,28 @@ def test_DemarchesSimpyException():
raise DemarchesSimpyException()
assert str(excinfo.value) == f"{bcolors.FAIL} [ERROR] [MAIN] There was an error{bcolors.ENDC}"


from src.demarches_simpy.dossier import DossierState


def test_DossierState_valid_from_str():
assert DossierState.from_str('en_construction') == DossierState.CONSTRUCTION
assert DossierState.from_str('en_instruction') == DossierState.INSTRUCTION
assert DossierState.from_str('accepte') == DossierState.ACCEPTE
assert DossierState.from_str('refuse') == DossierState.REFUSE
assert DossierState.from_str('sans_suite') == DossierState.SANS_SUITE

def test_DossierState_invalid_from_str():
with pytest.raises(ValueError) as excinfo:
DossierState.from_str('invalid')

with pytest.raises(ValueError) as excinfo:
DossierState.from_str(None)

def test_DossierState_build_query():
assert DossierState.__build_query_suffix__(DossierState.CONSTRUCTION) == 'EnConstruction'
assert DossierState.__build_query_suffix__(DossierState.INSTRUCTION) == 'EnInstruction'
assert DossierState.__build_query_suffix__(DossierState.ACCEPTE) == 'Accepter'
assert DossierState.__build_query_suffix__(DossierState.REFUSE) == 'Refuser'
assert DossierState.__build_query_suffix__(DossierState.SANS_SUITE) == 'ClasserSansSuite'

0 comments on commit 99aeb02

Please sign in to comment.