def test_wrong_labels(self): # Normal set with self.assertRaises(ValueError) as context: self.c.set_value({"job": 1, "ok": 2}, 1) self.assertEqual("Invalid label name: job", str(context.exception)) with self.assertRaises(ValueError) as context: self.c.set_value({"__not_ok": 1, "ok": 2}, 1) self.assertEqual("Invalid label prefix: __not_ok", str(context.exception)) # Constructor set with self.assertRaises(ValueError) as context: Collector("x", "y", {"job": 1, "ok": 2}) self.assertEqual("Invalid label name: job", str(context.exception)) with self.assertRaises(ValueError) as context: Collector("x", "y", {"__not_ok": 1, "ok": 2}) self.assertEqual("Invalid label prefix: __not_ok", str(context.exception))
def test_not_help_text(self): with self.assertRaises(TypeError) as context: del self.data['doc'] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'doc'", str(context.exception))
def setUp(self): self.data = { 'name': "logged_users_total", 'doc': "Logged users in the application", 'const_labels': {"app": "my_app"}, } self.c = Collector(**self.data)
def setUp(self): self.data = { "name": "logged_users_total", "doc": "Logged users in the application", "const_labels": { "app": "my_app" }, } self.c = Collector(**self.data)
def test_register_sames(self): ''' check registering same metrics raises exceptoion ''' r = CollectorRegistry() r.register(Collector(**self.data)) with self.assertRaises(ValueError) as context: r.register(Collector(**self.data)) self.assertEqual( "Collector {} is already registered".format(self.data['name']), str(context.exception))
def test_deregister(self): ''' check collectors can be deregistered ''' r = CollectorRegistry() r.register(Collector(**self.data)) r.deregister(self.data['name']) self.assertEqual(0, len(r.collectors))
def test_deregister(self): """ check collectors can be deregistered """ r = CollectorRegistry() r.register(Collector(**self.data)) r.deregister(self.data["name"]) self.assertEqual(0, len(r.collectors))
def setUp(self): self.data = { "name": "logged_users_total", "doc": "Logged users in the application", "const_labels": {"app": "my_app"}, } self.c = Collector(**self.data)
def test_not_help_text(self): with self.assertRaises(TypeError) as context: del self.data["doc"] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'doc'", str(context.exception), )
def test_register(self): """ check collectors can be registered """ q = 100 collectors = [ Collector("test" + str(i), "Test" + str(i)) for i in range(q) ] r = CollectorRegistry() for i in collectors: r.register(i) self.assertEqual(q, len(r.collectors))
def test_register(self): ''' check collectors can be registered ''' q = 100 collectors = [ Collector('test' + str(i), 'Test' + str(i)) for i in range(q) ] r = CollectorRegistry() for i in collectors: r.register(i) self.assertEqual(q, len(r.collectors))
def test_get_all(self): q = 100 collectors = [ Collector("test" + str(i), "Test" + str(i)) for i in range(q) ] r = CollectorRegistry() for i in collectors: r.register(i) result = r.get_all() self.assertTrue(isinstance(result, list)) self.assertEqual(q, len(result))
def test_wrong_format(self): self.data = { "name": "logged_users_total", "doc": "Logged users in the application", "const_labels": { "app": "my_app" }, } f = text.TextFormatter() c = Collector(**self.data) with self.assertRaises(TypeError) as context: f.marshall_collector(c) self.assertEqual("Not a valid object format", str(context.exception))
def test_wrong_format(self): data = { 'name': "logged_users_total", 'doc': "Logged users in the application", 'const_labels': { "app": "my_app" }, } f = BinaryFormatter() c = Collector(**data) with self.assertRaises(TypeError) as context: f.marshall_collector(c) self.assertEqual('Not a valid object format', str(context.exception))
def test_get(self): r = CollectorRegistry() c = Collector(**self.data) r.register(c) self.assertEqual(c, r.get(c.name))
class TestCollectorDict(unittest.TestCase): def setUp(self): self.data = { "name": "logged_users_total", "doc": "Logged users in the application", "const_labels": {"app": "my_app"}, } self.c = Collector(**self.data) def test_initialization(self): self.assertEqual(self.data["name"], self.c.name) self.assertEqual(self.data["doc"], self.c.doc) self.assertEqual(self.data["const_labels"], self.c.const_labels) def test_set_value(self): data = ( ({"country": "sp", "device": "desktop"}, 520), ({"country": "us", "device": "mobile"}, 654), ({"country": "uk", "device": "desktop"}, 1001), ({"country": "de", "device": "desktop"}, 995), ) for m in data: self.c.set_value(m[0], m[1]) self.assertEqual(len(data), len(self.c.values)) def test_same_value(self): data = ( ({"country": "sp", "device": "desktop", "ts": "GMT+1"}, 520), ({"ts": "GMT+1", "country": "sp", "device": "desktop"}, 521), ({"country": "sp", "ts": "GMT+1", "device": "desktop"}, 522), ({"device": "desktop", "ts": "GMT+1", "country": "sp"}, 523), ) for m in data: self.c.set_value(m[0], m[1]) self.assertEqual(1, len(self.c.values)) self.assertEqual(523, self.c.values[data[0][0]]) def test_get_value(self): data = ( ({"country": "sp", "device": "desktop"}, 520), ({"country": "us", "device": "mobile"}, 654), ({"country": "uk", "device": "desktop"}, 1001), ({"country": "de", "device": "desktop"}, 995), ) for m in data: self.c.set_value(m[0], m[1]) for m in data: self.assertEqual(m[1], self.c.get_value(m[0])) def test_not_const_labels(self): del self.data["const_labels"] self.c = Collector(**self.data) def test_not_name(self): with self.assertRaises(TypeError) as context: del self.data["name"] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'name'", str(context.exception), ) def test_not_help_text(self): with self.assertRaises(TypeError) as context: del self.data["doc"] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'doc'", str(context.exception), ) def test_without_labels(self): data = (({}, 520), (None, 654), ("", 1001)) for i in data: self.c.set_value(i[0], i[1]) self.assertEqual(1, len(self.c.values)) self.assertEqual((data)[len(data) - 1][1], self.c.values[data[0][0]]) def test_wrong_labels(self): # Normal set with self.assertRaises(ValueError) as context: self.c.set_value({"job": 1, "ok": 2}, 1) self.assertEqual("Invalid label name: job", str(context.exception)) with self.assertRaises(ValueError) as context: self.c.set_value({"__not_ok": 1, "ok": 2}, 1) self.assertEqual("Invalid label prefix: __not_ok", str(context.exception)) # Constructor set with self.assertRaises(ValueError) as context: Collector("x", "y", {"job": 1, "ok": 2}) self.assertEqual("Invalid label name: job", str(context.exception)) with self.assertRaises(ValueError) as context: Collector("x", "y", {"__not_ok": 1, "ok": 2}) self.assertEqual("Invalid label prefix: __not_ok", str(context.exception)) def test_get_all(self): data = ( ({"country": "sp", "device": "desktop"}, 520), ({"country": "us", "device": "mobile"}, 654), ({"country": "uk", "device": "desktop"}, 1001), ({"country": "de", "device": "desktop"}, 995), ({"country": "zh", "device": "desktop"}, 520), ({"country": "ch", "device": "mobile"}, 654), ({"country": "ca", "device": "desktop"}, 1001), ({"country": "jp", "device": "desktop"}, 995), ({"country": "au", "device": "desktop"}, 520), ({"country": "py", "device": "mobile"}, 654), ({"country": "ar", "device": "desktop"}, 1001), ({"country": "pt", "device": "desktop"}, 995), ) for i in data: self.c.set_value(i[0], i[1]) def country_fetcher(x): return x[0]["country"] sorted_data = sorted(data, key=country_fetcher) sorted_result = sorted(self.c.get_all(), key=country_fetcher) self.assertEqual(sorted_data, sorted_result)
def test_not_const_labels(self): del self.data["const_labels"] self.c = Collector(**self.data)
def test_not_const_labels(self): del self.data['const_labels'] self.c = Collector(**self.data)
class TestCollectorDict(unittest.TestCase): def setUp(self): self.data = { 'name': "logged_users_total", 'doc': "Logged users in the application", 'const_labels': {"app": "my_app"}, } self.c = Collector(**self.data) def test_initialization(self): self.assertEqual(self.data['name'], self.c.name) self.assertEqual(self.data['doc'], self.c.doc) self.assertEqual(self.data['const_labels'], self.c.const_labels) def test_set_value(self): data = ( ({'country': "sp", "device": "desktop"}, 520), ({'country': "us", "device": "mobile"}, 654), ({'country': "uk", "device": "desktop"}, 1001), ({'country': "de", "device": "desktop"}, 995), ) for m in data: self.c.set_value(m[0], m[1]) self.assertEqual(len(data), len(self.c.values)) def test_same_value(self): data = ( ({'country': "sp", "device": "desktop", "ts": "GMT+1"}, 520), ({"ts": "GMT+1", 'country': "sp", "device": "desktop"}, 521), ({'country': "sp", "ts": "GMT+1", "device": "desktop"}, 522), ({"device": "desktop", "ts": "GMT+1", 'country': "sp"}, 523), ) for m in data: self.c.set_value(m[0], m[1]) self.assertEqual(1, len(self.c.values)) self.assertEqual(523, self.c.values[data[0][0]]) def test_get_value(self): data = ( ({'country': "sp", "device": "desktop"}, 520), ({'country': "us", "device": "mobile"}, 654), ({'country': "uk", "device": "desktop"}, 1001), ({'country': "de", "device": "desktop"}, 995), ) for m in data: self.c.set_value(m[0], m[1]) for m in data: self.assertEqual(m[1], self.c.get_value(m[0])) def test_not_const_labels(self): del self.data['const_labels'] self.c = Collector(**self.data) def test_not_name(self): with self.assertRaises(TypeError) as context: del self.data['name'] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'name'", str(context.exception)) def test_not_help_text(self): with self.assertRaises(TypeError) as context: del self.data['doc'] self.c = Collector(**self.data) self.assertEqual( "__init__() missing 1 required positional argument: 'doc'", str(context.exception)) def test_without_labels(self): data = ( ({}, 520), (None, 654), ("", 1001), ) for i in data: self.c.set_value(i[0], i[1]) self.assertEqual(1, len(self.c.values)) self.assertEqual((data)[len(data) - 1][1], self.c.values[data[0][0]]) def test_wrong_labels(self): # Normal set with self.assertRaises(ValueError) as context: self.c.set_value({'job': 1, 'ok': 2}, 1) self.assertEqual('Invalid label name: job', str(context.exception)) with self.assertRaises(ValueError) as context: self.c.set_value({'__not_ok': 1, 'ok': 2}, 1) self.assertEqual('Invalid label prefix: __not_ok', str(context.exception)) # Constructor set with self.assertRaises(ValueError) as context: Collector("x", "y", {'job': 1, 'ok': 2}) self.assertEqual('Invalid label name: job', str(context.exception)) with self.assertRaises(ValueError) as context: Collector("x", "y", {'__not_ok': 1, 'ok': 2}) self.assertEqual('Invalid label prefix: __not_ok', str(context.exception)) def test_get_all(self): data = ( ({'country': "sp", "device": "desktop"}, 520), ({'country': "us", "device": "mobile"}, 654), ({'country': "uk", "device": "desktop"}, 1001), ({'country': "de", "device": "desktop"}, 995), ({'country': "zh", "device": "desktop"}, 520), ({'country': "ch", "device": "mobile"}, 654), ({'country': "ca", "device": "desktop"}, 1001), ({'country': "jp", "device": "desktop"}, 995), ({'country': "au", "device": "desktop"}, 520), ({'country': "py", "device": "mobile"}, 654), ({'country': "ar", "device": "desktop"}, 1001), ({'country': "pt", "device": "desktop"}, 995), ) for i in data: self.c.set_value(i[0], i[1]) def country_fetcher(x): return x[0]['country'] sorted_data = sorted(data, key=country_fetcher) sorted_result = sorted(self.c.get_all(), key=country_fetcher) self.assertEqual(sorted_data, sorted_result)