11test_that(" setting constraints works" , {
2- dset <- qf_dataset(
3- flights = nycflights13 :: flights ,
4- planes = nycflights13 :: planes
5- )
2+ dset <- example_dataset(constrained = FALSE )
63
74 constraints(dset $ planes ) <- list (
85 cstr_primary_key(tailnum )
@@ -34,14 +31,48 @@ test_that("setting constraints works", {
3431 )
3532 )
3633 )
34+
35+ # Foreign key referencing own table:
36+ enneagram <- as_qf_table(data.frame (
37+ number = c(1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ),
38+ disintegration = c(4 , 8 , 9 , 2 , 7 , 3 , 1 , 5 , 6 ),
39+ integration = c(7 , 4 , 6 , 1 , 8 , 9 , 5 , 2 , 3 )
40+ ))
41+ constraints(enneagram ) <- list (
42+ cstr_primary_key(number ),
43+ cstr_foreign_key(disintegration , .self $ number ),
44+ cstr_foreign_key(integration , .self $ number )
45+ )
46+ expect_identical(
47+ constraints(enneagram ),
48+ list (
49+ structure(
50+ list (cols = " number" ),
51+ class = c(" cstr_primary_key" , " cstr_unique_key" , " qf_constraint" )
52+ ),
53+ structure(
54+ list (cols = " disintegration" , ref_table = " .self" , ref_cols = " number" ),
55+ class = c(" cstr_foreign_key" , " qf_constraint" )
56+ ),
57+ structure(
58+ list (cols = " integration" , ref_table = " .self" , ref_cols = " number" ),
59+ class = c(" cstr_foreign_key" , " qf_constraint" )
60+ )
61+ )
62+ )
63+ expect_error(regex = " no information on other tables in the dataset" , {
64+ constraints(enneagram ) <- list (
65+ cstr_primary_key(number ),
66+ cstr_foreign_key(disintegration , .self $ number ),
67+ cstr_foreign_key(integration , .self $ number ),
68+ cstr_foreign_key(integration , some_other_table )
69+ )
70+ })
71+
3772})
3873
3974test_that(" alternative reference specification formats are equivalent" , {
40-
41- dset <- qf_dataset(
42- flights = nycflights13 :: flights ,
43- planes = nycflights13 :: planes
44- )
75+ dset <- example_dataset(constrained = FALSE )
4576 constraints(dset $ planes ) <- list (cstr_primary_key(tailnum ))
4677
4778 dset1 <- dset
@@ -58,27 +89,13 @@ test_that("alternative reference specification formats are equivalent", {
5889 )
5990 expect_identical(constraints(dset1 $ flights ), constraints(dset2 $ flights ))
6091 expect_identical(constraints(dset2 $ flights ), constraints(dset3 $ flights ))
61-
6292})
6393
6494test_that(" constraint checking works" , {
65-
66- dset <- withr :: with_package(" nycflights13" ,
67- qf_dataset(airlines , flights )
68- )
69-
70- constraints(dset $ airlines ) <- list (
71- cstr_primary_key(carrier ),
72- cstr_not_missing(name )
73- )
74- constraints(dset $ flights ) <- list (
75- cstr_primary_key(c(year , month , day , carrier , flight )),
76- cstr_foreign_key(carrier , airlines )
77- )
95+ dset <- example_dataset()
7896
7997 # Case 1: Primary key on 'flights' is invalid because of duplicate keys;
8098 # otherwise OK
81-
8299 result <- check_constraints(dset )
83100 expect_true(result $ airlines [[1 ]]$ satisfied )
84101 expect_true(result $ airlines [[2 ]]$ satisfied )
@@ -88,22 +105,33 @@ test_that("constraint checking works", {
88105 # Case 2: Primary key and not-missing constraint on 'airlines' is invalid
89106 # because of missing values for Delta Airlines; Foreign key on 'flights' is
90107 # invalid because it references this primary key.
91-
92108 dset2 <- dset
93109 dset2 $ airlines [dset $ airlines $ carrier == " DL" , ] <- NA
94-
95110 result2 <- check_constraints(dset2 )
96111 expect_false(result2 $ airlines [[1 ]]$ satisfied )
97112 expect_false(result2 $ airlines [[2 ]]$ satisfied )
98113 expect_false(result2 $ flights [[1 ]]$ satisfied )
99114 expect_false(result2 $ flights [[2 ]]$ satisfied )
100115
116+ # Foreign key referencing own table:
117+ enneagram <- as_qf_table(data.frame (
118+ number = c(1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ),
119+ disintegration = c(4 , 8 , 9 , 2 , 7 , 3 , 1 , 5 , 6 ),
120+ integration = c(7 , 4 , 6 , 1 , 8 , 9 , 5 , 2 , 3 )
121+ ))
122+ constraints(enneagram ) <- list (
123+ cstr_primary_key(number ),
124+ cstr_foreign_key(disintegration , .self $ number ),
125+ cstr_foreign_key(integration , .self $ number )
126+ )
127+ result9 <- check_constraints(enneagram )
128+ expect_true(result9 [[2 ]]$ satisfied )
129+ expect_true(result9 [[3 ]]$ satisfied )
130+
101131})
102132
103133test_that(" constraint validators work" , {
104- dset <- withr :: with_package(" nycflights13" ,
105- qf_dataset(airlines , flights )
106- )
134+ dset <- example_dataset(constrained = FALSE )
107135
108136 constraints(dset $ airlines ) <- list (
109137 cstr_not_missing(name )
0 commit comments