Example #1
0
if 'purchase' in form:

    # establish date of purchase and purchase key
    import datetime
    import random
    today = datetime.datetime.today()
    dateOfPurchase = today.strftime('%Y-%m-%d %H:%M:%S')
    ymd = today.strftime('%Y%m%d')
    fname = cust.get('firstName')
    lname = cust.get('lastName')
    transactionId = ymd + fname[0:4] + lname[0:4] + str(random.randint(
        0, 9999))

    # create Purchase entity
    purchase = Purchase()
    purchase.set('transactionId', transactionId)
    purchase.set('dateOfPurchase', dateOfPurchase)

    # add customer info
    purchase.set('customerKey', cust.getKey())
    purchase.set('firstName', cust.get('firstName'))
    purchase.set('lastName', cust.get('lastName'))
    purchase.set('phoneNumber', cust.get('phoneNumber'))
    purchase.set('email', cust.get('email'))
    purchase.set('streetAddressOfBuilding',
                 cust.get('streetAddressOfBuilding'))
    purchase.set('city', cust.get('city'))
    purchase.set('stateProvince', cust.get('stateProvince'))
    purchase.set('locality', cust.get('locality'))
    purchase.set('country', cust.get('country'))
    purchase.set('postalCode', cust.get('postalCode'))
class TestPurchase(unittest.TestCase):

    purchaseFromDict = None
    purchaseDefaults = None
    purchaseFromJson = None

    testDict = dict({
        # purchase info
        'transactionId':
        '00000000',
        'dateOfPurchase':
        '2020-01-01',
        'extendedPrice':
        1724.94,
        # customer info
        'customerKey':
        '00000000',
        'firstName':
        'Fred',
        'lastName':
        'Flintstone',
        'phoneNumber':
        '+1-222-333-4444',
        'email':
        '*****@*****.**',
        'streetAddressOfBuilding':
        '123 Rocky Way',
        'city':
        'Bedrock',
        'stateProvince':
        'ZZ',
        'country':
        'ZZ',
        'postalCode':
        '00000',
        'latitude':
        11.1111,
        'longitude':
        -11.1111,
        # products purchased
        'productsPurchased': [{
            'productKey': 'AAA111',
            'qtyPurchased': 111,
            'skuNumber': '11111',
            'category': 'AAA',
            'title': 'TEST AAA',
            'price': 1.11
        }, {
            'productKey': 'BBB222',
            'qtyPurchased': 222,
            'skuNumber': '22222',
            'category': 'BBB',
            'title': 'TEST BBB',
            'price': 2.22
        }, {
            'productKey': 'CCC333',
            'qtyPurchased': 333,
            'skuNumber': '33333',
            'category': 'CCC',
            'title': 'TEST CCC',
            'price': 3.33
        }]
    })

    testJson = '''{
        "transactionId"           : "00000000",
        "dateOfPurchase"          : "2020-01-01",
        "extendedPrice"           : 1724.94,
        "customerKey"             : "00000000",
        "firstName"               : "Fred",
        "lastName"                : "Flintstone",
        "phoneNumber"             : "+1-222-333-4444",
        "email"                   : "*****@*****.**",
        "streetAddressOfBuilding" : "123 Rocky Way",
        "city"                    : "Bedrock",
        "stateProvince"           : "ZZ",
        "country"                 : "ZZ",
        "postalCode"              : "00000",
        "latitude"                : 11.1111,
        "longitude"               : -11.1111,
        "productsPurchased"       : [
            {"productKey":"AAA111","qtyPurchased":111,"skuNumber":"11111","category":"AAA","title":"TEST AAA","price":1.11},
            {"productKey":"BBB222","qtyPurchased":222,"skuNumber":"22222","category":"BBB","title":"TEST BBB","price":2.22},
            {"productKey":"CCC333","qtyPurchased":333,"skuNumber":"33333","category":"CCC","title":"TEST CCC","price":3.33}
        ]
    }'''

    def setUp(self):
        self.purchaseFromDict = Purchase(self.testDict)
        self.purchaseDefaults = Purchase(True)
        self.purchaseFromJson = Purchase(self.testJson)

    def test_purchase_from_dict(self):
        expected = '00000000'
        actual = self.purchaseFromDict.getKey()
        self.assertEqual(expected, actual)

    def test_purchase_from_dict_get_and_set(self):
        self.purchaseFromDict.set('skuNumber', '99999999')
        expected = '99999999'
        actual = self.purchaseFromDict.get('skuNumber')
        self.assertEqual(expected, actual)

    def test_purchase_from_dict_to_json(self):
        expected = self.purchaseFromDict
        actual = self.purchaseFromJson
        self.assertEqual(expected, actual)

    def test_purchase_from_json(self):
        expected = '00000000'
        actual = self.purchaseFromJson.getKey()
        self.assertEqual(expected, actual)

    def test_purchase_from_blank(self):
        expected = ''
        actual = self.purchaseDefaults.getKey()
        self.assertEqual(expected, actual)