fixes fact jsonbfield for unit tests

This commit is contained in:
Chris Meyers
2016-11-21 17:25:18 -05:00
parent eeeea8ad88
commit ddd41e77be
6 changed files with 52 additions and 57 deletions

View File

@@ -97,7 +97,7 @@ def test_no_facts_db(hosts, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_basic_fields(hosts, fact_scans, get, user): def test_basic_fields(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
search = { search = {
'from': epoch, 'from': epoch,
@@ -115,7 +115,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
@pytest.mark.license_feature @pytest.mark.license_feature
def test_basic_options_fields(hosts, fact_scans, options, user): def test_basic_options_fields(hosts, fact_scans, options, user, monkeypatch_jsonbfield_get_db_prep_save):
hosts = hosts(host_count=1) hosts = hosts(host_count=1)
fact_scans(fact_scans=1) fact_scans(fact_scans=1)
@@ -131,7 +131,7 @@ def test_basic_options_fields(hosts, fact_scans, options, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_related_fact_view(hosts, fact_scans, get, user): def test_related_fact_view(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
(host, response) = setup_common(hosts, fact_scans, get, user, epoch=epoch) (host, response) = setup_common(hosts, fact_scans, get, user, epoch=epoch)
@@ -145,7 +145,7 @@ def test_related_fact_view(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_multiple_hosts(hosts, fact_scans, get, user): def test_multiple_hosts(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
(host, response) = setup_common(hosts, fact_scans, get, user, epoch=epoch, host_count=3) (host, response) = setup_common(hosts, fact_scans, get, user, epoch=epoch, host_count=3)
@@ -159,7 +159,7 @@ def test_multiple_hosts(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_param_to_from(hosts, fact_scans, get, user): def test_param_to_from(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
search = { search = {
'from': epoch - timedelta(days=10), 'from': epoch - timedelta(days=10),
@@ -176,7 +176,7 @@ def test_param_to_from(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_param_module(hosts, fact_scans, get, user): def test_param_module(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
search = { search = {
'module': 'packages', 'module': 'packages',
@@ -192,7 +192,7 @@ def test_param_module(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_param_from(hosts, fact_scans, get, user): def test_param_from(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
search = { search = {
'from': epoch + timedelta(days=1), 'from': epoch + timedelta(days=1),
@@ -208,7 +208,7 @@ def test_param_from(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_param_to(hosts, fact_scans, get, user): def test_param_to(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
search = { search = {
'to': epoch + timedelta(days=1), 'to': epoch + timedelta(days=1),
@@ -236,7 +236,7 @@ def _test_user_access_control(hosts, fact_scans, get, user_obj, team_obj):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_normal_user_403(hosts, fact_scans, get, user, team): def test_normal_user_403(hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_bob = user('bob', False) user_bob = user('bob', False)
response = _test_user_access_control(hosts, fact_scans, get, user_bob, team) response = _test_user_access_control(hosts, fact_scans, get, user_bob, team)
@@ -247,7 +247,7 @@ def test_normal_user_403(hosts, fact_scans, get, user, team):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_super_user_ok(hosts, fact_scans, get, user, team): def test_super_user_ok(hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_super = user('bob', True) user_super = user('bob', True)
response = _test_user_access_control(hosts, fact_scans, get, user_super, team) response = _test_user_access_control(hosts, fact_scans, get, user_super, team)
@@ -269,7 +269,7 @@ def test_user_admin_ok(organization, hosts, fact_scans, get, user, team):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_user_admin_403(organization, organizations, hosts, fact_scans, get, user, team): def test_user_admin_403(organization, organizations, hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_admin = user('johnson', False) user_admin = user('johnson', False)
org2 = organizations(1) org2 = organizations(1)
org2[0].admin_role.members.add(user_admin) org2[0].admin_role.members.add(user_admin)

View File

@@ -77,7 +77,7 @@ def test_no_fact_found(hosts, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_basic_fields(hosts, fact_scans, get, user): def test_basic_fields(hosts, fact_scans, get, user, monkeypatch_jsonbfield_get_db_prep_save):
hosts = hosts(host_count=1) hosts = hosts(host_count=1)
fact_scans(fact_scans=1) fact_scans(fact_scans=1)
@@ -100,7 +100,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_content(hosts, fact_scans, get, user, fact_ansible_json): def test_content(hosts, fact_scans, get, user, fact_ansible_json, monkeypatch_jsonbfield_get_db_prep_save):
(fact_known, response) = setup_common(hosts, fact_scans, get, user) (fact_known, response) = setup_common(hosts, fact_scans, get, user)
assert fact_known.host_id == response.data['host'] assert fact_known.host_id == response.data['host']
@@ -122,19 +122,19 @@ def _test_search_by_module(hosts, fact_scans, get, user, fact_json, module_name)
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_search_by_module_packages(hosts, fact_scans, get, user, fact_packages_json): def test_search_by_module_packages(hosts, fact_scans, get, user, fact_packages_json, monkeypatch_jsonbfield_get_db_prep_save):
_test_search_by_module(hosts, fact_scans, get, user, fact_packages_json, 'packages') _test_search_by_module(hosts, fact_scans, get, user, fact_packages_json, 'packages')
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_search_by_module_services(hosts, fact_scans, get, user, fact_services_json): def test_search_by_module_services(hosts, fact_scans, get, user, fact_services_json, monkeypatch_jsonbfield_get_db_prep_save):
_test_search_by_module(hosts, fact_scans, get, user, fact_services_json, 'services') _test_search_by_module(hosts, fact_scans, get, user, fact_services_json, 'services')
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.django_db @pytest.mark.django_db
def test_search_by_timestamp_and_module(hosts, fact_scans, get, user, fact_packages_json): def test_search_by_timestamp_and_module(hosts, fact_scans, get, user, fact_packages_json, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
module_name = 'packages' module_name = 'packages'
@@ -157,7 +157,7 @@ def _test_user_access_control(hosts, fact_scans, get, user_obj, team_obj):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_normal_user_403(hosts, fact_scans, get, user, team): def test_normal_user_403(hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_bob = user('bob', False) user_bob = user('bob', False)
response = _test_user_access_control(hosts, fact_scans, get, user_bob, team) response = _test_user_access_control(hosts, fact_scans, get, user_bob, team)
@@ -168,7 +168,7 @@ def test_normal_user_403(hosts, fact_scans, get, user, team):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_super_user_ok(hosts, fact_scans, get, user, team): def test_super_user_ok(hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_super = user('bob', True) user_super = user('bob', True)
response = _test_user_access_control(hosts, fact_scans, get, user_super, team) response = _test_user_access_control(hosts, fact_scans, get, user_super, team)
@@ -178,7 +178,7 @@ def test_super_user_ok(hosts, fact_scans, get, user, team):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_user_admin_ok(organization, hosts, fact_scans, get, user, team): def test_user_admin_ok(organization, hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_admin = user('johnson', False) user_admin = user('johnson', False)
organization.admin_role.members.add(user_admin) organization.admin_role.members.add(user_admin)
@@ -190,7 +190,7 @@ def test_user_admin_ok(organization, hosts, fact_scans, get, user, team):
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
@pytest.mark.ac @pytest.mark.ac
@pytest.mark.django_db @pytest.mark.django_db
def test_user_admin_403(organization, organizations, hosts, fact_scans, get, user, team): def test_user_admin_403(organization, organizations, hosts, fact_scans, get, user, team, monkeypatch_jsonbfield_get_db_prep_save):
user_admin = user('johnson', False) user_admin = user('johnson', False)
org2 = organizations(1) org2 = organizations(1)
org2[0].admin_role.members.add(user_admin) org2[0].admin_role.members.add(user_admin)

View File

@@ -26,7 +26,7 @@ def mock_feature_disabled(feature):
@pytest.mark.django_db @pytest.mark.django_db
def test_cleanup_granularity(fact_scans, hosts): def test_cleanup_granularity(fact_scans, hosts, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
hosts(5) hosts(5)
fact_scans(10, timestamp_epoch=epoch) fact_scans(10, timestamp_epoch=epoch)
@@ -40,7 +40,7 @@ def test_cleanup_granularity(fact_scans, hosts):
@pytest.mark.django_db @pytest.mark.django_db
def test_cleanup_older_than(fact_scans, hosts): def test_cleanup_older_than(fact_scans, hosts, monkeypatch_jsonbfield_get_db_prep_save):
''' '''
Delete half of the scans Delete half of the scans
''' '''
@@ -57,7 +57,7 @@ def test_cleanup_older_than(fact_scans, hosts):
@pytest.mark.django_db @pytest.mark.django_db
def test_cleanup_older_than_granularity_module(fact_scans, hosts): def test_cleanup_older_than_granularity_module(fact_scans, hosts, monkeypatch_jsonbfield_get_db_prep_save):
epoch = timezone.now() epoch = timezone.now()
hosts(5) hosts(5)
fact_scans(10, timestamp_epoch=epoch) fact_scans(10, timestamp_epoch=epoch)
@@ -71,7 +71,7 @@ def test_cleanup_older_than_granularity_module(fact_scans, hosts):
@pytest.mark.django_db @pytest.mark.django_db
def test_cleanup_logic(fact_scans, hosts): def test_cleanup_logic(fact_scans, hosts, monkeypatch_jsonbfield_get_db_prep_save):
''' '''
Reduce the granularity of half of the facts scans, by half. Reduce the granularity of half of the facts scans, by half.
''' '''

View File

@@ -39,7 +39,7 @@ def check_process_fact_message_module(fact_returned, data, module_name):
@pytest.mark.django_db @pytest.mark.django_db
def test_process_fact_message_ansible(fact_msg_ansible): def test_process_fact_message_ansible(fact_msg_ansible, monkeypatch_jsonbfield_get_db_prep_save):
receiver = FactCacheReceiver() receiver = FactCacheReceiver()
fact_returned = receiver.process_fact_message(fact_msg_ansible) fact_returned = receiver.process_fact_message(fact_msg_ansible)
@@ -47,7 +47,7 @@ def test_process_fact_message_ansible(fact_msg_ansible):
@pytest.mark.django_db @pytest.mark.django_db
def test_process_fact_message_packages(fact_msg_packages): def test_process_fact_message_packages(fact_msg_packages, monkeypatch_jsonbfield_get_db_prep_save):
receiver = FactCacheReceiver() receiver = FactCacheReceiver()
fact_returned = receiver.process_fact_message(fact_msg_packages) fact_returned = receiver.process_fact_message(fact_msg_packages)
@@ -55,7 +55,7 @@ def test_process_fact_message_packages(fact_msg_packages):
@pytest.mark.django_db @pytest.mark.django_db
def test_process_fact_message_services(fact_msg_services): def test_process_fact_message_services(fact_msg_services, monkeypatch_jsonbfield_get_db_prep_save):
receiver = FactCacheReceiver() receiver = FactCacheReceiver()
fact_returned = receiver.process_fact_message(fact_msg_services) fact_returned = receiver.process_fact_message(fact_msg_services)
@@ -63,7 +63,7 @@ def test_process_fact_message_services(fact_msg_services):
@pytest.mark.django_db @pytest.mark.django_db
def test_process_facts_message_ansible_overwrite(fact_scans, fact_msg_ansible): def test_process_facts_message_ansible_overwrite(fact_scans, fact_msg_ansible, monkeypatch_jsonbfield_get_db_prep_save):
''' '''
We pickypack our fact sending onto the Ansible fact interface. We pickypack our fact sending onto the Ansible fact interface.
The interface is <hostname, facts>. Where facts is a json blob of all the facts. The interface is <hostname, facts>. Where facts is a json blob of all the facts.

View File

@@ -4,6 +4,7 @@ import pytest
import mock import mock
import json import json
import os import os
import six
from datetime import timedelta from datetime import timedelta
# Django # Django
@@ -12,6 +13,8 @@ from django.utils.six.moves.urllib.parse import urlparse
from django.utils import timezone from django.utils import timezone
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from jsonbfield.fields import JSONField
# AWX # AWX
from awx.main.models.projects import Project from awx.main.models.projects import Project
@@ -476,3 +479,24 @@ def job_template_labels(organization, job_template):
return job_template return job_template
def dumps(value):
return DjangoJSONEncoder().encode(value)
# Taken from https://github.com/django-extensions/django-extensions/blob/54fe88df801d289882a79824be92d823ab7be33e/django_extensions/db/fields/json.py
def get_db_prep_save(self, value, connection, **kwargs):
"""Convert our JSON object to a string before we save"""
if value is None and self.null:
return None
# default values come in as strings; only non-strings should be
# run through `dumps`
if not isinstance(value, six.string_types):
value = dumps(value)
return value
@pytest.fixture
def monkeypatch_jsonbfield_get_db_prep_save(mocker):
JSONField.get_db_prep_save = get_db_prep_save

View File

@@ -1,29 +0,0 @@
import pytest
import six
from jsonbfield.fields import JSONField
from django.core.serializers.json import DjangoJSONEncoder
def dumps(value):
return DjangoJSONEncoder().encode(value)
# Taken from https://github.com/django-extensions/django-extensions/blob/54fe88df801d289882a79824be92d823ab7be33e/django_extensions/db/fields/json.py
def get_db_prep_save(self, value, connection, **kwargs):
"""Convert our JSON object to a string before we save"""
if value is None and self.null:
return None
# default values come in as strings; only non-strings should be
# run through `dumps`
if not isinstance(value, six.string_types):
value = dumps(value)
return value
@pytest.fixture
def monkeypatch_jsonbfield_get_db_prep_save(mocker):
JSONField.get_db_prep_save = get_db_prep_save