def test_import_data(self): """ Tests the import_data function """ #directory_path = r"C:/Users/Amir G/SP_Python220B_2019/students/amirg/" \ # r"lesson05/assignment/data" directory_path = r"data" linear.drop_data() list1 = linear.import_data(directory_path, 'products.csv', 'customers.csv') self.assertEqual(list1[0][0], 999) self.assertEqual(list1[0][1], 0) self.assertEqual(list1[0][2], 999) self.assertTrue(list1[0][3] > 0) self.assertEqual(list1[1][0], 999) self.assertEqual(list1[1][1], 0) self.assertEqual(list1[1][2], 999) self.assertTrue(list1[1][3] > 0) linear.drop_data() list2 = linear.import_data(directory_path, 'products.csv', 'nothing.csv') self.assertEqual(list2[0][0], 999) self.assertEqual(list2[0][1], 0) self.assertEqual(list2[0][2], 999) self.assertTrue(list2[0][3] > 0) self.assertEqual(len(list2), 1)
def test_import_data(self): linear.drop_data() good_import = linear.import_data('data', 'products.csv', 'customers.csv', 'rentals.csv') self.assertEqual(good_import[0][0], 9999) self.assertEqual(good_import[0][1], 0) self.assertEqual(good_import[0][2], 9999) self.assertEqual(type(good_import[0][3]), type(float(1.02))) self.assertEqual(good_import[0], good_import[1])
def test_submission(_all_answers): #linear cust/prod, parallel cust/prod l.drop_data() for answer in _all_answers: # needs a few more assert type(answer["processed"]) == int assert type(answer["count_prior"]) == int assert type(answer["count_new"]) == int assert answer["count_prior"] + answer["processed"] == answer["count_new"] assert type(answer["elapsed"]) == float assert answer["elapsed"] > 0.0
def test_drop_data(self): linear.drop_data() # Populate the database linear.import_data('data', 'products.csv', 'customers.csv', 'test.csv') mongo = linear.MongoDBConnection() with mongo: database = mongo.connection.media record_count = database['rentals'].count_documents({}) self.assertEqual(record_count, 4) # Make sure the database successfully clears linear.drop_data() with mongo: database = mongo.connection.media record_count = database['rentals'].count_documents({}) self.assertEqual(record_count, 0)
def test_1_import(self): """ Test that the records are successfully imported. """ # Start fresh so we don't mess up all our tests... linear.drop_data() self.assertEqual(linear.show_available_products(), {}) # Check for proper error handling of import_data() with self.assertRaises(FileNotFoundError): result = linear.import_data('data2', 'p.csv', 'c.csv', 'r.csv') # These tests are hard-coded to the expected values from the incluced CSV's. # Your results may vary if the data sets change. :) result = linear.import_data('data', 'products.csv', 'customers.csv', 'rentals.csv') self.assertEqual(result[0][0], 1000) self.assertEqual(result[0][1], 0) self.assertEqual(result[0][2], 1000) self.assertEqual(result[1][0], 1000) self.assertEqual(result[1][1], 0) self.assertEqual(result[1][2], 1000) linear_available = linear.show_available_products() # Run the parallel again -- we're going to grab the results of show_available_products() # to consistency check later. parallel.drop_data() self.assertEqual(parallel.show_available_products(), {}) with self.assertRaises(FileNotFoundError): result = linear.import_data('data2', 'p.csv', 'c.csv', 'r.csv') result = parallel.import_data('data', 'products.csv', 'customers.csv', 'rentals.csv') self.assertEqual(result[0][0], 1000) self.assertEqual(result[0][1], 0) self.assertEqual(result[0][2], 1000) self.assertEqual(result[1][0], 1000) self.assertEqual(result[1][1], 0) self.assertEqual(result[1][2], 1000) self.assertEqual(linear_available, parallel.show_available_products())
with mongo: # mongodb database; it all starts here d_b = mongo.connection.media #import customer data start1 = time.time() cdb = d_b["customers"] count_prior_i = cdb.count() customer_entry = import_table(cdb, path + cust_file) count_after_i = cdb.count() #print_mdb_collection(cdb) end1 = time.time() time1 = end1 - start1 #import Product Data start2 = time.time() pdb = d_b["product"] count_prior_j = pdb.count() product_entry = import_table(pdb, path + prod_file) count_after_j = pdb.count() #print_mdb_collection(pdb) end2 = time.time() time2 = end2 - start2 return (customer_entry, count_prior_i, count_after_i, time1), \ (product_entry, count_prior_j, count_after_j, time2) if __name__ == "__main__": print(import_data('../data/', "product.csv", "customer.csv")) drop_data()
""" Run linear and parallel imports and provide time data. """ import datetime import linear import parallel linear.drop_data() print("--- Running linear import ---") start_time = datetime.datetime.now() results = linear.import_data('data', 'products.csv', 'customers.csv', 'rentals.csv') print("Products:") print(" Records: {} processed, {} imported".format(results[0][0], results[0][2] - results[0][1])) print(" Runtime: {} seconds".format(results[0][3])) print() print("Customers:") print(" Records: {} processed, {} imported".format(results[1][0], results[1][2] - results[1][1])) print(" Runtime: {} seconds".format(results[1][3])) print() print("Linear import complete in {} seconds.".format( (datetime.datetime.now() - start_time).total_seconds())) print() parallel.drop_data() print("--- Running parallel import ---") start_time = datetime.datetime.now() results = parallel.import_data('data', 'products.csv', 'customers.csv', 'rentals.csv') print("Products:") print(" Records: {} processed, {} imported".format(results[0][0], results[0][2] - results[0][1]))