Debian Codegraph @

doc/tutorial_src/schema_tests1.c

raw symbols SCIP
1 #include <cgreen/cgreen.h>
2 #include <stdlib.h>
3 #include <mysql.h>
4 #include "person.h"
5
6 Describe(Person);
7 BeforeEach(Person) {}
8 AfterEach(Person) {}
9
10 static void create_schema() {
11 MYSQL *connection = mysql_init(NULL);
12 mysql_real_connect(connection, "localhost", "me", "secret", "test", 0, NULL, 0);
13 mysql_query(connection, "create table people (name, varchar(255) unique)");
14 mysql_close(connection);
15 }
16
17 static void drop_schema() {
18 MYSQL *connection = mysql_init(NULL);
19 mysql_real_connect(connection, "localhost", "me", "secret", "test", 0, NULL, 0);
20 mysql_query(connection, "drop table people");
21 mysql_close(connection);
22 }
23
24 Ensure(Person, can_add_person_to_database) {
25 create_schema();
26 Person *person = create_person();
27 set_person_name(person, "Fred");
28 save_person(person);
29 Person *found = find_person_by_name("Fred");
30 assert_that(get_person_name(found), is_equal_to_string("Fred"));
31 drop_schema();
32 }
33
34 Ensure(Person, cannot_add_duplicate_person) {
35 create_schema();
36 Person *person = create_person();
37 set_person_name(person, "Fred");
38 assert_that(save_person(person), is_true);
39 Person *duplicate = create_person();
40 set_person_name(duplicate, "Fred");
41 assert_that(save_person(duplicate), is_false);
42 drop_schema();
43 }
44
45 TestSuite *person_tests() {
46 TestSuite *suite = create_test_suite();
47 add_test_with_context(suite, Person, can_add_person_to_database);
48 add_test_with_context(suite, Person, cannot_add_duplicate_person);
49 return suite;
50 }
51
52 int main(int argc, char **argv) {
53 return run_test_suite(person_tests(), create_text_reporter());
54 }