1

我有一个追随者class

class PlaylistManager(models.Manager):
    def add_playlist(self, name):
        playlist = Playlist(name=name)
        playlist.save()
        return playlist

class Playlist(models.Model):
    name = models.CharField(max_length=30)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField(default=False)
    objects = PlaylistManager() # is a customer manager

    class Meta:
        db_table = 'playlists'

test正在关注

PLAYLIST = 'playlist' # 8 characters
class PlaylistTest(TestCase):
    def insert_playlist(playlist=PLAYLIST):
        Playlist.objects.add_playlist(playlist)

    def test_add_one_video_to_playlist(self):
        self.insert_playlist()
        self.assertEqual(Playlist.objects.count(), 1, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))

当我测试这个时,我得到以下错误

DatabaseError: value too long for type character varying(30)


----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)
Destroying test database for alias 'default'...

问题
- 为什么它失败了?
- 当我做max_length=300并运行测试时,一切都很好,但这是不可取的。

更新

PostgreSQL用作后端


更新 1
这是测试中发生的事情:

PLAYLIST='playlist'
class PlaylistTest(TestCase):
    def insert_playlist(playlist=PLAYLIST):
        Playlist.objects.add_playlist(playlist)

改了我PlaylistManager的调试一下

class PlaylistManager(models.Manager):
    def add_playlist(self, name):
        logging.warn('type ' + str(type(name)))
        logging.warn('adding ' + str(dir(name)))
        playlist = Playlist(name=name)
        playlist.save()
        return playlist

案例-1:如果我说playlist=PLAYLIST
什么进入add_playlistPlaylistManager是一个PlaylistTest对象

Creating test database for alias 'default'...
WARNING:root:type <class 'vlists.apps.playlists.tests.PlaylistTest'>
WARNING:root:adding ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addSkip', '_baseAssertEqual', '_classSetupFailed', '_cleanups', '_deprecate', '_fixture_setup', '_fixture_teardown', '_formatMessage', '_getAssertEqualityFunc', '_post_teardown', '_pre_setup', '_resultForDoCleanups', '_testMethodDoc', '_testMethodName', '_truncateMessage', '_type_equality_funcs', '_urlconf_setup', '_urlconf_teardown', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertContains', 'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals', 'assertFalse', 'assertFieldOutput', 'assertFormError', 'assertGreater', 'assertGreaterEqual', 'assertHTMLEqual', 'assertHTMLNotEqual', 'assertIn', 'assertIs', 'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone', 'assertItemsEqual', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', 'assertNotContains', 'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance', 'assertNotRegexpMatches', 'assertNumQueries', 'assertQuerysetEqual', 'assertRaises', 'assertRaisesMessage', 'assertRaisesRegexp', 'assertRedirects', 'assertRegexpMatches', 'assertSequenceEqual', 'assertSetEqual', 'assertTemplateNotUsed', 'assertTemplateUsed', 'assertTrue', 'assertTupleEqual', 'assert_', 'client', 'client_class', 'countTestCases', 'debug', 'defaultTestResult', 'doCleanups', 'fail', 'failIf', 'failIfAlmostEqual', 'failIfEqual', 'failUnless', 'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises', 'failureException', 'id', 'insert_playlist', 'longMessage', 'maxDiff', 'restore_warnings_state', 'run', 'save_warnings_state', 'setUp', 'setUpClass', 'settings', 'shortDescription', 'skipTest', 'tearDown', 'tearDownClass', 'test_add_one_video_to_playlist']

所以它失败了

**Case -2 : if I say `Playlist.objects.add_playlist('name')`**  
PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
    def insert_playlist(playlist=PLAYLIST):
        Playlist.objects.add_playlist('name') # this line has changed

    def test_add_one_video_to_playlist(self):
        self.insert_playlist()
        self.assertEqual(Playlist.objects.count(), 1, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))  

一切看起来都不错!

Creating test database for alias 'default'...
WARNING:root:type <type 'str'>
WARNING:root:adding ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
.
----------------------------------------------------------------------
Ran 1 test in 0.007s

OK
Destroying test database for alias 'default'...

更大的问题

  • 为什么我们需要传递原始字符串?

我在这里感到困惑

4

1 回答 1

2

这对我来说是一场灾难,我做错了

我写的不正确和真实

PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
    def insert_playlist(playlist_name=PLAYLIST):
        Playlist.objects.add_playlist(playlist_name)

    def test_add_one_video_to_playlist(self):
        self.insert_playlist()
        self.assertEqual(Playlist.objects.count(), 1, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))

我忘了添加self到我的类方法中,这就是为什么一直在传递类实例

正确实施

PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
    def insert_playlist(self, playlist_name=PLAYLIST): # added self as first parameter here
        Playlist.objects.add_playlist(playlist_name)

    def test_add_one_video_to_playlist(self):
        self.insert_playlist()
        self.assertEqual(Playlist.objects.count(), 1, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))

事情开始正常了!感谢所有回答和思考问题的人

于 2012-07-18T17:20:36.877 回答