Skip to content

Commit 5d807dd

Browse files
CopilotSoulPancake
andcommitted
Add real server test example for conflict options
Co-authored-by: SoulPancake <70265851+SoulPancake@users.noreply.github.com>
1 parent 3b06798 commit 5d807dd

2 files changed

Lines changed: 400 additions & 0 deletions

File tree

example/conflict-options/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Write Conflict Options Example
2+
3+
This example demonstrates the new write conflict options feature for idempotent operations in OpenFGA v1.10.0+.
4+
5+
## What This Example Tests
6+
7+
This example tests the conflict options against a **real OpenFGA server** (not mocks):
8+
9+
1. **Duplicate Write Handling (`on_duplicate_writes`)**
10+
- `ERROR`: Throws an exception when attempting to write an existing tuple (default behavior)
11+
- `IGNORE`: Silently skips duplicate writes, allowing idempotent operations
12+
13+
2. **Missing Delete Handling (`on_missing_deletes`)**
14+
- `ERROR`: Throws an exception when attempting to delete a non-existent tuple (default behavior)
15+
- `IGNORE`: Silently skips missing deletes, allowing idempotent cleanup
16+
17+
3. **Combined Operations**
18+
- Tests using both options together in a single write request
19+
- Verifies idempotency by calling the same operations multiple times
20+
21+
## Prerequisites
22+
23+
### Option 1: Local OpenFGA Server
24+
25+
Run OpenFGA locally using Docker:
26+
27+
```bash
28+
docker run -p 8080:8080 openfga/openfga run
29+
```
30+
31+
### Option 2: OpenFGA Cloud
32+
33+
Set environment variables for OpenFGA Cloud:
34+
35+
```bash
36+
export FGA_API_URL="https://api.us1.fga.dev"
37+
export FGA_CLIENT_ID="your-client-id"
38+
export FGA_CLIENT_SECRET="your-client-secret"
39+
export FGA_API_TOKEN_ISSUER="https://auth.us1.fga.dev"
40+
export FGA_API_AUDIENCE="https://api.us1.fga.dev"
41+
```
42+
43+
Or create a `.env` file in this directory with the same variables.
44+
45+
## Running the Example
46+
47+
From the repository root:
48+
49+
```bash
50+
# Install dependencies (if not already done)
51+
uv sync
52+
53+
# Run the example
54+
uv run python example/conflict-options/test_conflict_options.py
55+
```
56+
57+
Or from this directory:
58+
59+
```bash
60+
python test_conflict_options.py
61+
```
62+
63+
## Expected Output
64+
65+
The example will:
66+
67+
1. Create a test store
68+
2. Create an authorization model
69+
3. Test writing duplicate tuples with ERROR behavior (should fail)
70+
4. Test writing duplicate tuples with IGNORE behavior (should succeed)
71+
5. Test idempotency by writing the same tuple multiple times with IGNORE
72+
6. Test deleting non-existent tuples with ERROR behavior (should fail)
73+
7. Test deleting non-existent tuples with IGNORE behavior (should succeed)
74+
8. Test combined operations with both conflict options
75+
9. Test idempotent cleanup operations
76+
10. Delete the test store
77+
78+
You should see output like:
79+
80+
```
81+
================================================================================
82+
Testing OpenFGA Write Conflict Options
83+
================================================================================
84+
85+
1. Creating test store...
86+
✓ Store created: 01JAEQF7XXXXXXXXXXX
87+
88+
2. Creating authorization model...
89+
✓ Model created: 01JAEQF7XXXXXXXXXXX
90+
91+
3. Test 1: Writing initial tuple...
92+
✓ Tuple written successfully
93+
94+
4. Test 2: Writing duplicate tuple with ERROR behavior...
95+
✓ Expected error occurred: ApiException
96+
97+
5. Test 3: Writing duplicate tuple with IGNORE behavior...
98+
✓ Duplicate write ignored successfully (idempotent)
99+
100+
...
101+
102+
================================================================================
103+
✅ All conflict options tests completed successfully!
104+
================================================================================
105+
```
106+
107+
## What This Proves
108+
109+
This example demonstrates that the implementation:
110+
111+
1. ✅ Works correctly against a real OpenFGA server (not just mocks)
112+
2. ✅ Properly handles duplicate writes with both ERROR and IGNORE options
113+
3. ✅ Properly handles missing deletes with both ERROR and IGNORE options
114+
4. ✅ Enables true idempotent operations
115+
5. ✅ Supports combined operations with multiple conflict options
116+
6. ✅ Follows the OpenFGA v1.10.0 specification
117+
118+
## Code Structure
119+
120+
The example is structured to be:
121+
- **Self-contained**: Creates its own test store and model
122+
- **Comprehensive**: Tests all combinations of conflict options
123+
- **Clear**: Each test is labeled and shows expected vs actual behavior
124+
- **Clean**: Removes the test store at the end
125+
126+
## Use Cases Demonstrated
127+
128+
1. **Idempotent Writes**: Write the same tuple multiple times without errors
129+
2. **Idempotent Deletes**: Delete tuples that may or may not exist
130+
3. **Retry Safety**: Operations can be safely retried without side effects
131+
4. **Batch Operations**: Write and delete multiple tuples with mixed existence states

0 commit comments

Comments
 (0)