def test_name_withquality_and_stuff(self, test_app_context, a): connection = db.get_redis_db_from_context() thing_data = a a = Thing.from_dict(thing_data) self.setup_data_in_db(a, connection) b = Thing.get_from_database(thing_data, connection) assert b.Hash == '4f57967468deccdb718432a13fdd55eef9f8bd4f'
def test_save_and_load(self, test_app_context): connection = db.get_redis_db_from_context() d = { 'Name': 'TestSaveAndLoad', 'Quality': 'Good', 'StuffType': 'Silver', 'UseServerPrice': True, 'CurrentBuyPrice': 19.99, 'CurrentSellPrice': 13.99, 'BaseMarketValue': 15.99, 'Quantity': 100 } o_save = Thing.from_dict(d) od = o_save.to_dict() # Remove the key from the index if it already exists connection.srem(consts.KEY_THING_INDEX, o_save.Hash) # Write the Thing to the database. o_save.save_to_database(connection) # It should be near the end of the list. is_member = connection.sismember(consts.KEY_THING_INDEX, o_save.Hash) assert is_member # Reload it as a new object o_loaded = Thing.get_from_database(o_save.to_dict(), connection) assert o_loaded.FromDatabase assert o_loaded.Hash == o_save.Hash for k, v in o_loaded.to_dict().items(): assert od[k] == v
def test_all_exist(self, test_app_context, a, b, c): connection = db.get_redis_db_from_context() thing_data = [a, b, c] self.setup_data_in_db(Thing.from_dict(a), get_redis_db_from_context()) self.setup_data_in_db(Thing.from_dict(b), get_redis_db_from_context()) self.setup_data_in_db(Thing.from_dict(c), get_redis_db_from_context()) result = Thing.get_many_from_database(thing_data, connection).values() s = zip(thing_data, result) for a, b in s: assert a['Name'] == b.Name and b.FromDatabase
def test_one_doesnt_exist(self, test_app_context, a, c): connection = db.get_redis_db_from_context() b = {'Name': 'Nonexistent', 'StuffType': 'WoodenLog'} thing_data = [a, b, c] self.setup_data_in_db(Thing.from_dict(a), get_redis_db_from_context()) self.setup_data_in_db(Thing.from_dict(c), get_redis_db_from_context()) result = list( Thing.get_many_from_database(thing_data, connection).values()) assert len(result) == 3 assert result[0].Name == a['Name'] and result[0].FromDatabase assert result[1].Name == b['Name'] and not result[1].FromDatabase assert result[2].Name == c['Name'] and result[2].FromDatabase
def test_one_in_many_not_exist(self, test_app_context, a, c): connection = get_redis_db_from_context() b = {'Name': 'Nonexistent', 'StuffType': 'WoodenLog'} thing_data = [a, b, c] self.setup_data_in_db(Thing.from_dict(a), connection) self.setup_data_in_db(Thing.from_dict(c), connection) result = list( OrderThing.many_from_dict_and_check_exists(thing_data, connection).values()) assert len(result) == 3 assert result[0].Name == a['Name'] and result[0].ThingExists assert result[1].Name == b['Name'] and not result[1].ThingExists assert result[2].Name == c['Name'] and result[2].ThingExists
def test_all_exist(self, test_app_context, a, b, c): connection = get_redis_db_from_context() thing_data = [a, b, c] self.setup_data_in_db(Thing.from_dict(a), connection) self.setup_data_in_db(Thing.from_dict(b), connection) self.setup_data_in_db(Thing.from_dict(c), connection) result = OrderThing.many_from_dict_and_check_exists( thing_data, connection).values() s = zip(thing_data, result) for a, b in s: assert a['Name'] == b.Name and b.ThingExists
def test_class_init_from_dict(self, a): o = Thing.from_dict(a) assert o.Name == a['Name'] assert o.Quality == a['Quality'] assert o.StuffType == a['StuffType']
def test_class_init_from_dict_to_dict(self, a): o = Thing.from_dict(a) r = o.to_dict() for k, v in a.items(): assert r[k] == v
def test_one_exists(self, test_app_context, a): connection = get_redis_db_from_context() self.setup_data_in_db(Thing.from_dict(a), connection) result = OrderThing.from_dict_and_check_exists(a, connection) assert a['Name'] == result.Name assert result.ThingExists
def setup_data_in_db(thing: Thing, connection): do_exec = False if not isinstance(connection, redis.client.Pipeline): connection = connection.pipeline() do_exec = True connection.delete(consts.KEY_THING_META.format(thing.Hash)) # Remove the key from the index if it already exists connection.srem(consts.KEY_THING_INDEX, thing.Hash) # Write the Thing to the database. thing.save_to_database(connection) if do_exec: connection.execute()
def test_load_modify_save(self, test_app_context): connection = db.get_redis_db_from_context() d = { 'Name': 'TestSaveAndLoadDifference', 'Quality': 'Good', 'StuffType': 'Silver', 'UseServerPrice': True, 'CurrentBuyPrice': 19.99, 'CurrentSellPrice': 13.99, 'BaseMarketValue': 15.99, 'Quantity': 100 } a = Thing.from_dict(d) ad = a.to_dict() # Remove the key from the index if it already exists connection.srem(consts.KEY_THING_INDEX, a.Hash) # Write the Thing to the database. a.save_to_database(connection) # It should be near the end of the list. is_member = connection.sismember(consts.KEY_THING_INDEX, a.Hash) assert is_member # Reload it as a new object b = Thing.get_from_database(a.to_dict(), connection) assert b.Hash == a.Hash for k, v in b.to_dict().items(): assert ad[k] == v b.Quantity = 50 b.save_to_database(connection) c = Thing.from_dict( connection.hgetall(consts.KEY_THING_META.format(a.Hash))) assert c.Quantity == 50
def test_can_deserialize_into_thing(self, test_app_context, order_a, thing_a_bought): connection = get_redis_db_from_context() self.setup_order_data_in_db(order_a, connection) self.setup_data_in_db(Thing.from_dict(thing_a_bought), connection) order_a = Order.get_from_database_by_hash(order_a.Hash, connection) things_bought_from_gwp = Thing.get_many_from_database( order_a.ThingsBoughtFromGwp, connection) first_item = list(things_bought_from_gwp.values())[0] thing = Thing.get_from_database(thing_a_bought, connection) assert thing.FromDatabase assert thing.Quantity == thing_a_bought['Quantity'] assert thing.Hash == first_item.Hash
def test_app_context(): a = app.make_app() a.config['TESTING'] = True a.app_context().push() g._database = get_redis_database_connection(db_number=0, redis_client=fakeredis.FakeRedis) pipe = g._database.pipeline() pipe.set(consts.KEY_CONFIGURATION_PRIME_COST, 300, nx=True) pipe.set(consts.KEY_API_VERSION, '1.2', nx=True) pipe.set(consts.KEY_API_VERSION_SUPPORTED, '1.1', nx=True) pipe.hset(consts.KEY_CONFIGURATION_ORDERS, consts.HASH_KEY_ORDER_TICK_DELAY, '60000') pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_START, 23) pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_LENGTH, 3600) pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_PREABMLE, 7200) pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_NEXT, 0) pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_SET, "false") pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_BUY_PRICE_MULTIPLIER, "0.20") pipe.hmset(consts.KEY_API_MAINTENANCE_WINDOW, {"Start": "1551049200", "Stop": "1551052800"}) pipe.hset(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_IGNORE_THINGS, json.dumps( ['8697f432058b914ba2b20c5bd6f0678548126e21', 'cdf9187a28bcb1b219a3a4aeaf3c99a65e7eb882']) ) pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_SELL_PRICE_MULTIPLIER, "0.75") pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MIN_SELL_PRICE_MULTIPLIER, "0.2") breakpoints = [ {'PriceStart': 0, 'PriceStop': 5, 'StockMin': 1000, 'StockMax': 100000, 'CapBuyPrice': 2.5}, {'PriceStart': 5, 'PriceStop': 25, 'StockMin': 200, 'StockMax': 250, 'CapBuyPrice': 2.75}, {'PriceStart': 25, 'PriceStop': 50, 'StockMin': 100, 'StockMax': 150, 'CapBuyPrice': 3.0}, {'PriceStart': 50, 'PriceStop': 100, 'StockMin': 10, 'StockMax': 15, 'CapBuyPrice': 3.5}, {'PriceStart': 100, 'PriceStop': 100000, 'StockMin': 1, 'StockMax': 15, 'CapBuyPrice': 4.0}, {'PriceStart': 100000, 'PriceStop': 200000, 'StockMin': 1, 'StockMax': 5, 'CapBuyPrice': 4.5} ] pipe.hset(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_PRICEBREAKS, json.dumps(breakpoints)) thing = Thing("Silver") thing.save_to_database(pipe) pipe.execute() yield a
def subscription_update(colony_hash): db_connection = db.get_redis_db_from_context() colony = Colony.get_from_database_by_hash(colony_hash, db.get_redis_db_from_context()) if not colony: current_app.logger.warning('{} colony not found in database'.format(colony.Hash)) return Response(consts.ERROR_NOT_FOUND, status=consts.HTTP_NOT_FOUND) sub_data = request.json # Validate token if 'Token' not in sub_data: current_app.logger.error('{} Subscription token was not in payload.'.format(colony.Hash)) return Response(consts.ERROR_INVALID, status=consts.HTTP_INVALID) # Fetch our token from DB token_in_db = db_connection.get(consts.KEY_PRIME_TOKEN_DATA.format(colony.Hash)) # Has it expired or ever existed? if token_in_db is None: current_app.logger.warning('{} Subscription token was not in database or has expired.'.format(colony.Hash)) return Response(consts.ERROR_INVALID, status=consts.HTTP_INVALID) # They should match if token_in_db != sub_data['Token']: current_app.logger.error( '{} Subscription tokens did not match {} != {}.'.format(colony.Hash, sub_data['Token'], token_in_db)) return Response(consts.ERROR_INVALID, status=consts.HTTP_INVALID) expiryTick = DaysPerQuadrum * TicksPerDay + colony.LastGameTick pipe = db_connection.pipeline() # Update subscription tick for Colony. pipe.set(consts.KEY_PRIME_SUBSCRIPTION_DATA.format(colony.Hash), int(expiryTick)) # Remove the token to prevent reuse. pipe.delete(consts.KEY_PRIME_TOKEN_DATA.format(colony_hash)) # Subscriptions expire after 42 days in real life. pipe.expireat(consts.KEY_PRIME_SUBSCRIPTION_DATA.format(colony.Hash), date_utils.add_days_to_current_time(30)) # Update Silver acquired. thing = Thing("Silver") subscriptionCost = int(db_connection.get(consts.KEY_CONFIGURATION_PRIME_COST)) pipe.hincrby(consts.KEY_THING_META.format(thing.Hash), 'Quantity', subscriptionCost) pipe.execute() current_app.logger.debug('{} Subscription successful.'.format(colony.Hash)) return Response("OK", status=consts.HTTP_OK)
def fallback_on_def(request, translate_items): need_fallback = [] for thing_hash, name in translate_items.items(): if name is None: need_fallback.append(thing_hash) connection = get_redis_database_connection(request) things = Thing.get_many_from_database_by_hash_without_history(need_fallback, connection) for thing_hash, thing in things.items(): translate_items[thing_hash] = thing.FullName return translate_items
def test_all_none(self, test_app_context): connection = db.get_redis_db_from_context() a = {'Name': 'Nonexistent1', 'Quality': 'Good'} b = {'Name': 'Nonexistent2', 'StuffType': 'WoodenLog'} c = { 'Name': 'Nonexistent3', 'Quality': 'Good', 'StuffType': 'WoodenLog' } thing_data = [a, b, c] result = Thing.get_many_from_database(thing_data, connection).values() s = list(zip(thing_data, result)) assert len(s) == 3 for a, b in s: assert not b.FromDatabase
def test_can_deserialize_into_order_thing(self, test_app_context, order_a, thing_a_bought): connection = get_redis_db_from_context() self.setup_order_data_in_db(order_a, connection) self.setup_data_in_db(Thing.from_dict(thing_a_bought), connection) order_a = Order.get_from_database_by_hash(order_a.Hash, connection) things_bought_from_gwp = OrderThing.many_from_dict_and_check_exists( order_a.ThingsBoughtFromGwp, connection) first_item = list( things_bought_from_gwp.values())[0].to_dict(keep_quantity=True) for k, v in thing_a_bought.items(): assert first_item[k] == v
def colony_set_supported_things(colony_hash: str): print("Received Supported things list from colony") colony = Colony.get_from_database_by_hash(colony_hash, db.get_redis_db_from_context()) if not colony: return Response(consts.ERROR_NOT_FOUND, status=consts.HTTP_NOT_FOUND) try: gz_post_data = request.files['things'] # Decompress payload thing_file = gzip.GzipFile(fileobj=gz_post_data, mode='r') # Deserialize JSON back in to {'Locale': str, 'Things': List[Dict[str, str]]} payload = json.loads(thing_file.read().decode('UTF8')) # Set locale locale = payload['Locale'].lower() # This is a List[Dict[str, str]] supported_things_json = payload['Things'] print("Supported things parsed") except json.JSONDecodeError: print("Error in things list found") return Response(consts.ERROR_INVALID, status=consts.HTTP_INVALID) # Need to construct things then add Localized name to index pipe = db.get_redis_db_from_context().pipeline() pipe.sadd(consts.KEY_THING_LOCALE_KNOWN_LANGUAGES, locale) for thing_json in supported_things_json: thing = Thing.from_dict(thing_json) pipe.zincrby(KEY_THING_LOCALE_THING_NAMES.format(locale, thing.Hash), 1, thing.LocalizedName) pipe.execute() # This is immediately saved. colony.SupportedThings = supported_things_json print("Supported things saved") return Response('OK', status=200, mimetype='application/json')
def market_get_items(colony_hash): print("Sending Market items to Colony") connection = db.get_redis_db_from_context() colony = Colony.get_from_database_by_hash(colony_hash, db.get_redis_db_from_context()) if not colony: return Response(consts.ERROR_NOT_FOUND, status=consts.HTTP_NOT_FOUND) things = Thing.get_many_from_database(colony.SupportedThings, connection) #thing_data = [thing.to_dict() for thing in things.values()] thing_data = [ thing.to_dict() for thing in things.values() if thing.Quantity > 0 ] print("Market items sent") content = gzip.compress(json.dumps(thing_data).encode('utf8'), 5) response = make_response(content, 200) response.headers['Content-length'] = len(content) response.headers['Content-Encoding'] = 'gzip' response.headers['Content-Type'] = "application/json" return response
with open('{}_thing_data_{}.json'.format(version, language), 'r', encoding='utf-8') as json_file: json_data = json.load(json_file) for thing_data in json_data: # Don't add poor quality stuff to DB if 'Quality' in thing_data and thing_data['Quality'] == "Poor": continue thing = Thing.from_dict( { 'Name': thing_data['Name'], 'Quality': thing_data['Quality'] if thing_data['Quality'] is not None else "", 'StuffType': thing_data['StuffType'] if thing_data['StuffType'] is not None else "", 'BaseMarketValue': thing_data['BaseMarketValue'], 'MinifiedContainer': thing_data['MinifiedContainer'], 'UseServerPrice': True, 'CurrentBuyPrice': thing_data['BaseMarketValue'], 'CurrentSellPrice': thing_data['BaseMarketValue'] }) if not translate_only: thing.save_to_database(pipe) if not language_added: pipe.sadd(lib.gwpcc.consts.KEY_THING_LOCALE_KNOWN_LANGUAGES, thing_data['LanguageCode']) language_added = True # Set full name for Thing, Ours is the version of truth, give it a high score. pipe.zadd(lib.gwpcc.consts.KEY_THING_LOCALE_THING_NAMES.format(thing_data['LanguageCode'], thing.Hash),
def test_name_with_quality(self, test_app_context, b): connection = db.get_redis_db_from_context() a = Thing.from_dict(b) self.setup_data_in_db(a, connection) b = Thing.get_from_database(b, connection) assert b.Hash == 'aafd448dfc6644cd92bdab58470a5f52d56677ec'
def test_nonexistent(self, test_app_context): connection = db.get_redis_db_from_context() thing_data = {'Name': 'Doesn\'t_exist'} assert not Thing.get_from_database(thing_data, connection).FromDatabase
def test_name_with_stuff(self, test_app_context, c): connection = db.get_redis_db_from_context() a = Thing.from_dict(c) self.setup_data_in_db(a, connection) b = Thing.get_from_database(c, connection) assert b.Hash == 'f5000009817fdc766c908591571982ace2f211d0'
def test_class_init(self): o = Thing('Test') assert o.Name == 'Test'
def test_class_init_from_dict_hash(self, a): o = Thing.from_dict(a) assert '4f57967468deccdb718432a13fdd55eef9f8bd4f' == o.Hash