'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'       : prods_purchased
})

# test blank product entity
purchase = Purchase(True)
print("\nBlank Purchase Entity")
print('Transaction ID: ' + purchase.getKey())
print('Date: ' + purchase.get('dateOfPurchase'))
print('Customer: ' + purchase.getCustomerName())
print(purchase.toJson())

# test purchase entity initialized from dictionary
purchase = Purchase(doc)
print("\nPurchase Entity Initialized from Dictionary")
print('Transaction ID: ' + purchase.getKey())
print('Date: ' + purchase.get('dateOfPurchase'))
print('Customer: ' + purchase.getCustomerName())
print(purchase.toJson())
Example #2
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'))
Example #3
0
			"productKey" : "valentines_day_chocolate_pack",
			"qtyPurchased" : 434,
			"MainProductInfo" : {
				"skuNumber" : "VALE270",
				"category" : "chocolate",
				"title" : "Valentines Day Chocolate Pack",
				"description" : "Nam gravida libero ac malesuada cursus. Vivamus semper justo sed dictum aliquam.",
				"price" : 19.99
			}
		}
	]
}'''.replace('%key%', key).replace('%date%', dateOfPurchase)

# adding a new purchase
print("Adding a Single Test Purchase")
if service.addOne(Purchase(doc)):
    print("Purchase " + key + " added successfully")

# running a query for a single item
print("\nFetch Purchase by Key")
doc = service.fetchByKey(key)
if doc:
    print(doc.toJson())
else:
    print("Unable to find purchase " + key + "\n")
"""
# updating a single purchase
updateDoc = {
    'purchasePhoto' :'REVISED PHOTO',
    'MainPurchaseInfo.price' : 2.22
};
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)
 def setUp(self):
     self.purchaseFromDict = Purchase(self.testDict)
     self.purchaseDefaults = Purchase(True)
     self.purchaseFromJson = Purchase(self.testJson)
# testing database connection to sweetscomplete.customer collection

# tell python where to find module source code
import os,sys
sys.path.append(os.path.realpath("src"))

import db.mongodb.connection
from sweetscomplete.entity.purchase import Purchase

# testing blank purchase entity
purch = Purchase(True)
print("\nBlank Entity:")
print('Class: '    + str(type(purch)))
print('Key: '      + purch.getKey())
print('Customer: ' + purch.getCustomer())
print('Date: '     + purch.get('dateOfPurchase'))
print('JSON:'      + purch.toJson())

# setting up the connection + collection
conn = db.mongodb.connection.Connection('localhost', 27017, Purchase)
db   = conn.getDatabase("sweetscomplete")

# testing purchases collection query
purch = db.purchases.find_one()
print("\nResult from Query:")
print('Class: '    + str(type(purch)))
print('Key: '      + purch.getKey())
print('Customer: ' + purch.getCustomer())
print('Date: '     + purch.get('dateOfPurchase'))
print('JSON:'      + purch.toJson())