Commit 3820fd0a authored by Kirill Smelkov's avatar Kirill Smelkov

py2: *: tests: Use OrderedDict when preparing messages

On py3 dict is guaranteed to be ordered by sequence of how it was
inserted into, but on py2 there is no such guarantee. -> Explicitly
order entries generated by tests so that their serialization is stable
and matches what is expected.
parent e6b5e156
......@@ -26,6 +26,7 @@ from golang import func, defer, b
import io, json, re
from pytest import raises
from collections import OrderedDict as odict
# tLogMeasure provides LogMeasure testing environment.
......@@ -572,8 +573,8 @@ def test_LogMeasure_sync():
# jstats returns json-encoded stats message corresponding to counters dict.
# tau goes directly to stats['utc'] as is.
def jstats(tau, counters): # -> str
g_cc = {} # global
cell_cc = {} # per-cell
g_cc = odict() # global
cell_cc = odict() # per-cell
for cc, value in counters.items():
if cc.startswith("rrc_"):
......@@ -581,18 +582,18 @@ def jstats(tau, counters): # -> str
else:
g_cc[cc] = value
s = {
"message": "stats",
"utc": tau,
"cells": {"1": {"counters": {"messages": cell_cc}}},
"counters": {"messages": g_cc},
}
s = odict((
("message", "stats"),
("utc", tau),
("cells", {"1": {"counters": {"messages": cell_cc}}}),
("counters", {"messages": g_cc}),
))
return json.dumps(s)
def test_jstats():
assert jstats(0, {}) == '{"message": "stats", "utc": 0, "cells": {"1": {"counters": {"messages": {}}}}, "counters": {"messages": {}}}'
assert jstats(123.4, {"rrc_x": 1, "s1_y": 2, "rrc_z": 3, "x2_zz": 4}) == \
assert jstats(123.4, odict((("rrc_x", 1), ("s1_y", 2), ("rrc_z", 3), ("x2_zz", 4)))) == \
'{"message": "stats", "utc": 123.4, "cells": {"1": {"counters": {"messages": {"rrc_x": 1, "rrc_z": 3}}}}, "counters": {"messages": {"s1_y": 2, "x2_zz": 4}}}'
......@@ -609,19 +610,19 @@ def jdrb_stats(tau, qci_dlul): # -> str
dlul["dl_tx_time_notailtti_err"] = 0 # ----//----
dlul["ul_tx_time_notailtti_err"] = 0 #
s = {
"message": "x.drb_stats",
"utc": tau,
"qci_dict": qci_dlul,
}
s = odict((
("message", "x.drb_stats"),
("utc", tau),
("qci_dict", qci_dlul),
))
return json.dumps(s)
def test_jdrb_stats():
# NOTE json encodes 5 and 9 keys are strings, not integers
x = 0.01
assert jdrb_stats(100, {5: drb_trx(0.1,1234, 0.2,4321),
9: drb_trx(1.1,7777, 1.2,8888)}) == ( \
assert jdrb_stats(100, odict(((5, drb_trx(0.1,1234, 0.2,4321)),
((9, drb_trx(1.1,7777, 1.2,8888)))))) == ( \
'{"message": "x.drb_stats", "utc": 100, "qci_dict":' + \
' {"5": {"dl_tx_bytes": 1234, "dl_tx_time": %(0.1+x)r, "dl_tx_time_notailtti": %(0.1-x)r,' + \
' "ul_tx_bytes": 4321, "ul_tx_time": %(0.2+x)r, "ul_tx_time_notailtti": %(0.2-x)r,' + \
......@@ -638,8 +639,10 @@ def test_jdrb_stats():
# drb_trx returns dict describing dl/ul transmissions of a data radio bearer.
# such dict is used as per-QCI entry in x.drb_stats
def drb_trx(dl_tx_time, dl_tx_bytes, ul_tx_time, ul_tx_bytes):
return {"dl_tx_bytes": dl_tx_bytes, "dl_tx_time": dl_tx_time + 0.01, "dl_tx_time_notailtti": dl_tx_time - 0.01,
"ul_tx_bytes": ul_tx_bytes, "ul_tx_time": ul_tx_time + 0.01, "ul_tx_time_notailtti": ul_tx_time - 0.01}
return odict((
("dl_tx_bytes", dl_tx_bytes), ("dl_tx_time", dl_tx_time + 0.01), ("dl_tx_time_notailtti", dl_tx_time - 0.01),
("ul_tx_bytes", ul_tx_bytes), ("ul_tx_time", ul_tx_time + 0.01), ("ul_tx_time_notailtti", ul_tx_time - 0.01),
))
# ionone returns empty data source.
......
......@@ -26,6 +26,7 @@ import io
import json
from pytest import raises
from collections import OrderedDict as odict
@func
......@@ -180,19 +181,19 @@ def test_Reader_readahead_vs_eof():
@func
def test_Reader_timestamp_from_sync_wo_utc():
def jevent(time, event, args_dict={}):
d = {
"event": event,
"time" : time,
}
d = odict((
("event", event),
("time", time),
))
d.update(args_dict)
return json.dumps({"meta": d})
def jsync(time, srv_time):
d = {
"state": "attached" if srv_time is not None else "detached",
"reason": "periodic",
"generator": "...",
}
d = odict((
("state", "attached" if srv_time is not None else "detached"),
("reason", "periodic"),
("generator", "..."),
))
if srv_time is not None:
d['srv_time'] = srv_time
return jevent(time, "sync", d)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment