Exemple #1
0
        public void Where_Clause_Added_If_Panel_Has_Where()
        {
            var panel = MockPanel.Panel();
            var pi    = MockPanel.EdEnc();

            /*
             * No WHERE clause
             */
            pi.Concept.SqlSetWhere = null;
            panel.SubPanels.ElementAt(0).PanelItems = new[] { pi };
            var ob = new SubPanelSqlSet(panel, Options);

            Assert.DoesNotContain("WHERE", ob.ToString());

            /*
             * One WHERE clause
             */
            pi.Concept.SqlSetWhere = "1 = 1";
            ob = new SubPanelSqlSet(panel, Options);

            Assert.Contains("WHERE 1 = 1", ob.ToString());

            /*
             * Two WHERE clauses
             */
            pi.Concept.SqlFieldNumeric = "Num";
            pi.NumericFilter           = new NumericFilter {
                Filter = new[] { 5.0M }, FilterType = NumericFilterType.EqualTo
            };
            ob = new SubPanelSqlSet(panel, Options);

            Assert.Contains("WHERE 1 = 1 AND Num = 5.0", ob.ToString());
        }
Exemple #2
0
        public void Person_Level_Query_Returns_Single_Column()
        {
            var panel = MockPanel.Panel();
            var ob    = new SubPanelSqlSet(panel, Options);
            var sql   = ob.ToString();
            var cols  = GetColumns(sql);
            var col   = StripSetAlias(cols[0]).Trim();

            Assert.Single(cols);
            Assert.Equal(Options.FieldPersonId, col);
        }
Exemple #3
0
        public void Multiple_Panel_Items_In_Subpanel_Returns_Union()
        {
            var panel = MockPanel.Panel();

            panel.SubPanels.ElementAt(0).PanelItems = new List <PanelItem>()
            {
                MockPanel.EdEnc(), MockPanel.HmcEnc()
            };
            var ob  = new SubPanelSqlSet(panel, Options);
            var sql = ob.ToString();

            Assert.Contains("UNION ALL", sql);
        }
Exemple #4
0
        public void Alias_Placeholder_Filled()
        {
            var panel         = MockPanel.Panel();
            var pi            = MockPanel.EdEnc();
            var expectedAlias = "_S000";

            pi.Concept.SqlSetWhere = "@.X != @.Y";
            panel.SubPanels.ElementAt(0).PanelItems = new[] { pi };

            var ob = new SubPanelSqlSet(panel, Options);

            Assert.Contains($"{expectedAlias}.X != {expectedAlias}.Y", ob.ToString());
        }
Exemple #5
0
        public void EventId_Field_Included_If_Present()
        {
            var panel  = MockPanel.Panel();
            var field  = "EventField";
            var field2 = "EventishField";
            var pi     = MockPanel.EdEnc();
            var pi2    = MockPanel.HmcEnc();

            pi.Index = 0;
            pi.Concept.IsEventBased  = true;
            pi.Concept.SqlFieldEvent = $"{Options.Alias}.{field}";

            pi2.Index                 = 0;
            pi2.SubPanelIndex         = 1;
            pi2.Concept.IsEventBased  = true;
            pi2.Concept.SqlFieldEvent = $"{Options.Alias}.{field2}";

            panel.SubPanels.ElementAt(0).PanelItems = new[] { pi };
            panel.SubPanels.Add(new SubPanel
            {
                Index           = 1,
                PanelIndex      = 0,
                IncludeSubPanel = true,
                PanelItems      = new[] { pi2 },
                JoinSequence    = new SubPanelJoinSequence {
                    SequenceType = SequenceType.Event
                }
            });

            var ob             = new PanelSequentialSqlSet(panel, Options);
            var sql            = ob.ToString();
            var expectedAlias1 = "_S000";
            var expectedAlias2 = "_S010";

            /*
             * Event fields added with alias.
             */
            Assert.Contains($"{expectedAlias1}.{field}", sql);
            Assert.Contains($"{expectedAlias2}.{field2}", sql);

            /*
             * Event fields inherited into parent set and join with parent alias.
             */
            var expectedParentAlias1 = "_T0";
            var expectedParentAlias2 = "_T1";

            Assert.Contains($"{expectedParentAlias1}.{field} = {expectedParentAlias2}.{field2}", sql);
        }
Exemple #6
0
        public void Dataset_Joined_Query_Includes_Salt()
        {
            var panel = MockPanel.Panel();

            panel.SubPanels.Add(new SubPanel
            {
                Index           = 1,
                PanelIndex      = 0,
                IncludeSubPanel = true,
                PanelItems      = new[] { MockPanel.HmcEnc() },
                JoinSequence    = new SubPanelJoinSequence {
                    SequenceType = SequenceType.Encounter
                }
            });

            var ob  = new DatasetJoinedSqlSet(panel, Options);
            var sql = ob.ToString();

            Assert.Equal(sql, sql);
        }
Exemple #7
0
        public void Sequence_Level_Subquery_Returns_Four_Columns()
        {
            var panel = MockPanel.Panel();

            panel.SubPanels.Add(new SubPanel
            {
                Index           = 1,
                PanelIndex      = 0,
                IncludeSubPanel = true,
                PanelItems      = new[] { MockPanel.HmcEnc() },
                JoinSequence    = new SubPanelJoinSequence {
                    SequenceType = SequenceType.Encounter
                }
            });

            var ob      = new PanelSequentialSqlSet(panel, Options);
            var sql     = ob.ToString();
            var colsStr = GetContentBetween(sql, "(SELECT", "FROM Encounter");
            var cols    = colsStr.Trim().Split(',', StringSplitOptions.RemoveEmptyEntries);

            Assert.Equal(4, cols.Length);
        }
Exemple #8
0
        public void Specialization_Where_Clauses_Added()
        {
            var panel = MockPanel.Panel();
            var pi    = MockPanel.EdEnc();
            var specs = new List <ConceptSpecialization>
            {
                new ConceptSpecialization {
                    Id = Guid.NewGuid(), SpecializationGroupId = 1, SqlSetWhere = "2 = 2"
                }
            };

            pi.Concept.SqlSetWhere = null;
            panel.SubPanels.ElementAt(0).PanelItems = new[] { pi };
            pi.Specializations = specs;

            /*
             * One specialization
             */
            var ob = new SubPanelSqlSet(panel, Options);

            Assert.Contains("WHERE 2 = 2", ob.ToString());

            /*
             * Two specializations
             */
            specs.Add(new ConceptSpecialization {
                Id = Guid.NewGuid(), SpecializationGroupId = 2, SqlSetWhere = "'A' = 'A'"
            });
            ob = new SubPanelSqlSet(panel, Options);
            Assert.Contains("WHERE 2 = 2 AND 'A' = 'A'", ob.ToString());

            /*
             * Two specializations with a normal WHERE clause
             */
            pi.Concept.SqlSetWhere = "1 = 1";
            ob = new SubPanelSqlSet(panel, Options);

            Assert.Contains("WHERE 1 = 1 AND 2 = 2 AND 'A' = 'A'", ob.ToString());
        }
Exemple #9
0
        public void Under_Threshold_Not_Shifted()
        {
            var orig       = 5;
            var obfuscator = new ObfuscationService();
            var opts       = new DeidentificationOptions {
                Cohort = new DeidentificationOptions.CohortObfuscationOptions {
                    Enabled = true, LowCellSizeMasking = new DeidentificationOptions.CohortObfuscationOptions.LowCellSizeMaskingOptions {
                        Enabled = true, Threshold = 10
                    }
                }
            };
            var count = new PatientCount {
                Value = orig
            };
            var ctx = MockPanel.Context();

            obfuscator.Obfuscate(ref count, ctx, opts);

            Assert.NotEqual(orig, count.Value);
            Assert.Equal(count.Value, opts.Cohort.LowCellSizeMasking.Threshold);
            Assert.True(count.WithinLowCellThreshold);
        }
Exemple #10
0
        public void Original_Value_Changed()
        {
            var orig       = 50;
            var obfuscator = new ObfuscationService();
            var opts       = new DeidentificationOptions {
                Cohort = new DeidentificationOptions.CohortObfuscationOptions {
                    Enabled = true, Noise = new DeidentificationOptions.CohortObfuscationOptions.NoiseOptions {
                        Enabled = true, LowerBound = -10, UpperBound = 10
                    }
                }
            };
            var count = new PatientCount {
                Value = orig
            };
            var ctx = MockPanel.Context();

            obfuscator.Obfuscate(ref count, ctx, opts);

            Assert.NotEqual(orig, count.Value);
            Assert.Equal(count.PlusMinus, Math.Max(opts.Cohort.Noise.LowerBound, opts.Cohort.Noise.UpperBound));
            Assert.False(count.WithinLowCellThreshold);
        }
Exemple #11
0
        public void Sequence_Level_Query_Returns_Single_Column()
        {
            var panel = MockPanel.Panel();

            panel.SubPanels.Add(new SubPanel
            {
                Index           = 1,
                PanelIndex      = 0,
                IncludeSubPanel = true,
                PanelItems      = new[] { MockPanel.HmcEnc() },
                JoinSequence    = new SubPanelJoinSequence {
                    SequenceType = SequenceType.Encounter
                }
            });

            var ob   = new PanelSequentialSqlSet(panel, Options);
            var sql  = ob.ToString();
            var cols = GetColumns(sql);
            var col  = StripSetAlias(cols[0]);

            Assert.Single(cols);
            Assert.Equal(Options.FieldPersonId, col);
        }
Exemple #12
0
        public void Dummy_EventId_Field_Added_If_Absent()
        {
            var panel = MockPanel.Panel();

            panel.SubPanels.Add(new SubPanel
            {
                Index           = 1,
                PanelIndex      = 0,
                IncludeSubPanel = true,
                PanelItems      = new[] { MockPanel.HmcEnc() },
                JoinSequence    = new SubPanelJoinSequence {
                    SequenceType = SequenceType.Encounter
                }
            });

            var ob             = new PanelSequentialSqlSet(panel, Options);
            var sql            = ob.ToString();
            var colsStr        = GetContentBetween(sql, "(SELECT", "FROM Encounter");
            var cols           = colsStr.Trim().Split(',', StringSplitOptions.RemoveEmptyEntries);
            var defaultNoField = "'' AS EventId";
            var included       = cols.Any(c => c.Trim().Equals(defaultNoField));

            Assert.True(included);
        }
Exemple #13
0
        public void Same_Logic_Different_Structure_Produces_Same_Shift()
        {
            var g1         = Guid.NewGuid();
            var g2         = Guid.NewGuid();
            var g3         = Guid.NewGuid();
            var orig       = 50;
            var obfuscator = new ObfuscationService();
            var opts       = new DeidentificationOptions {
                Cohort = new DeidentificationOptions.CohortObfuscationOptions {
                    Enabled = true, Noise = new DeidentificationOptions.CohortObfuscationOptions.NoiseOptions {
                        Enabled = true, LowerBound = -10, UpperBound = 10
                    }
                }
            };

            var count1 = new PatientCount {
                Value = orig
            };
            var count2 = new PatientCount {
                Value = orig
            };
            var count3 = new PatientCount {
                Value = orig
            };
            var count4 = new PatientCount {
                Value = orig
            };

            var ctx1 = MockPanel.Context();
            var ctx2 = MockPanel.Context();
            var ctx3 = MockPanel.Context();
            var ctx4 = MockPanel.Context();

            // Set context one with two panels, the first with two concepts, second with one.
            ctx1.Allowed.ElementAt(0).SubPanels.ElementAt(0).PanelItems = new List <PanelItem> {
                new PanelItem {
                    Concept = new Concept {
                        Id = g1
                    }
                }, new PanelItem {
                    Concept = new Concept {
                        Id = g2
                    }
                }
            };
            ctx1.Allowed.Append(new Panel {
                SubPanels = new List <SubPanel> {
                    new SubPanel {
                        PanelItems = new List <PanelItem> {
                            new PanelItem {
                                Concept = new Concept {
                                    Id = g3
                                }
                            }
                        }
                    }
                }
            });

            // Set context two to same as one, but with panel one concept order flipped.
            ctx2.Allowed.ElementAt(0).SubPanels.ElementAt(0).PanelItems = new List <PanelItem> {
                new PanelItem {
                    Concept = new Concept {
                        Id = g2
                    }
                }, new PanelItem {
                    Concept = new Concept {
                        Id = g1
                    }
                }
            };
            ctx2.Allowed.Append(new Panel {
                SubPanels = new List <SubPanel> {
                    new SubPanel {
                        PanelItems = new List <PanelItem> {
                            new PanelItem {
                                Concept = new Concept {
                                    Id = g3
                                }
                            }
                        }
                    }
                }
            });

            // Set context three to same as one, but with panel order flipped.
            ctx3.Allowed.ElementAt(0).SubPanels.ElementAt(0).PanelItems = new List <PanelItem> {
                new PanelItem {
                    Concept = new Concept {
                        Id = g1
                    }
                }, new PanelItem {
                    Concept = new Concept {
                        Id = g2
                    }
                }
            };
            ctx3.Allowed.Prepend(new Panel {
                SubPanels = new List <SubPanel> {
                    new SubPanel {
                        PanelItems = new List <PanelItem> {
                            new PanelItem {
                                Concept = new Concept {
                                    Id = g3
                                }
                            }
                        }
                    }
                }
            });

            // Set context four to a combination of two and three, with both concept order and panel order flipped.
            ctx4.Allowed.ElementAt(0).SubPanels.ElementAt(0).PanelItems = new List <PanelItem> {
                new PanelItem {
                    Concept = new Concept {
                        Id = g2
                    }
                }, new PanelItem {
                    Concept = new Concept {
                        Id = g1
                    }
                }
            };
            ctx4.Allowed.Prepend(new Panel {
                SubPanels = new List <SubPanel> {
                    new SubPanel {
                        PanelItems = new List <PanelItem> {
                            new PanelItem {
                                Concept = new Concept {
                                    Id = g3
                                }
                            }
                        }
                    }
                }
            });

            obfuscator.Obfuscate(ref count1, ctx1, opts);
            obfuscator.Obfuscate(ref count2, ctx2, opts);
            obfuscator.Obfuscate(ref count3, ctx3, opts);
            obfuscator.Obfuscate(ref count4, ctx4, opts);

            Assert.NotEqual(orig, count1.Value);
            Assert.NotEqual(orig, count2.Value);
            Assert.NotEqual(orig, count3.Value);
            Assert.NotEqual(orig, count4.Value);
            Assert.False(count1.WithinLowCellThreshold);
            Assert.False(count2.WithinLowCellThreshold);
            Assert.False(count3.WithinLowCellThreshold);
            Assert.False(count4.WithinLowCellThreshold);
            Assert.Equal(count1.Value, count2.Value);
            Assert.Equal(count1.Value, count3.Value);
            Assert.Equal(count1.Value, count4.Value);
        }