Skip to content

Commit

Permalink
Add fact retrieve endpoint with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevan Holdaway committed Jan 29, 2018
1 parent c4c1452 commit 5fb7a3a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion quipucords/api/fact/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

from api.models import FactCollection
from api.common.serializer import (NotEmptySerializer,
CustomJSONField)
CustomJSONField,
ChoiceField)


class FactCollectionSerializer(NotEmptySerializer):
"""Serializer for the FactCollection model."""

sources = CustomJSONField(required=True)
status = ChoiceField(
read_only=True, choices=FactCollection.FC_STATUS_CHOICES)

class Meta:
"""Meta class for FactCollectionSerializer."""
Expand Down
27 changes: 27 additions & 0 deletions quipucords/api/fact/tests_fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ def create_expect_201(self, data):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
return response.json()

def retrieve_expect_200(self, identifier):
"""Create a source, return the response as a dict."""
url = reverse('facts-detail', args=(identifier,))
response = self.client.get(url)

if response.status_code != status.HTTP_200_OK:
print('Failure cause: ')
print(response.json())
self.assertEqual(response.status_code, status.HTTP_200_OK)
return response.json()

################################################################
# Test Model Create
################################################################
Expand Down Expand Up @@ -216,3 +227,19 @@ def test_source_empty_facts(self):
self.assertEqual(
response_json['invalid_sources'][0]['errors']['facts'],
messages.FC_REQUIRED_ATTRIBUTE)

##############################################################
# Test Model Retrieve
##############################################################
def test_greenpath_retreive(self):
"""Retrieve fact collection object via API."""
request_json = {'sources':
[{'source_id': self.net_source.id,
'source_type': self.net_source.source_type,
'facts': [{'key': 'value'}]}]}

response_json = self.create_expect_201(
request_json)
identifier = response_json['id']
response_json = self.retrieve_expect_200(identifier)
self.assertEqual(response_json['id'], identifier)
4 changes: 3 additions & 1 deletion quipucords/api/fact/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
# pylint: disable=too-many-ancestors


class FactViewSet(mixins.CreateModelMixin,
class FactViewSet(mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet):
"""ModelViewSet to publish system facts."""

Expand All @@ -43,6 +44,7 @@ class FactViewSet(mixins.CreateModelMixin,

def create(self, request, *args, **kwargs):
"""Create a fact collection."""
# pylint: disable=unused-argument
# Validate incoming request body
has_errors, validation_result = validate_fact_collection_json(
request.data)
Expand Down

0 comments on commit 5fb7a3a

Please sign in to comment.