Skip to content

Commit

Permalink
refs #23 add test for create project form
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganeAD authored and mrjmad committed Mar 12, 2018
1 parent 72820c0 commit 0b19f6e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
23 changes: 19 additions & 4 deletions hymaintenance/high_ui/tests/views/test_project_views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

from django.test import Client, TestCase
from django.test import Client, RequestFactory, TestCase

from customers.models import Company
from high_ui.views import CreateProjectView
from maintenance.forms import INACTIF_CONTRACT_INPUT
from maintenance.models import MaintenanceContract
from maintenance.models.contract import AVAILABLE_TOTAL_TIME, CONSUMMED_TOTAL_TIME
from maintenance.tests.factories import MaintenanceTypeFactory, MaintenanceUserFactory


class ProjectCreateViewTestCase(TestCase):

def test_i_can_post_and_form_to_create_a_project(self):
MaintenanceUserFactory(email="[email protected]",
password="azerty")
@classmethod
def setUpTestData(cls):
cls.user = MaintenanceUserFactory(email="[email protected]",
password="azerty")
# TO CHANGE : Refs #51
MaintenanceTypeFactory(name="support",
css_class="type-support",
Expand All @@ -23,6 +26,16 @@ def test_i_can_post_and_form_to_create_a_project(self):
css_class="type-correction",
label_for_company_detailview="correction")

def test_i_can_get_the_form(self):
factory = RequestFactory()
request = factory.get("/high_ui/project/add/")
request.user = self.user
view = CreateProjectView()
view.request = request
context = view.get_context_data()
self.assertEqual(3, len(context["maintenance_types"]))

def test_i_can_post_and_form_to_create_a_project(self):
company_name = "Black Mesa"
# No support contract
contract1_visible = INACTIF_CONTRACT_INPUT
Expand Down Expand Up @@ -53,7 +66,9 @@ def test_i_can_post_and_form_to_create_a_project(self):
"contract3_visible": contract3_visible,
"contract3_total_type": contract3_total_type,
"contract3_number_hours": contract3_number_hours}, follow=True)
company = Company.objects.get(name=company_name)

self.assertEqual(response.status_code, 200)
self.assertRedirects(response, '/high_ui/')
self.assertEqual(1, Company.objects.filter(name=company_name).count())
self.assertEqual(2, MaintenanceContract.objects.filter(company_id=company.id).count())
97 changes: 96 additions & 1 deletion hymaintenance/maintenance/tests/forms/test_project_forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.test import TestCase

from customers.tests.factories import MaintenanceUserFactory
from customers.models import Company
from customers.tests.factories import CompanyFactory, MaintenanceUserFactory
from maintenance.forms import INACTIF_CONTRACT_INPUT, ProjectCreateForm
from maintenance.models import MaintenanceContract
from maintenance.models.contract import AVAILABLE_TOTAL_TIME, CONSUMMED_TOTAL_TIME
from maintenance.tests.factories import MaintenanceTypeFactory


Expand All @@ -20,3 +24,94 @@ def setUpTestData(cls):
MaintenanceTypeFactory(name="correction",
css_class="type-correction",
label_for_company_detailview="correction")

def __get_dict_for_post(self):
return {"company_name": "Black Mesa",
"contract1_visible": INACTIF_CONTRACT_INPUT,
"contract1_total_type": 0,
"contract1_number_hours": 0,
"contract2_visible": INACTIF_CONTRACT_INPUT,
"contract2_total_type": 0,
"contract2_number_hours": 0,
"contract3_visible": INACTIF_CONTRACT_INPUT,
"contract3_total_type": 0,
"contract3_number_hours": 0}

def test_valid_form_create_a_company(self):
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)
is_valid = form.is_valid()
form.save_company_and_contracts()

self.assertTrue(is_valid)
self.assertEqual(1, Company.objects.all().count())

def test_valid_form_create_a_support_contract(self):
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)

dict_for_post["contract1_visible"] = 1
dict_for_post["contract1_total_type"] = CONSUMMED_TOTAL_TIME

is_valid = form.is_valid()
form.save_company_and_contracts()
company = Company.objects.get(name="Black Mesa")

self.assertTrue(is_valid)
self.assertEqual(1, Company.objects.all().count())
self.assertEqual(1, MaintenanceContract.objects.filter(company_id=company, maintenance_type_id=1).count())

def test_valid_form_create_a_maintenance_contract(self):
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)

dict_for_post["contract2_visible"] = 1
dict_for_post["contract2_total_type"] = CONSUMMED_TOTAL_TIME

is_valid = form.is_valid()
form.save_company_and_contracts()
company = Company.objects.get(name="Black Mesa")

self.assertTrue(is_valid)
self.assertEqual(1, Company.objects.all().count())
self.assertEqual(1, MaintenanceContract.objects.filter(company_id=company, maintenance_type_id=2).count())

def test_valid_form_create_a_correction_contract(self):
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)

dict_for_post["contract3_visible"] = 1
dict_for_post["contract3_total_type"] = CONSUMMED_TOTAL_TIME

is_valid = form.is_valid()
form.save_company_and_contracts()
company = Company.objects.get(name="Black Mesa")

self.assertTrue(is_valid)
self.assertEqual(1, Company.objects.all().count())
self.assertEqual(1, MaintenanceContract.objects.filter(company_id=company, maintenance_type_id=3).count())

def test_valid_form_create_a_company_and_invisible_available_time_contract(self):
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)

dict_for_post["contract1_visible"] = 0
dict_for_post["contract1_total_type"] = AVAILABLE_TOTAL_TIME
dict_for_post["contract1_number_hours"] = 80

is_valid = form.is_valid()
form.save_company_and_contracts()
company = Company.objects.get(name="Black Mesa")

self.assertTrue(is_valid)
self.assertEqual(1, Company.objects.all().count())
self.assertEqual(1, MaintenanceContract.objects.filter(company_id=company, maintenance_type_id=1, visible=False, total_type=AVAILABLE_TOTAL_TIME, number_hours=80).count())

def test_invalid_form_create_already_existing_company(self):
CompanyFactory.create(name="Black Mesa")
dict_for_post = self.__get_dict_for_post()
form = ProjectCreateForm(data=dict_for_post)

self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors))
self.assertEqual(form.errors['company_name'].as_text(), "* This company already exists")

0 comments on commit 0b19f6e

Please sign in to comment.