Skip to content

Commit

Permalink
test(itam): history entry tests for device software
Browse files Browse the repository at this point in the history
!27 #15
  • Loading branch information
jon-nfc committed Jun 15, 2024
1 parent da746b8 commit 1220ddb
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/itam/tests/device/test_device_core_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ def setUpTestData(self):
organization = self.organization
)

self.deleted_pk = self.item_delete.pk

self.item_delete.delete()

self.history_delete = History.objects.filter(
item_pk = self.item_delete.pk,
item_pk = self.deleted_pk,
item_class = self.model._meta.model_name,
)

self.history_delete_children = History.objects.filter(
item_parent_pk = self.item_delete.pk,
item_parent_pk = self.deleted_pk,
item_parent_class = self.model._meta.model_name,
)

Expand Down
325 changes: 325 additions & 0 deletions app/itam/tests/device_software/test_device_software_core_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@

import pytest
import unittest
import requests

from django.test import TestCase, Client

from access.models import Organization

from core.models.history import History

from itam.models.device import Device, DeviceSoftware

from itam.models.software import Software, SoftwareVersion

class TeamHistory(TestCase):


model = DeviceSoftware


@classmethod
def setUpTestData(self):
""" Setup Test """

organization = Organization.objects.create(name='test_org')

self.organization = organization

self.item_parent = Device.objects.create(
name = 'test_item_' + self.model._meta.model_name,
organization = self.organization,
)

self.item_software = Software.objects.create(
name = 'test_item_' + self.model._meta.model_name,
organization = self.organization,
)

self.item_software_version = SoftwareVersion.objects.create(
name = 'test_item_' + self.model._meta.model_name,
organization = self.organization,
software = self.item_software,
)

self.item_create = self.model.objects.create(
installedversion = self.item_software_version,
organization = self.organization,
software = self.item_software,
device = self.item_parent,
)


self.history_create = History.objects.get(
action = History.Actions.ADD[0],
item_pk = self.item_create.pk,
item_class = self.model._meta.model_name,
)


self.item_software_version_changed = SoftwareVersion.objects.create(
name = 'test_item_changed' + self.model._meta.model_name,
organization = self.organization,
software = self.item_software,
)

self.item_change = self.item_create
self.item_change.installedversion = self.item_software_version_changed
self.item_change.save()

self.history_change = History.objects.get(
action = History.Actions.UPDATE[0],
item_pk = self.item_change.pk,
item_class = self.model._meta.model_name,
)




self.item_software_delete = Software.objects.create(
name = 'test_item_delete_' + self.model._meta.model_name,
organization = self.organization,
)

self.item_software_version_delete = SoftwareVersion.objects.create(
name = 'test_item_delete_' + self.model._meta.model_name,
organization = self.organization,
software = self.item_software,
)

self.item_delete = self.model.objects.create(
installedversion = self.item_software_version_delete,
organization = self.organization,
software = self.item_software_delete,
device = self.item_parent,
)

self.deleted_pk = self.item_delete.pk

self.item_delete.delete()

self.history_delete = History.objects.get(
action = History.Actions.DELETE[0],
item_pk = self.deleted_pk,
item_class = self.model._meta.model_name,
)

self.history_delete_children = History.objects.filter(
item_parent_pk = self.deleted_pk,
item_parent_class = self.model._meta.model_name,
)



def test_history_entry_item_add_field_action(self):
""" Ensure action is "add" for item creation """

history = self.history_create.__dict__

assert history['action'] == int(History.Actions.ADD[0])
# assert type(history['action']) is int


@pytest.mark.skip(reason="figure out best way to test")
def test_history_entry_item_add_field_after(self):
""" Ensure after field contains correct value """

history = self.history_create.__dict__

assert history['after'] == str('{}')
# assert type(history['after']) is str


def test_history_entry_item_add_field_before(self):
""" Ensure before field is an empty JSON string for create """

history = self.history_create.__dict__

assert history['before'] == str('{}')
# assert type(history['before']) is str


def test_history_entry_item_add_field_item_pk(self):
""" Ensure history entry field item_pk is the created items pk """

history = self.history_create.__dict__

assert history['item_pk'] == self.item_create.pk
# assert type(history['item_pk']) is int


def test_history_entry_item_add_field_item_class(self):
""" Ensure history entry field item_class is the model name """

history = self.history_create.__dict__

assert history['item_class'] == self.model._meta.model_name
# assert type(history['item_class']) is str


def test_history_entry_item_add_field_parent_pk(self):
""" Ensure history entry field parent_pk is the created parents pk """

history = self.history_create.__dict__

assert history['item_parent_pk'] == self.item_parent.pk
# assert type(history['parentpk']) is int


def test_history_entry_item_add_field_parent_class(self):
""" Ensure history entry field parent_class is the model name """

history = self.history_create.__dict__

assert history['item_parent_class'] == self.item_parent._meta.model_name
# assert type(history['item_class']) is str




################################## Change ##################################




def test_history_entry_item_change_field_action(self):
""" Ensure action is "add" for item creation """

history = self.history_change.__dict__

assert history['action'] == int(History.Actions.UPDATE[0])
# assert type(history['action']) is int


def test_history_entry_item_change_field_after(self):
""" Ensure after field contains correct value """

history = self.history_change.__dict__

assert history['after'] == str('{"installedversion_id": ' + str(self.item_software_version_changed.pk) + '}')
# assert type(history['after']) is str


@pytest.mark.skip(reason="figure out best way to test")
def test_history_entry_item_change_field_before(self):
""" Ensure before field is an empty JSON string for create """

history = self.history_change.__dict__

assert history['before'] == str('{}')
# assert type(history['before']) is str


def test_history_entry_item_change_field_item_pk(self):
""" Ensure history entry field item_pk is the created items pk """

history = self.history_change.__dict__

assert history['item_pk'] == self.item_create.pk
# assert type(history['item_pk']) is int


def test_history_entry_item_change_field_item_class(self):
""" Ensure history entry field item_class is the model name """

history = self.history_change.__dict__

assert history['item_class'] == self.model._meta.model_name
# assert type(history['item_class']) is str


def test_history_entry_item_change_field_parent_pk(self):
""" Ensure history entry field parent_pk is the created parent pk """

history = self.history_change.__dict__

assert history['item_parent_pk'] == self.item_parent.pk
# assert type(history['item_pk']) is int


def test_history_entry_item_change_field_parent_class(self):
""" Ensure history entry field parent_class is the model name """

history = self.history_change.__dict__

assert history['item_parent_class'] == self.item_parent._meta.model_name
# assert type(history['item_class']) is str




################################## Delete ##################################




def test_history_entry_item_delete_children_entries_not_exist(self):
""" When an item is deleted, it's children history entries must be removed """

assert self.history_delete_children.exists() is False


def test_history_entry_item_delete_field_action(self):
""" Ensure action is "add" for item creation """

history = self.history_delete.__dict__

assert history['action'] == int(History.Actions.DELETE[0])
# assert type(history['action']) is int


def test_history_entry_item_delete_field_after(self):
""" Ensure after field contains correct value """

history = self.history_delete.__dict__

assert history['after'] == None
# assert type(history['after']) is str


@pytest.mark.skip(reason="figure out best way to test")
def test_history_entry_item_delete_field_before(self):
""" Ensure before field is an empty JSON string for create """

history = self.history_delete.__dict__

assert history['before'] == str('{}')
# assert type(history['before']) is str


def test_history_entry_item_delete_field_item_pk(self):
""" Ensure history entry field item_pk is the created items pk """

history = self.history_delete.__dict__

assert history['item_pk'] == self.deleted_pk
# assert type(history['item_pk']) is int


def test_history_entry_item_delete_item_class(self):
""" Ensure history entry field item_class is the model name """

history = self.history_delete.__dict__

assert history['item_class'] == self.model._meta.model_name
# assert type(history['item_class']) is str



def test_history_entry_item_delete_field_parent_pk(self):
""" Ensure history entry field item_pk is the created parents pk """

history = self.history_delete.__dict__

assert history['item_parent_pk'] == self.item_parent.pk
# assert type(history['item_pk']) is int


def test_history_entry_item_delete_field_parent_class(self):
""" Ensure history entry field parent_class is the model name """

history = self.history_delete.__dict__

assert history['item_parent_class'] == self.item_parent._meta.model_name
# assert type(history['item_class']) is str

0 comments on commit 1220ddb

Please sign in to comment.