Skip to content

Commit 8903212

Browse files
authored
Port PrimitiveValueTest test for core::Query (#1530)
(From java.) Also fix a bug uncovered by the test. (oops)
1 parent 6ab0195 commit 8903212

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

Firestore/core/src/firebase/firestore/core/filter.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ using model::FieldValue;
2929

3030
std::shared_ptr<Filter> Filter::Create(FieldPath path,
3131
Operator op,
32-
FieldValue value) {
32+
FieldValue value_rhs) {
3333
// TODO(rsgowman): Java performs a number of checks here, and then invokes the
3434
// ctor of the relevant Filter subclass. Port those checks here.
3535
return std::make_shared<RelationFilter>(std::move(path), op,
36-
std::move(value));
36+
std::move(value_rhs));
3737
}
3838

3939
} // namespace core

Firestore/core/src/firebase/firestore/core/filter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Filter {
4848
*/
4949
static std::shared_ptr<Filter> Create(model::FieldPath path,
5050
Operator op,
51-
model::FieldValue value);
51+
model::FieldValue value_rhs);
5252

5353
virtual ~Filter() {
5454
}

Firestore/core/src/firebase/firestore/core/relation_filter.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ namespace core {
2727
using model::FieldPath;
2828
using model::FieldValue;
2929

30-
RelationFilter::RelationFilter(FieldPath field, Operator op, FieldValue value)
31-
: field_(std::move(field)), op_(op), value_(std::move(value)) {
30+
RelationFilter::RelationFilter(FieldPath field,
31+
Operator op,
32+
FieldValue value_rhs)
33+
: field_(std::move(field)), op_(op), value_rhs_(std::move(value_rhs)) {
3234
}
3335

3436
const FieldPath& RelationFilter::field() const {
@@ -47,22 +49,22 @@ bool RelationFilter::Matches(const model::Document& doc) const {
4749

4850
bool RelationFilter::MatchesValue(const FieldValue& other) const {
4951
// Only compare types with matching backend order (such as double and int).
50-
return FieldValue::Comparable(value_.type(), other.type()) &&
52+
return FieldValue::Comparable(other.type(), value_rhs_.type()) &&
5153
MatchesComparison(other);
5254
}
5355

5456
bool RelationFilter::MatchesComparison(const FieldValue& other) const {
5557
switch (op_) {
5658
case Operator::LessThan:
57-
return value_ < other;
59+
return other < value_rhs_;
5860
case Operator::LessThanOrEqual:
59-
return value_ <= other;
61+
return other <= value_rhs_;
6062
case Operator::Equal:
61-
return value_ == other;
63+
return other == value_rhs_;
6264
case Operator::GreaterThan:
63-
return value_ > other;
65+
return other > value_rhs_;
6466
case Operator::GreaterThanOrEqual:
65-
return value_ >= other;
67+
return other >= value_rhs_;
6668
}
6769
UNREACHABLE();
6870
}

Firestore/core/src/firebase/firestore/core/relation_filter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class RelationFilter : public Filter {
3535
* Creates a new filter that compares fields and values. Only intended to be
3636
* called from Filter::Create().
3737
*/
38-
RelationFilter(model::FieldPath field, Operator op, model::FieldValue value);
38+
RelationFilter(model::FieldPath field,
39+
Operator op,
40+
model::FieldValue value_rhs);
3941

4042
const model::FieldPath& field() const override;
4143

@@ -49,7 +51,7 @@ class RelationFilter : public Filter {
4951

5052
const model::FieldPath field_;
5153
const Operator op_;
52-
const model::FieldValue value_;
54+
const model::FieldValue value_rhs_;
5355
};
5456

5557
} // namespace core

Firestore/core/test/firebase/firestore/core/query_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace core {
2929

3030
using model::Document;
3131
using model::FieldValue;
32+
using model::ResourcePath;
3233
using testutil::Doc;
3334
using testutil::Filter;
3435

@@ -67,6 +68,35 @@ TEST(QueryTest, EmptyFieldsAreAllowedForQueries) {
6768
EXPECT_FALSE(query.Matches(doc2));
6869
}
6970

71+
TEST(QueryTest, PrimitiveValueFilter) {
72+
Query query1 = Query::AtPath(ResourcePath::FromString("collection"))
73+
.Filter(Filter("sort", ">=", 2));
74+
Query query2 = Query::AtPath(ResourcePath::FromString("collection"))
75+
.Filter(Filter("sort", "<=", 2));
76+
77+
Document doc1 =
78+
Doc("collection/1", 0, {{"sort", FieldValue::IntegerValue(1)}});
79+
Document doc2 =
80+
Doc("collection/2", 0, {{"sort", FieldValue::IntegerValue(2)}});
81+
Document doc3 =
82+
Doc("collection/3", 0, {{"sort", FieldValue::IntegerValue(3)}});
83+
Document doc4 = Doc("collection/4", 0, {{"sort", FieldValue::FalseValue()}});
84+
Document doc5 =
85+
Doc("collection/5", 0, {{"sort", FieldValue::StringValue("string")}});
86+
87+
EXPECT_FALSE(query1.Matches(doc1));
88+
EXPECT_TRUE(query1.Matches(doc2));
89+
EXPECT_TRUE(query1.Matches(doc3));
90+
EXPECT_FALSE(query1.Matches(doc4));
91+
EXPECT_FALSE(query1.Matches(doc5));
92+
93+
EXPECT_TRUE(query2.Matches(doc1));
94+
EXPECT_TRUE(query2.Matches(doc2));
95+
EXPECT_FALSE(query2.Matches(doc3));
96+
EXPECT_FALSE(query2.Matches(doc4));
97+
EXPECT_FALSE(query2.Matches(doc5));
98+
}
99+
70100
} // namespace core
71101
} // namespace firestore
72102
} // namespace firebase

Firestore/core/test/firebase/firestore/testutil/testutil.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ inline std::shared_ptr<core::Filter> Filter(absl::string_view key,
105105
return Filter(key, op, model::FieldValue::StringValue(value));
106106
}
107107

108+
inline std::shared_ptr<core::Filter> Filter(absl::string_view key,
109+
absl::string_view op,
110+
int value) {
111+
return Filter(key, op, model::FieldValue::IntegerValue(value));
112+
}
113+
108114
// Add a non-inline function to make this library buildable.
109115
// TODO(zxu123): remove once there is non-inline function.
110116
void dummy();

0 commit comments

Comments
 (0)