Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests custom fields #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions amocrm_api/custom_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from copy import deepcopy
from collections import UserDict, defaultdict, Mapping
from datetime import datetime, date
import dateutil.parser as datetime_parcer

from marshmallow import fields, pre_load, pre_dump, validate, ValidationError
from multidict import MultiDict
Expand Down Expand Up @@ -252,6 +254,16 @@ def _serialize(self, value, attr, obj):
class DateField(_SingleMixin, _CustomFieldMixin, fields.Date):
field_type = FIELD_TYPE.DATE

def _deserialize(self, value, attr, data):
if value:
return datetime_parcer.parse(value[0]['value']).date()

def _serialize(self, value, attr, data):
if value is not None:
if not isinstance(value, date):
raise ValidationError('Expected date, got %s' % type(value))
return [{'value': value.isoformat()}]


class UrlField(TextField):
field_type = FIELD_TYPE.URL
Expand Down
38 changes: 35 additions & 3 deletions tests/test_custom_fields.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import uuid
from datetime import date

import pytest

from amocrm_api.models import Contact
from amocrm_api.constants import FIELD_TYPE, ELEMENT_TYPE
from amocrm_api.custom_fields import CUSTOM_FIELD_MAP
from amocrm_api.custom_fields import CUSTOM_FIELD_MAP # , SmartAddress, LegalEntity


@pytest.mark.parametrize('field_type,value', (
(FIELD_TYPE.TEXT, 'VALUE1'),
(FIELD_TYPE.NUMERIC, 2),
(FIELD_TYPE.SELECT, 'yellow'),
# (FIELD_TYPE.MULTISELECT, ['yellow', ]), # TODO: wtf
(FIELD_TYPE.DATE, date(1999, 1, 1)),
(FIELD_TYPE.URL, 'https://google.com'),
# (FIELD_TYPE.MULTITEXT, 'text'), # TODO
(FIELD_TYPE.TEXTAREA, 'text'),
(FIELD_TYPE.RADIOBUTTON, 'yellow'),
(FIELD_TYPE.STREETADDRESS, 'Volkova, 9'),
# (FIELD_TYPE.SMART_ADDRESS,
# SmartAddress(address1='Volkova, 9', city='Kiev', country='Ukraine')), # TODO
(FIELD_TYPE.BIRTHDAY, date(1999, 1, 1)),
# (FIELD_TYPE.legal_entity,
# [LegalEntity(name='entity1', entity_type='type1', vat_id=1), ]), # TODO wtf
# (FIELD_TYPE.ITEMS, ), # TODO: not implemented
))
def test_custom_fields(client, field_type, value):
enums = None
need_enums = [FIELD_TYPE.SELECT, FIELD_TYPE.MULTISELECT,
FIELD_TYPE.RADIOBUTTON, FIELD_TYPE.ITEMS]
if field_type in need_enums:
enums = ['red', 'yellow', 'green']

field = CUSTOM_FIELD_MAP[field_type](
name=('__TEST_FIELD_%s' % uuid.uuid1()),
element_type=ELEMENT_TYPE.CONTACT
element_type=ELEMENT_TYPE.CONTACT,
enums=enums,
)

class MyContact(Contact):
Expand All @@ -29,6 +51,16 @@ class MyContact(Contact):
m1.save()

m2 = MyContact.get_one(id=m1.id)
assert m2.my_field == m1.my_field
if field_type == FIELD_TYPE.SMART_ADDRESS:
Copy link
Owner Author

@vgavro vgavro Jan 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this? if Entity == Entity is not working same way, implement Entity.__eq__ in requests-client

assert m2.my_field.address1 == m1.my_field.address1
assert m2.my_field.city == m1.my_field.city
assert m2.my_field.country == m1.my_field.country
elif field_type == FIELD_TYPE.legal_entity:
assert m2.my_field.name == m1.my_field.name
assert m2.my_field.entity_type == m1.my_field.entity_type
assert m2.my_field.vat_id == m1.my_field.vat_id
else:
assert m2.my_field == m1.my_field
assert m2.custom_fields[field.metadata['id']] == m1.my_field
assert m2.custom_fields[field.metadata['name']] == m1.my_field
m1.delete()