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