예제 #1
0
def main():
    time.sleep(10.0)
    while True:
        try:
            db = Database('clickhouse:8123', 'default')
            metrics = {"pew": 0.0, "mew": 0.0, "dew": 0.0, "few": 0.0, "new": 0.0, "sew": 0.0}
            while True:
                metric = random.choice(list(metrics.keys()))
                metrics[metric] += math.sin((random.randint(0, 9999)/10000.0) * math.pi * 2)
                m = Metrics(Name=metric, Time=datetime.now(), PartitionDate=date.today().isoformat(), Value=metrics[metric])
                print(m)
                sys.stdout.flush()
                db.insert([m])
                time.sleep(0.15)
        except:
            logging.error(traceback.format_exc())
            time.sleep(1)
            continue
예제 #2
0
class ArrayFieldsTest(unittest.TestCase):
    def setUp(self):
        self.database = Database('localhost:8123', 'test-db')
        self.database.create_table(ModelWithArrays)

    def tearDown(self):
        self.database.drop_database()
        self.database.close()

    def test_insert_and_select(self):
        instance = ModelWithArrays(date_field='2016-08-30',
                                   arr_str=[
                                       'goodbye,', 'cruel', 'world',
                                       'special chars: ,"\\\'` \n\t\\[]'
                                   ],
                                   arr_date=['2010-01-01'])
        self.database.insert([instance])
        query = 'SELECT * from $db.modelwitharrays ORDER BY date_field'
        for model_cls in (ModelWithArrays, None):
            results = list(self.database.select(query, model_cls))
            self.assertEqual(len(results), 1)
            self.assertEqual(results[0].arr_str, instance.arr_str)
            self.assertEqual(results[0].arr_int, instance.arr_int)
            self.assertEqual(results[0].arr_date, instance.arr_date)

    def test_conversion(self):
        instance = ModelWithArrays(arr_int=('1', '2', '3'),
                                   arr_date=['2010-01-01'])
        self.assertEqual(instance.arr_str, [])
        self.assertEqual(instance.arr_int, [1, 2, 3])
        self.assertEqual(instance.arr_date, [date(2010, 1, 1)])

    def test_assignment_error(self):
        instance = ModelWithArrays()
        for value in (7, 'x', [date.today()], ['aaa'], [None]):
            with self.assertRaises(ValueError):
                instance.arr_int = value
 def setUp(self):
     self.database = Database('localhost:8123', 'test-db')
     self.database.create_table(ModelWithEnum)
     self.database.create_table(ModelWithEnumArray)
class EnumFieldsTest(unittest.TestCase):

    def setUp(self):
        self.database = Database('localhost:8123', 'test-db')
        self.database.create_table(ModelWithEnum)
        self.database.create_table(ModelWithEnumArray)

    def tearDown(self):
        self.database.drop_database()
        self.database.close()

    def test_insert_and_select(self):
        self.database.insert([
            ModelWithEnum(date_field='2016-08-30', enum_field=Fruit.apple),
            ModelWithEnum(date_field='2016-08-31', enum_field=Fruit.orange)
        ])
        query = 'SELECT * from $table ORDER BY date_field'
        results = list(self.database.select(query, ModelWithEnum))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].enum_field, Fruit.apple)
        self.assertEqual(results[1].enum_field, Fruit.orange)

    def test_ad_hoc_model(self):
        self.database.insert([
            ModelWithEnum(date_field='2016-08-30', enum_field=Fruit.apple),
            ModelWithEnum(date_field='2016-08-31', enum_field=Fruit.orange)
        ])
        query = 'SELECT * from $db.modelwithenum ORDER BY date_field'
        results = list(self.database.select(query))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].enum_field.name, Fruit.apple.name)
        self.assertEqual(results[0].enum_field.value, Fruit.apple.value)
        self.assertEqual(results[1].enum_field.name, Fruit.orange.name)
        self.assertEqual(results[1].enum_field.value, Fruit.orange.value)

    def test_conversion(self):
        self.assertEqual(ModelWithEnum(enum_field=3).enum_field, Fruit.orange)
        self.assertEqual(ModelWithEnum(enum_field='apple').enum_field, Fruit.apple)
        self.assertEqual(ModelWithEnum(enum_field=Fruit.banana).enum_field, Fruit.banana)

    def test_assignment_error(self):
        for value in (0, 17, 'pear', '', None, 99.9):
            with self.assertRaises(ValueError):
                ModelWithEnum(enum_field=value)

    def test_default_value(self):
        instance = ModelWithEnum()
        self.assertEqual(instance.enum_field, Fruit.apple)

    def test_enum_array(self):
        instance = ModelWithEnumArray(
            date_field='2016-08-30',
            enum_array=[Fruit.apple, Fruit.apple, Fruit.orange]
        )
        self.database.insert([instance])
        query = 'SELECT * from $table ORDER BY date_field'
        results = list(self.database.select(query, ModelWithEnumArray))
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].enum_array, instance.enum_array)
 def setUp(self):
     self.database = Database('localhost:8123', 'test-db')
     self.database.create_table(Person)
     self.database.create_table(NamedModel)
class DatabaseTestCase(unittest.TestCase):
    def setUp(self):
        self.database = Database('localhost:8123', 'test-db')
        self.database.create_table(Person)
        self.database.create_table(NamedModel)

    def tearDown(self):
        self.database.drop_table(Person)
        self.database.drop_table(NamedModel)
        self.database.drop_database()
        self.database.close()

    def _insert_and_check(self, data, count):
        self.database.insert(data)
        self.assertEqual(count, self.database.count(Person))

    def test_insert__generator(self):
        self._insert_and_check(self._sample_data(), len(data))

    def test_insert__list(self):
        self._insert_and_check(list(self._sample_data()), len(data))

    def test_insert__iterator(self):
        self._insert_and_check(iter(self._sample_data()), len(data))

    def test_insert__empty(self):
        self._insert_and_check([], 0)

    def test_count(self):
        self.database.insert(self._sample_data())
        self.assertEqual(self.database.count(Person), 100)
        self.assertEqual(
            self.database.count(Person, "first_name = 'Courtney'"), 2)
        self.assertEqual(
            self.database.count(Person, "birthday > '2000-01-01'"), 22)
        self.assertEqual(
            self.database.count(Person, "birthday < '1970-03-01'"), 0)

    def test_select(self):
        self._insert_and_check(self._sample_data(), len(data))
        query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name"
        results = list(self.database.select(query, Person))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].last_name, 'Durham')
        self.assertEqual(results[0].height, 1.72)
        self.assertEqual(results[1].last_name, 'Scott')
        self.assertEqual(results[1].height, 1.70)

    def test_select_partial_fields(self):
        self._insert_and_check(self._sample_data(), len(data))
        query = "SELECT first_name, last_name FROM `test-db`.person " \
                "WHERE first_name = 'Whitney' ORDER BY last_name"
        results = list(self.database.select(query, Person))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].last_name, 'Durham')
        self.assertEqual(results[0].height, 0)  # default value
        self.assertEqual(results[1].last_name, 'Scott')
        self.assertEqual(results[1].height, 0)  # default value

    def test_select_ad_hoc_model(self):
        self._insert_and_check(self._sample_data(), len(data))
        query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name"
        results = list(self.database.select(query))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].__class__.__name__, 'AdHocModel')
        self.assertEqual(results[0].last_name, 'Durham')
        self.assertEqual(results[0].height, 1.72)
        self.assertEqual(results[1].last_name, 'Scott')
        self.assertEqual(results[1].height, 1.70)

    def test_models_naming(self):
        names = set(
            map(lambda x: x.name,
                self.database.select('SHOW TABLES FROM `test-db`')))
        self.assertSetEqual(names, {'person', 'custom_name'})

    def test_special_chars(self):
        s = u'אבגד \\\'"`,.;éåäöšž\n\t\0\b\r'
        p = Person(first_name=s)
        self.database.insert([p])
        p = list(self.database.select("SELECT * from $table", Person))[0]
        self.assertEqual(p.first_name, s)

    def _sample_data(self):
        for entry in data:
            yield Person(**entry)