forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht11-sqlite.c
126 lines (101 loc) · 3.31 KB
/
t11-sqlite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "test_lib.h"
#include "odb.h"
#ifdef GIT2_SQLITE_BACKEND
#include "t03-data.h"
#include "fileops.h"
#include "git2/odb_backend.h"
static int cmp_objects(git_odb_object *odb_obj, git_rawobj *raw)
{
if (raw->type != git_odb_object_type(odb_obj))
return -1;
if (raw->len != git_odb_object_size(odb_obj))
return -1;
if ((raw->len > 0) && (memcmp(raw->data, git_odb_object_data(odb_obj), raw->len) != 0))
return -1;
return 0;
}
static git_odb *open_sqlite_odb(void)
{
git_odb *odb;
git_odb_backend *sqlite;
if (git_odb_new(&odb) < GIT_SUCCESS)
return NULL;
if (git_odb_backend_sqlite(&sqlite, ":memory") < GIT_SUCCESS)
return NULL;
if (git_odb_add_backend(odb, sqlite, 0) < GIT_SUCCESS)
return NULL;
return odb;
}
#define TEST_WRITE(PTR) {\
git_odb *db; \
git_oid id1, id2; \
git_odb_object *obj; \
db = open_sqlite_odb(); \
must_be_true(db != NULL); \
must_pass(git_oid_mkstr(&id1, PTR.id)); \
must_pass(git_odb_write(&id2, db, PTR##_obj.data, PTR##_obj.len, PTR##_obj.type)); \
must_be_true(git_oid_cmp(&id1, &id2) == 0); \
must_pass(git_odb_read(&obj, db, &id1)); \
must_pass(cmp_objects(obj, &PTR##_obj)); \
git_odb_object_close(obj); \
git_odb_close(db); \
}
BEGIN_TEST(sqlite0, "write a commit, read it back (sqlite backend)")
TEST_WRITE(commit);
END_TEST
BEGIN_TEST(sqlite1, "write a tree, read it back (sqlite backend)")
TEST_WRITE(tree);
END_TEST
BEGIN_TEST(sqlite2, "write a tag, read it back (sqlite backend)")
TEST_WRITE(tag);
END_TEST
BEGIN_TEST(sqlite3, "write a zero-byte entry, read it back (sqlite backend)")
TEST_WRITE(zero);
END_TEST
BEGIN_TEST(sqlite4, "write a one-byte entry, read it back (sqlite backend)")
TEST_WRITE(one);
END_TEST
BEGIN_TEST(sqlite5, "write a two-byte entry, read it back (sqlite backend)")
TEST_WRITE(two);
END_TEST
BEGIN_TEST(sqlite6, "write some bytes in an entry, read it back (sqlite backend)")
TEST_WRITE(some);
END_TEST
BEGIN_SUITE(sqlite)
ADD_TEST(sqlite0);
ADD_TEST(sqlite1);
ADD_TEST(sqlite2);
ADD_TEST(sqlite3);
ADD_TEST(sqlite4);
ADD_TEST(sqlite5);
ADD_TEST(sqlite6);
END_SUITE
#else /* no sqlite builtin */
BEGIN_SUITE(sqlite)
/* empty */
END_SUITE
#endif