def test_to_repr(v, expected_json_key): value = Value(v) r = value.to_repr() assert len(r) == 2 # Value + excludeFromIndexes assert r['excludeFromIndexes'] is False assert r[expected_json_key] == v
def test_from_repr_could_not_find_supported_value_key(): data = { 'excludeFromIndexes': False, } with pytest.raises(NotImplementedError) as ex_info: Value.from_repr(data) assert 'excludeFromIndexes' in ex_info.value.args[0]
def property_filters(): # type: () -> List[PropertyFilter] return [ PropertyFilter(prop='prop1', operator=PropertyFilterOperator.LESS_THAN, value=Value('value1')), PropertyFilter(prop='prop2', operator=PropertyFilterOperator.GREATER_THAN, value=Value(1234)) ]
def test_to_repr_non_supported_type(): class NonSupportedType(object): pass value = Value(NonSupportedType()) with pytest.raises(Exception) as ex_info: value.to_repr() assert NonSupportedType.__name__ in ex_info.value.args[0]
def test_to_repr_with_datetime_value(): dt = datetime(year=2018, month=7, day=15, hour=11, minute=22, second=33, microsecond=456789) value = Value(dt) r = value.to_repr() assert r['timestampValue'] == '2018-07-15T11:22:33.456789000Z'
def test_from_repr_with_null_value(): data = {'excludeFromIndexes': False, 'nullValue': 'NULL_VALUE'} value = Value.from_repr(data) assert value.excludeFromIndexes is False assert value.value is None
def test_from_repr(json_key, json_value): data = {'excludeFromIndexes': False, json_key: json_value} value = Value.from_repr(data) assert value.excludeFromIndexes is False assert value.value == json_value
def test_query(creds, kind, project): # type: (str, str, str) -> None with requests.Session() as s: ds = Datastore(project=project, service_file=creds, session=s) property_filter = PropertyFilter(prop='value', operator=PropertyFilterOperator.EQUAL, value=Value(42)) query = Query(kind=kind, query_filter=Filter(property_filter)) before = ds.runQuery(query, session=s) num_results = len(before.entity_results) transaction = ds.beginTransaction(session=s) mutations = [ ds.make_mutation(Operation.INSERT, Key(project, [PathElement(kind)]), properties={'value': 42}), ds.make_mutation(Operation.INSERT, Key(project, [PathElement(kind)]), properties={'value': 42}), ] ds.commit(mutations, transaction=transaction, session=s) # TODO: figure out why this is flaky without the sleep # Seems to only be flaky intermittently in py2. time.sleep(2) after = ds.runQuery(query, session=s) assert len(after.entity_results) == num_results + 2
def test_from_repr_with_geo_point_value(lat_lng): data = { 'excludeFromIndexes': False, 'geoPointValue': lat_lng.to_repr() } value = Value.from_repr(data) assert value.value == lat_lng
def test_from_repr_with_datetime_value(): data = { 'excludeFromIndexes': False, 'timestampValue': '1998-07-12T11:22:33.456789000Z' } value = Value.from_repr(data) expected_value = datetime(year=1998, month=7, day=12, hour=11, minute=22, second=33, microsecond=456789) assert value.value == expected_value
def value(): # type: () -> Value return Value('value')
def test_to_repr_with_key_value(key): value = Value(key) r = value.to_repr() assert r['keyValue'] == key.to_repr()
def query_filter(): # type: () -> Filter inner_filter = PropertyFilter(prop='property_name', operator=PropertyFilterOperator.EQUAL, value=Value(123)) return Filter(inner_filter)
def test_to_repr_with_geo_point_value(lat_lng): value = Value(lat_lng) r = value.to_repr() assert r['geoPointValue'] == lat_lng.to_repr()
def test_from_repr_with_key_value(key): data = {'excludeFromIndexes': False, 'keyValue': key.to_repr()} value = Value.from_repr(data) assert value.value == key
def test_to_repr_with_null_value(): value = Value(None) r = value.to_repr() assert r['nullValue'] == 'NULL_VALUE'
def value(): # type: () -> Value return Value(value='foobar', exclude_from_indexes=False)
def test_to_repr_exclude_from_indexes(): value = Value(123, exclude_from_indexes=True) r = value.to_repr() assert r['excludeFromIndexes']