一般来说,我是数据库的新手,如果这个问题太基础了,请抱歉。
我正在尝试测试与Playlist
模型有关系的Track
模型。
class Track(db.Model):
"""
Model for storing track information
"""
id = db.Column(db.Integer, primary_key=True)
artist = db.Column(db.String(140))
title = db.Column(db.String(140))
created = db.Column(db.DateTime, default=func.now())
track_id = db.Column(db.String(140))
tempo = db.Column(db.Float)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, title, artist, uri):
self.title = title
self.artist = artist
self.uri = uri
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'id' : self.id,
'title': self.title,
'artist': self.artist,
'track_id': self.track_id,
'tempo':self.tempo
}
然后有这个辅助表:
tracks = db.Table('tracks',
db.Column('track_id', db.Integer, db.ForeignKey('track.id')),
db.Column('playlist_id', db.Integer, db.ForeignKey('playlist.id')))
这里是Playlist
模型:
class Playlist(db.Model):
"""
Model for storing playlist information belonging to a specific user
"""
__tablename__ = 'playlist'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
created = db.Column(db.DateTime, default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
tracks = db.relationship('Track',
secondary=tracks,
backref=db.backref('playlists',
lazy='dynamic'))
def __init__(self, title):
self.title = title
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'id' : self.id,
'created' : self.created,
'title': self.title,
'tracks': [ item.serialize for item in self.tracks]
}
现在我正在尝试测试Playlist
,就像这样:
test_playlist_model.py
import unittest
# third party libs
from sqlalchemy.exc import IntegrityError
# application modules
from project.api.models.base import db
from project.api.models.all import Playlist, tracks
from project.tests.base import BaseTestCase
from project.tests.utils import add_playlist
class TestPlaylistModel(BaseTestCase):
def test_add_track(self):
track = add_track(title='Karma Police',
artist='Radiohead',
uri='spofity:test')
self.assertEqual(track.title, 'Karma Police')
self.assertEqual(track.artist, 'Radiohead')
self.assertEqual(track.uri, 'spofity:test')
def test_add_playlist(self):
track = add_track(title='Karma Police',
artist='Radiohead',
uri='spofity:test')
playlist = add_playlist(title='Alternative')
self.assertEqual(playlist.title, 'Alternative')
self.assertTrue(playlist.id)
self.assertEqual(playlist.tracks, track.serialize())
if __name__ == '__main__':
unittest.main()
utils.py
from project.api.models.base import db
from project.api.models.all import Playlist
def add_playlist(title):
playlist = Playlist(
title=title)
db.session.add(playlist)
db.session.commit()
return playlist
def add_track(title, artist, uri):
track = Track(
title=title,
artist=artist,
uri=uri
)
db.session.add(track)
db.session.commit()
return track
但是我得到了:
======================================================================
ERROR: test_add_playlist (test_playlist_model.TestPlaylistModel)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/project/tests/test_playlist_model.py", line 34, in test_add_playlist
self.assertEqual(playlist.tracks, track.serialize())
TypeError: 'dict' object is not callable
我想念什么?
答案 0 :(得分:1)
您的方法Track.serialize
实际上是一个属性,这意味着当您访问它时,它会返回一个字典,就像它是一个属性一样。
您应该将括号放在track.serialize
之后(并考虑将属性重命名为track.serialized
以避免混淆)或删除@property
装饰器。
class Playlist(db.Model):
...
def serialize(self):
...
class Track(db.Model):
...
def serialize(self):
...