- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我的问题是我想建立一个单独的测试数据库,与我的开发数据库分开。应用程序本身与 Django-Rest-Framework 快速入门教程几乎相同,只是我使用 LDAP 后端。我的开发数据库使用 MySQL。我有单独的设置文件用于测试。
我在此处放置的完整错误回溯:http://dpaste.com/1W3TX1E ,但有趣的部分是:
sqlite3.OperationalError: no such table: auth_user
完整的测试设置在这里:http://dpaste.com/1K7KHK4 .我的相关设置是(安装的应用程序中缺少 pyldap 和 django-ldap-auth,它们是使用 pip 手动安装的):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'njord',
'permissions',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
目前,该应用程序基本上只使用 LDAP 进行身份验证。我认为这就是我出现错误的原因 - 当我进行测试时,我的数据库中没有任何内容 - 没有用户,没有组。如果用户通过 LDAP 服务器进行身份验证,则会在数据库中创建用户以及他/她所在的所有组,因此它是按需发生的。
什么有效:我的应用程序的开发版本适用于外部 LDAP,测试也适用于开发数据库。
什么不起作用:使用 sqllite3 内存数据库进行测试(我重复一遍,使用开发数据库和模拟 ldap 进行测试)。
我测试的代码在这里:
from django.test import TestCase, Client
import django
import ldap
from mockldap import MockLdap
class AuthenticationTests(TestCase):
"""
Set Up the structure ( Accounts, Groups ) and try authentication
"""
# Top level hierarchy
top = ('dc=ee', {'dc': ['ee']})
test = ('dc=test,dc=ee', {'dc': ['test']})
# Top level groups
people = ('ou=people,dc=test,dc=ee', {'ou': ['people'], 'objectClass': ['organizationalUnit', 'top']})
groups = ('ou=groups,dc=test,dc=ee', {'ou': ['groups'], 'objectClass': ['organizationalUnit', 'top']})
# Groups
admins = ('cn=admins,ou=groups,dc=test,dc=ee', {'cn': ['admins'], 'memberUid': ['admin'],
'objectClass': ['sambaGroupMapping', 'posixGroup', 'top'],
'gidNumber': ['1']})
group1 = ('cn=group1,ou=groups,dc=test,dc=ee', {'cn': ['group1'],
'memberUid': ['alice', 'bob'],
'objectClass': ['sambaGroupMapping', 'posixGroup', 'top']})
group2 = ('cn=group2,ou=groups,dc=test,dc=ee', {'cn': ['group2'], 'memberUid': ['admin', 'bob', 'karl'],
'objectClass': ['sambaGroupMapping', 'posixGroup', 'top']})
# Users
admin = ('uid=admin,ou=people,dc=test,dc=ee', {'uid': ['admin'], 'userPassword': ['ldaptest'], 'sn': ['Joe'],
'cn': ['Admin Joe'], 'mail': ['admin.joe@test.ee'],
'givenName': ['Admin'], 'objectClass':
['top', 'person', 'posixAccount', 'shadowAccount',
'inetOrgPerson', 'sambaSamAccount']})
alice = ('uid=alice,ou=people,dc=test,dc=ee', {'uid': ['alice'], 'userPassword': ['ldaptest'], 'sn': ['Cooper'],
'cn': ['Alice Cooper'], 'mail': ['alice.cooper@test.ee'],
'givenName': ['Alice'], 'objectClass':
['top', 'person', 'posixAccount', 'shadowAccount',
'inetOrgPerson', 'sambaSamAccount']})
bob = ('uid=bob,ou=people,dc=test,dc=ee', {'uid': ['bob'], 'userPassword': ['ldaptest'], 'sn': ['Marley'],
'cn': ['Bob Marley'], 'mail': ['bob.marley@test.ee'],
'givenName': ['Bob'], 'objectClass':
['top', 'person', 'posixAccount', 'shadowAccount',
'inetOrgPerson', 'sambaSamAccount']})
karl = ('uid=karl,ou=people,dc=test,dc=ee', {'uid': ['karl'], 'userPassword': ['ldaptest'], 'sn': ['Suur'],
'cn': ['Karl Suur'], 'mail': ['karl.suur@test.ee'],
'givenName': ['Karl'], 'objectClass':
['top', 'person', 'posixAccount', 'shadowAccount',
'inetOrgPerson', 'sambaSamAccount']})
# This is the content of our mock LDAP directory. It takes the form
# {dn: {attr: [value, ...], ...}, ...}.
directory = dict([top, test, people, groups, admins, group1, group2, admin, alice, bob, karl])
@classmethod
def setUpTestData(cls):
# We only need to create the MockLdap instance once. The content we
# pass in will be used for all LDAP connections.
cls.mockldap = MockLdap(cls.directory)
@classmethod
def tearDownClass(cls):
del cls.mockldap
def setUp(self):
# Patch ldap.initialize
django.setup()
self.mockldap.start()
self.ldapobj = self.mockldap['ldap://localhost/']
def tearDown(self):
# Stop patching ldap.initialize and reset state.
self.mockldap.stop()
del self.ldapobj
def test_some_basic_mockldap_auth(self):
searchStr = 'uid=alice,ou=people,dc=test,dc=ee'
results = _do_simple_ldap_search(searchStr)
ldapName = results[0][0]
ldapPropDict = results[0][1]
self.assertEqual(searchStr, ldapName)
self.assertEqual(len(ldapPropDict), 7)
def test_index_visible_for_all(self):
c = Client()
response = c.get("/")
self.assertContains(response, "/users/", 1)
self.assertContains(response, "/groups/", 1)
def test_login(self):
c = Client()
response = c.post("/api-auth/login/", {'username': 'bob', 'password': 'ldaptest'})
print(response.status_code)
response = c.get("/users/")
print(response._container)
def _do_simple_ldap_search(searchStr):
conn = ldap.initialize('ldap://localhost/')
conn.simple_bind_s(searchStr, 'ldaptest')
results = conn.search_s(searchStr, ldap.SCOPE_SUBTREE, )
return results
代码也可在此处获得:http://dpaste.com/3D2H4NK (语法高亮)。
我不确定是什么问题。我唯一能想到的是,因为创建时数据库中没有用户,所以没有创建数据库,但我不确定。任何帮助深表感谢。
我已经完成了所有的迁移。
(venv)user@box:~/workspace/fileshare/njord$ python manage.py showmigrationsadmin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
(venv)user@box:~/workspace/fileshare/njord$ python manage.py makemigrationsNo changes detected
(venv)user@box:~/workspace/fileshare/njord$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, sessions, auth, contenttypes
Running migrations:
No migrations to apply.
最佳答案
运行
python manage.py makemigrations <appname>
对于 INSTALLED_APPS
中的每个应用程序.(特别是在 auth_user
上有 ForeignKey 字段的应用程序)
我遇到了不同的错误让我来到这里,但我认为原因是一样的。
django.db.utils.OperationalError: (1005, "Can't create table '<test_db>.#sql-3821_1c9' (errno: 150)")
在这两种情况下,所有表都与 auth
相关模块仅在测试时不创建,不执行 makemigrations
命令。
我从 here 找到了解决方案
关于Django 在单独的数据库上测试给出 "OperationalError: no such table: auth_user",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390997/
我有一个 Pandas 数据框,如下所示。 activity User_Id \ 0 VIEWED MOVIE 158d292ec18a49 1 VIEWED
我正在尝试在运行 12.04 的 Ubuntu 机器上使用 mysql 创建一个远程数据库。 它有一个启用了远程登录的根用户。我已经启动了服务器。 输出 sudo netstat -tap | gre
这个问题在这里已经有了答案: sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file Non
我正在尝试使用 SQLAlchemy(使用 Flask)创建模型,但我无法在互联网上的任何地方找到解决我的问题的方法。 它给我的错误是 sqlalchemy.exc.OperationalError:
我收到以下错误: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: s_amodel [SQL: '
我搜索了很多这个错误,但我只找到了一些背后有更多信息的信息,比如“FATAL: ...”。我的没有。它只是说 sqlalchemy.exc.OperationalError: (psycopg2.Op
我在使用 flask_alchemy 进行单元测试时遇到问题 生产环境我用的是postgresql数据库 "SQLALCHEMY_DATABASE_URI": "postgresql://login:
运行.py文件: from flaskblog import app, db if __name__ == '__main__': db.create_all() app.run(de
我已在本地成功运行我的 Flask 应用程序,但是当我将其部署到我的产品环境时,出现此错误: [2019-08-26 00:15:36,229] ERROR in app: Exception on
服务 我的服务基于flask + postgresql + gunicorn + supervisor + nginx docker部署的时候,运行服务后,再访问api,有时候会报错,有时候还可以。
我定义了表名 users_table 并运行 db.create_all() 来创建表,但是在提交更新用户信息时出现错误“no such table user_table”。 我如何测试: (unde
我目前正在第一次编写一些 Airflow DAG 完整性测试。我遇到了一个错误,我的一些运算符(operator)/任务引用了 Airflow 变量,例如: test_var= Variable.ge
我正在尝试通过 flask 中的 sqlalchemy 执行原始 sql 查询。在 psql 中运行时,原始 sql 查询会给出正确的输出。 postgres=# SELECT distin
import requests import time import csv import ast import sys import mysql.connector config = { 'user
我用了很长时间的Postgres 14和PGAdmin4 6.7,一切都很好。这是在Windows11上。昨天我试着升级到Postgres 15和PGAdmin4 7.6,但这就是它失败的地方!。Po
错误: sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication fai
我正在使用 Python、Flask 和 SQLAlchemy。我在构建应用程序时一直使用本地数据库,并且它在以下代码中运行良好: from flask import Flask from flask
我正在尝试使用 mysql 来运行 python-flask Web 应用程序。但是,我在主应用程序文件中连接到数据库,每次尝试将数据输入数据库(即注册客户)时,我都会收到错误消息: Tracebac
我想从当前数据库切换到另一个 MySQL 数据库(从后台)。所以,我想这与我设置数据库设置的方式有关。 engine = create_engine('mysql+mysqldb://user:pw@
我在 Google AppEngine 上使用带有 Flask 的应用程序,我使用此常量 SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@ip_address
我是一名优秀的程序员,十分优秀!