| 
									
										
										
										
											2015-05-05 20:15:40 +00:00
										 |  |  | ExUnit.start() | 
					
						
							| 
									
										
										
										
											2018-01-26 16:55:15 +00:00
										 |  |  | Postgrex.Types.define(ISN.PostgrexTypes, [ISN], []) | 
					
						
							| 
									
										
										
										
											2015-05-21 06:46:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 11:12:38 +00:00
										 |  |  | defmodule ISN.TestHelper do | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |   def conn_options do | 
					
						
							|  |  |  |     db_options = [ | 
					
						
							| 
									
										
										
										
											2015-08-10 19:57:49 +00:00
										 |  |  |       sync_connect: true, | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |       hostname: "localhost", | 
					
						
							| 
									
										
										
										
											2017-01-22 21:19:23 +00:00
										 |  |  |       types: ISN.PostgrexTypes, | 
					
						
							| 
									
										
										
										
											2018-01-26 16:55:15 +00:00
										 |  |  |       database: "isn_test" | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 21:16:07 +00:00
										 |  |  |     db_user = System.get_env("POSTGRES_USER") | 
					
						
							|  |  |  |     db_pass = System.get_env("POSTGRES_PASSWORD") | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |     db_options = | 
					
						
							| 
									
										
										
										
											2017-01-22 21:19:23 +00:00
										 |  |  |       if db_user do | 
					
						
							| 
									
										
										
										
											2017-02-25 09:24:45 +00:00
										 |  |  |         Keyword.put(db_options, :username, db_user) | 
					
						
							| 
									
										
										
										
											2017-01-22 21:19:23 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         db_options | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-01-26 16:55:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |     db_options = | 
					
						
							| 
									
										
										
										
											2017-01-22 21:19:23 +00:00
										 |  |  |       if db_pass do | 
					
						
							| 
									
										
										
										
											2017-02-25 09:24:45 +00:00
										 |  |  |         Keyword.put(db_options, :password, db_pass) | 
					
						
							| 
									
										
										
										
											2017-01-22 21:19:23 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         db_options | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |     db_options | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 06:46:07 +00:00
										 |  |  |   defmacro query(pid, statement, params) do | 
					
						
							|  |  |  |     quote bind_quoted: [pid: pid, statement: statement, params: params] do | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  |       case Postgrex.query!(pid, statement, params, []) do | 
					
						
							| 
									
										
										
										
											2015-08-10 19:57:49 +00:00
										 |  |  |         %Postgrex.Result{rows: nil} -> :ok | 
					
						
							|  |  |  |         %Postgrex.Result{rows: rows} -> rows | 
					
						
							|  |  |  |         %Postgrex.Error{} = err -> err | 
					
						
							| 
									
										
										
										
											2015-05-21 06:46:07 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 16:55:15 +00:00
										 |  |  | db_options = Keyword.merge(ISN.TestHelper.conn_options(), database: "postgres") | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  | {:ok, pid} = Postgrex.start_link(db_options) | 
					
						
							| 
									
										
										
										
											2015-06-18 15:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  | Postgrex.query!(pid, "DROP DATABASE IF EXISTS isn_test;", []) | 
					
						
							|  |  |  | Postgrex.query!(pid, "CREATE DATABASE isn_test;", []) | 
					
						
							| 
									
										
										
										
											2018-01-26 16:55:15 +00:00
										 |  |  | {:ok, pid} = Postgrex.start_link(ISN.TestHelper.conn_options()) | 
					
						
							| 
									
										
										
										
											2016-07-17 09:14:41 +00:00
										 |  |  | Postgrex.query!(pid, "CREATE EXTENSION isn;", []) |