Skip to content

Commit

Permalink
More set tests
Browse files Browse the repository at this point in the history
Summary:
I've added sets to the test predicate in our test DB.
That in itself is a good test and found a couple of bugs.

I've also added a test.

Also added a round-tripping test to exercise the JSON encoding.

Reviewed By: simonmar

Differential Revision: D63611597

fbshipit-source-id: cdaf19383fdfc72f3290d51b8c6c5c88a95e48cb
  • Loading branch information
Josef Svenningsson authored and facebook-github-bot committed Nov 14, 2024
1 parent 6af2302 commit f6a950a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
31 changes: 17 additions & 14 deletions glean/schema/cpp/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -14583,7 +14583,7 @@ template<> struct Repr_<facebook::glean::cpp::schema::Glean::Test::KitchenSink_e


template<> struct Repr_<facebook::glean::cpp::schema::Glean::Test::KitchenSink> {
using Type = Tuple<Byte, Nat, Bool, String, facebook::glean::cpp::schema::Sys::Blob, Maybe<Tuple<>>, Tuple<Byte, Nat>, Sum<facebook::glean::cpp::schema::Glean::Test::Predicate_, facebook::glean::cpp::schema::Sys::Blob>, Enum<3>, Repr<facebook::glean::cpp::schema::Glean::Test::Rec>, Repr<facebook::glean::cpp::schema::Glean::Test::Sum_>, Repr<facebook::glean::cpp::schema::Glean::Test::Enum_>, Array<Byte>, Array<Nat>, Array<Bool>, Array<String>, Array<facebook::glean::cpp::schema::Glean::Test::Predicate_>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Rec>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Sum_>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Enum_>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayByte>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayNat>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayBool>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayString>>>;
using Type = Tuple<Byte, Nat, Bool, String, facebook::glean::cpp::schema::Sys::Blob, Maybe<Tuple<>>, Tuple<Byte, Nat>, Sum<facebook::glean::cpp::schema::Glean::Test::Predicate_, facebook::glean::cpp::schema::Sys::Blob>, Enum<3>, Repr<facebook::glean::cpp::schema::Glean::Test::Rec>, Repr<facebook::glean::cpp::schema::Glean::Test::Sum_>, Repr<facebook::glean::cpp::schema::Glean::Test::Enum_>, Array<Byte>, Array<Nat>, Array<Bool>, Array<String>, Array<facebook::glean::cpp::schema::Glean::Test::Predicate_>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Rec>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Sum_>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::Enum_>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayByte>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayNat>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayBool>>, Array<Repr<facebook::glean::cpp::schema::Glean::Test::ArrayString>>, Set<Nat>, Set<String>, Set<facebook::glean::cpp::schema::Glean::Test::Predicate_>>;
};


Expand Down Expand Up @@ -14618,33 +14618,36 @@ struct KitchenSink {
std::vector<ArrayNat> array2_of_nat;
std::vector<ArrayBool> array2_of_bool;
std::vector<ArrayString> array2_of_string;
std::set<uint64_t> set_of_nat;
std::set<std::string> set_of_string;
std::set<Fact<Predicate_>> set_of_pred;

bool operator==(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
== std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
== std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
bool operator!=(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
!= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
!= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
bool operator<(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
< std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
< std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
bool operator<=(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
<= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
<= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
bool operator>(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
> std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
> std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
bool operator>=(const KitchenSink& other) const {
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string)
>= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string);
return std::tie(byt,nat,bool_,string_,pred,maybe_,record_,sum_,enum_,named_record_,named_sum_,named_enum_,array_of_byte,array_of_nat,array_of_bool,array_of_string,array_of_pred,array_of_named_record,array_of_named_sum,array_of_named_enum,array2_of_byte,array2_of_nat,array2_of_bool,array2_of_string,set_of_nat,set_of_string,set_of_pred)
>= std::tie(other.byt,other.nat,other.bool_,other.string_,other.pred,other.maybe_,other.record_,other.sum_,other.enum_,other.named_record_,other.named_sum_,other.named_enum_,other.array_of_byte,other.array_of_nat,other.array_of_bool,other.array_of_string,other.array_of_pred,other.array_of_named_record,other.array_of_named_sum,other.array_of_named_enum,other.array2_of_byte,other.array2_of_nat,other.array2_of_bool,other.array2_of_string,other.set_of_nat,other.set_of_string,other.set_of_pred);
}
void outputRepr(Output<Repr<KitchenSink>> out) const {
outputValue(out, std::make_tuple(byt, nat, bool_, string_, pred, maybe_, record_, sum_, enum_, named_record_, named_sum_, named_enum_, array_of_byte, array_of_nat, array_of_bool, array_of_string, array_of_pred, array_of_named_record, array_of_named_sum, array_of_named_enum, array2_of_byte, array2_of_nat, array2_of_bool, array2_of_string));
outputValue(out, std::make_tuple(byt, nat, bool_, string_, pred, maybe_, record_, sum_, enum_, named_record_, named_sum_, named_enum_, array_of_byte, array_of_nat, array_of_bool, array_of_string, array_of_pred, array_of_named_record, array_of_named_sum, array_of_named_enum, array2_of_byte, array2_of_nat, array2_of_bool, array2_of_string, set_of_nat, set_of_string, set_of_pred));
}
}; // struct KitchenSink

Expand Down
4 changes: 3 additions & 1 deletion glean/schema/source/test.angle
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ type KitchenSink =
array2_of_nat : [ArrayNat],
array2_of_bool : [ArrayBool],
array2_of_string : [ArrayString],
set_of_nat : set nat,
set_of_string : set string,
set_of_pred : set Predicate,
}

predicate Predicate : KitchenSink
Expand Down Expand Up @@ -276,4 +279,3 @@ predicate Qux : string
predicate FooToFoo : Foo -> Foo

}

8 changes: 6 additions & 2 deletions glean/test/lib/TestData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module TestData
) where

import Data.Default
import qualified Data.Set as Set

import Glean.Typed
import Glean.Types
Expand Down Expand Up @@ -42,6 +43,8 @@ kitchenSink1 = def
, Glean.Test.kitchenSink_maybe_ = Just def
, Glean.Test.kitchenSink_bool_ = True
, Glean.Test.kitchenSink_string_ = "Hello\0world!\0"
, Glean.Test.kitchenSink_set_of_nat = Set.fromList [toNat 1, toNat 2]
, Glean.Test.kitchenSink_set_of_string = Set.fromList ["apa", "bepa"]
}

mkTestFacts :: NewFact m => (m () -> m ()) -> (m () -> m ()) -> m ()
Expand Down Expand Up @@ -92,8 +95,8 @@ mkTestFacts first second = do
kitchenSink2Fact0b <- makeFact @Glean.Test.Predicate kitchenSink2Term0b

let
-- refers to kitchenSink2Fact0 through the sum_ field and the
-- array_of_pred field
-- refers to kitchenSink2Fact0 through the sum_ field, the
-- array_of_pred field and the set_of_pred field.
kitchenSink2Term1 = def
{ Glean.Test.kitchenSink_pred = sysBlobFact2
, Glean.Test.kitchenSink_sum_ =
Expand All @@ -104,6 +107,7 @@ mkTestFacts first second = do
, Glean.Test.kitchenSink_named_sum_ = Glean.Test.Sum_wed True
, Glean.Test.kitchenSink_named_record_ = rec
, Glean.Test.kitchenSink_maybe_ = Nothing
, Glean.Test.kitchenSink_set_of_pred = Set.fromList [kitchenSink2Fact0]
}

kitchenSink2Term1b = kitchenSink2Term1
Expand Down
8 changes: 8 additions & 0 deletions glean/test/tests/Angle/AngleTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ main = withUnitTest $ testRunner $ TestList
, TestLabel "angleIfThenElse" $ angleIfThenElse id
, TestLabel "angleIfThenElse/page" $ angleIfThenElse (limit 1)
, TestLabel "angleTypeTest" angleTypeTest
, TestLabel "angleSet" angleSetTest
]

ignorePredK :: Glean.Test.KitchenSink -> Glean.Test.KitchenSink
Expand Down Expand Up @@ -979,3 +980,10 @@ angleTypeTest = dbTestCase $ \env repo -> do
|]
print r
assertEqual "angle - inference 6" 1 (length r)

angleSetTest :: Test
angleSetTest = dbTestCase $ \env repo -> do
r <- runQuery_ env repo $ angleData @Glean.Test.Predicate
[s| glean.test.Predicate { set_of_string = all ("bepa" | "apa") } |]
print r
assertEqual "angle - set matching" 2 (length r)
7 changes: 7 additions & 0 deletions glean/test/tests/EncodingTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import qualified Data.ByteString.Base16 as Hex
import qualified Data.ByteString.Char8 as ByteString
import Data.Default
import Data.IntMap.Strict (IntMap)
import qualified Data.Set as Set
import Foreign.C.String
import Foreign.C.Types
import Foreign.Ptr
Expand Down Expand Up @@ -88,6 +89,12 @@ kitchenSink = KitchenSink
, [ False ] ]
, kitchenSink_array2_of_string =
[ [ "a", "bc", "def" ] ]
, kitchenSink_set_of_nat =
Set.fromList [ Nat 1, Nat 3 ]
, kitchenSink_set_of_string =
Set.fromList [ "foo", "bar" ]
, kitchenSink_set_of_pred =
Set.fromList [ Glean.Test.Predicate 5432 Nothing ]
}

data E = E
Expand Down
25 changes: 22 additions & 3 deletions glean/test/tests/RTSTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ instance Arbitrary Type where
, pure T.NatTy
, pure T.StringTy
, T.ArrayTy <$> arbitrary
, T.SetTy <$> arbitrary
, T.RecordTy . fields <$> children 0
, T.SumTy . fields <$> children 1
, T.PredicateTy <$> arbitrary
Expand All @@ -65,6 +66,22 @@ instance Arbitrary Type where
fields tys =
[ T.FieldDef (Text.pack $ 'x' : show (i :: Int)) ty
| (i,ty) <- zip [0..] tys ]
shrink (T.ArrayTy ty) = ty : [T.ArrayTy sty | sty <- shrink ty]
shrink (T.SetTy ty) = ty : [T.SetTy sty | sty <- shrink ty]
shrink (T.RecordTy fields) = [ ty | T.FieldDef _ ty <- fields ] ++
(T.RecordTy <$> shrinkList shrinkField fields)
shrink (T.SumTy fields) = (T.SumTy <$>
filter (not . null) (shrinkList shrinkField fields)) ++
[ ty | T.FieldDef _ ty <- fields ]
shrink (T.EnumeratedTy enums) = T.EnumeratedTy <$>
filter (not . null) (shrinkList (const []) enums)
shrink (T.MaybeTy ty) = ty : (T.MaybeTy <$> shrink ty)
shrink _ = []

shrinkField
:: Arbitrary (T.Type_ pref tref)
=> T.FieldDef_ pref tref -> [T.FieldDef_ pref tref]
shrinkField (T.FieldDef name ty) = T.FieldDef name <$> shrink ty

valueFor :: Type -> Gen Value
valueFor T.ByteTy = Byte <$> arbitrary
Expand All @@ -82,7 +99,7 @@ valueFor (T.SumTy fields) = do
Alt i <$> valueFor (T.fieldDefType field)
valueFor (T.SetTy ty) = fmap Set $ sized $ \n -> do
k <- choose (0,n)
nub <$> vectorOf k (resize (n `div` k) $ valueFor ty)
nub . sort <$> vectorOf k (resize (n `div` k) $ valueFor ty)
valueFor T.PredicateTy{} = Ref <$> arbitrary
valueFor (T.NamedTy (ExpandedType _ ty)) = valueFor ty
valueFor (T.MaybeTy ty) = do
Expand Down Expand Up @@ -112,8 +129,10 @@ main = withUnitTest $ testRunner $ TestList
forAll arbitrary $ \ty ->
forAll (valueFor ty) $ \val -> prop_roundtripValue ty val

, TestLabel "value typecheck" $ TestCase $ assertProperty "mismatch" $
forAll arbitrary $ \ty ->
, TestLabel "value typecheck" $ TestCase $
assertPropertyWithArgs "mismatch" stdArgs{ maxSuccess = 1000 } $
forAllShrink arbitrary shrink $ \ty ->
collect ty $
forAll (valueFor ty) $ \val -> prop_typecheckValue ty val

-- test strings more thoroughly as they are quite complicated
Expand Down

0 comments on commit f6a950a

Please sign in to comment.