Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import sys
- import inspect
- from itertools import chain
- from rest_framework.test import APITestCase
- from mixer.backend.django import mixer
- from django.db import connection
- class ModelTests(APITestCase):
- def test_models(self):
- for name, model in inspect.getmembers(sys.modules['api.models']):
- if inspect.isclass(model):
- # skip many2many tables
- if '2' not in model.__name__:
- object_ = mixer.blend(model)
- object_.save()
- assert len(model.objects.all()) > 0, model
- django_fields = []
- for f in model._meta.get_fields():
- django_fields.append(f.attname if hasattr(f, 'attname') else f.name)
- with connection.cursor() as cursor:
- cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = %s;",
- [model._meta.db_table])
- columns_in_db = list([c.replace('__', '_') for c in chain.from_iterable(cursor.fetchall())])
- for f_name in columns_in_db:
- assert f_name in django_fields, (f_name, model)
Add Comment
Please, Sign In to add comment