我有一个追随者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_playlist
的PlaylistManager
是一个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'...
更大的问题
- 为什么我们需要传递原始字符串?
我在这里感到困惑