Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): Add core integration tests #24

Merged
merged 43 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a1ef49d
test: initial core integration tests implementation
jorgehermo9 Aug 3, 2024
b3a6280
test: add integration tests based on web examples
jorgehermo9 Aug 3, 2024
b784317
test: add integration tests based on web examples
jorgehermo9 Aug 3, 2024
f640716
test: add integration tests based on web examples
jorgehermo9 Aug 3, 2024
cec60e7
test(core): Add more tests for field accessing
jorgehermo9 Sep 14, 2024
e0f4be6
test(core): Add more tests for field aliasing
jorgehermo9 Sep 14, 2024
721c94e
test(core): add more tests for quoted keys
jorgehermo9 Sep 15, 2024
dc8daae
test(core): rename properties to fields
jorgehermo9 Sep 15, 2024
3b632ec
test(core): rename properties to fields
jorgehermo9 Sep 15, 2024
a37d95b
test(core): add tests for equals query argument
jorgehermo9 Sep 15, 2024
9e9c534
test(core): rename module
jorgehermo9 Sep 15, 2024
1fd1ee9
test(core): Add generic array filtering tests
jorgehermo9 Sep 15, 2024
1df6757
test(core): Add more test for filtering
jorgehermo9 Sep 18, 2024
11d32a3
test(core): Add more test for filtering
jorgehermo9 Sep 18, 2024
309545a
test(core): Add more test for filtering
jorgehermo9 Sep 18, 2024
1c307b0
test(core): Add initial parser unit tests
jorgehermo9 Sep 18, 2024
65902b4
test(core): Add more IT tests
jorgehermo9 Sep 18, 2024
f051234
test(core): Add more IT tests
jorgehermo9 Sep 18, 2024
8112cc8
test(core): Add more IT tests
jorgehermo9 Sep 18, 2024
481a953
test(core): Add other module
jorgehermo9 Sep 18, 2024
fcfa441
test(core): Add operator module
jorgehermo9 Sep 18, 2024
deb35db
test(core): Add operator module
jorgehermo9 Sep 18, 2024
a86daa8
test(core): Add more tests for field accessing
jorgehermo9 Sep 18, 2024
2d79216
test(core): rename tests
jorgehermo9 Sep 18, 2024
3b83916
test(core): Add tests for not_equal operation
jorgehermo9 Sep 24, 2024
f79383e
test(core): Add tests for not_equal operation
jorgehermo9 Sep 24, 2024
5e8ebfe
test(core): Add tests for not_equal operation
jorgehermo9 Sep 24, 2024
65c6930
test(core): Add new tests for greater than and fix not_equal tests
jorgehermo9 Sep 25, 2024
05d0512
test(core): Add new tests for greater than
jorgehermo9 Sep 25, 2024
42a192a
test(core): Add new tests for greater than
jorgehermo9 Sep 25, 2024
57304e8
test(core): delete unused files
jorgehermo9 Sep 25, 2024
0f74628
test(core): Add tests for greater or equal argument
jorgehermo9 Sep 28, 2024
036ab66
test(core): Add tests for less operation argument
jorgehermo9 Sep 29, 2024
bcaef6e
test(core): Add tests for less_equal operation argument
jorgehermo9 Sep 29, 2024
3e318b7
test(core): Add tests for match operation
jorgehermo9 Sep 29, 2024
dc57575
test(core): Add tests for match operation
jorgehermo9 Sep 29, 2024
94c9f25
test(core): Add generic array_filtering tests
jorgehermo9 Sep 29, 2024
a0b6ee8
Merge branch 'main' into feature/testing
jorgehermo9 Sep 29, 2024
37c0b57
test(core): Add match argument operation tests
jorgehermo9 Sep 29, 2024
6f09b59
test(core): Add match argument operation tests
jorgehermo9 Sep 29, 2024
7a99c7f
test(core): Add not_match argument operation tests
jorgehermo9 Sep 29, 2024
0035650
test(core): Add indexing operator tests
jorgehermo9 Sep 29, 2024
f75b07d
test(core): Add indexing operator tests
jorgehermo9 Sep 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/core/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,23 @@ mod tests {

#[test]
#[should_panic]
fn pos_integer_fails_when_bigger_than_u64() {
fn pos_integer_fails_when_bigger_than_u64_max() {
let input = "18446744073709551616"; // u64::MAX + 1
get_next_token(&input);
}

#[rstest]
#[case::neg_integer("-5", -5)]
#[case::zero("-0", 0)]
#[case::neg_integer_max("-9223372036854775808", i64::MIN)]
#[case::i64_min("-9223372036854775808", i64::MIN)]
fn neg_integer_parses(#[case] input: &str, #[case] expected: i64) {
let expected = Token::NegInteger(expected);
assert_next_token(input, expected);
}

#[test]
#[should_panic]
fn neg_integer_fails_when_smaller_than_i64() {
fn neg_integer_fails_when_smaller_than_i64_min() {
let input = "-9223372036854775809"; // i64::MIN - 1
get_next_token(&input);
}
Expand Down Expand Up @@ -261,4 +261,17 @@ mod tests {
let input = r#"'Java Script'"#;
get_next_token(input);
}

#[test]
fn unknown_character_error() {
let input = "$";

let mut lexer = Token::lexer(input);
let token = lexer.next().expect("There should be at least one token");

assert!(matches!(token, Err(Error::UnknownCharacter)));
}

// TODO: Add more tests for various consecutive tokens. For example,
// Two consecutive strings, two consecutive floats, etc.
}
28 changes: 28 additions & 0 deletions crates/core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,31 @@ impl FromStr for Query {
Parser::new(s).parse()
}
}

#[cfg(test)]
mod tests {
use super::*;
// TODO: add more parser tests

#[test]
#[should_panic]
fn same_alias() {
r#"{
id: alias
models: alias
}"#
.parse::<Query>()
.unwrap();
}

#[test]
#[should_panic]
fn alias_collides_with_existent_field() {
r#"{
id: models
models
}"#
.parse::<Query>()
.unwrap();
}
}
6 changes: 4 additions & 2 deletions crates/core/src/query/query_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
QueryKey,
};
use derive_getters::Getters;
use derive_more::{Constructor, Display};
use derive_more::Constructor;
use regex::Regex;
use serde_json::Value;
use thiserror::Error;
Expand Down Expand Up @@ -466,14 +466,16 @@ impl QueryArguments {
}

impl<'a> QueryArgument {
// TODO: Maybe we should return false and not default to NULL.
const DEFAULT_INSPECTED_VALUE: Cow<'static, Value> = Cow::Owned(Value::Null);

fn satisfies(&'a self, value: &Value, context: &Context<'a>) -> Result<bool, Error<'a>> {
let argument_key = self.key();

let inspected_value = match argument_key.inspect(value, context) {
Ok(value) => value,
// TODO: only return null value for the KeyNotFound error?
// TODO: only return null value for the KeyNotFound error? Check the test with this TODO at the not_equal.rs test

// TODO: the query inspection should not use InternalError, it is too generic
Err(error @ InternalError::KeyNotFound(_)) => {
log::info!("{error}, using null value");
Expand Down
199 changes: 199 additions & 0 deletions crates/core/tests/array_filtering/equal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
use gq_core::query::Query;
use rstest::rstest;
use serde_json::{json, Value};

use crate::fixtures::{ai_models, products, programming_languages};

#[rstest]
fn string_argument_value(programming_languages: Value) {
let query: Query = r#"languages(name = "JavaScript")"#.parse().unwrap();
let expected = json!([
{
"name": "JavaScript",
"popular": true,
"year": 1995
}
]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn integer_argument_value(programming_languages: Value) {
let query: Query = r#"languages(year = 1995)"#.parse().unwrap();
let expected = json!([
{
"name": "JavaScript",
"popular": true,
"year": 1995
},
{
"name": "Java",
"popular": false,
"year": 1995
}
]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn float_argument_value(products: Value) {
let query: Query = r#"products(price = 14.95)"#.parse().unwrap();
let expected = json!([
{
"name": "Product 2",
"quantity": 5,
"price": 14.95
}
]);

let result = query.apply(products).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn boolean_argument_value(programming_languages: Value) {
let query: Query = r#"languages(popular = true)"#.parse().unwrap();
let expected = json!([
{
"name": "JavaScript",
"popular": true,
"year": 1995
},
{
"name": "Rust",
"popular": true,
"year": 2010
}
]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn null_argument_value(ai_models: Value) {
let query: Query = r#"models(score = null)"#.parse().unwrap();
let expected = json!([
{
"name": "Claude",
"openSource": false,
"score": null
}
]);

let result = query.apply(ai_models).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn float_argument_value_with_null_field_value(ai_models: Value) {
let query: Query = r#"models(score = 88.7)"#.parse().unwrap();
let expected = json!([
{
"name": "LLAMA",
"openSource": true,
"score": 88.7,
"tags": ["Text Generation", "Open Source"]
}
]);

let result = query.apply(ai_models).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn array_field_value_includes_argument_value(ai_models: Value) {
let query: Query = r#"models(tags = "Text Generation")"#.parse().unwrap();
let expected = json!([
{
"name": "GPT-4O",
"openSource": false,
"score": 71.49,
"tags": ["NLP", "Text Generation"]
},
{
"name": "LLAMA",
"openSource": true,
"score": 88.7,
"tags": ["Text Generation", "Open Source"]
}
]);

let result = query.apply(ai_models).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn array_field_value_includes_multiple_argument_value(ai_models: Value) {
let query: Query = r#"models(tags = "NLP", tags = "Text Generation")"#.parse().unwrap();
let expected = json!([
{
"name": "GPT-4O",
"openSource": false,
"score": 71.49,
"tags": ["NLP", "Text Generation"]
},
]);

let result = query.apply(ai_models).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn multiple_arguments(programming_languages: Value) {
let query: Query = r#"languages(popular = true, year = 1995)"#.parse().unwrap();
let expected = json!([
{
"name": "JavaScript",
"popular": true,
"year": 1995
}
]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

#[rstest]
fn value_not_found(programming_languages: Value) {
let query: Query = r#"languages(name = "value_not_found")"#.parse().unwrap();
let expected = json!([]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

// TODO: Assert that a warning is logged
#[rstest]
fn missing_field(programming_languages: Value) {
let query: Query = r#"languages(missing_field = 5)"#.parse().unwrap();
let expected = json!([]);

let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}

// TODO: assert that a warning is logged
#[rstest]
fn incomparable_types_error(programming_languages: Value) {
let query: Query = r#"languages(name = 1995)"#.parse().unwrap();
let expected = json!([]);
let result = query.apply(programming_languages).unwrap();

assert_eq!(result, expected);
}
Loading