Example #1
0
    def _create_products(self):
        print "Adding {0} products ".format(self.num_products),
        serial_num_int = 1

        # Products are manufactured more than 30 days ago
        min_delta = - timezone.timedelta(days=30).total_seconds()
        max_delta = 2 * min_delta
        now = timezone.now()

        for _ in xrange(self.num_products):
            # Random product type
            product_type = random.randint(0, len(Product.PRODUCT_CHOICES)-1)

            # Get how it must be stored in the DB
            kind = Product.PRODUCT_CHOICES[product_type][0]

            sale = None

            # Generate a 10 digit serial number
            serial_num = "{0:0>10d}".format(serial_num_int)
            serial_num_int += 1

            # Create a random date
            date = now - timezone.timedelta(seconds=random.randint(max_delta,min_delta))

            #Create the product
            product = Product(kind=kind, sale=sale, date=date, serial_num=serial_num)
            product.save()

            sys.stdout.write('.')

        print "OK"
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_create_voucher(self):
        """
        Test that creates a redeemed voucher for a given customer.
        This tests the POST from endpoint /loyal/customer/${id}/vouchers
        """

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

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

        # Create voucher
        voucher_data = {"redeemed_with": p.pk}
        url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk])
        response = self.client.post(url, voucher_data)

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        voucher_data.update({
            'date':response.data.get("date", None),
            'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[1]),
        })

        self.assertEqual(response.data, voucher_data)
Example #4
0
    def test_create_voucher_product_occupied(self):
        """
        Test that tries to create a redeemed voucher for a product that has already been redeemed.
        This tests the POST from endpoint /loyal/customer/${id}/vouchers
        """

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

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

        # Create voucher in DB
        v = Voucher(owned_by=c, redeemed_with=p)
        v.save()

        # Create voucher through API
        voucher_data = {"redeemed_with": p.pk}
        url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk])
        response = self.client.post(url, voucher_data)

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Example #5
0
    def test_create_stamp(self):
        """
        Test that creates a normal stamp for a given customer.
        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()

        # Create stamp
        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)

        stamp_data['link'] = self.get_non_customer_url(self.STAMP_ENDP, args=[1])

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, stamp_data)
Example #6
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 #7
0
    def test_create_stamp(self):
        """
        Test that creates a normal stamp for a given customer.
        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()

        # Create stamp
        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)

        stamp_data['link'] = self.get_non_customer_url(self.STAMP_ENDP,
                                                       args=[1])

        # Confirm it's OK
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, stamp_data)
Example #8
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 #9
0
    def test_create_product_repeat_serial(self):
        """
        Test that tries to create a product with a repeated serial number
        This tests the POST from endpoint /loyal/products
        """

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

        # Create  through API
        url = self.get_url(self.PROD_LIST_ENDP)
        response = self.client.post(url, self.new_product)

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Example #10
0
    def test_create_product_repeat_serial(self):
        """
        Test that tries to create a product with a repeated serial number
        This tests the POST from endpoint /loyal/products
        """

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

        # Create  through API
        url = self.get_url(self.PROD_LIST_ENDP)
        response = self.client.post(url, self.new_product)

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Example #11
0
    def test_sales_list_populated(self):
        """
        Test that for a given customer we can see it's purchases.
        We create 2 sales directly in the DB, one without products the other with 1 product
        This tests the GET from endpoint /loyal/customer/${id}/sales
        """

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

        # Create sales
        s = Sale(customer=c, date=timezone.now())
        s.save()

        s = Sale(customer=c, date=timezone.now())
        s.save()

        # Create product
        new_product = dict(self.new_product)
        new_product['sale'] = s
        p = Product(**new_product)
        p.save()

        # Get list
        url = self.get_url(self.SALE_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))

        sale_data = {
            "date": response.data[0].get("date", None),
            'products': [],
            'products_links': []
        }
        self.assertEqual(response.data[0], sale_data)

        sale_data = {
            "date":
            response.data[1].get("date", None),
            'products': [str(p)],
            'products_links':
            [self.get_non_customer_url(self.PRODUCT_ENDP, args=[1])],
        }
        self.assertEqual(response.data[1], sale_data)
Example #12
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 #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)
Example #14
0
    def test_sales_list_populated(self):
        """
        Test that for a given customer we can see it's purchases.
        We create 2 sales directly in the DB, one without products the other with 1 product
        This tests the GET from endpoint /loyal/customer/${id}/sales
        """

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

        # Create sales
        s = Sale(customer=c, date=timezone.now())
        s.save()

        s = Sale(customer=c, date=timezone.now())
        s.save()

        # Create product
        new_product = dict(self.new_product)
        new_product['sale'] = s
        p = Product(**new_product)
        p.save()

        # Get list
        url = self.get_url(self.SALE_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))

        sale_data = {"date": response.data[0].get("date", None),
                     'products':[],
                     'products_links':[]
        }
        self.assertEqual(response.data[0], sale_data)

        sale_data = {"date": response.data[1].get("date", None),
                     'products':[str(p)],
                     'products_links': [self.get_non_customer_url(self.PRODUCT_ENDP, args=[1])],
        }
        self.assertEqual(response.data[1], sale_data)
Example #15
0
    def _creation(self, customer=False, product=False, sale=False):
        c = None; p = None; s = None

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

        # Create sale
        if sale:
            s = Sale(customer=c, date=timezone.now())
            s.save()

        # Create product
        if product:
            p = Product(**self.new_product)
            p.sale = s
            p.save()

        return (c, p, s)
Example #16
0
    def test_create_stamp_non_existan_owner(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()

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

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Example #17
0
    def test_create_stamp_non_existan_owner(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()

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

        # Confirm it's not OK
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Example #18
0
    def test_voucher_list_populated(self):
        """
        Test that for a given customer we can see it's vouchers.
        We create 2 vouchers directly in the DB, one available and one redeemed.
        This tests the GET from endpoint /loyal/customer/${id}/vouchers
        """

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

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

        # Create voucher
        v = Voucher(owned_by=c, redeemed_with=p)
        v.save()

        # Create free voucher
        v = Voucher(owned_by=c)
        v.save()

        # Get list
        url = self.get_url(self.VOUCH_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))

        voucher_data = {"redeemed_with": p.pk,
                        "date": response.data[0].get("date", None),
                        'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[1])}
        self.assertEqual(response.data[0], voucher_data)

        voucher_data = {"redeemed_with": None,
                        "date": response.data[1].get("date", None),
                        'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[2])}
        self.assertEqual(response.data[1], voucher_data)
Example #19
0
    def _create_products(self):
        print "Adding {0} products ".format(self.num_products),
        serial_num_int = 1

        # Products are manufactured more than 30 days ago
        min_delta = -timezone.timedelta(days=30).total_seconds()
        max_delta = 2 * min_delta
        now = timezone.now()

        for _ in xrange(self.num_products):
            # Random product type
            product_type = random.randint(0, len(Product.PRODUCT_CHOICES) - 1)

            # Get how it must be stored in the DB
            kind = Product.PRODUCT_CHOICES[product_type][0]

            sale = None

            # Generate a 10 digit serial number
            serial_num = "{0:0>10d}".format(serial_num_int)
            serial_num_int += 1

            # Create a random date
            date = now - timezone.timedelta(
                seconds=random.randint(max_delta, min_delta))

            #Create the product
            product = Product(kind=kind,
                              sale=sale,
                              date=date,
                              serial_num=serial_num)
            product.save()

            sys.stdout.write('.')

        print "OK"
Example #20
0
    def test_sales_list_populated(self):
        """
        Test that we can retrieve products from the DB using the endpoint.
        We create 2 products directly in the DB
        This tests the GET from endpoint /loyal/products
        """

        # Create product
        new_product = dict(self.new_product)
        expected_response = dict(self.EMPTY_LIST)

        p = Product(**new_product)
        p.save()
        products = [p]

        new_product['serial_num'] = str(1 + int(new_product['serial_num']))
        p = Product(**new_product)
        p.save()
        products.append(p)

        expected_response['results'] = ProductSerializer(products,
                                                         many=True).data
        expected_response['count'] = 2

        for product in expected_response['results']:
            product['to_modify'] = self.URL_TEST_PREFIX + product['to_modify']

        # Get list
        url = self.get_url(self.PROD_LIST_ENDP)
        response = self.client.get(url)

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

        self.assertEqual(response.data, expected_response)
Example #21
0
    def test_sales_list_populated(self):
        """
        Test that we can retrieve products from the DB using the endpoint.
        We create 2 products directly in the DB
        This tests the GET from endpoint /loyal/products
        """

        # Create product
        new_product = dict(self.new_product)
        expected_response = dict(self.EMPTY_LIST)

        p = Product(**new_product)
        p.save()
        products = [p]

        new_product['serial_num'] = str(1+int(new_product['serial_num']))
        p = Product(**new_product)
        p.save()
        products.append(p)

        expected_response['results'] = ProductSerializer(products, many=True).data
        expected_response['count'] = 2

        for product in expected_response['results']:
            product['to_modify'] = self.URL_TEST_PREFIX +  product['to_modify']


        # Get list
        url = self.get_url(self.PROD_LIST_ENDP)
        response = self.client.get(url)

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

        self.assertEqual(response.data, expected_response)