def tearDown(self): """ Test teardown """ # Remove the tables and their data from the database Base.metadata.drop_all(engine) # Delete test upload folder shutil.rmtree(upload_path())
def test_file_upload(self): #construct form as dictionary #use instance of Python's StringIO class to simulate a file object data = { "file": (StringIO("File contents"), "test.txt") } #send dictionary to api/files endpoint with content-type of multipart/form-data response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) print ("data is {}".format(data)) #make sure response data points to a url where the file can be accessed self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") #make sure the file has been saved correctly in the uploads folder path = upload_path("test.txt") self.assertTrue(os.path.isfile(path)) with open(path) as f: contents = f.read() self.assertEqual(contents, "File contents")
def setUp(self): """ Test setup """ self.client = app.test_client() # Set up the tables in the database Base.metadata.create_all(engine) # Create folder for test uploads os.mkdir(upload_path())
def test_get_uploaded_file(self): path = upload_path("test.txt") with open(path, "w") as f: f.write("File contents") response = self.client.get("/uploads/test.txt") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "text/plain") self.assertEqual(response.data, "File contents")
def test_get_uploaded_file(self): #get location where file will exist and create the file path = upload_path("test.txt") #write file contents with open(path, "w") as f: f.write("File contents") #response object created through the GET request to the new file response = self.client.get("/uploads/test.txt") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "text/plain") self.assertEqual(response.data, "File contents")
def test_file_upload(self): data = {"file": (StringIO("File contents"), "test.txt")} response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) print data path = upload_path("test.txt") self.assertTrue(os.path.isfile(path)) with open(path) as f: contents = f.read() self.assertEqual(contents, "File contents")
def test_file_upload(self): data={ "file": (StringIO("File contents"), "test.txt") } response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") path = upload_path("test.txt") self.assertTrue(os.path.isfile(path)) with open(path) as f: contents = f.read() self.assertEqual(contents, "File contents")