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

Backend] Implement Karate tests for apartment_images table #85

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 2 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
interval: 5s
timeout: 5s
retries: 5
volumes:
- ./migrations:/docker-entrypoint-initdb.d/migrations

graphql-engine-test:
image: hasura/graphql-engine:latest
Expand Down
10 changes: 10 additions & 0 deletions migrations/default/1_create_apartment_images/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE apartment_images (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
apartment_id UUID NOT NULL REFERENCES apartments(id) ON DELETE CASCADE,
image_url TEXT NOT NULL,
uploaded_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_apartment_images_apartment_id ON apartment_images(apartment_id);
12 changes: 12 additions & 0 deletions migrations/default/1_create_apartments/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE apartments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name TEXT NOT NULL,
description TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);


INSERT INTO apartments (id, name, description) VALUES
('11111111-1111-1111-1111-111111111111', 'Modern Loft in Heredia', 'A beautiful modern loft');
8 changes: 8 additions & 0 deletions migrations/default/2_create_apartment_images/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE apartment_images (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
apartment_id UUID NOT NULL REFERENCES apartments(id) ON DELETE CASCADE,
image_url TEXT NOT NULL,
uploaded_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_apartment_images_apartment_id ON apartment_images(apartment_id);
2 changes: 2 additions & 0 deletions migrations/default/3_insert_test_data/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO apartment_images (id, apartment_id, image_url) VALUES
('11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', 'https://example.com/test-image.jpg');
225 changes: 225 additions & 0 deletions tests/karate/features/apartments/apartment_images.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
Feature: Apartment Images Management

Background:
* url baseUrl
* print '=== Loading feature file ==='
* print 'URL set to:', baseUrl
* header x-hasura-admin-secret = adminSecret

Scenario: Check GraphQL endpoint health
Given path '/'
And request { query: "query { __typename }" }
When method POST
Then status 200
And match response == { data: { __typename: 'query_root' } }

Scenario: Check apartment_images schema
Given path '/'
And request
"""
{
"query": "query { __type(name: \"apartment_images\") { name fields { name type { name kind } } } }"
}
"""
When method POST
Then status 200
And match response.errors == '#notpresent'
* print 'Schema:', response.data.__type

Scenario: Create new apartment image
Given path '/'
And request
"""
{
"query": "mutation($apartment_id: uuid!, $image_url: String!) { insert_apartment_images_one(object: { apartment_id: $apartment_id, image_url: $image_url }) { id } }",
"variables": {
"apartment_id": "11111111-1111-1111-1111-111111111111",
"image_url": "https://example.com/test-image.jpg"
}
}
"""
When method POST
Then status 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating a new resource or object (such as an apartment image), the HTTP status code should ideally be 201 Created instead of 200 OK

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright ill modify it

And match response.errors == '#notpresent'
* def created_id = response.data.insert_apartment_images_one.id

Scenario: Verify apartment_images schema exists
Given path '/'
And request
"""
{
"query": "query {
__type(name: \"apartment_images\") {
name
fields {
name
type {
name
kind
}
}
}
}"
}
"""
When method POST
Then status 200
And match response.data.__type != null
* print 'Schema:', response.data.__type

Scenario: Verify mutations are available
Given path '/'
And request
"""
{
"query": "query {
__schema {
mutationType {
fields {
name
}
}
}
}"
}
"""
When method POST
Then status 200
And match response.data.__schema.mutationType != null
* print 'Available mutations:', response.data.__schema.mutationType.fields

Scenario: Verify query permissions
Given path '/'
And request
"""
{
"query": "query {
__schema {
queryType {
fields {
name
description
}
}
}
}"
}
"""
When method POST
Then status 200
And match response.data.__schema.queryType != null
* print 'Available queries:', response.data.__schema.queryType.fields

Scenario: Verify apartment_images table exists
Given path '/'
And request
"""
{
"query": "query {
__type(name: \"apartment_images\") {
name
fields {
name
type {
name
kind
}
}
}
}"
}
"""
When method POST
Then status 200
And match response.errors == '#notpresent'
* print 'Schema:', response.data.__type

Scenario: Query apartment image by ID
Given path '/'
And request
"""
{
"query": "query GetApartmentImage($id: uuid!) {
apartment_images_by_pk(id: $id) {
id
image_url
apartment_id
uploaded_at
}
}",
"variables": {
"id": "11111111-1111-1111-1111-111111111111"
}
}
"""
When method POST
Then status 200
And match response.errors == '#notpresent'
And match response.data.apartment_images_by_pk != null

Scenario: Update apartment image
Given path '/'
And request
"""
{
"query": "mutation UpdateApartmentImage($id: uuid!, $image_url: String!) {
update_apartment_images_by_pk(
pk_columns: {id: $id},
_set: {image_url: $image_url}
) {
id
image_url
uploaded_at
}
}",
"variables": {
"id": "11111111-1111-1111-1111-111111111111",
"image_url": "https://example.com/updated-image.jpg"
}
}
"""
When method POST
Then status 200
And match response.errors == '#notpresent'
And match response.data.update_apartment_images_by_pk.image_url == 'https://example.com/updated-image.jpg'

Scenario: List apartment images
Given path '/'
And request
"""
{
"query": "query ListApartmentImages {
apartment_images(limit: 10, order_by: {uploaded_at: desc}) {
id
image_url
apartment_id
uploaded_at
}
}"
}
"""
When method POST
Then status 200
And match response.errors == '#notpresent'
And match response.data.apartment_images != null
And match response.data.apartment_images == '#[_ > 0]'

Scenario: Delete apartment image
Given path '/'
And request
"""
{
"query": "mutation DeleteApartmentImage($id: uuid!) {
delete_apartment_images_by_pk(id: $id) {
id
}
}",
"variables": {
"id": "11111111-1111-1111-1111-111111111111"
}
}
"""
When method POST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method in this Scenario could be Delete with the Status 204

Then status 200
And match response.errors == '#notpresent'
And match response.data.delete_apartment_images_by_pk != null
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
table:
name: apartment_images
schema: public

object_relationships:
- name: apartment
using:
foreign_key_constraint_on: apartment_id

select_permissions:
- role: admin
permission:
columns: '*'
filter: {}

insert_permissions:
- role: admin
permission:
check: {}
columns: '*'

update_permissions:
- role: admin
permission:
columns: '*'
filter: {}
check: {}

delete_permissions:
- role: admin
permission:
filter: {}
51 changes: 51 additions & 0 deletions tests/metadata/databases/default/tables/tables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
- table:
schema: public
name: apartments
select_permissions:
- role: admin
permission:
columns: '*'
filter: {}
insert_permissions:
- role: admin
permission:
check: {}
columns: '*'
update_permissions:
- role: admin
permission:
columns: '*'
filter: {}
check: {}
delete_permissions:
- role: admin
permission:
filter: {}

- table:
schema: public
name: apartment_images
object_relationships:
- name: apartment
using:
foreign_key_constraint_on: apartment_id
select_permissions:
- role: admin
permission:
columns: '*'
filter: {}
insert_permissions:
- role: admin
permission:
check: {}
columns: '*'
update_permissions:
- role: admin
permission:
columns: '*'
filter: {}
check: {}
delete_permissions:
- role: admin
permission:
filter: {}
9 changes: 9 additions & 0 deletions tests/metadata/databases/default/tables/track_tables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- table:
name: apartments
schema: public
source: default

- table:
name: apartment_images
schema: public
source: default