Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
Merge 7d38f7c into 0e4f303
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jan 24, 2017
2 parents 0e4f303 + 7d38f7c commit d0bba3e
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision History

## 1.3 (unreleased)

- Optimized the formatting of empty lists to create consistent diffs.
- Added `ModelMixin` to add ORM methods to mapped classes.

## 1.2 (2017/01/06)

- Updated base class to hide `pytest` traceback in wrapped methods.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ COVERAGE_SPACE := $(BIN_)coverage.space

RANDOM_SEED ?= $(shell date +%s)

PYTEST_CORE_OPTS := --doctest-modules -ra -vv
PYTEST_CORE_OPTS := -ra -vv
PYTEST_COV_OPTS := --cov=$(PACKAGE) --no-cov-on-fail --cov-report=term-missing --cov-report=html
PYTEST_RANDOM_OPTS := --random --random-seed=$(RANDOM_SEED)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@yorm.attr(value=yorm.types.standard.Integer)
@yorm.sync("path/to/{self.name}.yml")
@yorm.sync("tmp/path/to/{self.name}.yml")
class Sample:
"""Sample class for fake mapping."""

Expand All @@ -37,7 +37,7 @@ def test_no_file_create_when_fake(self, tmpdir):
sample = Sample('sample')

# ensure no file is created
assert "path/to/sample.yml" == sample.__mapper__.path
assert "tmp/path/to/sample.yml" == sample.__mapper__.path
assert not os.path.exists(sample.__mapper__.path)

# change object values
Expand Down
4 changes: 2 additions & 2 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IntegerList(List):
@yorm.attr(object=EmptyDictionary)
@yorm.attr(string=String)
@yorm.attr(true=Boolean)
@yorm.sync("path/to/{self.category}/{self.name}.yml")
@yorm.sync("tmp/path/to/{self.category}/{self.name}.yml")
class SampleStandardDecorated:
"""Sample class using standard attribute types."""

Expand Down Expand Up @@ -131,7 +131,7 @@ def test_automatic_save_after_first_modification(self, tmpdir):
def test_automatic_save_after_first_modification_on_list(self, tmpdir):
tmpdir.chdir()
sample = SampleStandardDecorated('sample')
assert "array: []\n" in sample.__mapper__.text
assert "array:\n-\n" in sample.__mapper__.text

sample.array.append(42)
assert "array:\n- 42\n" in sample.__mapper__.text
Expand Down
1 change: 1 addition & 0 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_from_top_constants():
def test_from_top_clases():
from yorm import Mappable
from yorm import Converter, Container
from yorm import ModelMixin


def test_from_top_decorators():
Expand Down
4 changes: 1 addition & 3 deletions tests/test_list_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Example:

def test_list_mapping_using_shortened_syntax():
obj = Example()
obj.things.append(1)
obj.things.append(2.0)
obj.things.append("3")
obj.things = [1, 2.0, "3"]

expect(obj.__mapper__.text) == strip("""
things:
Expand Down
74 changes: 48 additions & 26 deletions tests/test_nested_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ def test_append_triggers_save(self):
log("Checking text...")
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.0
""") == top.__mapper__.text
Expand All @@ -121,22 +123,26 @@ def test_set_by_index_triggers_save(self):
top.nested_list = [{'number': 1.5}]
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.5
""") == top.__mapper__.text
top.nested_list[0] = {'number': 1.6}
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.6
""") == top.__mapper__.text
Expand All @@ -154,26 +160,31 @@ def test_delete_index_triggers_save(self):
top.nested_list = [{'number': 1.8}, {'number': 1.9}]
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.8
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.9
""") == top.__mapper__.text
del top.nested_list[0]
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 1.9
""") == top.__mapper__.text
Expand All @@ -183,9 +194,11 @@ def test_set_dict_as_attribute_triggers_save(self):
top.nested_dict.number = 2
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 2.0
nested_list: []
nested_list:
-
""") == top.__mapper__.text


Expand All @@ -197,22 +210,26 @@ def test_nested_list_item_value_change_triggers_save(self):
top.nested_list = [{'number': 3}]
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 3.0
""") == top.__mapper__.text
top.nested_list[0].number = 4
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 0.0
number: 4.0
""") == top.__mapper__.text
Expand All @@ -225,7 +242,8 @@ def test_nested_dict_item_value_change_triggers_save(self):
nested_list_2:
- 5.0
number: 0.0
nested_list: []
nested_list:
-
""") == top.__mapper__.text
top.nested_dict.nested_list_2.append(6)
assert strip("""
Expand All @@ -234,36 +252,40 @@ def test_nested_dict_item_value_change_triggers_save(self):
- 5.0
- 6.0
number: 0.0
nested_list: []
nested_list:
-
""") == top.__mapper__.text

def test_dict_in_list_value_change_triggers_save(self):
top = Top()
log("Appending to list...")
top.nested_list.append(None)
top.nested_list.append('foobar')
log("Setting nested value...")
top.nested_list[0].nested_dict_3.number = 8
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
nested_list_3: []
nested_list_3:
-
number: 8.0
number: 0.0
""") == top.__mapper__.text

def test_list_in_dict_append_triggers_save(self):
top = Top()
top.nested_list.append(None)
top.nested_list.append(None)
top.nested_list.append('foobar')
top.nested_list.append('foobar')
for nested_dict_2 in top.nested_list:
nested_dict_2.number = 9
nested_dict_2.nested_dict_3.nested_list_3.append(10)
assert strip("""
nested_dict:
nested_list_2: []
nested_list_2:
-
number: 0.0
nested_list:
- nested_dict_3:
Expand Down Expand Up @@ -327,7 +349,7 @@ def test_alias_dict(sample):

def test_alias_dict_in_list():
top = Top()
top.nested_list.append(None)
top.nested_list.append('foobar')
ref1 = top.nested_list[0]
ref2 = top.nested_list[0].nested_dict_3
ref3 = top.nested_list[0].nested_dict_3.nested_list_3
Expand Down
3 changes: 2 additions & 1 deletion yorm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .decorators import sync, sync_object, sync_instances, attr
from .utilities import create, find, match, load, save, delete
from .bases import Container, Converter, Mappable
from .mixins import ModelMixin

__project__ = 'YORM'
__version__ = '1.2'
__version__ = '1.3b2'
26 changes: 26 additions & 0 deletions yorm/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from yorm import utilities


class ModelMixin:
"""Adds ORM methods to a mapped class."""

@classmethod
def new(cls, *args, **kwargs):
return utilities.create(cls, *args, **kwargs)

@classmethod
def find(cls, *args, **kwargs):
return utilities.find(cls, *args, **kwargs)

@classmethod
def match(cls, *args, **kwargs):
return utilities.match(cls, *args, **kwargs)

def load(self):
return utilities.load(self)

def save(self):
return utilities.save(self)

def delete(self):
return utilities.delete(self)
6 changes: 4 additions & 2 deletions yorm/tests/test_bases_mappable.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def test_init(self):
var1: ''
var2: 0
var3: false
var4: []
var4:
-
var5:
status: false
""") == text
Expand Down Expand Up @@ -132,7 +133,8 @@ def test_set_converted(self):
var1: 42
var2: 1
var3: false
var4: []
var4:
-
var5:
status: true
""") == text
Expand Down
12 changes: 6 additions & 6 deletions yorm/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class SampleDecoratedIdentifiers:
def __repr__(self):
return "<decorated w/ UUID {}>".format(id(self))

@decorators.sync("path/to/{n}.yml", {'n': 'name'})
@decorators.sync("tmp/path/to/{n}.yml", {'n': 'name'})
class SampleDecoratedAttributes:
"""Sample decorated class using an attribute value for paths."""

Expand All @@ -109,7 +109,7 @@ def __init__(self, name):
def __repr__(self):
return "<decorated w/ specified attributes {}>".format(id(self))

@decorators.sync("path/to/{self.name}.yml")
@decorators.sync("tmp/path/to/{self.name}.yml")
class SampleDecoratedAttributesAutomatic:
"""Sample decorated class using an attribute value for paths."""

Expand Down Expand Up @@ -166,15 +166,15 @@ def test_filename_attributes(self):
"""Verify attributes can be used to determine filename."""
sample1 = self.SampleDecoratedAttributes('one')
sample2 = self.SampleDecoratedAttributes('two')
assert "path/to/one.yml" == sample1.__mapper__.path
assert "path/to/two.yml" == sample2.__mapper__.path
assert "tmp/path/to/one.yml" == sample1.__mapper__.path
assert "tmp/path/to/two.yml" == sample2.__mapper__.path

def test_filename_attributes_automatic(self):
"""Verify attributes can be used to determine filename (auto save)."""
sample1 = self.SampleDecoratedAttributesAutomatic('one')
sample2 = self.SampleDecoratedAttributesAutomatic('two')
assert "path/to/one.yml" == sample1.__mapper__.path
assert "path/to/two.yml" == sample2.__mapper__.path
assert "tmp/path/to/one.yml" == sample1.__mapper__.path
assert "tmp/path/to/two.yml" == sample2.__mapper__.path

def test_filename_attributes_combination(self):
"""Verify attributes can be used to determine filename (combo)."""
Expand Down
6 changes: 3 additions & 3 deletions yorm/tests/test_diskutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def describe_delete():
@pytest.fixture
def existing_path(tmpdir):
tmpdir.chdir()
path = "path/to/file.ext"
path = "tmp/path/to/file.ext"
os.makedirs(os.path.dirname(path))
open(path, 'w').close()
return path

@pytest.fixture
def existing_dirpath(tmpdir):
tmpdir.chdir()
dirpath = "path/to/directory"
dirpath = "tmp/path/to/directory"
os.makedirs(dirpath)
return dirpath

Expand All @@ -56,7 +56,7 @@ def it_deletes_existing_files(existing_path):
expect(os.path.exists(existing_path)).is_false()

def it_ignores_missing_files():
diskutils.delete("path/to/non/file")
diskutils.delete("tmp/path/to/non/file")

def it_deletes_directories(existing_dirpath):
diskutils.delete(existing_dirpath)
Expand Down

0 comments on commit d0bba3e

Please sign in to comment.