Changeset - a960569196d0
[Not reviewed]
default
0 2 0
Branko Majic (branko) - 10 years ago 2013-11-09 19:00:25
branko@majic.rs
CONNT-17: PEP8 styling fix. Added tests for the communications summary method.
2 files changed with 59 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conntrackt/models.py
Show inline comments
 
@@ -175,25 +175,25 @@ class Project(RelatedCollectorMixin, mod
 
        entity_colors = dict(zip(entity_ids, colors))
 

	
 
        # Set-up an empty list where the resulting data will be stored.
 
        communications = []
 

	
 
        # Process each communication, and add the information to result.
 
        for communication in Communication.objects.filter(source__entity__project=self).select_related().order_by("source__entity__pk"):
 
            communications.append({"source": communication.source.entity.name,
 
                                   "source_color": entity_colors[communication.source.entity.pk],
 
                                   "destination": communication.destination.entity.name,
 
                                   "destination_color": entity_colors[communication.destination.entity.pk],
 
                                   "protocol": communication.protocol,
 
                                   "port": communication.port,})
 
                                   "port": communication.port})
 

	
 
        # Finally return the result.
 
        return communications
 

	
 

	
 
class Location(RelatedCollectorMixin, models.Model):
 
    """
 
    Implements a model with information about location. Locations can further be
 
    assigned to entities, letting the user group different servers and equipment
 
    based on location.
 

	
 
    Locations are not tied to specific project, and they do not have to be
conntrackt/tests/test_models.py
Show inline comments
 
@@ -14,24 +14,25 @@
 
# 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
 
# Django Conntrackt.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
# Standard Python library import.
 
import collections
 

	
 
# Python third-party library imports.
 
import mock
 
from palette import Color
 

	
 
# Django imports.
 
from django.core.exceptions import ValidationError
 
from django.db import IntegrityError
 
from django.db.models import Model
 
from django.test import TestCase
 

	
 
# Application imports.
 
from conntrackt.models import Project, Location, Entity, Interface, Communication
 
from conntrackt.models import SearchManager
 
from conntrackt.models import NestedObjects
 
from conntrackt.utils import list_formatter_callback
 
@@ -170,24 +171,81 @@ class ProjectTest(TestCase):
 

	
 
        project = ProjectFactory(pk=1)
 

	
 
        self.assertEqual(project.get_absolute_url(), "/conntrackt/project/1/")
 

	
 
    def test_custom_manager(self):
 
        """
 
        Tests if the custom manager is being used.
 
        """
 

	
 
        self.assertIsInstance(Project.objects, SearchManager)
 

	
 
    def test_get_project_communications_summary_count(self):
 
        """
 
        Test if the method returns correct number of entries in the list.
 
        """
 

	
 
        # Set-up some test data to work with.
 
        setup_test_data()
 

	
 
        # Get the first project from test data.
 
        project = Project.objects.get(pk=1)
 

	
 
        # Fetch the project communications
 
        communications = project.get_project_communications_summary()
 

	
 
        # Validate the number of returned communications.
 
        self.assertEqual(len(communications), 6)
 

	
 
    def test_get_project_communications_summary_return_value(self):
 
        """
 
        Test if the method returns correct type.
 
        """
 

	
 
        # Set-up some test data.
 
        setup_test_data()
 

	
 
        # Fetch one of the projects.
 
        project = Project.objects.get(pk=1)
 

	
 
        # Get the communications summary for the project.
 
        communications = project.get_project_communications_summary()
 

	
 
        # Validate the return value type.
 
        self.assertIsInstance(communications, list)
 

	
 
        # Perform verification on every summary element returned.
 
        for comm in communications:
 
            # Verify the type of element and its size.
 
            self.assertIsInstance(comm, dict)
 
            self.assertEqual(len(comm), 6)
 

	
 
            # Verify the presence of correct dictionary keys.
 
            keys = comm.keys()
 
            self.assertIn("source", keys)
 
            self.assertIn("source_color", keys)
 
            self.assertIn("destination", keys)
 
            self.assertIn("destination_color", keys)
 
            self.assertIn("protocol", keys)
 
            self.assertIn("port", keys)
 

	
 
            # Verify the value types.
 
            self.assertIsInstance(comm["source"], unicode)
 
            self.assertIsInstance(comm["source_color"], Color)
 
            self.assertIsInstance(comm["destination"], unicode)
 
            self.assertIsInstance(comm["destination_color"], Color)
 
            self.assertIsInstance(comm["protocol"], unicode)
 
            self.assertIsInstance(comm["port"], int)
 

	
 

	
 
class LocationTest(TestCase):
 

	
 
    def test_unique_name(self):
 
        """
 
        Test if unique location name is enforced.
 
        """
 

	
 
        location = LocationFactory()
 

	
 
        self.assertRaises(IntegrityError, LocationFactory, name=location.name)
 

	
0 comments (0 inline, 0 general)