def test_get_with_a_failing_empty_path(self, app): # Given stock = Stock(price=1) # When with pytest.raises(GetPathError): offer_name = stock.get('offer.name')
def test_for_valid_relationship_dicts_with_nested_modifications(self, app): # Given offer_dict = {'name': 'foo', 'type': 'bar'} offer = Offer(**offer_dict) ApiHandler.save(offer) stock_dict1 = {'offerId': humanize(offer.id), 'price': 1} stock1 = Stock(**stock_dict1) ApiHandler.save(stock1) stock_dict1['id'] = humanize(stock1.id) stock_dict2 = {'offerId': humanize(offer.id), 'price': 2} stock2 = Stock(**stock_dict2) ApiHandler.save(stock2) stock_dict2['id'] = humanize(stock2.id) stock_dict2['price'] = 3 offer_dict['stocks'] = [stock_dict1, stock_dict2] offer.modify(offer_dict) # When ApiHandler.save(offer) # Then assert offer.name == offer_dict['name'] offer_stock1 = [s for s in offer.stocks if s.id == stock1.id][0] offer_stock2 = [s for s in offer.stocks if s.id == stock2.id][0] assert offer_stock1.id == stock1.id assert offer_stock1.price == stock_dict1['price'] assert offer_stock2.id == stock2.id assert offer_stock2.price == stock_dict2['price']
def test_create_or_modify_with_relationship_search(self, app): # Given offer = Offer.create_or_modify( { '__SEARCH_BY__': 'name', 'name': 'foo', 'type': 'bar' }, with_add=True, with_flush=True) stock1 = Stock.create_or_modify({ '__SEARCH_BY__': ['offer', 'price'], 'offer': offer, 'price': 2 }) # When stock2 = Stock.create_or_modify({ '__SEARCH_BY__': ['offer', 'price'], 'offer': offer, 'price': 3 }) #Then assert offer.id != None assert offer.name == 'foo' assert offer.type == 'bar' assert stock1.offer.id == offer.id assert stock2.offer.id == offer.id assert stock1.id == None assert stock2.id == None
def create_sell_order(cls, **kwargs): user_stock_quantity = UserStockQuantity.get_or_create( user_id=kwargs.get('user_id'), stock_id=kwargs.get('stock_id')) user_stock_quantity.quantity -= kwargs.get("quantity") instance = cls.create(**{**kwargs, "is_buy": False}) Stock.add_stock_quantity(kwargs.get('stock_id'), kwargs.get("quantity")) user_stock_quantity.save() return instance
def test_get_with_a_silent_empty_path_return(self, app): # Given stock = Stock(price=1) # When offer_name = stock.get('offer.name', with_get_path_error=False) # Then assert offer_name is None
def test_get_with_an_index_path_at_value(self, app): # Given offer = Offer(name='foo', type='bar') stock = Stock(offer=offer, price=1) # When stock_price = stock.get('offer.stocks.0.price') # Then assert stock_price == stock.price
def test_get_with_an_index_path_at_entity(self, app): # Given offer = Offer(name='foo', type='bar') stock1 = Stock(offer=offer, price=1) # When stock2 = stock1.get('offer.stocks.0') # Then assert stock1.id == stock2.id
def test_get_with_a_string_path(self, app): # Given offer = Offer(name='foo', type='bar') stock = Stock(offer=offer, price=1) # When offer_name = stock.get('offer.name') # Then assert offer_name == offer.name
def test_for_valid_relationship_dict(self): # Given test_object = Stock() offer_dict = {'name': 'foo', 'type': 'bar'} stock_dict = {'offer': offer_dict, 'price': 1} # When test_object.modify(stock_dict) # Then assert test_object.price == stock_dict['price'] assert test_object.offer.name == offer_dict['name']
def test_on_datetime_list_returns_string_with_date_in_ISO_8601_list(self): # Given offer = Offer() stock = Stock() stock.offer = offer stock.beginningDatetime = now stock.endDatetime = now + timedelta(hours=3) offer.stocks = [stock] # When serialized_list = serialize(offer.dateRange) # Then for date in serialized_list: self._assert_is_in_ISO_8601_format(date)
def test_modify_a_property_with_no_fset(self, app): # When stock = Stock() offer = Offer(name="foo", notDeletedStocks=[stock], type="bar") # Then assert offer.notDeletedStocks == []
def test_should_return_soft_deleted_entities_when_option_is_given(self): # given offer = Offer(name='foo', type='bar') stock1 = Stock(price=10, isSoftDeleted=True) stock2 = Stock(price=5, isSoftDeleted=False) offer.stocks = [stock1, stock2] includes = [{ 'key': 'stocks', 'includes': ['price'], 'with_soft_deleted_entities': True }] # when offer_dict = as_dict(offer, includes=includes) # then assert len(offer_dict['stocks']) == 2
def test_for_valid_relationships(self, app): # Given stock_dict1 = {'price': 1} stock1 = Stock(**stock_dict1) stock_dict2 = {'price': 2} stock2 = Stock(**stock_dict2) offer_dict = {'name': 'foo', 'stocks': [stock1, stock2], 'type': 'bar'} offer = Offer(**offer_dict) # When ApiHandler.save(offer) # Then assert offer.name == offer_dict['name'] offer_stock1 = [s for s in offer.stocks if s.id == stock1.id][0] offer_stock2 = [s for s in offer.stocks if s.id == stock2.id][0] assert offer_stock1.price == stock1.price assert offer_stock2.price == stock2.price
def test_create_or_modify_with_flatten_new_datum(self, app): # Given datum = { 'offer.name': 'foo', 'offer.offerTags.0.tag.label': 'bar', 'offer.type': 'bar', 'price': 2 } # When stock = Stock.create_or_modify(datum) # Then for (key, value) in datum.items(): assert stock.get(key) == value stock = Stock.create_or_modify(datum) for (key, value) in datum.items(): assert stock.get(key) == value
def test_for_valid_one_to_many_relationship(self, app): # Given offer = Offer(name='foo', type='bar') stock = Stock(offer=offer, price=1) # When ApiHandler.save(stock) # Then assert stock.offerId == offer.id
def sell_stock(self, quantity, brand): stock = Stock.query.filter(Stock.brand == brand).one_or_none() stock_infos = Stock.get_stock_infos_by_user_id(brand, self.id) if not stock_infos or (quantity > stock_infos.get('user_quantity')): return False else: Order.create_sell_order(quantity=quantity, user_id=self.id, stock_id=stock.id) self.funds += stock.price * quantity return True
def test_for_valid_relationship_dict_with_nested_creation(self, app): # Given offer_dict = {'name': 'foo', 'type': 'bar'} stock_dict = {'offer': offer_dict, 'price': 1} stock = Stock(**stock_dict) # When ApiHandler.save(stock) # Then assert stock.price == stock_dict['price'] assert stock.offer.name == offer_dict['name']
def test_dictify_with_relationships_includes(self, app): # given offer = Offer() stock = Stock() offer.stocks = [stock] # when stock_dict = as_dict(stock) offer_dict = as_dict(offer, includes=["stocks"]) # then assert 'stocks' in offer_dict assert len(offer_dict['stocks']) == 1 assert offer_dict['stocks'][0]['id'] == stock_dict['id']
def test_returns_total_data_count_with_has_more(self, app): # Given offer = Offer(name='foo', type='ThingType.JEUX_ABO') ApiHandler.save(offer) page = 4 paginate = 10 prices_length = 35 stocks = [] for price in range(prices_length): stock = Stock(price=price) stock.offer = offer stocks.append(stock) ApiHandler.save(*stocks) # When result = get_result(Stock, page=page, paginate=paginate, with_total_data_count=True) # Then assert len(result['data']) == page * paginate - prices_length assert result['has_more'] == False assert result['total_data_count'] == prices_length
def test_for_valid_relationship(self, app): # Given offer_dict = {'name': 'foo', 'type': 'bar'} offer = Offer(**offer_dict) ApiHandler.save(offer) stock_dict = {'offer': offer, 'price': 1} stock = Stock(**stock_dict) # When ApiHandler.save(stock) # Then assert stock.price == stock_dict['price'] assert stock.offer.id == offer.id assert stock.offer.name == offer_dict['name']
def test_for_valid_relationship_dict_with_nested_modification(self, app): # Given offer_dict = {'name': 'foo', 'type': 'bar'} offer = Offer(**offer_dict) ApiHandler.save(offer) offer_dict['id'] = humanize(offer.id) offer_dict['name'] = 'fooo' stock_dict = {'offer': offer_dict, 'price': 1} stock = Stock(**stock_dict) # When ApiHandler.save(stock) # Then assert stock.price == stock_dict['price'] assert stock.offer.id == offer.id assert stock.offer.name == offer_dict['name']
def test_create_or_modify_with_flatten_search_existing_datum(self, app): # Given offer = Offer(name='foo', type='bar') ApiHandler.save(offer) datum = { 'offer.name': 'foo', 'offer.__SEARCH_BY__': 'name', 'offer.type': 'bar', 'price': 2, } # When stock = Stock.create_or_modify(datum) # Then for (key, value) in datum.items(): if key.endswith('__SEARCH_BY__'): continue assert stock.get(key) == value assert stock.offer.id == offer.id
def test_return_only_not_soft_deleted_stocks(self, app): # Given offer = Offer(name='foo', type='ThingType.JEUX_ABO') ApiHandler.save(offer) stock1 = Stock(price=1) stock1.offer = offer stock2 = Stock(price=2) stock2.offer = offer stock3 = Stock(price=3) stock3.offer = offer stock4 = Stock(price=4) stock4.offer = offer stock1.isSoftDeleted = True ApiHandler.save(stock1, stock2, stock3, stock4) # When result = get_result(Stock) data = result['data'] # Then assert data[0]['id'] == humanize(stock2.id) assert data[1]['id'] == humanize(stock3.id) assert data[2]['id'] == humanize(stock4.id)