How to add new regression tests on AGE source code
Tags: apacheag, postgres, database, testing
Published: 2023-04-16 22:00:00


Certainly! Here's a step-by-step guide to add your test SQL script to your AGE source code.

After having your function installed at the source code (Congrats) you have to attach you test suite with it to be able to get it merged into the project's repo.

So if you need to learn how to add your first function to your source code, you can find that here on my blog.

https://dev.to/rrrokhtar/guide-to-age-contribution-and-modifying-the-source-code-to-add-new-functions-l7m

Firstly you must be aware of the testing strategy that is followed. 
We assert SQL script with its output with a pre-run script output.

For example: 
```SQL
INSERT 1 INTO tbl;
SELECT * FROM tbl;
-----
num
-----
1
```
That is the output SQL for example test.out
and the SQL script it self will be having the commands only
i.e.
```SQL
INSERT 1 INTO tbl;
SELECT * FROM tbl;
```

So that, we can compare the pre-made output with the SQL script new output.

The apache age tests are on **regress** directory.
it contains:
- expected
- sql

those the directories which we will have to add our test suite into.

the SQL script into sql directory and sql.out into expected directory. 

But if we are having a very large script, how can we get the output file contains the SQL commands and their output directly without having to make copy and paste.

**How can we automate that?**

For example after having adding the age_fact() function; factorial function.

I have created the following script for testing:

```SQL
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
LOAD 'age';
SET search_path TO ag_catalog;
SELECT create_graph('math_test');
-- Factorial function tests age_fact()
-- 0! = 1
SELECT * FROM age_fact(0);
-- 1! = 1
SELECT * FROM age_fact(1);
-- 5! = 120
SELECT * FROM age_fact(5);
-- 10! = 3628800
SELECT * FROM age_fact(100);
-- 20! = 2432902008176640000
SELECT * FROM age_fact(500);


-- Overflow test
SELECT * FROM age_fact(32178);


-- Inside the cypher query
-- At CREATE clause
SELECT * FROM cypher('math_test', $$
CREATE (n:Node {value: fact(5)})
RETURN n.value
$$) AS (value agtype);

-- At MATCH clause
SELECT * FROM cypher('math_test', $$
MATCH (n:Node {value: fact(5)})
RETURN n.value
$$) AS (value agtype);

-- At RETURN clause
SELECT * FROM cypher('math_test', $$
MATCH (n:Node {value: fact(5)})
RETURN fact(toInteger(n.value))
$$) AS (value agtype);

-- At WHERE clause
SELECT * FROM cypher('math_test', $$
MATCH (n:Node)
WHERE n.value = fact(5)
RETURN n.value
$$) AS (value agtype);

-- At SET clause
SELECT * FROM cypher('math_test', $$
MATCH (n:Node)
SET n.value = fact(3)
RETURN n.value
$$) AS (value agtype);

-- CLEANUP

SELECT * FROM drop_graph('math_test', true);

-- End of math.sql
```

**Having Apache license on top of the SQL script is mandatory**

The key point of solving the problem of making the output manually is 

### **FAIL** then **PASS**
1. Run the regression tests
2. Get it failed and get the output/your_test.out
3. Move that to expected.out after making sure of that is the expected output
4. Re-run the tests
5. (everything passes) 

#### Congrats you have added regression your tests

### References and resources

-     https://github.com/apache/age
-     https://age.apache.org/