def test_create_new_alternative_already_in_db(self): """ test SavePack.create_alternative method if type_alternative == 'Su' and alternative already in db """ Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, display=False, ) data = { 'type_alternative': 'Su', 'substitut': 'P24', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, ) self.assertFalse(db_create_alternative.count() == 2) self.assertEqual(db_create_alternative.count(), 1) self.assertEqual(db_create_alternative[0].display, True)
def test_delete_used_alternative_substitut(self): """test SavePack.delete_alternative method with used alternative""" db_alternative = Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) ConsoAlternative.objects.create( user=self.usertest, datetime_alter=datetime.datetime(2020, 5, 13, 13, 55, tzinfo=pytz.utc), alternative=db_alternative, ) data = {'id_alternative': db_alternative.id} alternative = AlternativeManager(self.usertest, data) alternative.delete_alternative() filter_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) self.assertTrue(filter_alternative.exists()) self.assertEqual(filter_alternative[0].display, False)
def test_delete_used_alternative_activity(self): """test SavePack.delete_alternative method with used alternative""" db_alternative = Alternative.objects.create( user=self.usertest, type_alternative='Ac', type_activity='So', activity='PSYCHOLOGUE', ) ConsoAlternative.objects.create( user=self.usertest, datetime_alter=datetime.datetime(2020, 5, 13, 13, 55, tzinfo=pytz.utc), alternative=db_alternative, ) data = {'id_alternative': db_alternative.id} alternative = AlternativeManager(self.usertest, data) alternative.delete_alternative() filter_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Ac', type_activity='So', activity='PSYCHOLOGUE', ) self.assertTrue(filter_alternative.exists()) self.assertEqual(filter_alternative[0].display, False)
def test_get_str(self): """test method get_str""" data = 'None' self.assertEqual(AlternativeManager.get_str(data), None) data = 1637 self.assertEqual(AlternativeManager.get_str(data), '1637') data = 'Su' self.assertEqual(AlternativeManager.get_str(data), 'Su')
def test_if_strNone_get_None_or_str(self): data = 'None' self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), None) data = 1637 self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), '1637') data = 'Su' self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), 'Su')
def test_str_get_float(self): """test method str_get_float""" data = 'None' self.assertEqual(AlternativeManager.str_get_float(data), None) data = '2' self.assertEqual(AlternativeManager.str_get_float(data), 2.0) data = '2.0' self.assertEqual(AlternativeManager.str_get_float(data), 2.0) data = 'erreur' self.assertRaises(ValueError, AlternativeManager.str_get_float(data)) self.assertEqual(AlternativeManager.str_get_float(data), None)
def delete_alternative(request, id_alternative): """ Used when user click on the trash of one of the alternative Don't delete it but change display attribute into False if already used in ConsoAlternative """ if Alternative.objects.filter(user=request.user, id=id_alternative).exists(): data = {'id_alternative': id_alternative} new_alternative = AlternativeManager(request.user, data) new_alternative.delete_alternative() return redirect('QuitSoonApp:alternatives') raise Http404()
def if_strNone_get_None_or_float(self): data = 'None' self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), None) data = 2 self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), 2.0) data = 2.0 self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), 2.0) data = 'erreur' self.assertEqual(AlternativeManager.if_strNone_get_None_or_str(data), None)
def test_create_new_alternative(self): """test SavePack.create_alternative method if type_alternative != 'Su'""" data = { 'type_alternative': 'Ac', 'type_activity': 'Sp', 'activity': 'COURSE', } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Ac', type_activity='Sp', activity='COURSE', ) self.assertTrue(db_create_alternative.exists())
def test_create_new_alternative_substitut(self): """test SavePack.create_alternative method if type_alternative == 'Su'""" data = { 'type_alternative': 'Su', 'substitut': 'P24', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, ) self.assertTrue(db_create_alternative.exists())
def test_delete_unused_alternative_substitut(self): """test AlternativeManager.delete_alternative method with unused alternative""" db_alternative = Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) data = {'id_alternative': db_alternative.id} alternative = AlternativeManager(self.usertest, data) alternative.delete_alternative() filter_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) self.assertFalse(filter_alternative.exists())
def test_delete_unused_alternative_activity(self): """test AlternativeManager.delete_alternative method with unused alternative""" db_alternative = Alternative.objects.create( user=self.usertest, type_alternative='Ac', type_activity='So', activity='PSYCHOLOGUE', ) data = {'id_alternative': db_alternative.id} alternative = AlternativeManager(self.usertest, data) alternative.delete_alternative() filter_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Ac', type_activity='So', activity='PSYCHOLOGUE', ) self.assertFalse(filter_alternative.exists())
def test_create_new_alternative_substitut_already_in_db(self): """test SavePack.create_alternative method if type_alternative == 'Su'""" Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) data = { 'type_alternative': 'Su', 'substitut': 'ECIG', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) self.assertFalse(db_create_alternative.count() == 2)
def test_get_request_data(self): datas = { 'type_alternative': 'Su', 'substitut': 'P24', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, datas) self.assertEqual(alternative.get_request_data('type_alternative'), 'Su') self.assertEqual(alternative.get_request_data('type_activity'), None) self.assertEqual(alternative.get_request_data('activity'), None) self.assertEqual(alternative.get_request_data('substitut'), 'P24') self.assertEqual(alternative.get_request_data('nicotine'), 2)
def test_get_alternative(self): old = Alternative.objects.create( user=self.usertest, type_alternative='Ac', type_activity='Sp', activity='COURSE', display=False, ) datas = { 'type_alternative': 'Ac', 'type_activity': 'Sp', 'activity': 'COURSE', } alt = AlternativeManager(self.usertest, datas) self.assertTrue(alt.get_alternative)
def alternatives(request): """Healthy parameters, user different activities or substitutes""" context = {} alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) if request.method == 'POST': # get wich type of alternative form_data = {'type_alternative': request.POST['type_alternative']} alternative_form = TypeAlternativeForm(request.user, form_data) if alternative_form.is_valid(): final_data = { 'type_alternative': alternative_form.cleaned_data['type_alternative'] } if alternative_form.cleaned_data['type_alternative'] == 'Ac': form_data = { 'type_activity': request.POST['type_activity'], 'activity': request.POST['activity'] } activity_form = ActivityForm(request.user, form_data) if activity_form.is_valid(): # keep only fields from alternative_form & activity_form final_data['type_activity'] = activity_form.cleaned_data[ 'type_activity'] final_data['activity'] = activity_form.cleaned_data[ 'activity'] # Create a AlternativeManager object and create a new alternative new_alternative = AlternativeManager( request.user, final_data) new_alternative.create_alternative() alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) elif alternative_form.cleaned_data['type_alternative'] == 'Su': form_data = { 'substitut': request.POST['substitut'], 'nicotine': request.POST['nicotine'] } substitut_form = SubstitutForm(request.user, form_data) if substitut_form.is_valid(): # keep only fields from alternative_form & substitut_form final_data['substitut'] = substitut_form.cleaned_data[ 'substitut'] final_data['nicotine'] = substitut_form.cleaned_data[ 'nicotine'] # Create a AlternativeManager object and create a new alternative new_alternative = AlternativeManager( request.user, final_data) new_alternative.create_alternative() alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) # select users packs for display in paquets page displayed_alt = Alternative.objects.filter(user=request.user, display=True) context = { 'alternative_form': alternative_form, 'activity_form': activity_form, 'substitut_form': substitut_form, # get packs per type 'Sp': displayed_alt.filter(type_activity='Sp'), 'Lo': displayed_alt.filter(type_activity='Lo'), 'So': displayed_alt.filter(type_activity='So'), 'Su': displayed_alt.filter(type_alternative='Su'), } return render(request, 'QuitSoonApp/alternatives.html', context)