import unittest
import modules
import os
import time
import random

class EntityTestCase(unittest.TestCase):
    """Test new_entity function."""

    def setUp(self):

        # simulating an entity ID
        self.new_entity_id = str(random.randint(100000, 999999))

        # defining path structure (today) and template locations
        self.path = 'entities/' + time.strftime('%Y/%m/%d')
        self.id_full_path = self.path + '/' + self.new_entity_id + '.md'
        self.draft_location = self.path + '/draft_entity.md'
        self.am_template_location = 'templates/am_template.md'
        self.am_pep_template_location = 'templates/am_pep_template.md'
        self.empty_template_location = 'templates/empty_template.md'

        # opening templates to read them later and compare them
        with open(self.empty_template_location) as empty_temp_object:
            self.empty_template = empty_temp_object.read()
        with open(self.am_template_location) as am_temp_object:
            self.am_template = am_temp_object.read()
        with open(self.am_pep_template_location) as am_pep_temp_object:
            self.am_pep_template = am_pep_temp_object.read()

    # uncomment this section to test new file creations
    # but it can clutter the folders when ran too many times
    #def test_file_creation(self):
    #    """Test random id file and empty file."""
    #    modules.io.create_entity(self.new_entity_id)
    #    # check if file exist
    #    self.assertTrue(os.path.isfile(self.id_full_path))
    #    self.assertEqual(self.id_full_path, self.id_full_path)

    def test_empty_file(self):
        """Test if empty file is named correctly."""
        modules.io.create_entity()
        self.assertTrue(self.draft_location)

    def test_created_templates(self):
        """Test if all template creations are flushed correctly."""

        # create skeleton draft and open it to compare it
        modules.io.create_entity()
        with open(self.draft_location) as draft_location_object:
            draft_template = draft_location_object.read()
        self.assertEqual(self.empty_template, draft_template)

#    def test_am_template(self):
#        """Test if adverse media template works."""
#        modules.io.create_entity(AM = True)

#        with open('templates/am_template.md') as am_filename:
#            am_template = am_filename.read()

#        with open(self.draft_path) as new_am_filename:
#            new_am_template = new_am_filename.read()

#        self.assertEqual(am_template, new_am_template)

unittest.main()

@@ -60,17 +60,16 @@
  random%0A
-%0A
 class En
@@ -133,25 +133,24 @@
 unction.%22%22%22%0A
-%0A
     def setU
@@ -720,16 +720,81 @@
 late.md'
+%0A        self.pep_template_location = 'templates/pep_template.md'
 %0A%0A      
@@ -1229,16 +1229,137 @@
 t.read()
+%0A        with open(self.pep_template_location) as pep_temp_object:%0A            self.pep_template = pep_temp_object.read()
 %0A%0A    # 
@@ -2004,27 +2004,17 @@
 ate 
+are 
 creat
-ions are flush
 ed c
@@ -2026,17 +2026,16 @@
 tly.%22%22%22%0A
-%0A
         
@@ -2309,60 +2309,309 @@
 e)%0A%0A
-#    def test_am_template(self):%0A#        %22%22%22Test if
+        # create PEP template%0A        modules.io.create_entity(entity_PEP = True, entity_AM = False)%0A        with open(self.draft_location) as draft_location_object:%0A            draft_template = draft_location_object.read()%0A        self.assertEqual(self.pep_template, draft_template)%0A%0A        # create
  adv
@@ -2633,20 +2633,9 @@
 late
- works.%22%22%22%0A#
+%0A
     
@@ -2663,16 +2663,23 @@
 _entity(
+entity_
 AM = Tru
@@ -2681,18 +2681,16 @@
 = True)%0A
-%0A#
         
@@ -2703,53 +2703,55 @@
 pen(
-'templates/am_template.md') as am_filename
+self.draft_location) as draft_location_object
 :%0A
-#
     
@@ -2758,18 +2758,21 @@
         
-am
+draft
 _templat
@@ -2775,35 +2775,45 @@
 plate = 
-am_filename
+draft_location_object
 .read()%0A
 %0A#      
@@ -2804,26 +2804,24 @@
 .read()%0A
-%0A#
         
 with ope
@@ -2816,164 +2816,300 @@
     
-with open(self.draft_path) as new_am_filename:%0A#            new_am_template = new_am_filename.read()%0A%0A#        self.assertEqual(am_template, new_am_template
+self.assertEqual(self.am_template, draft_template)%0A%0A        # create PEP with adverse media template%0A        modules.io.create_entity(entity_AM = True, entity_PEP = True)%0A        with open(self.draft_location) as draft_location_object:%0A            draft_template = draft_location_object.read(
 )%0A%0Au

test for new templates