def test_entity_with_all_supported_types():
    service = gen_tenant_id()
    e = gen_entity(entity_id=1,
                   bool_v=True,
                   int_v=123,
                   num_v=321.07,
                   text_v='wada wada',
                   timex=current_timex() + '+00:00',
                   array_v=[123, {}, False],
                   structured_v={'x': 1},
                   geoj_v={'type': 'Point', 'coordinates': [30.02, 10.03],
                           'crs': {'properties': {'name': 'EPSG4326'},
                                   'type': 'name'},
                           'meta': {'srid': 4326}
                           },
                   slf_point_v='41.3763726, 2.186447514',
                   slf_line_v=['1.0, 2.0', '3.0, 4.0'],
                   slf_polygon_v=['0.0, 0.0', '1.0, 0.0', '0.0, 1.0',
                                  '0.0, 0.0'],
                   slf_box_v=['40.63913831188419, -8.653321266174316',
                              '40.63881265804603, -8.653149604797363'])

    insert_entities(e, service=service)

    result_set = query_entity_by_id(e['id'], service)

    actual = query_result_name_value_pairs(result_set)
    expected = entity_name_value_pairs(e)
    assert actual == expected
    def run_inconsistent_attr_type_in_batch_scenario(self):
        tenant = gen_tenant_id()
        good_entity = gen_entity(1, 'Text', 'wada wada')
        bad_entity = gen_entity(2, 'DateTime', current_timex())

        self.insert_entities(tenant, [good_entity, bad_entity])

        rs = self.fetch_rows(tenant)

        assert len(rs) == 2
        assert_partial_entity(rs[0], good_entity)
        assert_partial_entity(rs[1], bad_entity)
    def _do_success_scenario_with_keep_raw_on(self):
        tenant = gen_tenant_id()
        e1, e2, e3 = [gen_entity(k + 1, 'Number', k + 1) for k in range(3)]

        self.insert_entities(tenant, [e1])
        self.insert_entities(tenant, [e2, e3])

        rs = self.fetch_rows(tenant)

        assert len(rs) == 3
        assert_saved_original(rs[0], e1)
        assert_saved_original(rs[1], e2)
        assert_saved_original(rs[2], e3)
    def run_success_scenario(self):
        tenant = gen_tenant_id()
        e1, e2, e3 = [gen_entity(k + 1, 'Number', k + 1) for k in range(3)]

        self.insert_entities(tenant, [e1])
        self.insert_entities(tenant, [e2, e3])

        rs = self.fetch_rows(tenant)

        assert len(rs) == 3
        assert_inserted_entity(rs[0], e1)
        assert_inserted_entity(rs[1], e2)
        assert_inserted_entity(rs[2], e3)
    def run_changed_attr_type_scenario(self):
        tenant = gen_tenant_id()
        good_entity = gen_entity(1, 'Number', 123)
        bad_entity = gen_entity(2, 'Text', 'shud of been a nbr!')

        self.insert_entities(tenant, [good_entity])
        self.insert_entities(tenant, [bad_entity])

        rs = self.fetch_rows(tenant)

        assert len(rs) == 2
        assert_inserted_entity(rs[0], good_entity)
        assert_failed_entity(rs[1], bad_entity)
    def run_data_loss_scenario(self):
        tenant = gen_tenant_id()
        good_entity = gen_entity(1, 'Number', 1)
        bad_entity = gen_entity(1, 'Number', 2)
        bad_entity[ORIGINAL_ENTITY_COL] = 'kaboom!'

        self.insert_entities(tenant, [good_entity])
        with pytest.raises(ValueError):
            self.insert_entities(tenant, [bad_entity])

        rs = self.fetch_rows(tenant)

        assert len(rs) == 1
        assert_inserted_entity(rs[0], good_entity)
    def run_query_failed_entities_scenario(self, fetch_batch_id_clause: str):
        tenant = gen_tenant_id()
        good_entities = [gen_entity(k + 1, 'Number', 123) for k in range(2)]
        bad_entities = [gen_entity(k + 1, 'Text', 'shud of been a nbr!')
                        for k in range(3)]

        self.insert_entities(tenant, [good_entities[0]])
        self.insert_entities(tenant, [bad_entities[0]])
        self.insert_entities(tenant, [good_entities[1]])
        self.insert_entities(tenant, [bad_entities[1], bad_entities[2]])

        rs = self.query_failed_inserts(tenant, fetch_batch_id_clause)

        assert len(rs) == 2
        assert rs[0]['batch']
        assert rs[1]['batch']

        counts = sorted([rs[0]['count'], rs[1]['count']])
        assert counts == [1, 2]
 def __init__(self, insert_max_size: int, min_batches: int):
     self.insert_max_size = insert_max_size
     self.min_batches = min_batches
     self.unique_tenant_id = gen_tenant_id()