Browse Source

Add graph transaction test.

scossu 1 year ago
parent
commit
6645e65e8b
2 changed files with 55 additions and 4 deletions
  1. 4 4
      include/graph.h
  2. 51 0
      test/test_graph.c

+ 4 - 4
include/graph.h

@@ -12,9 +12,9 @@ typedef struct graph_t LSUP_Graph;
 
 /** @brief Graph iterator.
  *
- * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
- * over lookup results with #LSUP_graph_iter_next. It must be freed with
- * #LSUP_graph_iter_free when done.
+ * This opaque handle is generated by #LSUP_graph_lookup_txn and is used to
+ * iterate over lookup results with #LSUP_graph_iter_next. It must be freed
+ * with #LSUP_graph_iter_free when done.
  */
 typedef struct graph_iter_t LSUP_GraphIterator;
 
@@ -347,7 +347,7 @@ LSUP_graph_lookup_txn (void *txn, const LSUP_Graph *gr, const LSUP_Term *s,
 
 /** @brief Advance a cursor obtained by a lookup and return a matching triple.
  *
- * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
+ * @param it[in] Iterator handle obtained through #LSUP_graph_lookup_txn.
  *
  * @param spo[out] Triple handle pointer to be populated with the next result.
  * If not NULL, it will allocate a new triple and new terms, and should be

+ 51 - 0
test/test_graph.c

@@ -213,6 +213,42 @@ _graph_remove (LSUP_StoreType type)
 }
 
 
+static int
+_graph_txn (LSUP_StoreType type)
+{
+    const LSUP_StoreInt *sif = LSUP_store_int (type);
+    if (!(sif->features & LSUP_STORE_TXN)) return 0;
+
+    if (sif->setup_fn) sif->setup_fn (NULL, true);
+
+    LSUP_Triple *trp = create_triples();
+
+    LSUP_Graph *gr = LSUP_graph_new (
+            LSUP_iriref_new (NULL, NULL), type, NULL, NULL, 0);
+
+    void *txn;
+    size_t ct;
+
+    EXPECT_PASS (LSUP_graph_begin (gr, 0, &txn));
+    EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
+    LSUP_graph_abort (gr, txn);
+
+    // NOTE that ct reports the count before the txn was aborted. This is
+    // intentional.
+    EXPECT_INT_EQ (ct, 8);
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
+
+    EXPECT_PASS (LSUP_graph_begin (gr, 0, &txn));
+    EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
+    LSUP_graph_commit (gr, txn);
+
+    EXPECT_INT_EQ (ct, 8);
+    EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
+
+    return 0;
+}
+
+
 static int
 test_environment()
 {
@@ -276,6 +312,20 @@ BACKEND_TBL
 }
 
 
+static int test_graph_txn()
+{
+    /*
+     * Test transactions only if the backend supports them.
+     */
+#define ENTRY(a, b) \
+    if (_graph_txn (LSUP_STORE_##a) != 0) return -1;
+BACKEND_TBL
+#undef ENTRY
+
+    return 0;
+}
+
+
 static int test_graph_copy()
 {
     LSUP_Triple *trp = create_triples();
@@ -329,6 +379,7 @@ int graph_tests()
     RUN (test_graph_lookup);
     RUN (test_graph_remove);
     RUN (test_graph_copy);
+    RUN (test_graph_txn);
 
     return 0;
 }