From 23fbf956720710605439db60a0a28563da01239c Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Sat, 10 Mar 2018 12:24:56 -0500 Subject: [PATCH] Add a script to create mock data It's convenient to have a script to easily add mock data to test the API and model with. Let's do that. Change-Id: I0f108b19d35280faa472850bdeb9ac83f3faa505 --- README.rst | 4 ++ api/migrations/0001_initial.py | 2 +- standalone/mockdata.py | 68 ++++++++++++++++++++++++++++++++++ standalone/test.py | 13 ------- 4 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 standalone/mockdata.py delete mode 100644 standalone/test.py diff --git a/README.rst b/README.rst index d2831f6..b4d44d1 100644 --- a/README.rst +++ b/README.rst @@ -33,6 +33,10 @@ This is python3 only right now. # Run test server -> http://127.0.0.1:8000/api/v1/ tox -e runserver + # Create mock data + source .tox/runserver/bin/activate + python standalone/mockdata.py + # Run actual tests or get coverage tox -e pep8 tox -e py35 diff --git a/api/migrations/0001_initial.py b/api/migrations/0001_initial.py index 0ffba09..77bafa9 100644 --- a/api/migrations/0001_initial.py +++ b/api/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.3 on 2018-03-10 16:49 +# Generated by Django 2.0.3 on 2018-03-10 17:18 from django.db import migrations, models import django.db.models.deletion diff --git a/standalone/mockdata.py b/standalone/mockdata.py new file mode 100644 index 0000000..ec4183a --- /dev/null +++ b/standalone/mockdata.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# Copyright (c) 2018 Red Hat, Inc. +# +# This file is part of ARA Records Ansible. +# +# ARA is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# ARA is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ARA. If not, see . + +# Creates fake data in the database, bypassing the API. +import django +import hashlib +import os +import sys +from django.core import serializers + +parent_directory = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +sys.path.append(parent_directory) +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ara.settings') +django.setup() + +from api import models + +playbook, _ = models.Playbook.objects.get_or_create( + started='2016-05-06T17:20:25.749489-04:00', + path='/tmp/test.yml', + ansible_version='2.3.4', + completed=False, +) +print(serializers.serialize('json', + models.Playbook.objects.all(), + indent=2)) + +play, _ = models.Play.objects.get_or_create( + started='2016-05-06T17:20:25.749489-04:00', + name='Test play', + playbook=playbook, +) +print(serializers.serialize('json', + models.Play.objects.all(), + indent=2)) + +content = 'foo'.encode('utf8') +filecontent, _ = models.FileContent.objects.get_or_create( + contents=content, + sha1=hashlib.sha1(content).hexdigest() +) +print(serializers.serialize('json', + models.FileContent.objects.all(), + indent=2)) + +file, _ = models.File.objects.get_or_create( + playbook=playbook, + content=filecontent, + path='/tmp/anothertest.yml' +) +print(serializers.serialize('json', + models.File.objects.all(), + indent=2)) diff --git a/standalone/test.py b/standalone/test.py deleted file mode 100644 index bbc4c12..0000000 --- a/standalone/test.py +++ /dev/null @@ -1,13 +0,0 @@ -import os -import sys - -import django - -parent_directory = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -sys.path.append(parent_directory) -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ara.settings") -django.setup() - -from api import models - -print(models.Playbook.objects.all())