Skip to content

Commit

Permalink
Merge pull request #145 from jacebrowning/simplify-orm-tests
Browse files Browse the repository at this point in the history
Simplify ORM unit tests
  • Loading branch information
jacebrowning committed Dec 26, 2019
2 parents b0d1afc + 5894f82 commit bf57d3a
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions datafiles/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@

@dataclass
class MyClass:
foobar: int


@dataclass
class MyClass2:
foo: int
bar: int


def describe_manager():
@pytest.fixture
def manager():
model = create_model(MyClass, pattern='files/{self.foobar}.yml')
model = create_model(MyClass, pattern='files/{self.foo}.yml')
return Manager(model)

def describe_all():
Expand All @@ -37,27 +32,27 @@ def describe_get_or_none():
@patch('datafiles.mapper.Mapper.exists', True)
@patch('datafiles.mapper.Mapper.modified', False)
def when_file_exists(mock_load, expect, manager):
expect(manager.get_or_none(foobar=1)) == MyClass(foobar=1)
expect(manager.get_or_none(foo=1, bar=2)) == MyClass(foo=1, bar=2)
expect(mock_load.called) == True

@patch('datafiles.mapper.Mapper.exists', False)
def when_file_missing(expect, manager):
expect(manager.get_or_none(foobar=2)) == None
expect(manager.get_or_none(foo=3, bar=4)) == None

def describe_get_or_create():
@patch('datafiles.mapper.Mapper.load')
@patch('datafiles.mapper.Mapper.save')
@patch('datafiles.mapper.Mapper.exists', True)
@patch('datafiles.mapper.Mapper.modified', False)
def when_file_exists(mock_load, mock_save, expect, manager):
expect(manager.get_or_create(foobar=1)) == MyClass(foobar=1)
expect(manager.get_or_create(foo=1, bar=2)) == MyClass(foo=1, bar=2)
expect(mock_load.called) == False
expect(mock_save.called) == True

@patch('datafiles.mapper.Mapper.save')
@patch('datafiles.mapper.Mapper.exists', False)
def when_file_missing(mock_save, expect, manager):
expect(manager.get_or_create(foobar=2)) == MyClass(foobar=2)
expect(manager.get_or_create(foo=1, bar=2)) == MyClass(foo=1, bar=2)
expect(mock_save.called) == True

def describe_filter():
Expand All @@ -67,8 +62,6 @@ def when_no_files_exist(expect, manager):
expect(items) == []

@patch('datafiles.mapper.Mapper.exists', False)
def with_partial_positional_arguments(expect):
model = create_model(MyClass2, pattern='files/{self.foobar}.yml')
manager = Manager(model)
def with_partial_positional_arguments(expect, manager):
items = list(manager.filter(foo=1))
expect(items) == []

0 comments on commit bf57d3a

Please sign in to comment.