Created
September 12, 2014 04:21
-
-
Save markhollands/886b96e9895c55638b44 to your computer and use it in GitHub Desktop.
Swift Test of a record with an id field with a renamed column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'swift' | |
| require 'swift/adapter/mysql' | |
| require 'swift/migrations' | |
| Swift.setup :default, Swift::Adapter::Mysql, db: "test", user: "", password: "", host: "localhost" | |
| # This record type is the same as the one below apart from the id column being renamed in the database | |
| class TestRecord < Swift::Record | |
| store :testrecord | |
| attribute :id, Swift::Type::Integer, serial: true, key: true, field: 'test_id' | |
| attribute :attribute, Swift::Type::String | |
| end | |
| class TestRecordFixedNames < Swift::Record | |
| store :testrecordfixednames | |
| attribute :id, Swift::Type::Integer, serial: true, key: true | |
| attribute :attribute, Swift::Type::String | |
| end | |
| TestRecord.migrate! | |
| TestRecordFixedNames.migrate! | |
| describe TestRecord do | |
| it "creates a user and queries this id attribute" do | |
| now = Time.now.strftime("%Y%m%dT%H%M%S") | |
| test = TestRecord.create(:attribute => now) | |
| # expect(test.id).not_to eq(nil) | |
| id = test.id | |
| puts "Created a record with id : #{test.id} : #{test.attribute}" | |
| results = TestRecord.execute(%Q{select * from #{TestRecord} where #{TestRecord.attribute} = ?}, now) | |
| expect(results.count).to eq(1) | |
| result = results.first | |
| id = result.id | |
| results = TestRecord.execute(%Q{select * from #{TestRecord} where #{TestRecord.id} = ?}, id) | |
| puts "Found #{results.count} records with an id #{id}" | |
| expect(results.count).to eq(1) | |
| result = TestRecord.get id: id | |
| expect(result).not_to eq(nil) | |
| if (result.nil?) | |
| puts "Result was null for .get method" | |
| else | |
| puts "Found result with id #{id}" | |
| end | |
| end | |
| end | |
| describe TestRecordFixedNames do | |
| it "creates a fixed names user and queries this id attribute" do | |
| now = Time.now.strftime("%Y%m%dT%H%M%S") | |
| test = TestRecordFixedNames.create(:attribute => now) | |
| id = test.id | |
| puts "Created a record with id : #{test.id} : #{test.attribute}" | |
| results = TestRecordFixedNames.execute(%Q{select * from #{TestRecordFixedNames} where #{TestRecordFixedNames.attribute} = ?}, now) | |
| expect(results.count).to eq(1) | |
| result = results.first | |
| id = result.id | |
| results = TestRecordFixedNames.execute(%Q{select * from #{TestRecordFixedNames} where #{TestRecordFixedNames.id} = ?}, id) | |
| expect(results.count).to eq(1) | |
| puts "Found #{results.count} records with an id #{id}" | |
| result = TestRecordFixedNames.get id: id | |
| expect(result).not_to eq(nil) | |
| if (result.nil?) | |
| puts "Result was null for .get method" | |
| else | |
| puts "Found result with id #{id}" | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment