Skip to content

Commit

Permalink
Merge pull request #72 from imZack/develop
Browse files Browse the repository at this point in the history
0.0.7
  • Loading branch information
imZack committed Aug 24, 2015
2 parents 4eae2da + 788b67d commit 414a7ac
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 339 deletions.
2 changes: 1 addition & 1 deletion sanji/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: UTF-8 -*-

import os
import json
import simplejson as json
import logging

_logger = logging.getLogger("sanji.sdk.bundle")
Expand Down
87 changes: 0 additions & 87 deletions sanji/config.py

This file was deleted.

2 changes: 1 addition & 1 deletion sanji/connection/mockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import sys
import json
import simplejson as json
import uuid
from mock import Mock

Expand Down
60 changes: 53 additions & 7 deletions sanji/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from sanji.model_initiator import ModelInitiator
from voluptuous import Schema


class Model(object):
def __init__(self, name, path, schema=None):

def __init__(self, name, path, schema=None, model_cls=dict):
self.model_cls = model_cls
self.schema = schema
if schema is not None and not isinstance(schema, Schema):
raise TypeError("schema should be instance of voluptuous.Schema")

if not issubclass(model_cls, dict):
raise TypeError("model_cls should be derivative dict class")

self.model = ModelInitiator(
model_name=name,
model_path=path
)

def _cast_model(self, obj):
return self.model_cls(obj)

@property
def maxId(self):
"""int: current max id of objects"""
Expand All @@ -17,6 +29,21 @@ def maxId(self):

return max(map(lambda obj: obj["id"], self.model.db))

def validation(self, instance):
"""Valid input instance is vaild or not
Args:
Object: input instance
Returns:
Object: Instance after vaildation or original instance
if schema is None
Raises:
Error: If vaildation failed
"""
if self.schema is None:
return instance

return self.schema(instance)

def add(self, obj):
"""Add a object
Args:
Expand All @@ -25,13 +52,14 @@ def add(self, obj):
Object: Object with id
Raises:
TypeError: If add object is not a dict
MultipleInvalid: If input object is invaild
"""
if isinstance(obj, list):
if not isinstance(obj, dict):
raise TypeError("Add object should be a dict object")

obj = self.validation(obj)
obj = self._cast_model(obj)
obj["id"] = self.maxId + 1
self.model.db.append(obj)

return obj

def get(self, id):
Expand All @@ -45,7 +73,7 @@ def get(self, id):
"""
for obj in self.model.db:
if obj["id"] == id:
return obj
return self._cast_model(obj)

return None

Expand All @@ -54,9 +82,20 @@ def remove(self, id):
Args:
id (int): Object's id should be deleted
Returns:
None
len(int): affected rows
"""
before_len = len(self.model.db)
self.model.db = [t for t in self.model.db if t["id"] != id]
return before_len - len(self.model.db)

def removeAll(self):
"""Remove all objects
Returns:
len(int): affected rows
"""
before_len = len(self.model.db)
self.model.db = []
return before_len - len(self.model.db)

def update(self, id, newObj):
"""Update a object
Expand All @@ -66,12 +105,15 @@ def update(self, id, newObj):
Returns:
Object: Updated object
None: If specified object id is not found
MultipleInvalid: If input object is invaild
"""
newObj = self.validation(newObj)
for obj in self.model.db:
if obj["id"] != id:
continue
newObj.pop("id", None)
obj.update(newObj)
obj = self._cast_model(obj)
return obj

return None
Expand All @@ -81,4 +123,8 @@ def getAll(self):
Returns:
List: list of all objects
"""
return self.model.db
objs = []
for obj in self.model.db:
objs.append(self._cast_model(obj))

return objs
2 changes: 1 addition & 1 deletion sanji/model_initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: UTF-8 -*-

import logging
import json
import simplejson as json
import os
import shutil
import subprocess
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def read(*paths):

setup(
name="sanji",
version="0.0.6",
version="0.0.7",
description="Sanji Framework SDK",
long_description=read('README.rst'),
url="https://github.com/Sanji-IO/sanji",
Expand Down
32 changes: 32 additions & 0 deletions tests/sanji/model/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import tempfile
import simplejson as json
from voluptuous import Schema
from voluptuous import MultipleInvalid

if sys.version_info >= (2, 7):
import unittest
Expand All @@ -26,11 +28,29 @@ def setUp(self):
os.mkdir(self.temp_dir + "/data")
with open(self.temp_dir + "/data/test.json.factory", "w") as fp:
json.dump(factory_data, fp)
with open(self.temp_dir + "/data/schema.json.factory", "w") as fp:
json.dump(factory_data, fp)
self.model = Model(name="test", path=self.temp_dir)
self.model.add({"key": "value1"})
self.model.add({"key": "value2"})
self.model.add({"key": "value3"})

schema = Schema({"key": int})
self.model_with_schema = Model(
name="schema", path=self.temp_dir, schema=schema)

def test__init__(self):
"""Create a Model instance with wrong schema type,
should raise TypeError"""
with self.assertRaises(TypeError):
Model(name="test", path="/tmp", schema={"key": int})

def test__init__invaild_model_cls(self):
"""Create a Model instance with wrong model_cls type,
should raise TypeError"""
with self.assertRaises(TypeError):
Model(name="test", path="/tmp", model_cls=list)

def test_add(self):
"""Add an object, should return inserted object with id"""
newObj = self.model.add({"key": "value"})
Expand All @@ -41,6 +61,12 @@ def test_add_invaild(self):
with self.assertRaises(TypeError):
self.model.add(["test"])

def test_add_invaild_schema(self):
"""Add an invaild object(against schema),
should raise MutlitpleInvalid"""
with self.assertRaises(MultipleInvalid):
self.model_with_schema.add({"key": []})

def test_get(self):
"""Get an object by id, should return correct object"""
obj = self.model.get(3)
Expand All @@ -63,6 +89,12 @@ def test_update(self):
self.assertIsNotNone(new_obj)
self.assertEqual(self.model.get(1)["key"], "updated value")

def test_update_invaild(self):
"""Update an invaild object(against schema),
should raise MutlitpleInvalid"""
with self.assertRaises(MultipleInvalid):
self.model_with_schema.update(1, {"key": "updated value"})

def test_update_non_exist(self):
"""Update object which is not exist, should return None"""
new_obj = self.model.update(10, {"key": "updated value"})
Expand Down
Loading

0 comments on commit 414a7ac

Please sign in to comment.