def test_limit_negative(self, ctx, product_manager: ProductManager): with raises(IntMustBePositive): product_manager.search(ctx, limit=-1, offset=2, product_id=None, terms=None)
def test_happy_path(self, ctx, mock_product_repository: MagicMock, product_manager: ProductManager): # When... product_manager.delete(ctx, TEST_PRODUCT_NAME) # Expect... mock_product_repository.delete_product.assert_called_once_with( ctx, TEST_PRODUCT_NAME)
def test_product_not_found(self, ctx, mock_product_repository: ProductRepository, product_manager: ProductManager): mock_product_repository.search_product_by = MagicMock(return_value=([], 0)) with raises(ProductNotFoundError): product_manager.get_by_id(ctx, product_id=TEST_PRODUCT_ID)
def test_not_found(self, ctx, mock_product_repository: MagicMock, product_manager: ProductManager): # Given... mock_product_repository.delete_product = MagicMock( side_effect=ProductNotFoundError) # When... with raises(ProductNotFoundError): product_manager.delete(ctx, TEST_PRODUCT_NAME)
def test_happy_path(self, ctx, mock_product_repository: ProductRepository, sample_product: Product, product_manager: ProductManager): req = FullMutationRequest( name='panthere', selling_price=9999, buying_price=999, ) mock_product_repository.update_product = MagicMock() mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) product_manager.update_or_create(ctx, req, product_id=1) mock_product_repository.update_product.assert_called_once_with( ctx, **asdict(req), product_id=1)
def test_invalid_mutation_request( self, ctx, mock_product_repository: ProductRepository, sample_product: Product, field: str, value, product_manager: ProductManager): req = FullMutationRequest( name='', selling_price=999, buying_price=99, ) req = FullMutationRequest(**{**asdict(req), **{field: value}}) mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) mock_product_repository.update_product = MagicMock() with raises(ValueError): product_manager.update_or_create(ctx, req, product_id=None)
def test_happy_path(self, ctx, mock_product_repository: ProductRepository, sample_product: Product, product_manager: ProductManager): mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) result = product_manager.get_by_id(ctx, product_id=TEST_PRODUCT_ID) assert sample_product == result mock_product_repository.search_product_by.assert_called_once()
def test_missing_required_field(self, ctx, mock_product_repository: ProductRepository, sample_product: Product, product_manager: ProductManager): req = FullMutationRequest( name=None, selling_price=99, buying_price=999, ) req.community = None mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) mock_product_repository.update_product = MagicMock() with raises(MissingRequiredField): product_manager.update_or_create(ctx, req, product_id=None) mock_product_repository.update_product.assert_not_called()
def test_product_not_found(self, ctx, mock_product_repository: ProductRepository, sample_product: Product, product_manager: ProductManager): req = FullMutationRequest( name='MiNET', selling_price=9999, buying_price=999, ) mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) mock_product_repository.update_product = MagicMock( side_effect=ProductNotFoundError) with raises(ProductNotFoundError): product_manager.update_or_create(ctx, req, TEST_PRODUCT_ID) mock_product_repository.search_product_by.assert_called_once_with( ctx, product_id=TEST_PRODUCT_ID)
def test_happy_path(self, ctx, mock_product_repository: ProductRepository, sample_product: Product, product_manager: ProductManager): mock_product_repository.search_product_by = MagicMock( return_value=([sample_product], 1)) result, count = product_manager.search(ctx, limit=42, offset=2, product_id=None, terms='abc') assert [sample_product] == result assert 1 == count mock_product_repository.search_product_by.assert_called_once_with( ctx, limit=42, offset=2, product_id=None, terms='abc')
def init(testing=True): """ Initialize and wire together the dependency of the application. """ if testing: configuration = TEST_CONFIGURATION else: configuration = CONFIGURATION Database.init_db(configuration.DATABASE, testing=testing) # Repositories: ping_repository = PingSQLRepository() member_sql_repository = MemberSQLRepository() network_object_sql_repository = NetworkObjectSQLRepository() device_sql_repository = DeviceSQLRepository() room_sql_repository = RoomSQLRepository() elk_repository = ElasticSearchRepository(configuration) money_repository = MoneySQLRepository() switch_network_manager = SwitchSNMPNetworkManager() account_sql_repository = AccountSQLRepository() product_sql_repository = ProductSQLRepository() payment_method_sql_repository = PaymentMethodSQLRepository() transaction_sql_repository = TransactionSQLRepository() account_type_sql_repository = AccountTypeSQLRepository() # Managers health_manager = HealthManager(ping_repository) switch_manager = SwitchManager( switch_repository=network_object_sql_repository, ) port_manager = PortManager(port_repository=network_object_sql_repository, ) device_manager = DeviceManager( device_repository=device_sql_repository, member_repository=member_sql_repository, room_repository=room_sql_repository, vlan_repository=network_object_sql_repository, ip_allocator=device_sql_repository, ) member_manager = MemberManager( member_repository=member_sql_repository, money_repository=money_repository, membership_repository=member_sql_repository, logs_repository=elk_repository, configuration=configuration, ) room_manager = RoomManager(room_repository=room_sql_repository, ) account_manager = AccountManager( account_repository=account_sql_repository, member_repository=member_sql_repository, ) product_manager = ProductManager( product_repository=product_sql_repository, ) payment_method_manager = PaymentMethodManager( payment_method_repository=payment_method_sql_repository) transaction_manager = TransactionManager( transaction_repository=transaction_sql_repository, ) account_type_manager = AccountTypeManager( account_type_repository=account_type_sql_repository) # HTTP Handlers: health_handler = HealthHandler(health_manager) transaction_handler = TransactionHandler(transaction_manager) member_handler = MemberHandler(member_manager) device_handler = DeviceHandler(device_manager) room_handler = RoomHandler(room_manager) switch_handler = SwitchHandler(switch_manager) port_handler = PortHandler(port_manager, switch_manager, switch_network_manager) temporary_account_handler = TemporaryAccountHandler() account_type_handler = AccountTypeHandler(account_type_manager) payment_method_handler = PaymentMethodHandler(payment_method_manager) account_handler = AccountHandler(account_manager) product_handler = ProductHandler(product_manager) # Connexion will use this function to authenticate and fetch the information of the user. if os.environ.get('TOKENINFO_FUNC') is None: os.environ[ 'TOKENINFO_FUNC'] = 'src.interface_adapter.http_api.auth.token_info' app = connexion.FlaskApp(__name__, specification_dir='openapi') app.add_api( 'swagger.yaml', # resolver=RestyResolver('src.interface_adapter.http_api'), resolver=ADHResolver({ 'health': health_handler, 'transaction': transaction_handler, 'member': member_handler, 'device': device_handler, 'room': room_handler, 'switch': switch_handler, 'port': port_handler, 'temporary_account': temporary_account_handler, 'account_type': account_type_handler, 'payment_method': payment_method_handler, 'account': account_handler, 'product': product_handler, }), validate_responses=True, strict_validation=True, pythonic_params=True, auth_all_paths=True, ) app.app.config.update(configuration.API_CONF) return app
def product_manager(mock_product_repository): return ProductManager(product_repository=mock_product_repository)