def test_bind_concrete_base(): engine = bloop.Engine() engine.client = Mock(spec=bloop.client.Client) class Concrete(bloop.new_base()): pass engine.bind(base=Concrete) engine.client.create_table.assert_called_once_with(Concrete) engine.client.validate_table.assert_called_once_with(Concrete)
def test_unbound_engine_view(): """Trying to mutate an unbound model through an EngineView fails""" class UnboundModel(bloop.new_base()): id = bloop.Column(bloop.String, hash_key=True) instance = UnboundModel(id="foo") with pytest.raises(bloop.exceptions.UnboundModel): with bloop.Engine().context() as view: view._dump(UnboundModel, instance)
def test_bind_different_engines(): first_engine = bloop.Engine() first_engine.client = Mock(spec=bloop.client.Client) second_engine = bloop.Engine() second_engine.client = Mock(spec=bloop.client.Client) class Concrete(bloop.new_base()): pass first_engine.bind(base=Concrete) second_engine.bind(base=Concrete) # Create/Validate are only called once per model, regardless of how many # times the model is bound to different engines first_engine.client.create_table.assert_called_once_with(Concrete) first_engine.client.validate_table.assert_called_once_with(Concrete) second_engine.client.create_table.assert_not_called() second_engine.client.validate_table.assert_not_called() # The model (and its columns) are bound to each engine's TypeEngine, # regardless of how many times the model has been bound already assert Concrete in first_engine.type_engine.bound_types assert Concrete in second_engine.type_engine.bound_types
def test_bind_skip_abstract_models(): class Abstract(bloop.new_base()): class Meta: abstract = True class Concrete(Abstract): pass class AlsoAbstract(Concrete): class Meta: abstract = True class AlsoConcrete(AlsoAbstract): pass engine = bloop.Engine() engine.client = Mock(spec=bloop.client.Client) engine.bind(base=Abstract) engine.client.create_table.assert_any_call(Concrete) engine.client.validate_table.assert_any_call(Concrete) engine.client.create_table.assert_any_call(AlsoConcrete) engine.client.validate_table.assert_any_call(AlsoConcrete)
def engine(): engine = bloop.Engine() engine.client = Mock(spec=bloop.client.Client) engine.bind(base=BaseModel) return engine
def build_engine(profile_name) -> bloop.Engine: session = boto3.Session(profile_name=profile_name) dynamodb = session.client("dynamodb") dynamodbstreams = session.client("dynamodbstreams") engine = bloop.Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams) return engine
def test_bind_non_model(): """Can't bind things that don't subclass new_base()""" engine = bloop.Engine() engine.client = Mock(spec=bloop.client.Client) with pytest.raises(ValueError): engine.bind(base=object())