예제 #1
0
    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
예제 #2
0
    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]
예제 #3
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))
     ]
예제 #4
0
    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]
예제 #5
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'
예제 #6
0
    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
예제 #7
0
    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
예제 #8
0
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
예제 #9
0
    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
예제 #10
0
    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
예제 #11
0
 def value():
     # type: () -> Value
     return Value('value')
예제 #12
0
    def test_to_repr_with_key_value(key):
        value = Value(key)

        r = value.to_repr()

        assert r['keyValue'] == key.to_repr()
예제 #13
0
 def query_filter():
     # type: () -> Filter
     inner_filter = PropertyFilter(prop='property_name',
                                   operator=PropertyFilterOperator.EQUAL,
                                   value=Value(123))
     return Filter(inner_filter)
예제 #14
0
    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()
예제 #15
0
    def test_from_repr_with_key_value(key):
        data = {'excludeFromIndexes': False, 'keyValue': key.to_repr()}

        value = Value.from_repr(data)

        assert value.value == key
예제 #16
0
    def test_to_repr_with_null_value():
        value = Value(None)

        r = value.to_repr()

        assert r['nullValue'] == 'NULL_VALUE'
예제 #17
0
 def value():
     # type: () -> Value
     return Value(value='foobar', exclude_from_indexes=False)
예제 #18
0
    def test_to_repr_exclude_from_indexes():
        value = Value(123, exclude_from_indexes=True)

        r = value.to_repr()

        assert r['excludeFromIndexes']