Skip to content

Commit

Permalink
test: fix all FieldPath tests + add DSL test
Browse files Browse the repository at this point in the history
Prior to this commit, all FieldPath tests were passing
  due to a false positive with `__eq__`.
  • Loading branch information
0xMochan committed May 17, 2023
1 parent 7e320e8 commit ca93eab
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 53 deletions.
24 changes: 19 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from contextlib import contextmanager

import pytest

Expand Down Expand Up @@ -559,9 +560,22 @@ def identity(x):
return x


def fieldpath_test_mode(func):
def wrapper(*args, **kwargs):
FieldPath.__test_mode = True
func(*args, **kwargs)
@contextmanager
def fieldpath_test_mode():
"""Temporarily turns on `FieldPath`'s testing mode which undo's custom DSL-type
behavior that overrides `__eq__`.
return wrapper
`FieldPath.__test_mode` is the name of the field inside the class but here, we use
`FieldPath._FieldPath__test_mode` which seemingly shouldn't work. This is due
Python's name-mangling that occurs when a class field starts with at-least 2
underscores. We use this odd python feature as a sense of a "private" variable
since external users shouldn't be touching this field anyways (and single `_` is
reserved for "normal" fields in this DSL).
You can find out more information about that here:
https://docs.python.org/3/tutorial/classes.html#private-variables
"""

FieldPath._FieldPath__test_mode = True # type: ignore
yield
FieldPath._FieldPath__test_mode = False # type: ignore
166 changes: 148 additions & 18 deletions tests/test_fieldpath.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import pytest

from subgrounds.query import (
Argument,
DataRequest,
Document,
InputValue,
Query,
Selection,
)
from subgrounds.schema import TypeMeta, TypeRef
from subgrounds.subgraph import FieldPath, Subgraph
from subgrounds.subgrounds import Subgrounds
from subgrounds.subgraph import FieldPath
from tests.conftest import fieldpath_test_mode

# from tests.conftest import *

Expand Down Expand Up @@ -86,8 +76,8 @@ def test_fieldpath_building_1(subgraph):
)
query = pairs.id

FieldPath.__test_mode = True
assert query == expected
with fieldpath_test_mode():
assert query == expected


def test_fieldpath_building_2(subgraph):
Expand Down Expand Up @@ -228,8 +218,8 @@ def test_fieldpath_building_2(subgraph):
selection=[subgraph.Pair.id, subgraph.Pair.reserveUSD],
)

FieldPath.__test_mode = True
assert query == expected
with fieldpath_test_mode():
assert query == expected


def test_fieldpath_building_3(subgraph):
Expand Down Expand Up @@ -371,5 +361,145 @@ def test_fieldpath_building_3(subgraph):

query = [pairs.id, pairs.reserveUSD]

FieldPath.__test_mode = True
assert query == expected
with fieldpath_test_mode():
assert query == expected


def test_fieldpath_building_4(subgraph):
expected = [
FieldPath(
subgraph,
TypeRef.Named(name="Query", kind="OBJECT"),
TypeRef.Named(name="String", kind="SCALAR"),
[
(
{
"first": 100,
"orderBy": "token0__symbol",
"orderDirection": "desc",
},
TypeMeta.FieldMeta(
name="pairs",
description="",
args=[
TypeMeta.ArgumentMeta(
name="first",
description="",
type=TypeRef.Named(name="Int", kind="SCALAR"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="skip",
description="",
type=TypeRef.Named(name="Int", kind="SCALAR"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="where",
description="",
type=TypeRef.Named(
name="Pair_filter", kind="INPUT_OBJECT"
),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="orderBy",
description="",
type=TypeRef.Named(name="Pair_orderBy", kind="ENUM"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="orderDirection",
description="",
type=TypeRef.Named(name="OrderDirection", kind="ENUM"),
defaultValue=None,
),
],
type=TypeRef.non_null_list("Pair", kind="OBJECT"),
),
),
(
None,
TypeMeta.FieldMeta(
name="id",
description="",
args=[],
type=TypeRef.Named(name="String", kind="SCALAR"),
),
),
],
),
FieldPath(
subgraph,
TypeRef.Named(name="Query", kind="OBJECT"),
TypeRef.Named(name="BigDecimal", kind="SCALAR"),
[
(
{
"first": 100,
"orderBy": "token0__symbol",
"orderDirection": "desc",
},
TypeMeta.FieldMeta(
name="pairs",
description="",
args=[
TypeMeta.ArgumentMeta(
name="first",
description="",
type=TypeRef.Named(name="Int", kind="SCALAR"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="skip",
description="",
type=TypeRef.Named(name="Int", kind="SCALAR"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="where",
description="",
type=TypeRef.Named(
name="Pair_filter", kind="INPUT_OBJECT"
),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="orderBy",
description="",
type=TypeRef.Named(name="Pair_orderBy", kind="ENUM"),
defaultValue=None,
),
TypeMeta.ArgumentMeta(
name="orderDirection",
description="",
type=TypeRef.Named(name="OrderDirection", kind="ENUM"),
defaultValue=None,
),
],
type=TypeRef.non_null_list("Pair", kind="OBJECT"),
),
),
(
None,
TypeMeta.FieldMeta(
name="reserveUSD",
description="",
args=[],
type=TypeRef.Named(name="BigDecimal", kind="SCALAR"),
),
),
],
),
]

pairs = subgraph.Query.pairs(
first=100,
orderBy=subgraph.Pair.token0.symbol,
orderDirection="desc",
)

query = [pairs.id, pairs.reserveUSD]

with fieldpath_test_mode():
assert query == expected
61 changes: 31 additions & 30 deletions tests/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from subgrounds.subgraph import FieldPath, Filter, Object, Subgraph, SyntheticField
from subgrounds.subgrounds import Subgrounds
from subgrounds.utils import identity
from tests.conftest import fieldpath_test_mode

# class TestAddType(unittest.TestCase):
# def setUp(self):
Expand Down Expand Up @@ -1956,8 +1957,8 @@ def test_object(subgraph: Subgraph):

Pair = subgraph.Pair

FieldPath.__test_mode = True
assert Pair == expected
with fieldpath_test_mode():
assert Pair == expected


def test_field_path_1(subgraph: Subgraph):
Expand Down Expand Up @@ -2004,8 +2005,8 @@ def test_field_path_2(subgraph: Subgraph):

fpath = subgraph.Pair.id

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_field_path_3(subgraph: Subgraph):
Expand Down Expand Up @@ -2037,8 +2038,8 @@ def test_field_path_3(subgraph: Subgraph):

fpath = subgraph.Pair.token0.id

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_synthetic_field_path_1(subgraph: Subgraph):
Expand All @@ -2064,8 +2065,8 @@ def test_synthetic_field_path_1(subgraph: Subgraph):
subgraph.Pair.reserveCAD = sfield
fpath = subgraph.Pair.reserveCAD

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_synthetic_field_path_2(subgraph: Subgraph):
Expand Down Expand Up @@ -2100,8 +2101,8 @@ def test_synthetic_field_path_2(subgraph: Subgraph):
subgraph.Token.frenchName = sfield
fpath = subgraph.Pair.token0.frenchName

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_synthetic_field_path_3(subgraph: Subgraph):
Expand All @@ -2125,8 +2126,8 @@ def test_synthetic_field_path_3(subgraph: Subgraph):
subgraph.Pair.token0Id = subgraph.Pair.token0.id
fpath = subgraph.Pair.token0Id

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_filter_1(subgraph: Subgraph):
Expand All @@ -2143,8 +2144,8 @@ def test_filter_1(subgraph: Subgraph):

filter_ = subgraph.Pair.reserveUSD > 100

FieldPath.__test_mode = True
assert filter_ == expected
with fieldpath_test_mode():
assert filter_ == expected


def test_field_path_args_1(subgraph: Subgraph):
Expand Down Expand Up @@ -2208,8 +2209,8 @@ def test_field_path_args_1(subgraph: Subgraph):
orderDirection="desc",
)

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_field_path_args_2(subgraph: Subgraph):
Expand Down Expand Up @@ -2266,8 +2267,8 @@ def test_field_path_args_2(subgraph: Subgraph):
where=[subgraph.Pair.reserveUSD > 100, subgraph.Pair.token0 == "abcd"],
)

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_field_path_args_3(subgraph: Subgraph):
Expand Down Expand Up @@ -2321,8 +2322,8 @@ def test_field_path_args_3(subgraph: Subgraph):

fpath = subgraph.Query.pairs(first=100, orderBy=subgraph.Pair.reserveUSD)

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_field_path_extend_1(subgraph: Subgraph):
Expand Down Expand Up @@ -2397,8 +2398,8 @@ def test_field_path_extend_1(subgraph: Subgraph):

fpath = FieldPath._extend(fpath1, fpath2)

FieldPath.__test_mode = True
assert fpath == expected
with fieldpath_test_mode():
assert fpath == expected


def test_mk_request_1(subgraph: Subgraph):
Expand Down Expand Up @@ -2489,8 +2490,8 @@ def test_mk_request_1(subgraph: Subgraph):

req = sg.mk_request([pairs.id, pairs.token0.symbol])

FieldPath.__test_mode = True
assert req == expected
with fieldpath_test_mode():
assert req == expected


def test_mk_request_2(sg: Subgrounds, subgraph: Subgraph):
Expand Down Expand Up @@ -2570,8 +2571,8 @@ def test_mk_request_2(sg: Subgrounds, subgraph: Subgraph):

req = sg.mk_request([pairs.id, pairs.token0Id])

FieldPath.__test_mode = True
assert req == expected
with fieldpath_test_mode():
assert req == expected


def test_mk_request_3(sg: Subgrounds, subgraph: Subgraph):
Expand Down Expand Up @@ -2649,8 +2650,8 @@ def test_mk_request_3(sg: Subgrounds, subgraph: Subgraph):

req = sg.mk_request([subgraph.Query.swaps.timestamp, subgraph.Query.swaps.price])

FieldPath.__test_mode = True
assert req == expected
with fieldpath_test_mode():
assert req == expected


def test_mk_request_4(sg: Subgrounds, subgraph: Subgraph):
Expand Down Expand Up @@ -2726,8 +2727,8 @@ def test_mk_request_4(sg: Subgrounds, subgraph: Subgraph):

req = sg.mk_request([subgraph.Query.swaps.timestamp, subgraph.Query.swaps.my_value])

FieldPath.__test_mode = True
assert req == expected
with fieldpath_test_mode():
assert req == expected


def test_synthetic_field_1(subgraph: Subgraph):
Expand Down

0 comments on commit ca93eab

Please sign in to comment.