'Test to verify that various initial conditions are correctly surfaced in various aggregate functions
test_init_cond creates several different types of aggregates, and given various initial conditions it verifies that
they correctly impact the aggregate\'s execution
@since 2.6.0
@jira_ticket PYTHON-211
@expected_result initial conditions are correctly evaluated as part of the aggregates
@test_category aggregate'

def test_init_cond(self):

    c = Cluster(protocol_version=3)
    s = c.connect(self.keyspace_name)
    encoder = Encoder()
    expected_values = range(4)
    for init_cond in ((-1), 0, 1):
        cql_init = encoder.cql_encode_all_types(init_cond)
        with self.VerifiedAggregate(self, **self.make_aggregate_kwargs('sum_int', 'int', init_cond=cql_init)) as va:
            sum_res = s.execute(('SELECT    %s(v)    AS    sum    FROM    t' % va.function_kwargs['name']))[0].sum
            self.assertEqual(sum_res, (int(init_cond) + sum(expected_values)))
    for init_cond in ([], ['1', '2']):
        cql_init = encoder.cql_encode_all_types(init_cond)
        with self.VerifiedAggregate(self, **self.make_aggregate_kwargs('extend_list', 'list<text>', init_cond=cql_init)) as va:
            list_res = s.execute(('SELECT    %s(v)    AS    list_res    FROM    t' % va.function_kwargs['name']))[0].list_res
            self.assertListEqual(list_res[:len(init_cond)], init_cond)
            self.assertEqual(set((i for i in list_res[len(init_cond):])), set((str(i) for i in expected_values)))
    expected_map_values = dict(((i, i) for i in expected_values))
    expected_key_set = set(expected_values)
    for init_cond in ({}, {1: 2, 3: 4}, {5: 5}):
        cql_init = encoder.cql_encode_all_types(init_cond)
        with self.VerifiedAggregate(self, **self.make_aggregate_kwargs('update_map', 'map<int,    int>', init_cond=cql_init)) as va:
            map_res = s.execute(('SELECT    %s(v)    AS    map_res    FROM    t' % va.function_kwargs['name']))[0].map_res
            self.assertDictContainsSubset(expected_map_values, map_res)
            init_not_updated = dict(((k, init_cond[k]) for k in (set(init_cond) - expected_key_set)))
            self.assertDictContainsSubset(init_not_updated, map_res)
    c.shutdown()
