Skip to content

Commit

Permalink
Refactor Host constructor and tests
Browse files Browse the repository at this point in the history
The logic of creating a Host model from JSON dict has been scattered
between the Host DB model constructor and the deserialization method.
Moved everything to the deserializer without changing any logic.

Refactored the (de)serialization tests to not work around the bugs and
quirks of the Host model constructor:

* Invalid default values for account and display_name
* Inability to provide all fields as keyword arguments

Added tests for the ability to fill an empty string into ansible_host.
This is no longer done by a common trusted method.
  • Loading branch information
Glutexo committed Jul 31, 2019
1 parent 2c26d4c commit 9513d69
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 218 deletions.
27 changes: 0 additions & 27 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,6 @@ class Host(db.Model):
canonical_facts = db.Column(JSONB)
system_profile_facts = db.Column(JSONB)

def __init__(
self,
canonical_facts,
display_name=display_name,
ansible_host=None,
account=account,
facts=None,
system_profile_facts=None,
):

if not canonical_facts:
raise InventoryException(
title="Invalid request", detail="At least one of the canonical fact fields must be present."
)

self.canonical_facts = canonical_facts

if display_name:
# Only set the display_name field if input the display_name has
# been set...this will make it so that the "default" logic will
# get called during the save to fill in an empty display_name
self.display_name = display_name
self._update_ansible_host(ansible_host)
self.account = account
self.facts = facts
self.system_profile_facts = system_profile_facts or {}

def save(self):
db.session.add(self)

Expand Down
19 changes: 12 additions & 7 deletions app/serialization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from app.models import Host as ModelsHost
from app.exceptions import InputFormatException
from app.exceptions import InventoryException


__all__ = ("Host", "CanonicalFacts", "Facts")
Expand All @@ -9,14 +10,18 @@ class Host:
@classmethod
def from_json(cls, d):
canonical_facts = CanonicalFacts.from_json(d)
facts = Facts.from_json(d.get("facts"))
if not canonical_facts:
raise InventoryException(
title="Invalid request", detail="At least one of the canonical fact fields must be present."
)

return ModelsHost(
canonical_facts,
d.get("display_name", None),
d.get("ansible_host"),
d.get("account"),
facts,
d.get("system_profile", {}),
canonical_facts=canonical_facts,
display_name=d.get("display_name") or None,
ansible_host=d.get("ansible_host"), # Empty string allows a user to clear out
account=d.get("account"),
facts=Facts.from_json(d.get("facts")),
system_profile_facts=d.get("system_profile") or {},
)

@classmethod
Expand Down
Loading

0 comments on commit 9513d69

Please sign in to comment.