async def test_read_drawing(self): """ Test basic read_drawing functionality User Story: As API user, I want to initiate a basic read request to verify that the basic functionality works """ client = W24TechreadClient.make_from_env(None) async with client as session: request = session.read_drawing(DRAWING, [W24AskPageThumbnail()]) # check whether the first message give us the state information message_first = await request.__anext__() self._assert_message_is_progress_started(message_first) # check whether the second message gives us the information # about the requested thumbnail message_second = await request.__anext__() self.assertEqual( message_second.message_type, W24TechreadMessageType.ASK) self.assertEqual( message_second.message_subtype, W24AskType.PAGE_THUMBNAIL) self.assertGreater(len(message_second.payload_bytes), 0) # check whether we close the iteration correctly with self.assertRaises(StopAsyncIteration): await request.__anext__()
async def test_read_title_block(self): """ Is TitleBlock read correctly? User Story: As API user, I want to obtain the title block information from the Technical Drawing that I am submitting. """ asks = [W24AskTitleBlock()] client = W24TechreadClient.make_from_env(None) async with client as session: request = session.read_drawing(DRAWING_BYTES, asks) # check whether the first message give us the state information self._assert_message_is_progress_started(await request.__anext__()) # check whether the second message gives us the information # about the requested thumbnail response = await request.__anext__() self._assert_message_is_ask_response(response, W24AskType.TITLE_BLOCK) # check whether we close the iteration correctly with self.assertRaises(StopAsyncIteration): await request.__anext__() # check whether the payload can be parsed correctly W24TitleBlock.parse_obj(response.payload_dict)
async def test_client_usernames(self): """ Test access username User Story: As API user I want to access my own username, so that I can verify that the login process worked correctly. """ client = W24TechreadClient.make_from_env(None) async with client as session: self.assertEqual(type(session.username), str)
async def test_license_path_invalid(self): """ Test Invalid License Path File User Story: As API user, I want to obtain an exception when the path to the license file as invalid, so that I can update it. """ with self.assertRaises(LicenseError): _ = W24TechreadClient.make_from_env(license_path="/invalid_path")
async def test_string_as_model_bytes(self) -> None: """ Test whether submitting a string as model_bytes raises the correct exception. See Github Issue #13 """ client = W24TechreadClient.make_from_env() with self.assertRaises(UnsupportedMediaType): async with client as session: await session.read_drawing(b"", asks=[], model="").__anext__()
async def test_license_invalid(self): """ Test Invalid License File User Story: As API user, I want to obtain an exception when the license that I supplied is invalid, so that I know that the license expired / was disabled / ... """ with self.assertRaises(UnauthorizedException): _ = W24TechreadClient.make_from_env( license_path=LICENSE_PATH_INVALID_CREDS)
async def test_upload_model(self) -> None: """ Test whether we can upload an associaed model. User Story: As API user, want to be able to upload an associated model file, so that I can support Werk24's training effort """ asks: List[W24Ask] = [W24AskVariantCAD(is_training=True)] drawing = get_drawing() model = get_model() client = W24TechreadClient.make_from_env(None) async with client as session: async for _ in session.read_drawing(drawing, asks, model): pass
async def test_unsupported_file_format(self) -> None: """ Unsupported file format triggers DRAWING_FILE_FORMAT_UNSUPPORTED? """ client = W24TechreadClient.make_from_env() asks: List[W24Ask] = [W24AskPageThumbnail()] async with client as session: async for message in session.read_drawing(b"", asks=asks): if message.message_type == W24TechreadMessageType.ASK: self.assertEqual( message.exceptions[0].exception_type, W24TechreadExceptionType. DRAWING_FILE_FORMAT_UNSUPPORTED)
async def test_client_without_session(self) -> None: """ Test whether Client without started session throws RuntimeError User Story: As API user I want to obtail a clear error message if I make a request to the client without entering a session, so that I can change my code swiftly. """ environs = os.environ client = W24TechreadClient(environs['W24TECHREAD_SERVER_HTTPS'], environs['W24TECHREAD_SERVER_WSS'], environs['W24TECHREAD_VERSION']) with self.assertRaises(RuntimeError): async with client: pass
async def test_uploading_huge_file(self) -> None: """ Huge file produces DRAWING_FILE_SIZE_TOO_LARGE? """ client = W24TechreadClient.make_from_env() asks: List[W24Ask] = [W24AskVariantCAD(is_training=True)] # create a new file that is larger than the limit of 5 MB file_size = 10 * 1024 * 1024 + 10 file = b"0" * file_size # perform the call async with client as session: async for msg in session.read_drawing(file, asks=asks): self.assertEqual( msg.exceptions[0].exception_type, W24TechreadExceptionType.DRAWING_FILE_SIZE_TOO_LARGE) return
def init_client(self) -> None: """ Initiate the TechreadClient from the environment variables. TODO: if we are not able to do this, we might want to TODO give the user a second chance an chose a license file """ try: self.client = W24TechreadClient.make_from_env() # Update the Status bar to reflect the current username username = self.client.username self.statusBar().showMessage(f"Logged in as {username}") # If a license error occured, let the user know. except LicenseError as exception: self._warn(f"LICENSE ERROR: {exception}") sys.exit()
async def test_cognito_error(self): """ Test UnauthorizedException if Cognito Identity is unavailable User Story: As API user, I want to obtain an exception when the Cognito service is down, so that I can retry. """ # mock the boto3 client to raise a ClientError boto3_client = boto3.client m = mock.Mock() m.side_effect = ClientError({}, {}) mock.patch('boto3.client', m).start() # assert with self.assertRaises(UnauthorizedException): _ = W24TechreadClient.make_from_env(None) # restore boto3.client = boto3_client
async def test_read_drawing_with_hooks(self): """ Test basic read drawing with hooks functionality User Story: As API user, I want to initiate a basic read request and receive the responses on the hooks so that I can implement asyncronous feedback loops with my user """ callback = mock.Mock() hooks = [Hook( ask=W24AskPageThumbnail(), function=callback )] client = W24TechreadClient.make_from_env(None) async with client as session: await session.read_drawing_with_hooks( DRAWING, hooks=hooks) self.assertTrue(callback.called)
def make_client() -> W24TechreadClient: """ Make the client. This will automatically fetch the authentication information from the environment variables. We will provide you with separate .env files for the development and production environments Returns: W24TechreadClient: Client instance """ try: return W24TechreadClient.make_from_env() # If a license error occured, let the user know. # NOTE: This will not catch deactivated users. # They will only learn about their status once # they send a request. except LicenseError as exception: print(f"LICENSE ERROR: {exception}") sys.exit()