'Ensure that documents may be created with custom primary keys.'

def test_custom_id_field(self):

    class User(Document, ):
        username = StringField(primary_key=True)
        name = StringField()
        meta = {'allow_inheritance': True}
    User.drop_collection()
    self.assertEqual(User._fields['username'].db_field, '_id')
    self.assertEqual(User._meta['id_field'], 'username')
    self.assertRaises(ValidationError, User(name='test').save)
    with self.assertRaises(ValueError):
        class EmailUser(User, ):
            email = StringField(primary_key=True)
    class EmailUser(User, ):
        email = StringField()
    user = User(username='test', name='test    user')
    user.save()
    user_obj = User.objects.first()
    self.assertEqual(user_obj.id, 'test')
    self.assertEqual(user_obj.pk, 'test')
    user_son = User.objects._collection.find_one()
    self.assertEqual(user_son['_id'], 'test')
    self.assertTrue(('username' not in user_son['_id']))
    User.drop_collection()
    user = User(pk='mongo', name='mongo    user')
    user.save()
    user_obj = User.objects.first()
    self.assertEqual(user_obj.id, 'mongo')
    self.assertEqual(user_obj.pk, 'mongo')
    user_son = User.objects._collection.find_one()
    self.assertEqual(user_son['_id'], 'mongo')
    self.assertTrue(('username' not in user_son['_id']))
