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_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_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 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_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)