Guest User

Untitled

a guest
Nov 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import inspect
  4.  
  5. from itertools import chain
  6. from rest_framework.test import APITestCase
  7. from mixer.backend.django import mixer
  8. from django.db import connection
  9.  
  10.  
  11. class ModelTests(APITestCase):
  12. def test_models(self):
  13. for name, model in inspect.getmembers(sys.modules['api.models']):
  14. if inspect.isclass(model):
  15. # skip many2many tables
  16. if '2' not in model.__name__:
  17. object_ = mixer.blend(model)
  18. object_.save()
  19. assert len(model.objects.all()) > 0, model
  20.  
  21. django_fields = []
  22. for f in model._meta.get_fields():
  23. django_fields.append(f.attname if hasattr(f, 'attname') else f.name)
  24.  
  25. with connection.cursor() as cursor:
  26. cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = %s;",
  27. [model._meta.db_table])
  28. columns_in_db = list([c.replace('__', '_') for c in chain.from_iterable(cursor.fetchall())])
  29.  
  30. for f_name in columns_in_db:
  31. assert f_name in django_fields, (f_name, model)
Add Comment
Please, Sign In to add comment