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_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_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_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_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 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_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_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_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_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)
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_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_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_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_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