Skip to content

Commit

Permalink
added more tests for spender and removed unused code (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: harshithaputtaswamy <harshithaputtaswamy>
  • Loading branch information
harshithaputtaswamy authored Apr 11, 2022
1 parent 6516802 commit 7732c51
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 76 deletions.
4 changes: 1 addition & 3 deletions fyle/platform/apis/v1beta/common/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def get_by_id(self, id_) -> Dict:
query_params=query_params,
)

if id_ and response["count"] < 1:
raise exceptions.NotFoundItemError("Not found item with ID")
elif id_ and response["count"] > 1:
if id_ and response["count"] > 1:
raise exceptions.MultipleObjectReturned("Multiple Objects returned")

return response.get("data")[0]
Expand Down
68 changes: 2 additions & 66 deletions fyle/platform/internals/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,21 @@
Contains general serializers
"""

import enum
import json
from datetime import date, datetime

from requests import Response


class GeneralObject:
"""Class for General Object"""

def __init__(self, object_as_dict):
self.__dict__ = object_as_dict

def to_json(self):
"""Class method to convert dict to json"""
return json.loads(serialize(self))

def get(self, item, default=None):
"""Class method for get item"""
return self.__dict__.get(item, default)

def __getattr__(self, item):
return None

def __repr__(self):
return self.__dict__.__str__()


def deserialize(dictionary):
"""General JSON deserializer method"""
if not isinstance(dictionary, dict):
return dictionary

for key, value in dictionary.items():
if isinstance(value, dict):
deserialized_value = deserialize(value)
dictionary[key] = deserialized_value
elif isinstance(value, list):
for index, item in enumerate(value):
deserialized_item = deserialize(item)
value[index] = deserialized_item
dictionary[key] = value
deserialized_object = GeneralObject(dictionary)
return deserialized_object


class ComplexEncoder(json.JSONEncoder):
"""Class for Custom JSON Encoder"""

def default(self, o):
if isinstance(o, Response):
try:
encoded_obj = o.json()
except Exception:
encoded_obj = {}
elif isinstance(o, date):
encoded_obj = o.isoformat()
elif isinstance(o, datetime):
encoded_obj = o.isoformat() + "Z"
elif isinstance(o, enum.Enum):
encoded_obj = o.value
elif isinstance(o, GeneralObject):
encoded_obj = o.__dict__
elif hasattr(o, '__class__'):
if hasattr(o, 'serialize'):
encoded_obj = o.serialize()
else:
return None
elif isinstance(o, Exception):
encoded_obj = {
"error": o.__class__.__name__,
"args": o.args,
}
else:
encoded_obj = json.JSONEncoder.default(self, o)

return encoded_obj


def serialize(instance, **kwargs):
def serialize(instance):
"""JSON serializer method"""
return json.dumps(instance, cls=ComplexEncoder, **kwargs)
return json.dumps(instance)
Binary file added test/common/fixtures/sample_files/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion test/common/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ def fyle_connect():

with open('test_credentials.json', 'w') as fp:
json.dump(fyle_config, fp)
return connection
return connection


def get_sample_file_path():
basepath = path.dirname(__file__)
file_path = path.join(basepath, 'fixtures/sample_files/')

return file_path
6 changes: 3 additions & 3 deletions test/integration/admin/test_admin_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
import logging
from os import path
from test.common.utilities import dict_compare_keys
from test.common.utilities import dict_compare_keys, get_sample_file_path

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -227,8 +227,8 @@ def test_bulk_generate_file_urls(fyle, mock_data):


def test_upload_file_to_aws(fyle, mock_data):
basepath = path.dirname(__file__)
file_path = path.join(basepath, 'fixtures/sample_files/uber_expenses_2.txt')
basepath = get_sample_file_path()
file_path = path.join(basepath, 'uber_expenses_2.txt')
file_data = open(file_path, 'rb')

try:
Expand Down
Loading

0 comments on commit 7732c51

Please sign in to comment.