Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spec/fixtures/dynamic_definition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
users: {
name: -> { "Jimbo #{SecureRandom.hex(8)}" }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just demonstrating what a "dynamic" definition could look like, could use faker instead if more realistic usage was desirable

}
}
1 change: 1 addition & 0 deletions spec/fixtures/empty_def_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions spec/fixtures/inapplicable_def_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
foo: {
bar: "baz"
}
}
176 changes: 176 additions & 0 deletions spec/fixtures/sample_dump.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
--
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a very basic dump file created for this purpose

may be desirable in the future to also test

  • different pg dump versions
  • more exotic column types
  • may want some data that could intentionally try to break line_regex, a la bobby tables https://xkcd.com/327/

Copy link

@yorsant yorsant Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I haven't looked at the test jet, but I started to use the gem (thanks for creating it), and I had an error (extra data after last expected column) that may be related to having the delimiter inside text columns.
Some instructions could be added to the readme about handling this case.
Also, I had to specify plain text format and utf-8 for my dumb file to avoid weird characters.

I'll read more to confirm if this is a bug unless someone has experienced it before and reply with some advice.

-- PostgreSQL database dump
--

-- Dumped from database version 11.18
-- Dumped by pg_dump version 14.6 (Homebrew)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

--
-- Name: organizations; Type: TABLE; Schema: public; Owner: luke
--

CREATE TABLE public.organizations (
id integer NOT NULL,
name character varying NOT NULL,
created_at timestamp without time zone
);


ALTER TABLE public.organizations OWNER TO luke;

--
-- Name: organizations_id_seq; Type: SEQUENCE; Schema: public; Owner: luke
--

CREATE SEQUENCE public.organizations_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


ALTER TABLE public.organizations_id_seq OWNER TO luke;

--
-- Name: organizations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: luke
--

ALTER SEQUENCE public.organizations_id_seq OWNED BY public.organizations.id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: luke
--

CREATE TABLE public.users (
id integer NOT NULL,
name character varying NOT NULL,
org_id smallint NOT NULL,
created_at timestamp without time zone,
birthdate date
);


ALTER TABLE public.users OWNER TO luke;

--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: luke
--

CREATE SEQUENCE public.users_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


ALTER TABLE public.users_id_seq OWNER TO luke;

--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: luke
--

ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;


--
-- Name: organizations id; Type: DEFAULT; Schema: public; Owner: luke
--

ALTER TABLE ONLY public.organizations ALTER COLUMN id SET DEFAULT nextval('public.organizations_id_seq'::regclass);


--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: luke
--

ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);


--
-- Data for Name: organizations; Type: TABLE DATA; Schema: public; Owner: luke
--

COPY public.organizations (id, name, created_at) FROM stdin;
1 Test org 2023-02-04 16:27:51.797666
2 Test org 2 2023-02-04 16:27:55.293966
\.


--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: luke
--

COPY public.users (id, name, org_id, created_at, birthdate) FROM stdin;
1 test user 1 1 2023-02-04 16:28:34.391467 1990-01-01
2 test user 2 1 2023-02-04 16:28:44.292242 1989-01-01
3 test user 3 2 2023-02-04 16:28:52.430384 1988-01-01
\.


--
-- Name: organizations_id_seq; Type: SEQUENCE SET; Schema: public; Owner: luke
--

SELECT pg_catalog.setval('public.organizations_id_seq', 2, true);


--
-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: luke
--

SELECT pg_catalog.setval('public.users_id_seq', 3, true);


--
-- Name: organizations organizations_pkey; Type: CONSTRAINT; Schema: public; Owner: luke
--

ALTER TABLE ONLY public.organizations
ADD CONSTRAINT organizations_pkey PRIMARY KEY (id);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: luke
--

ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);


--
-- Name: users_org_idx; Type: INDEX; Schema: public; Owner: luke
--

CREATE INDEX users_org_idx ON public.users USING btree (org_id);


--
-- Name: users fk_users_organizations; Type: FK CONSTRAINT; Schema: public; Owner: luke
--

ALTER TABLE ONLY public.users
ADD CONSTRAINT fk_users_organizations FOREIGN KEY (org_id) REFERENCES public.organizations(id);


--
-- PostgreSQL database dump complete
--

5 changes: 5 additions & 0 deletions spec/fixtures/static_def.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
users: {
name: "Jimbo"
}
}
59 changes: 57 additions & 2 deletions spec/pg_dump_anonymize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,62 @@
expect(PgDumpAnonymize::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
it "can process a file with no transformations, producing an exact copy" do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't put too much thought into these, definitely not suggesting they come close to exhausting the set of behaviour that would be good to test

def_file = "spec/fixtures/empty_def_file.rb"
dump_file = File.open("spec/fixtures/sample_dump.sql")
out_file = Tempfile.new('foo')

PgDumpAnonymize.anonymize(def_file, dump_file, out_file)

dump_file.rewind
out_file.rewind
expect(dump_file.read).to eql(out_file.read)
end

it "can process a file with a static transformation" do
def_file = "spec/fixtures/static_def.rb"
dump_file = File.open("spec/fixtures/sample_dump.sql")
out_file = Tempfile.new('foo')

PgDumpAnonymize.anonymize(def_file, dump_file, out_file)

out_file.rewind
expect(out_file.grep(/test user/i)).to eql([])
out_file.rewind
expect(out_file.grep(/Jimbo/i).size).to eql(3)
end

it "can process a file with a dynamic transformation" do
def_file = "spec/fixtures/dynamic_definition.rb"
dump_file = File.open("spec/fixtures/sample_dump.sql")
out_file = Tempfile.new('foo')

PgDumpAnonymize.anonymize(def_file, dump_file, out_file)

out_file.rewind
expect(out_file.grep(/test user/i)).to eql([])
out_file.rewind
matches = out_file.grep(/Jimbo/i)
line1, line2, line3 = matches

jimboA, jimboB, jimboC = matches.map do |line|
values = line.split("\t")
values[1]
end
expect(jimboA).not_to eql(jimboB)
expect(jimboA).not_to eql(jimboC)
expect(jimboB).not_to eql(jimboC)
end

it "treats def file table references to absent tables as a noop" do
def_file = "spec/fixtures/inapplicable_def_file.rb"
dump_file = File.open("spec/fixtures/sample_dump.sql")
out_file = Tempfile.new('foo')

PgDumpAnonymize.anonymize(def_file, dump_file, out_file)

dump_file.rewind
out_file.rewind
expect(dump_file.read).to eql(out_file.read)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "bundler/setup"
require "pg_dump_anonymize"
require "tempfile"
require "securerandom"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down