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

Update Number Type #172

Merged
merged 4 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion moncli/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
from .board import Board, InvalidColumnValue
from .item import Item, UpdateNotFound
from .item import TooManyChangeSimpleColumnValueParameters,NotEnoughChangeSimpleColumnValueParameters
from .client import MondayClient
from .client import MondayClient
from .column_value.base import ColumnValue
18 changes: 17 additions & 1 deletion moncli/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytz, json
from datetime import datetime
from schematics.exceptions import ConversionError

from schematics.types import BaseType

Expand Down Expand Up @@ -77,4 +78,19 @@ def _convert(self, value: tuple):
return data

def _export(self, value):
return value
return value

class NumberType(MondayType):
native_type = (int, float)
allow_casts = (str, )
null_value = ""

def _cast(self, value):
try:
number_value = int(value) if int(value) else float(value)
return number_value
except TypeError:
raise ConversionError('Couldn\'t interpret str {} as int or float.'.format(value))

def _export(self, value):
return str(value)
64 changes: 64 additions & 0 deletions tests/monday_types_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json

from unittest.mock import patch
from nose.tools import ok_, eq_, raises
from schematics.common import NONEMPTY

from moncli import client, entities as en
from moncli.entities import column_value as cv
from moncli.enums import ColumnType
from moncli import types as t


def test_should_succeed_when_to_native_returns_an_int_or_float_when_passed_a_numbervalue_value_with_api_data():
Ashatz marked this conversation as resolved.
Show resolved Hide resolved
pass
Ashatz marked this conversation as resolved.
Show resolved Hide resolved

def test_should_succeed_when_to_native_returns_a_int_or_float_when_passed_a_int_or_float_value():
Ashatz marked this conversation as resolved.
Show resolved Hide resolved
# Arrange
number_type = t.NumberType(id=1)

# Act

int_value = number_type.to_native(1)
float_value = number_type.to_native(1.0)

# Assert
eq_(int_value,1)
eq_(float_value,1.0)

def test_should_succeed_when_to_native_returns_a_none_when_passed_a_none():
Ashatz marked this conversation as resolved.
Show resolved Hide resolved
# Arrange
number_type = t.NumberType(id=1)

# Act

value = number_type.to_native(None)

# Assert
eq_(value,None)


def test_should_succeed_when_to_primitive_returns_empty_str_when_passed_a_none():
Ashatz marked this conversation as resolved.
Show resolved Hide resolved
# Arrange
number_type = t.NumberType(id=1)

# Act
value = number_type.to_primitive(None)

# Assert
eq_(value,'')


def test_should_succeed_when_to_primitive_returns_export_string_when_passed_a_int_or_float_value():
Ashatz marked this conversation as resolved.
Show resolved Hide resolved
# Arrange
number_type = t.NumberType(id=1)

# Act
int_value = number_type.to_primitive(1)
float_value = number_type.to_primitive(1.0)



# Assert
eq_(int_value,'1')
eq_(float_value,'1.0')