Exemple #1
0
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 = json.loads(self.testJson)
        actual   = json.loads(self.purchaseFromDict.toJson())
        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)
    '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())
# 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())