def translate_pools(data): pools = [] for item in data: p = Pool() p.uuid = item["id"] p.account = item["accountNumber"] p.created = convert_to_datetime(item["created"]) p.quantity = item["quantity"] p.end_date = convert_to_datetime(item["endDate"]) p.start_date = convert_to_datetime(item["startDate"]) p.updated = convert_to_datetime(item["updated"]) for prod_attr in item["productAttributes"]: name = prod_attr["name"] value = prod_attr["value"] p.product_attributes[name] = value p.product_id = item["productId"] p.product_name = item["productName"] provided_products = [] for prov_prod in item["providedProducts"]: entry = dict() entry["id"] = prov_prod["productId"] entry["name"] = prov_prod["productName"] provided_products.append(entry) p.provided_products = provided_products pools.append(p) return pools
def test_uploading_single_pool(self): found = Pool.objects() self.assertEquals(len(found), 0) datestr = "2012-12-06T11:13:06.432367+00:00" p = Pool() p.uuid = "a" p.account = 1 p.active = True p.contract = 1 p.product_id = "something" p.product_name = "something_name" p.product_attributes = {} p.provided_products = [] p.created = datestr p.start_date = datestr p.end_date = datestr p.updated = datestr p.quantity = 0 example = {"objects":[p]} post_data = utils.obj_to_json(example) LOG.info("Calling api for pool import with post data: '%s'" % (post_data)) resp = self.raw_api_client.post('/api/v1/pool/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) self.assertEquals(resp.status_code, 204) # Now check that the server api saved the object as expected found = Pool.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, p.uuid) self.assertEquals(found[0].account, p.account) self.assertEquals(found[0].active, p.active) self.assertEquals(found[0].contract, p.contract) self.assertEquals(found[0].product_id, p.product_id) self.assertEquals(found[0].product_name, p.product_name) self.assertEquals(found[0].product_attributes, p.product_attributes) self.assertEquals(found[0].provided_products, p.provided_products) self.assertEquals(found[0].quantity, p.quantity) self.assertEquals(type(found[0].created), datetime.datetime) self.assertEquals(str(found[0].created), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(type(found[0].updated), datetime.datetime) self.assertEquals(str(found[0].updated), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(type(found[0].start_date), datetime.datetime) self.assertEquals(str(found[0].start_date), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(type(found[0].end_date), datetime.datetime) self.assertEquals(str(found[0].end_date), "2012-12-06 11:13:06.432000+00:00")
def test_date_as_string_is_converted_on_save(self): found = Pool.objects() self.assertEquals(len(found), 0) datestr = "2012-12-06T11:13:06.432367" p = Pool() p.uuid = "a" p.account = 1 p.active = True p.contract = 1 p.product_id = "something" p.product_name = "something_name" p.product_attributes = {} p.provided_products = [] p.created = datestr p.start_date = datestr p.end_date = datestr p.updated = datestr p.quantity = 0 p.save() found = Pool.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, p.uuid) self.assertEquals(found[0].account, p.account) self.assertEquals(found[0].active, p.active) self.assertEquals(found[0].contract, p.contract) self.assertEquals(found[0].product_id, p.product_id) self.assertEquals(found[0].product_name, p.product_name) self.assertEquals(found[0].product_attributes, p.product_attributes) self.assertEquals(found[0].provided_products, p.provided_products) self.assertEquals(found[0].quantity, p.quantity) self.assertEquals(type(found[0].created), datetime.datetime) self.assertEquals(type(found[0].updated), datetime.datetime) self.assertEquals(type(found[0].start_date), datetime.datetime) self.assertEquals(type(found[0].end_date), datetime.datetime)
def test_uploading_duplicate(self): # # Similar to test_uploading_single_pool, except for this test we will save the Pool object we create # then upload the same exact data and verify we have only 1 record in the DB...no duplicate should be present. # found = Pool.objects() self.assertEquals(len(found), 0) datestr = "2012-12-06T11:13:06.432367" p = Pool() p.uuid = "a" p.account = 1 p.active = True p.contract = 1 p.product_id = "something" p.product_name = "something_name" p.product_attributes = {} p.provided_products = [] p.created = datestr p.start_date = datestr p.end_date = datestr p.updated = datestr p.quantity = 0 p.save() self.assertEquals(len(found), 1) example = {"objects":[p]} post_data = utils.obj_to_json(example) LOG.info("Calling api for pool import with post data: '%s'" % (post_data)) resp = self.raw_api_client.post('/api/v1/pool/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) self.assertEquals(resp.status_code, 204) # Now check that the server api saved the object as expected found = Pool.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, p.uuid)
def test_upload_newer_spliceserver(self): found = Pool.objects() self.assertEquals(len(found), 0) # Create 'older' pool and save to DB older = Pool() older.uuid = "a" older.account = 20 older.contract = 400 older.active = False older.product_id = "something older" older.product_name = "something older name" older.product_attributes = {} older.provided_products = [] older.created = "2012-11-06T11:13:06.432367+00:00" older.updated = "2012-11-06T11:13:06.432367+00:00" # 1 month older older.start_date = older.updated older.end_date = older.updated older.quantity = 1 older.save() found = Pool.objects() self.assertEquals(len(found), 1) datestr = "2012-12-06T11:13:06.432367+00:00" newer = Pool() newer.uuid = older.uuid newer.account = 1 newer.active = True newer.contract = 1 newer.product_id = "something" newer.product_name = "something_name" newer.product_attributes = {} newer.provided_products = [] newer.created = datestr newer.start_date = datestr newer.end_date = datestr newer.updated = datestr newer.quantity = 0 example = {"objects": [newer]} post_data = utils.obj_to_json(example) LOG.info("Calling api for pool with post data: '%s'" % (post_data)) resp = self.raw_api_client.post('/api/v1/pool/', format='json', data=post_data, SSL_CLIENT_CERT=self.expected_valid_splice_server_identity_pem) LOG.info("Status Code: %s, Response: %s" % (resp.status_code, resp)) self.assertEquals(resp.status_code, 204) # Now check that the server api kept the 'newer' as is and ignored the older found = Pool.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, newer.uuid) self.assertEquals(found[0].active, newer.active) self.assertEquals(found[0].account, newer.account) self.assertEquals(found[0].contract, newer.contract) self.assertEquals(found[0].quantity, newer.quantity) self.assertEquals(found[0].product_id, newer.product_id) self.assertEquals(found[0].product_name, newer.product_name) self.assertEquals(found[0].product_attributes, newer.product_attributes) self.assertEquals(found[0].provided_products, newer.provided_products) self.assertEquals(str(found[0].created), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(str(found[0].updated), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(str(found[0].start_date), "2012-12-06 11:13:06.432000+00:00") self.assertEquals(str(found[0].end_date), "2012-12-06 11:13:06.432000+00:00")