def test_create_pack_already_in_db(self): """test PackManager.create_pack method if type_cig == 'GR'""" db_pack = Paquet.objects.filter( user=self.usertest, type_cig='GR', brand='TEST AUTRE', qt_paquet=50, price=30, display=False, ) datas ={ 'type_cig':'GR', 'brand':'TEST AUTRE', 'qt_paquet':50, 'price':30, } pack = PackManager(self.usertest, datas) pack.create_pack() db_pack = Paquet.objects.filter( user=self.usertest, type_cig='GR', brand='TEST AUTRE', qt_paquet=50, price=30, ) self.assertFalse(db_pack.count() == 2) self.assertEqual(db_pack.count(), 1) self.assertEqual(db_pack[0].display, True)
def test_delete_used_pack_ind(self): """test PackManager.delete_pack method with used pack""" db_pack = Paquet.objects.create( user=self.usertest, type_cig='IND', brand='ELPASO', qt_paquet=5, price=10, ) ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 5, 13), time_cig=datetime.time(13, 55), paquet=db_pack, ) datas ={'id_pack': db_pack.id} pack = PackManager(self.usertest, datas) pack.delete_pack() filter_pack = Paquet.objects.filter( user=self.usertest, type_cig='IND', brand='ELPASO', qt_paquet=5, price=10, ) self.assertTrue(filter_pack.exists()) self.assertEqual(filter_pack[0].display, False)
def test_init_first_with_non_displayed_pack(self): """test PackManager init_first method with non displayed pack""" db_pack = Paquet.objects.create( user=self.usertest, type_cig='ROL', brand='TEST AUTRE', qt_paquet=50, price=30, display=False, first=False, ) data = { 'type_cig': 'ROL', 'brand': 'TEST AUTRE', 'qt_paquet': 50, 'price': 30, } pack = PackManager(self.usertest, data) pack.init_first() pack.create_pack() filter_pack = Paquet.objects.filter(user=self.usertest, display=True, first=True) self.assertTrue(filter_pack.exists()) self.assertEqual(filter_pack.count(), 1) self.assertEqual(filter_pack[0].id, db_pack.id)
def test_update_pack_g_per_cig(self): """test PackManager.update_pack_g_per_cig method""" paquet = Paquet.objects.create( user=self.usertest, type_cig='ROL', brand='TABACO', qt_paquet=40, price=15, g_per_cig=0.8, price_per_cig=0.3 ) self.assertEqual(paquet.g_per_cig, 0.8) self.assertEqual(paquet.price_per_cig, 0.3) datas ={ 'type_cig':'ROL', 'brand':'TABACO', 'qt_paquet':40, 'price':15, 'g_per_cig':0.6 } pack = PackManager(self.usertest, datas) pack.update_pack_g_per_cig() find_pack = Paquet.objects.get( id=paquet.id, ) self.assertEqual(find_pack.g_per_cig, Decimal('0.6')) self.assertEqual(find_pack.price_per_cig, Decimal('0.22'))
def new_parameters(request): """View changing user smoking habits when starting using app""" if request.method == 'POST': # make request.POST mutable data = dict(request.POST) for field in data: data[field] = data[field][0] # check wich form (existing packs or new pack) try: if data['ref_pack']: existing_pack = True except KeyError: existing_pack = False # if new pack, createpack with PaquetFormCreation if not existing_pack: paquet_form = PaquetFormCreation(request.user, data) if paquet_form.is_valid(): new_pack = PackManager(request.user, paquet_form.cleaned_data) # define new_pack as the first pack (ref to use for stats) new_pack.init_first() # create pack new_db_object = new_pack.create_pack() # include field ref_pack with new pack id in data for ParametersForm data['ref_pack'] = new_db_object.id # reset parameters parameter_form = ParametersForm(request.user, data) if parameter_form.is_valid(): reset = ProfileManager(request.user, parameter_form.cleaned_data) reset.new_profile() return redirect('QuitSoonApp:profile') raise Http404()
def test_get_initial_g_per_cig_u(self): """test PackManager.g_per_cig property method when create new pack & if type_cig == 'IND'""" data = { 'type_cig': 'IND', 'brand': 'Camel', 'qt_paquet': 20, 'price': 10, } pack = PackManager(self.usertest, data) self.assertEqual(pack.g_per_cig, None)
def test_price_per_cig_u(self): """test PackManager.g_per_cig property method if type_cig == 'IND'""" data = { 'type_cig': 'IND', 'brand': 'Camel', 'qt_paquet': 20, 'price': 10, } pack = PackManager(self.usertest, data) self.assertEqual(pack.price_per_cig, 0.5)
def test_get_initial_g_per_cig_g(self): """test PackManager.g_per_cig property method when create new pack & if type_cig == 'ROL'""" data = { 'type_cig': 'ROL', 'brand': '1637', 'qt_paquet': 30, 'price': 11, } pack = PackManager(self.usertest, data) self.assertEqual(pack.g_per_cig, 0.8)
def test_get_unit_g(self): """test PackManager.unit method if type_cig == ROL""" data = { 'type_cig': 'ROL', 'brand': '1637', 'qt_paquet': 30, 'price': 11, } pack = PackManager(self.usertest, data) self.assertEqual(pack.unit, 'G')
def test_get_unit_u(self): """test PackManager.unit method if type_cig == IND""" data = { 'type_cig': 'IND', 'brand': 'Camel', 'qt_paquet': 20, 'price': 10, } pack = PackManager(self.usertest, data) self.assertEqual(pack.unit, 'U')
def test_get_price_per_cig_gr(self): """test PackManager.get_price_per_cig method if type_cig == 'IND'""" datas ={ 'type_cig':'ROL', 'brand':'1637', 'qt_paquet':30, 'price':11, } pack = PackManager(self.usertest, datas) self.assertEqual(pack.get_price_per_cig, Decimal('0.2933333333333333333333333333')) self.assertEqual(pack.price_per_cig, Decimal('0.2933333333333333333333333333'))
def change_g_per_cig(request): """ User can change the number of gr per cigarette and so chane the price of each cigarette """ if request.method == 'POST': form = PaquetFormCustomGInCig(request.user, request.POST) if form.is_valid(): change_pack = PackManager(request.user, form.cleaned_data) change_pack.update_pack_g_per_cig() return redirect('QuitSoonApp:paquets')
def test_get_new_g_per_cig(self): """test PackManager.get_g_per_cig method when create new pack & if type_cig == 'ROL'""" datas ={ 'type_cig':'ROL', 'brand':'1637', 'qt_paquet':30, 'price':11, 'g_per_cig':0.9 } pack = PackManager(self.usertest, datas) self.assertEqual(pack.get_g_per_cig(datas['g_per_cig']), 0.9) self.assertEqual(pack.g_per_cig, 0.9)
def delete_pack(request, id_pack): """ Used when user click on the trash of one of the paquet Don't delete it but change display attribute into False if already used """ if Paquet.objects.filter(user=request.user, id=id_pack).exists(): data = {'id_pack': id_pack} new_pack = PackManager(request.user, data) new_pack.delete_pack() return redirect('QuitSoonApp:paquets') # asked out of form so not expected request from user raise Http404()
def test_delete_unused_pack_ind(self): """test PackManager.delete_pack method with unused pack""" db_pack = Paquet.objects.create( user=self.usertest, type_cig='IND', brand='CAMEL', qt_paquet=20, price=10, ) data = {'id_pack': db_pack.id} pack = PackManager(self.usertest, data) pack.delete_pack() self.assertFalse(Paquet.objects.filter(id=db_pack.id).exists())
def test_init_first(self): """test PackManager init_first method""" data = { 'type_cig': 'IND', 'brand': 'Camel', 'qt_paquet': 20, 'price': 10, } pack = PackManager(self.usertest, data) pack.init_first() self.assertTrue(pack.first) self.assertEqual( Paquet.objects.filter(user=self.usertest, first=True).count(), 0) pack.create_pack() self.assertEqual( Paquet.objects.filter(user=self.usertest, first=True).count(), 1)
def test_create_new_pack_gr(self): """test PackManager.create_pack method if type_cig == 'GR'""" datas ={ 'type_cig':'GR', 'brand':'TEST AUTRE', 'qt_paquet':50, 'price':30, } pack = PackManager(self.usertest, datas) pack.create_pack() db_pack = Paquet.objects.filter( user=self.usertest, type_cig='GR', brand='TEST AUTRE', qt_paquet=50, price=30, ) self.assertTrue(db_pack.exists()) self.assertEqual(db_pack[0].unit, 'G') self.assertEqual(db_pack[0].g_per_cig, Decimal('0.8')) self.assertEqual(db_pack[0].price_per_cig, Decimal('0.48'))
def test_create_new_pack_ind(self): """test PackManager.create_pack method if type_cig == 'IND'""" datas ={ 'type_cig':'IND', 'brand':'CAMEL', 'qt_paquet':20, 'price':10, } pack = PackManager(self.usertest, datas) pack.create_pack() db_pack = Paquet.objects.filter( user=self.usertest, type_cig='IND', brand='CAMEL', qt_paquet=20, price=10, ) self.assertTrue(db_pack.exists()) self.assertEqual(db_pack[0].unit, 'U') self.assertEqual(db_pack[0].g_per_cig, None) self.assertEqual(db_pack[0].price_per_cig, 0.5)
def paquets(request): """Smoking parameters, user different packs""" context = {'first': False} form = PaquetFormCreation(request.user) if not Paquet.objects.filter(user=request.user).exists(): context['first'] = True if request.method == 'POST': # receive smoking habits from user in a form = PaquetFormCreation(request.user, request.POST) if form.is_valid(): new_pack = PackManager(request.user, form.cleaned_data) new_pack.create_pack() form = PaquetFormCreation(request.user) context['first'] = False # select users packs for display in paquets page displayed_pack = Paquet.objects.filter(user=request.user, display=True) context['form'] = form # get packs per type context['ind'] = displayed_pack.filter(type_cig='IND') context['rol'] = displayed_pack.filter(type_cig='ROL') return render(request, 'QuitSoonApp/paquets.html', context)