Skip to content

Commit

Permalink
Added collection import tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzeman committed Feb 27, 2017
1 parent aabcb1c commit 942a57e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 18 deletions.
64 changes: 46 additions & 18 deletions tests/collection/basic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@
from byte import Collection, Model, Property


def test_simple_dynamic():
"""Test collections with dynamic models."""
def test_import_generator():
"""Test collection import generator."""
class Artist(Model):
class Options:
collection = Collection()

id = Property(int, primary_key=True)

name = Property(str)

# Import items into collections
assert list(Artist.Objects.import_items(
[{
'id': 1,
'name': 'Daft Punk'
}],
generator=True
)) == [
1
]

# Fetch artist, and validate properties
artist = Artist.Objects.get(1)

assert artist.id == 1
assert artist.name == 'Daft Punk'


def test_import_relations():
"""Test collection import with relations."""
class Artist(Model):
class Options:
collection = Collection()
Expand Down Expand Up @@ -32,26 +60,26 @@ class Options:

name = Property(str)

# Create objects
Artist.create(
id=1,
name='Daft Punk'
)
# Import items into collections
Artist.Objects.import_items([{
'id': 1,
'name': 'Daft Punk'
}])

Album.create(
id=1,
artist_id=1,
Album.Objects.import_items([{
'id': 1,
'artist_id': 1,

title='Discovery'
)
'title': 'Discovery'
}])

Track.create(
id=1,
artist_id=1,
album_id=1,
Track.Objects.import_items([{
'id': 1,
'artist_id': 1,
'album_id': 1,

name='One More Time'
)
'name': 'One More Time'
}])

# Fetch track, and ensure relations can be resolved
track = Track.Objects.get(1)
Expand Down
64 changes: 64 additions & 0 deletions tests/collection/relation_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Basic tests for collections."""

from byte import Collection, Model, Property


def test_simple_dynamic():
"""Test collection relations with dynamic models."""
class Artist(Model):
class Options:
collection = Collection()

id = Property(int, primary_key=True)

name = Property(str)

class Album(Model):
class Options:
collection = Collection()

id = Property(int, primary_key=True)
artist = Property(Artist)

title = Property(str)

class Track(Model):
class Options:
collection = Collection()

id = Property(int, primary_key=True)
artist = Property(Artist)
album = Property(Album)

name = Property(str)

# Create objects
Artist.create(
id=1,
name='Daft Punk'
)

Album.create(
id=1,
artist_id=1,

title='Discovery'
)

Track.create(
id=1,
artist_id=1,
album_id=1,

name='One More Time'
)

# Fetch track, and ensure relations can be resolved
track = Track.Objects.get(1)

assert track.id == 1

assert track.artist.id == 1

assert track.album.id == 1
assert track.album.artist.id == 1

0 comments on commit 942a57e

Please sign in to comment.