Permalink
Browse files

Add failing enum tests

  • Loading branch information...
Joe Van Dyk
Joe Van Dyk committed Oct 1, 2012
1 parent 3b5bc8d commit 4374e11708100184c4e46d53fd11115beb5f51df
@@ -3,6 +3,9 @@
class PostgresqlArray < ActiveRecord::Base
end
+class PostgresqlEnum < ActiveRecord::Base
+end
+
class PostgresqlTsvector < ActiveRecord::Base
end
@@ -37,6 +40,9 @@ def setup
@connection.execute("INSERT INTO postgresql_arrays (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )")
@first_array = PostgresqlArray.find(1)
+ @connection.execute("INSERT INTO postgresql_enums (mood, certainty) VALUES ('sad', 'not');")
+ @first_enum = PostgresqlEnum.find(1)
+
@connection.execute("INSERT INTO postgresql_tsvectors (text_vector) VALUES (' ''text'' ''vector'' ')")
@first_tsvector = PostgresqlTsvector.find(1)
@@ -68,6 +74,14 @@ def test_data_type_of_array_types
assert_equal :string, @first_array.column_for_attribute(:nicknames).type
end
+ def test_data_type_of_enum_types
+ assert_equal :string, @first_enum.column_for_attribute(:mood).type
+
+ # Note: 'certainty' contains /int/. I believe that AR thinks the type
+ # of this column is an integer, not a enum/string.
+ assert_equal :string, @first_enum.column_for_attribute(:certainty).type
+ end
+
def test_data_type_of_tsvector_types
assert_equal :tsvector, @first_tsvector.column_for_attribute(:text_vector).type
end
@@ -114,6 +128,22 @@ def test_money_values
assert_equal(-567.89, @second_money.wealth)
end
+ def test_default_enum_values
+ assert_equal 'happy', PostgresqlEnum.new.mood
+ assert_equal 'very', PostgresqlEnum.new.certainty
+ end
+
+ def test_update_enum
+ enum = @first_enum
+ enum.mood = 'sad'
+
+ # BelowFails: PG::Error: ERROR: column "certainty" is of type certainty but expression is of type integer
+ enum.certainty = 'somewhat'
+ enum.save!
+ assert_equal 'sad', PostgresqlEnum.find(@first_enum.id).mood
+ assert_equal 'somewhat', PostgresqlEnum.find(@first_enum.id).certainty
+ end
+
def test_update_tsvector
new_text_vector = "'new' 'text' 'vector'"
assert @first_tsvector.text_vector = new_text_vector
@@ -1,10 +1,12 @@
ActiveRecord::Schema.define do
- %w(postgresql_tsvectors postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
+ %w(postgresql_tsvectors postgresql_enums postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
+ execute 'DROP TYPE IF EXISTS mood CASCADE'
+ execute 'DROP TYPE IF EXISTS certainty CASCADE'
execute 'DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE'
execute 'CREATE SEQUENCE companies_nonstd_seq START 101 OWNED BY companies.id'
execute "ALTER TABLE companies ALTER COLUMN id SET DEFAULT nextval('companies_nonstd_seq')"
@@ -56,6 +58,16 @@
);
_SQL
+ execute <<_SQL
+ CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
+ CREATE TYPE certainty AS ENUM ('very', 'somewhat', 'not');
+ CREATE TABLE postgresql_enums (
+ id SERIAL PRIMARY KEY,
+ mood mood default 'happy',
+ certainty certainty default 'very'
+ );
+_SQL
+
execute <<_SQL
CREATE TABLE postgresql_tsvectors (
id SERIAL PRIMARY KEY,

0 comments on commit 4374e11

Please sign in to comment.