Example #1
0
    def test_create_stamp_product_occupied(self):
        """
        Test that tries to create a normal stamp with an unkown owner
        This tests the POST from endpoint /loyal/customer/${id}/stamps.
        """

        # Create customer in DB
        c = Customer(**self.new_customer)
        c.save()

        # Create product in DB
        p = Product(**self.new_product)
        p.save()

        # Stamp product in DB
        st = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        st.save()

        # Create stamp through API
        stamp_data = {"obtained_with": p.pk, "grouped_in": None}
        url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk])
        response = self.client.post(url, stamp_data)

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Example #2
0
    def test_create_stamp_product_occupied(self):
        """
        Test that tries to create a normal stamp with an unkown owner
        This tests the POST from endpoint /loyal/customer/${id}/stamps.
        """

        # Create customer in DB
        c = Customer(**self.new_customer)
        c.save()

        # Create product in DB
        p = Product(**self.new_product)
        p.save()

        # Stamp product in DB
        st = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        st.save()

        # Create stamp through API
        stamp_data = {"obtained_with": p.pk, "grouped_in": None}
        url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk])
        response = self.client.post(url, stamp_data)

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Example #3
0
    def test_voucher_autocreation_19(self):
        """
        Test that the model logic in Stamp is working correctly.
        This test adds 19 stamps.
        For every 10 stamps 1 voucher should be automatically added and the
        stamps should be linked to that new voucher.
        """

        # Create a customer and product
        c, _, _ = self._creation(customer=True)

        # Create 19 stamps
        for _ in xrange((2 * Stamp.STAMPS_PER_VOUCHER) - 1):
            stamp = Stamp(owned_by=c)
            stamp.save()

        # Retrieve stamps from DB
        stamps = Stamp.objects.all()
        self.assertEqual(19, len(stamps))

        # Retrieve vouchers from DB
        vouchers = Voucher.objects.all()
        self.assertEqual(1, len(vouchers))

        # Confirm that there are 10 stamps linked to the voucher
        self.assertEqual(10, vouchers[0].stamp_set.count())

        # Confirm that there are 9 free stamps
        self.assertEqual(9, Stamp.objects.filter(grouped_in__isnull=True).count())
Example #4
0
    def test_get_detail_one_customer_with_counters(self):
        """
        Verify that the customers detail endpoint returns customer info.
        This is a basic test with only 1 customer but wih data on the
        counters.
        This tests the GET endpoint /loyal/customer/${id}
        """

        num_sales = 3
        num_vouchers = 2

        # Create customer
        c = Customer(**self.new_customer)
        c.save()

        for _ in xrange(num_sales):
            s = Sale(customer=c, date=timezone.now())
            s.save()

        p = Product(**self.new_product)
        p.save()

        st = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        st.save()

        for _ in xrange(num_vouchers):
            v = Voucher(owned_by=c, redeemed_with=None)
            v.save()

        # Get details
        url = self.get_url(self.CUST_DET_ENDP, args=[c.pk])
        response = self.client.get(url)

        expected_customer = dict(self.new_customer)
        expected_customer = dict(self.new_customer)
        expected_customer.update({
            'available_stamps':
            1,
            'total_stamps':
            1,
            'stamps':
            self.get_test_url(self.STAMP_LIST_ENDP, args=[c.pk]),
            'available_vouchers':
            2,
            'total_vouchers':
            2,
            'vouchers':
            self.get_test_url(self.VOUCH_LIST_ENDP, args=[c.pk]),
            'num_purchases':
            3,
            'purchases':
            self.get_test_url(self.SALE_LIST_ENDP, args=[c.pk]),
        })

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(expected_customer, response.data)
Example #5
0
    def test_stamp_creation(self):
        # Create a customer and product
        c, p, _ = self._creation(customer=True, product=True)

        # Create a new stamp
        stamp = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        stamp.save()

        # Retrieve stamps from DB
        stamps = Stamp.objects.all()

        # We must only have created stamp
        self.assertEqual(1, len(stamps))
        self.assertEqual(stamp, stamps[0])
Example #6
0
    def _create_sales_and_stamps(self):
        # All sales will be in the last 30 days
        max_delta = -timezone.timedelta(days=30).total_seconds()
        now = timezone.now()

        # We'll only sell 80% of the total number of products
        products_to_sell = int(0.8 * self.num_products)

        # We'll generate a random number of sales
        num_sales = random.randint(self.num_customers, products_to_sell / 5)
        products_sold = 0

        print "Creating {0} sales ".format(num_sales),

        for i in xrange(num_sales):
            # Create random sale
            date = now - timezone.timedelta(
                seconds=random.randint(max_delta, 0))
            customer_num = random.randint(1, self.num_customers)
            customer = Customer.objects.get(pk=customer_num)
            sale = Sale(customer=customer, date=date)
            sale.save()

            # Attach products to customer
            randomize_products = products_to_sell - (num_sales - i)
            num_products = random.randint(1, randomize_products)

            if customer_num == 1:
                import pdb
                pdb.set_trace()

            for i in xrange(products_sold + 1,
                            products_sold + num_products + 1):
                product = Product.objects.get(pk=i)
                product.sale = sale
                product.save()

                # If it's a Widget we should add a Stamp
                if product.kind == Product.WIDGET:
                    stamp = Stamp(owned_by=customer,
                                  obtained_with=product,
                                  grouped_in=None)
                    stamp.save()

            products_to_sell -= num_products
            products_sold += num_products

            sys.stdout.write('.')

        print "OK"
Example #7
0
    def _create_free_stamps(self):
        free_stamps = self.num_customers
        print "Creating {0} free stamps".format(free_stamps),

        for _ in xrange(free_stamps):
            # Pick a random customer
            customer_id = random.randint(1, self.num_customers)
            customer = Customer.objects.get(pk=customer_id)

            # Create a free stamp for that user
            stamp = Stamp(owned_by=customer)
            stamp.save()
            sys.stdout.write('.')

        print "OK"
Example #8
0
    def _create_free_stamps(self):
        free_stamps = self.num_customers
        print "Creating {0} free stamps".format(free_stamps),

        for _ in xrange(free_stamps):
            # Pick a random customer
            customer_id = random.randint(1, self.num_customers)
            customer = Customer.objects.get(pk=customer_id)

            # Create a free stamp for that user
            stamp = Stamp(owned_by=customer)
            stamp.save()
            sys.stdout.write('.')

        print "OK"
Example #9
0
    def _create_sales_and_stamps(self):
        # All sales will be in the last 30 days
        max_delta = - timezone.timedelta(days=30).total_seconds()
        now = timezone.now()

        # We'll only sell 80% of the total number of products
        products_to_sell = int(0.8 * self.num_products)

        # We'll generate a random number of sales
        num_sales = random.randint(self.num_customers, products_to_sell/5)
        products_sold = 0

        print "Creating {0} sales ".format(num_sales),

        for i in xrange(num_sales):
            # Create random sale
            date = now - timezone.timedelta(seconds=random.randint(max_delta,0))
            customer_num = random.randint(1, self.num_customers)
            customer = Customer.objects.get(pk=customer_num)
            sale = Sale(customer=customer, date=date)
            sale.save()

            # Attach products to customer
            randomize_products = products_to_sell - (num_sales - i)
            num_products = random.randint(1, randomize_products)

            if customer_num == 1:
                import pdb
                pdb.set_trace()

            for i in xrange(products_sold+1, products_sold+num_products+1):
                product = Product.objects.get(pk=i)
                product.sale = sale
                product.save()

                # If it's a Widget we should add a Stamp
                if product.kind == Product.WIDGET:
                    stamp = Stamp(owned_by=customer, obtained_with=product, grouped_in=None)
                    stamp.save()

            products_to_sell -= num_products
            products_sold += num_products

            sys.stdout.write('.')

        print "OK"
Example #10
0
    def restore_object(self, attrs, instance=None):
        pk = self.context['view'].kwargs['pk']

        try:
            attrs['owned_by'] = Customer.objects.get(pk=pk)
        except Customer.DoesNotExist:
            raise Http404

        return Stamp(**attrs)
Example #11
0
    def test_stamp_list_populated(self):
        """
        Test that for a given customer we can see it's stamps.
        We create 2 stamps directly in the DB, one free and one from purchasing a product.
        This tests the GET from endpoint /loyal/customer/${id}/stamps.
        """

        # Create customer
        c = Customer(**self.new_customer)
        c.save()

        # Create product
        p = Product(**self.new_product)
        p.save()

        # Create stamp
        s = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        s.save()

        # Create free stamp
        s = Stamp(owned_by=c)
        s.save()

        # Get list
        url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk])
        response = self.client.get(url)

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(2, len(response.data))

        stamp_data = {
            'obtained_with': p.pk,
            'grouped_in': None,
            'link': self.get_non_customer_url(self.STAMP_ENDP, args=[1]),
        }
        self.assertEqual(response.data[0], stamp_data)

        stamp_data.update({
            'obtained_with':
            None,
            'link':
            self.get_non_customer_url(self.STAMP_ENDP, args=[2]),
        })
        self.assertEqual(response.data[1], stamp_data)
Example #12
0
    def test_voucher_autocreation(self):
        """
        Test that the model logic in Stamp is working correctly.
        This tests adds 10 stamps.
        For every 10 stamps 1 voucher should be automatically added and the
        stamps should be linked to that new voucher.
        """

        # Create a customer and product
        c, _, _ = self._creation(customer=True)

        # Create 9 stamps and confirm that no voucher has been added
        for _ in xrange(Stamp.STAMPS_PER_VOUCHER - 1):
            stamp = Stamp(owned_by=c)
            stamp.save()

        # Retrieve stamps from DB
        stamps = Stamp.objects.all()
        self.assertEqual(9, len(stamps))

        # Retrieve vouchers from DB
        vouchers = Voucher.objects.all()
        self.assertEqual(0, len(vouchers))

        # Add the 10th stamp
        stamp = Stamp(owned_by=c)
        stamp.save()

        # Retrieve stamps from DB
        stamps = Stamp.objects.all()
        self.assertEqual(10, len(stamps))

        # Retrieve vouchers from DB
        vouchers = Voucher.objects.all()
        self.assertEqual(1, len(vouchers))

        # Confirm that the 10 stamps are really linked to the voucher
        self.assertEqual(10, vouchers[0].stamp_set.count())
Example #13
0
    def test_stamp_list_populated(self):
        """
        Test that for a given customer we can see it's stamps.
        We create 2 stamps directly in the DB, one free and one from purchasing a product.
        This tests the GET from endpoint /loyal/customer/${id}/stamps.
        """

        # Create customer
        c = Customer(**self.new_customer)
        c.save()

        # Create product
        p = Product(**self.new_product)
        p.save()

        # Create stamp
        s = Stamp(owned_by=c, obtained_with=p, grouped_in=None)
        s.save()

        # Create free stamp
        s = Stamp(owned_by=c)
        s.save()

        # Get list
        url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk])
        response = self.client.get(url)

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(2, len(response.data))

        stamp_data = {
            'obtained_with': p.pk,
            'grouped_in': None,
            'link': self.get_non_customer_url(self.STAMP_ENDP, args=[1]),
        }
        self.assertEqual(response.data[0], stamp_data)

        stamp_data.update({
            'obtained_with': None,
            'link': self.get_non_customer_url(self.STAMP_ENDP, args=[2]),
        })
        self.assertEqual(response.data[1], stamp_data)